|
13 | 13 | */ |
14 | 14 | package io.trino.gateway.ha.handler; |
15 | 15 |
|
| 16 | +import jakarta.servlet.ReadListener; |
| 17 | +import jakarta.servlet.ServletInputStream; |
16 | 18 | import jakarta.servlet.http.HttpServletRequest; |
17 | 19 | import org.junit.jupiter.api.Test; |
18 | 20 | import org.junit.jupiter.api.TestInstance; |
19 | 21 | import org.junit.jupiter.api.TestInstance.Lifecycle; |
20 | 22 | import org.mockito.Mockito; |
21 | 23 |
|
| 24 | +import java.io.ByteArrayInputStream; |
22 | 25 | import java.io.IOException; |
| 26 | +import java.nio.charset.StandardCharsets; |
23 | 27 |
|
24 | 28 | import static io.trino.gateway.ha.handler.ProxyUtils.extractQueryIdIfPresent; |
25 | 29 | import static io.trino.gateway.ha.handler.ProxyUtils.getQueryUser; |
26 | 30 | import static io.trino.gateway.ha.handler.QueryIdCachingProxyHandler.AUTHORIZATION; |
27 | 31 | import static io.trino.gateway.ha.handler.QueryIdCachingProxyHandler.USER_HEADER; |
28 | 32 | import static org.assertj.core.api.Assertions.assertThat; |
| 33 | +import static org.mockito.Mockito.when; |
29 | 34 |
|
30 | 35 | @TestInstance(Lifecycle.PER_CLASS) |
31 | 36 | public class TestQueryIdCachingProxyHandler |
@@ -61,6 +66,44 @@ public void testExtractQueryIdFromUrl() |
61 | 66 | .isNull(); |
62 | 67 | } |
63 | 68 |
|
| 69 | + @Test |
| 70 | + public void testQueryIdFromKill() |
| 71 | + throws IOException |
| 72 | + { |
| 73 | + HttpServletRequest req = Mockito.mock(HttpServletRequest.class); |
| 74 | + |
| 75 | + String query = "CALL system.runtime.kill_query(query_id ==> '20200416_160256_03078_6b4yt', message ==> 'If he dies, he dies')"; |
| 76 | + ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(query.getBytes(StandardCharsets.UTF_8)); |
| 77 | + when(req.getInputStream()).thenReturn(new ServletInputStream() |
| 78 | + { |
| 79 | + @Override |
| 80 | + public boolean isFinished() |
| 81 | + { |
| 82 | + return byteArrayInputStream.available() > 0; |
| 83 | + } |
| 84 | + |
| 85 | + @Override |
| 86 | + public boolean isReady() |
| 87 | + { |
| 88 | + return true; |
| 89 | + } |
| 90 | + |
| 91 | + @Override |
| 92 | + public void setReadListener(ReadListener readListener) |
| 93 | + {} |
| 94 | + |
| 95 | + public int read() |
| 96 | + throws IOException |
| 97 | + { |
| 98 | + return byteArrayInputStream.read(); |
| 99 | + } |
| 100 | + }); |
| 101 | + |
| 102 | + when(req.getQueryString()).thenReturn(""); |
| 103 | + |
| 104 | + assertThat(extractQueryIdIfPresent(req)).isEqualTo("20200416_160256_03078_6b4yt"); |
| 105 | + } |
| 106 | + |
64 | 107 | @Test |
65 | 108 | public void testUserFromRequest() |
66 | 109 | throws IOException |
|
0 commit comments