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