use of io.servicetalk.http.api.HttpResponseMetaData in project servicetalk by apple.
the class AbstractHttpConnectionTest method requestShouldInsertLastPayloadChunkInRequestPayloadWhenMissing.
@SuppressWarnings("unchecked")
@Test
void requestShouldInsertLastPayloadChunkInRequestPayloadWhenMissing() throws Exception {
Buffer chunk1 = allocator.fromAscii("test");
Buffer chunk2 = allocator.fromAscii("payload");
StreamingHttpRequest req = newRequest(GET, "/foo", HTTP_1_1, headersFactory.newHeaders(), allocator, headersFactory).payloadBody(// NO chunk3 here!
from(chunk1, chunk2));
HttpResponseMetaData respMeta = newResponseMetaData(HTTP_1_1, OK, headersFactory.newHeaders().add(CONTENT_TYPE, TEXT_PLAIN));
Buffer chunk3 = allocator.fromAscii("payload");
HttpHeaders trailers = headersFactory.newEmptyTrailers();
Publisher<Object> respFlat = from(respMeta, chunk1, chunk2, chunk3, trailers);
ArgumentCaptor<Publisher<Object>> reqFlatCaptor = ArgumentCaptor.forClass(Publisher.class);
when(reqResp.apply(reqFlatCaptor.capture())).thenReturn(respFlat);
Single<StreamingHttpResponse> responseSingle = http.request(req);
StreamingHttpResponse resp = awaitIndefinitelyNonNull(responseSingle);
List<Object> objects = awaitIndefinitelyNonNull(reqFlatCaptor.getValue());
// User provided chunks
assertThat(objects.subList(0, 3), contains(req, chunk1, chunk2));
// Ensure new Last chunk inserted
assertThat(objects.get(3), instanceOf(HttpHeaders.class));
assertThat(resp.status(), equalTo(OK));
assertThat(resp.version(), equalTo(HTTP_1_1));
assertThat(resp.headers().get(CONTENT_TYPE), equalTo(TEXT_PLAIN));
assertThat(resp.payloadBody().toFuture().get(), contains(chunk1, chunk2, chunk3));
}
use of io.servicetalk.http.api.HttpResponseMetaData in project servicetalk by apple.
the class H2ToStH1ServerDuplexHandler method write.
@Override
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) {
if (msg instanceof HttpResponseMetaData) {
HttpResponseMetaData metaData = (HttpResponseMetaData) msg;
boolean realResponse = !isInterim(metaData.status());
if (realResponse) {
// Notify the CloseHandler only about "real" responses. We don't expose 1xx "interim responses" to the
// user, and handle them internally.
responseSent = true;
closeHandler.protocolPayloadBeginOutbound(ctx);
} else if (responseSent) {
// Discard an "interim response" if it arrives after a "real response" is already sent.
return;
}
Http2Headers h2Headers = h1HeadersToH2Headers(metaData.headers());
h2Headers.status(metaData.status().codeAsCharSequence());
writeMetaData(ctx, metaData, h2Headers, realResponse, promise);
} else if (msg instanceof Buffer) {
writeBuffer(ctx, (Buffer) msg, promise);
} else if (msg instanceof HttpHeaders) {
writeTrailers(ctx, msg, promise);
} else {
ctx.write(msg, promise);
}
}
use of io.servicetalk.http.api.HttpResponseMetaData in project servicetalk by apple.
the class AbstractHttpConnectionTest method requestShouldWriteFlatStreamToConnectionAndReadFlatStreamSplicedIntoResponseAndPayload.
@SuppressWarnings("unchecked")
@Test
void requestShouldWriteFlatStreamToConnectionAndReadFlatStreamSplicedIntoResponseAndPayload() throws Exception {
Buffer chunk1 = allocator.fromAscii("test");
Buffer chunk2 = allocator.fromAscii("payload");
Buffer chunk3 = allocator.fromAscii("payload");
HttpHeaders trailers = headersFactory.newEmptyTrailers();
HttpHeaders headers = headersFactory.newHeaders();
headers.add(TRANSFER_ENCODING, CHUNKED);
StreamingHttpRequest req = newTransportRequest(GET, "/foo", HTTP_1_1, headers, allocator, from(chunk1, chunk2, chunk3, trailers), false, headersFactory);
HttpResponseMetaData respMeta = newResponseMetaData(HTTP_1_1, OK, INSTANCE.newHeaders().add(CONTENT_TYPE, TEXT_PLAIN));
Publisher<Object> respFlat = from(respMeta, chunk1, chunk2, chunk3, trailers);
ArgumentCaptor<Publisher<Object>> reqFlatCaptor = ArgumentCaptor.forClass(Publisher.class);
when(reqResp.apply(reqFlatCaptor.capture())).thenReturn(respFlat);
Single<StreamingHttpResponse> responseSingle = http.request(req);
StreamingHttpResponse resp = awaitIndefinitelyNonNull(responseSingle);
assertThat(reqFlatCaptor.getValue().toFuture().get(), contains(req, chunk1, chunk2, chunk3, trailers));
assertThat(resp.status(), equalTo(OK));
assertThat(resp.version(), equalTo(HTTP_1_1));
assertThat(resp.headers().get(CONTENT_TYPE), equalTo(TEXT_PLAIN));
assertThat(resp.payloadBody().toFuture().get(), contains(chunk1, chunk2, chunk3));
}
use of io.servicetalk.http.api.HttpResponseMetaData in project servicetalk by apple.
the class HttpObjectDecoder method resetNow.
/**
* Resets the state of the decoder to prepare it for parsing a new incoming message.
* <p>
* Subclasses that override this method have to invoke this implementation using {@code super} keyword.
*/
protected void resetNow() {
T message = this.message;
this.message = null;
this.trailer = null;
contentLength = Long.MIN_VALUE;
parsingLine = 0;
cumulationIndex = -1;
if (!isDecodingRequest()) {
HttpResponseMetaData res = (HttpResponseMetaData) message;
if (res != null && isSwitchingToNonHttp1Protocol(res)) {
currentState = State.UPGRADED;
return;
}
}
currentState = State.SKIP_CONTROL_CHARS;
skippedControls = 0;
}
use of io.servicetalk.http.api.HttpResponseMetaData in project servicetalk by apple.
the class HttpResponseDecoderTest method assertResponseLine.
private static HttpResponseMetaData assertResponseLine(HttpProtocolVersion expectedVersion, HttpResponseStatus expectedStatus, EmbeddedChannel channel) {
HttpResponseMetaData response = channel.readInbound();
assertThat(response.version(), equalTo(expectedVersion));
assertThat(response.status().code(), is(expectedStatus.code()));
assertThat(response.status().reasonPhrase(), equalTo(expectedStatus.reasonPhrase()));
return response;
}
Aggregations