use of org.webpieces.httpparser.api.dto.HttpData in project webpieces by deanhiller.
the class TestHttp11Basic method testSendTwoRequestsStreamSecond.
@Test
public void testSendTwoRequestsStreamSecond() throws InterruptedException, ExecutionException {
HttpRequest req = Requests.createRequest(KnownHttpMethod.GET, "/xxxx");
HttpRequest req2 = Requests.createRequest(KnownHttpMethod.GET, "/xxxx");
mockChannel.write(req);
PassedIn in1 = mockListener.getSingleRequest();
req2.addHeader(new Header(KnownHeaderName.CONTENT_LENGTH, "20"));
mockChannel.write(req2);
Assert.assertEquals(0, mockListener.getNumRequestsThatCameIn());
byte[] buf = new byte[10];
DataWrapper dataWrapper = dataGen.wrapByteArray(buf);
HttpData data1 = new HttpData(dataWrapper, false);
mockChannel.write(data1);
DataWrapper dataWrapper2 = dataGen.wrapByteArray(buf);
HttpData data2 = new HttpData(dataWrapper2, true);
mockChannel.write(data2);
Assert.assertEquals(0, mockListener.getNumRequestsThatCameIn());
//send back request2's response first!!!! BUT verify it does not go to client per http11 pipelining rules
HttpResponse resp1 = Requests.createResponse(1);
resp1.addHeader(new Header(KnownHeaderName.CONTENT_LENGTH, "0"));
Http2Response headers1 = Http2Translations.responseToHeaders(resp1);
in1.stream.sendResponse(headers1);
HttpPayload payload = mockChannel.getFrameAndClear();
Assert.assertEquals(resp1, payload);
PassedIn in2 = mockListener.getSingleRequest();
HttpResponse resp2 = Requests.createResponse(2);
resp2.addHeader(new Header(KnownHeaderName.CONTENT_LENGTH, "0"));
Http2Response headers2 = Http2Translations.responseToHeaders(resp2);
in2.stream.sendResponse(headers2);
HttpPayload payload2 = mockChannel.getFrameAndClear();
Assert.assertEquals(resp2, payload2);
}
use of org.webpieces.httpparser.api.dto.HttpData in project webpieces by deanhiller.
the class TestHttp11Basic method testSendTwoRequestsStreamFirst.
@Test
public void testSendTwoRequestsStreamFirst() throws InterruptedException, ExecutionException {
HttpRequest req = Requests.createRequest(KnownHttpMethod.GET, "/xxxx");
HttpRequest req2 = Requests.createRequest(KnownHttpMethod.GET, "/xxxx");
req.addHeader(new Header(KnownHeaderName.CONTENT_LENGTH, "20"));
mockChannel.write(req);
PassedIn in1 = mockListener.getSingleRequest();
byte[] buf = new byte[10];
DataWrapper dataWrapper = dataGen.wrapByteArray(buf);
HttpData data1 = new HttpData(dataWrapper, false);
mockChannel.write(data1);
Assert.assertEquals(0, mockListener.getNumRequestsThatCameIn());
DataWrapper dataWrapper2 = dataGen.wrapByteArray(buf);
HttpData data2 = new HttpData(dataWrapper2, true);
mockChannel.write(data2);
Assert.assertEquals(0, mockListener.getNumRequestsThatCameIn());
mockChannel.write(req2);
Assert.assertEquals(0, mockListener.getNumRequestsThatCameIn());
//send back request2's response first!!!! BUT verify it does not go to client per http11 pipelining rules
HttpResponse resp1 = Requests.createResponse(1);
resp1.addHeader(new Header(KnownHeaderName.CONTENT_LENGTH, "0"));
Http2Response headers1 = Http2Translations.responseToHeaders(resp1);
in1.stream.sendResponse(headers1);
HttpPayload payload = mockChannel.getFrameAndClear();
Assert.assertEquals(resp1, payload);
PassedIn in2 = mockListener.getSingleRequest();
HttpResponse resp2 = Requests.createResponse(2);
resp2.addHeader(new Header(KnownHeaderName.CONTENT_LENGTH, "0"));
Http2Response headers2 = Http2Translations.responseToHeaders(resp2);
in2.stream.sendResponse(headers2);
HttpPayload payload2 = mockChannel.getFrameAndClear();
Assert.assertEquals(resp2, payload2);
}
use of org.webpieces.httpparser.api.dto.HttpData in project webpieces by deanhiller.
the class TestHttp11Basic method testUploadWithBody.
@Test
public void testUploadWithBody() throws InterruptedException, ExecutionException, TimeoutException {
String bodyStr = "hi there, how are you";
DataWrapper dataWrapper = dataGen.wrapByteArray(bodyStr.getBytes(StandardCharsets.UTF_8));
HttpRequest req = Requests.createRequest(KnownHttpMethod.GET, "/xxxx");
HttpData body = new HttpData(dataWrapper, true);
req.addHeader(new Header(KnownHeaderName.CONTENT_LENGTH, "" + dataWrapper.getReadableSize()));
mockChannel.write(req);
mockChannel.write(body);
PassedIn in1 = mockListener.getSingleRequest();
HttpRequest req1 = Http2Translations.translateRequest(in1.request);
Assert.assertEquals(req, req1);
DataFrame frame = (DataFrame) mockStreamWriter.getSingleFrame();
DataWrapper data = frame.getData();
Assert.assertEquals(bodyStr, data.createStringFromUtf8(0, data.getReadableSize()));
Assert.assertTrue(frame.isEndOfStream());
HttpResponse resp = Requests.createNobodyResponse();
Http2Response http2Resp = Http2Translations.responseToHeaders(resp);
CompletableFuture<StreamWriter> fut = in1.stream.sendResponse(http2Resp);
fut.get(2, TimeUnit.SECONDS);
HttpResponse respToClient = (HttpResponse) mockChannel.getFrameAndClear();
Assert.assertEquals(resp, respToClient);
}
use of org.webpieces.httpparser.api.dto.HttpData in project webpieces by deanhiller.
the class Requests method createPostRequest.
public static List<HttpPayload> createPostRequest(String url, String... argTuples) {
if (argTuples.length % 2 != 0)
throw new IllegalArgumentException("argTuples.length must be of even size (key/value)");
HttpUri httpUri = new HttpUri(url);
HttpRequestLine requestLine = new HttpRequestLine();
requestLine.setMethod(KnownHttpMethod.POST);
requestLine.setUri(httpUri);
HttpRequest req = new HttpRequest();
req.setRequestLine(requestLine);
req.addHeader(new Header(KnownHeaderName.HOST, "myhost.com"));
String encodedParams = "";
for (int i = 0; i < argTuples.length; i += 2) {
String key = URLEncoder.encode(argTuples[i], StandardCharsets.UTF_8);
String value = URLEncoder.encode(argTuples[i + 1], StandardCharsets.UTF_8);
if (!"".equals(encodedParams))
encodedParams += "&";
encodedParams += key + "=" + value;
}
HttpData data = new HttpData();
data.setEndOfData(true);
byte[] bytes = encodedParams.getBytes(StandardCharsets.UTF_8);
DataWrapper body = gen.wrapByteArray(bytes);
data.setBody(body);
req.addHeader(new Header(KnownHeaderName.CONTENT_LENGTH, "" + body.getReadableSize()));
req.addHeader(new Header(KnownHeaderName.CONTENT_TYPE, "application/x-www-form-urlencoded"));
List<HttpPayload> payloads = new ArrayList<>();
payloads.add(req);
payloads.add(data);
return payloads;
}
use of org.webpieces.httpparser.api.dto.HttpData in project webpieces by deanhiller.
the class HttpParserImpl method readInContentLengthBody.
private void readInContentLengthBody(MementoImpl memento) {
Integer toRead = memento.getContentLengthLeftToRead();
DataWrapper data = memento.getLeftOverData();
int readSize = Math.min(toRead, data.getReadableSize());
if (readSize == 0) {
//wait for more data
return;
}
int newSizeLeft = toRead - readSize;
boolean isEos;
if (newSizeLeft == 0) {
isEos = true;
memento.setContentLengthLeftToRead(null);
} else {
isEos = false;
memento.setContentLengthLeftToRead(newSizeLeft);
}
List<? extends DataWrapper> split = dataGen.split(data, readSize);
HttpData httpData = new HttpData(split.get(0), isEos);
memento.setLeftOverData(split.get(1));
memento.addMessage(httpData);
}
Aggregations