use of com.palantir.dialogue.Request in project conjure-java-runtime by palantir.
the class JaxRsClientDialogueEndpointTest method testPostWithBody_defaultContentType.
@Test
public void testPostWithBody_defaultContentType() {
Channel channel = stubNoContentResponseChannel();
StubServiceWithoutContentType service = JaxRsClient.create(StubServiceWithoutContentType.class, channel, runtime);
service.post("Hello, World!");
ArgumentCaptor<Endpoint> endpointCaptor = ArgumentCaptor.forClass(Endpoint.class);
ArgumentCaptor<Request> requestCaptor = ArgumentCaptor.forClass(Request.class);
verify(channel).execute(endpointCaptor.capture(), requestCaptor.capture());
Endpoint endpoint = endpointCaptor.getValue();
assertThat(endpoint.serviceName()).isEqualTo("StubServiceWithoutContentType");
assertThat(endpoint.endpointName()).isEqualTo("post");
assertThat(endpoint.httpMethod()).isEqualTo(HttpMethod.POST);
Request request = requestCaptor.getValue();
assertThat(request.body()).isPresent();
assertThat(request.body().get().contentType()).isEqualTo("application/json");
assertThat(request.headerParams().asMap()).containsExactly(new AbstractMap.SimpleImmutableEntry<>("Content-Length", ImmutableList.of("15")));
}
use of com.palantir.dialogue.Request in project conjure-java-runtime by palantir.
the class JaxRsClientDialogueEndpointTest method testTrailingWildcardParameter_emptyString.
@Test
public void testTrailingWildcardParameter_emptyString() {
Channel channel = stubNoContentResponseChannel();
StubService service = JaxRsClient.create(StubService.class, channel, runtime);
service.complexPath("dynamic0", "");
ArgumentCaptor<Endpoint> endpointCaptor = ArgumentCaptor.forClass(Endpoint.class);
ArgumentCaptor<Request> requestCaptor = ArgumentCaptor.forClass(Request.class);
verify(channel).execute(endpointCaptor.capture(), requestCaptor.capture());
UrlBuilder urlBuilder = mock(UrlBuilder.class);
endpointCaptor.getValue().renderPath(ImmutableMap.of(), urlBuilder);
// context path
verify(urlBuilder).pathSegment("foo");
verify(urlBuilder).pathSegment("static0");
verify(urlBuilder).pathSegment("dynamic0");
verify(urlBuilder).pathSegment("static1");
// Empty string must be included
verify(urlBuilder).pathSegment("");
}
use of com.palantir.dialogue.Request in project conjure-java-runtime by palantir.
the class JaxRsClientDialogueEndpointTest method testEmptyStringPathParameter.
@Test
public void testEmptyStringPathParameter() {
Channel channel = stubNoContentResponseChannel();
StubService service = JaxRsClient.create(StubService.class, channel, runtime);
service.innerPath("");
ArgumentCaptor<Endpoint> endpointCaptor = ArgumentCaptor.forClass(Endpoint.class);
ArgumentCaptor<Request> requestCaptor = ArgumentCaptor.forClass(Request.class);
verify(channel).execute(endpointCaptor.capture(), requestCaptor.capture());
UrlBuilder urlBuilder = mock(UrlBuilder.class);
endpointCaptor.getValue().renderPath(ImmutableMap.of(), urlBuilder);
// context path
verify(urlBuilder).pathSegment("foo");
verify(urlBuilder).pathSegment("begin");
verify(urlBuilder).pathSegment("");
verify(urlBuilder).pathSegment("end");
}
use of com.palantir.dialogue.Request in project dialogue by palantir.
the class LargeResponseTest method large_response.
@ParameterizedTest
@MethodSource("responseTestArguments")
public void large_response(CloseType closeType, TransferEncoding encoding) throws Exception {
AtomicBoolean used = new AtomicBoolean();
Undertow server = startServer(new BlockingHandler(exchange -> {
long responseBytes = 300 * 1024 * 1024;
encoding.apply(exchange, responseBytes);
String sessionId = Base64.getEncoder().encodeToString(exchange.getConnection().getSslSession().getId());
exchange.getResponseHeaders().put(HttpString.tryFromString("TlsSessionId"), sessionId);
if (!used.getAndSet(true)) {
OutputStream out = exchange.getOutputStream();
for (int i = 0; i < responseBytes; i++) {
out.write(7);
// Flush + pause 100ms every 1M for a response time of 30 seconds
if (i % (1024 * 1024) == 0) {
out.flush();
Thread.sleep(100);
}
}
}
}));
try {
String uri = "https://localhost:" + getPort(server);
ClientConfiguration conf = TestConfigurations.create(uri);
Meter closedConns = DialogueClientMetrics.of(conf.taggedMetricRegistry()).connectionClosedPartiallyConsumedResponse("client");
String firstSession;
Channel channel;
assertThat(closedConns.getCount()).isZero();
try (ApacheHttpClientChannels.CloseableClient client = ApacheHttpClientChannels.createCloseableHttpClient(conf, "client")) {
channel = ApacheHttpClientChannels.createSingleUri(uri, client);
ListenableFuture<Response> response = channel.execute(TestEndpoint.POST, Request.builder().build());
Response resp = response.get();
firstSession = resp.getFirstHeader("TlsSessionId").orElseThrow();
assertThat(resp.code()).isEqualTo(200);
try (InputStream responseStream = resp.body()) {
assertThat(responseStream.read()).isEqualTo(7);
long beforeClose = System.nanoTime();
closeType.close(responseStream, resp);
Duration closeDuration = Duration.ofNanos(System.nanoTime() - beforeClose);
assertThat(closeDuration).isLessThan(Duration.ofSeconds(2));
assertThat(closedConns.getCount()).isOne();
}
}
// Ensure that the client isn't left in a bad state (connection has been discarded, not incorrectly added
// back to the pool in an ongoing request state)
ListenableFuture<Response> response = channel.execute(TestEndpoint.POST, Request.builder().build());
try (Response resp = response.get()) {
assertThat(resp.code()).isEqualTo(200);
assertThat(resp.body()).isEmpty();
assertThat(resp.getFirstHeader("TlsSessionId").orElseThrow()).as("Expected a new connection").isNotEqualTo(firstSession);
assertThat(closedConns.getCount()).isOne();
}
} finally {
server.stop();
}
}
use of com.palantir.dialogue.Request in project dialogue by palantir.
the class ContentEncodingChannelTest method testCompression.
@Test
void testCompression() {
byte[] expected = "Hello".getBytes(StandardCharsets.UTF_8);
Request request = Request.builder().body(new RequestBody() {
@Override
public void writeTo(OutputStream output) throws IOException {
output.write(expected);
}
@Override
public String contentType() {
return "text/plain";
}
@Override
public boolean repeatable() {
return true;
}
@Override
public OptionalLong contentLength() {
return OptionalLong.of(expected.length);
}
@Override
public void close() {
}
}).build();
Request wrapped = ContentEncodingChannel.wrap(request);
assertThat(wrapped.body()).hasValueSatisfying(body -> {
assertThat(body.contentLength()).isEmpty();
assertThat(inflate(content(body))).isEqualTo(expected);
});
}
Aggregations