|
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; |
23 | 26 |
|
24 | 27 | import static io.trino.gateway.ha.handler.ProxyUtils.extractQueryIdIfPresent; |
25 | 28 | import static io.trino.gateway.ha.handler.ProxyUtils.getQueryUser; |
26 | 29 | import static io.trino.gateway.ha.handler.QueryIdCachingProxyHandler.AUTHORIZATION; |
27 | 30 | import static io.trino.gateway.ha.handler.QueryIdCachingProxyHandler.USER_HEADER; |
| 31 | +import static java.nio.charset.StandardCharsets.UTF_8; |
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,63 @@ public void testExtractQueryIdFromUrl() |
61 | 66 | .isNull(); |
62 | 67 | } |
63 | 68 |
|
| 69 | + @Test |
| 70 | + void testQueryIdFromKill() |
| 71 | + throws IOException |
| 72 | + { |
| 73 | + assertThat( |
| 74 | + extractQueryIdIfPresent( |
| 75 | + prepareMockRequestWithBody("CALL system.runtime.kill_query(query_id => '20200416_160256_03078_6b4yt', message => 'If he dies, he dies')"))) |
| 76 | + .isEqualTo("20200416_160256_03078_6b4yt"); |
| 77 | + |
| 78 | + assertThat( |
| 79 | + extractQueryIdIfPresent( |
| 80 | + prepareMockRequestWithBody("CALL system.runtime.kill_query('20200416_160256_03078_6b4yt', 'If he dies, he dies')"))) |
| 81 | + .isEqualTo("20200416_160256_03078_6b4yt"); |
| 82 | + |
| 83 | + assertThat(extractQueryIdIfPresent(prepareMockRequestWithBody("CALL system.runtime.kill_query( query_id=>'20200416_160256_03078_6b4yt')"))) |
| 84 | + .isEqualTo("20200416_160256_03078_6b4yt"); |
| 85 | + |
| 86 | + assertThat(extractQueryIdIfPresent(prepareMockRequestWithBody("CALL system.runtime.kill_query( '20200416_160256_03078_6b4yt')"))) |
| 87 | + .isEqualTo("20200416_160256_03078_6b4yt"); |
| 88 | + } |
| 89 | + |
| 90 | + HttpServletRequest prepareMockRequestWithBody(String query) |
| 91 | + throws IOException |
| 92 | + { |
| 93 | + HttpServletRequest request = Mockito.mock(HttpServletRequest.class); |
| 94 | + |
| 95 | + ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(query.getBytes(UTF_8)); |
| 96 | + when(request.getInputStream()).thenReturn(new ServletInputStream() |
| 97 | + { |
| 98 | + @Override |
| 99 | + public boolean isFinished() |
| 100 | + { |
| 101 | + return byteArrayInputStream.available() > 0; |
| 102 | + } |
| 103 | + |
| 104 | + @Override |
| 105 | + public boolean isReady() |
| 106 | + { |
| 107 | + return true; |
| 108 | + } |
| 109 | + |
| 110 | + @Override |
| 111 | + public void setReadListener(ReadListener readListener) |
| 112 | + {} |
| 113 | + |
| 114 | + public int read() |
| 115 | + throws IOException |
| 116 | + { |
| 117 | + return byteArrayInputStream.read(); |
| 118 | + } |
| 119 | + }); |
| 120 | + |
| 121 | + when(request.getQueryString()).thenReturn(""); |
| 122 | + |
| 123 | + return request; |
| 124 | + } |
| 125 | + |
64 | 126 | @Test |
65 | 127 | public void testUserFromRequest() |
66 | 128 | throws IOException |
|
0 commit comments