-
Notifications
You must be signed in to change notification settings - Fork 72
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add proxy handler for PUT request #543
base: main
Are you sure you want to change the base?
Changes from all commits
18e0d44
bea076a
773d4a9
7e0fa5d
0ff2fac
c58ac48
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
/* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.trino.gateway.ha; | ||
|
||
import okhttp3.MediaType; | ||
import okhttp3.OkHttpClient; | ||
import okhttp3.Request; | ||
import okhttp3.RequestBody; | ||
import okhttp3.Response; | ||
import okhttp3.mockwebserver.Dispatcher; | ||
import okhttp3.mockwebserver.MockResponse; | ||
import okhttp3.mockwebserver.MockWebServer; | ||
import okhttp3.mockwebserver.RecordedRequest; | ||
import org.junit.jupiter.api.AfterAll; | ||
import org.junit.jupiter.api.BeforeAll; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.TestInstance; | ||
import org.junit.jupiter.api.TestInstance.Lifecycle; | ||
|
||
import static com.google.common.net.HttpHeaders.CONTENT_TYPE; | ||
import static com.google.common.net.MediaType.JSON_UTF_8; | ||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
@TestInstance(Lifecycle.PER_CLASS) | ||
public class TestPutRequestHandler | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also, consider renaming to |
||
{ | ||
private final OkHttpClient httpClient = new OkHttpClient(); | ||
private MockWebServer mockTrinoServer = new MockWebServer(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add |
||
|
||
private final int routerPort = 21001 + (int) (Math.random() * 1000); | ||
private final int customBackendPort = 21000 + (int) (Math.random() * 1000); | ||
|
||
private static final String OK = "OK"; | ||
private static final int NOT_FOUND = 404; | ||
|
||
private final String customPutEndpoint = "/v1/custom"; // this is enabled in test-config-template.yml | ||
private final String healthCheckEndpoint = "/v1/info"; | ||
|
||
@BeforeAll | ||
public void setup() | ||
throws Exception | ||
{ | ||
HaGatewayTestUtils.prepareMockBackend(mockTrinoServer, customBackendPort, "default custom response"); | ||
mockTrinoServer.setDispatcher(new Dispatcher() { | ||
@Override | ||
public MockResponse dispatch(RecordedRequest request) | ||
{ | ||
if (request.getPath().equals(healthCheckEndpoint)) { | ||
return new MockResponse().setResponseCode(200) | ||
.setHeader(CONTENT_TYPE, JSON_UTF_8) | ||
.setBody("{\"starting\": false}"); | ||
} | ||
|
||
if (request.getMethod().equals("PUT") && request.getPath().equals(customPutEndpoint)) { | ||
return new MockResponse().setResponseCode(200) | ||
.setHeader(CONTENT_TYPE, JSON_UTF_8) | ||
.setBody(OK); | ||
} | ||
|
||
return new MockResponse().setResponseCode(NOT_FOUND); | ||
} | ||
}); | ||
|
||
// seed database | ||
HaGatewayTestUtils.TestConfig testConfig = | ||
HaGatewayTestUtils.buildGatewayConfigAndSeedDb(routerPort, "test-config-template.yml"); | ||
|
||
// Start Gateway | ||
String[] args = {testConfig.configFilePath()}; | ||
HaGatewayLauncher.main(args); | ||
|
||
// Set up backend | ||
HaGatewayTestUtils.setUpBackend( | ||
"custom", "http://localhost:" + customBackendPort, "externalUrl", true, "adhoc", routerPort); | ||
} | ||
|
||
@AfterAll | ||
public void cleanup() | ||
throws Exception | ||
{ | ||
if (mockTrinoServer != null) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think |
||
mockTrinoServer.shutdown(); | ||
} | ||
} | ||
|
||
@Test | ||
public void testPutRequestHandler() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Make test method package-private. https://trino.io/docs/current/develop/tests.html Same for |
||
throws Exception | ||
{ | ||
RequestBody requestBody = | ||
RequestBody.create(MediaType.parse("application/json; charset=utf-8"), "SELECT 1"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fix deprecation warning. |
||
Request putRequest = | ||
new Request.Builder() | ||
.url("http://localhost:" + routerPort + customPutEndpoint) | ||
.put(requestBody) | ||
.build(); | ||
|
||
try (Response response = httpClient.newCall(putRequest).execute()) { | ||
assertThat(response.body().string()).isEqualTo(OK); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fix NPE warning. |
||
} | ||
|
||
Request postRequest = | ||
new Request.Builder() | ||
.url("http://localhost:" + routerPort + customPutEndpoint) | ||
.post(requestBody) | ||
.build(); | ||
try (Response response = httpClient.newCall(postRequest).execute()) { | ||
assertThat(response.code()).isEqualTo(NOT_FOUND); | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Move to
io.trino.gateway.proxyserver
.