Search in sources :

Example 21 with HttpResponse

use of org.webpieces.httpparser.api.dto.HttpResponse in project webpieces by deanhiller.

the class TestResponseParsing method testBasic.

@Test
public void testBasic() {
    HttpResponseStatus status = new HttpResponseStatus();
    status.setKnownStatus(KnownStatusCode.HTTP_200_OK);
    HttpResponseStatusLine statusLine = new HttpResponseStatusLine();
    statusLine.setStatus(status);
    HttpResponse response = new HttpResponse();
    response.setStatusLine(statusLine);
    String result1 = response.toString();
    String result2 = parser.marshalToString(response);
    String msg = "HTTP/1.1 200 OK\r\n\r\n";
    Assert.assertEquals(msg, result1);
    Assert.assertEquals(msg, result2);
}
Also used : HttpResponseStatus(org.webpieces.httpparser.api.dto.HttpResponseStatus) HttpResponse(org.webpieces.httpparser.api.dto.HttpResponse) HttpResponseStatusLine(org.webpieces.httpparser.api.dto.HttpResponseStatusLine) Test(org.junit.Test)

Example 22 with HttpResponse

use of org.webpieces.httpparser.api.dto.HttpResponse in project webpieces by deanhiller.

the class TestResponseParsing method createOkResponse.

static HttpResponse createOkResponse() {
    Header header1 = new Header();
    header1.setName(KnownHeaderName.ACCEPT);
    header1.setValue("CooolValue");
    Header header2 = new Header();
    //let's keep the case even though name is case-insensitive..
    header2.setName("CustomerHEADER");
    header2.setValue("betterValue");
    HttpResponseStatus status = new HttpResponseStatus();
    status.setKnownStatus(KnownStatusCode.HTTP_200_OK);
    HttpResponseStatusLine statusLine = new HttpResponseStatusLine();
    statusLine.setStatus(status);
    HttpResponse resp = new HttpResponse();
    resp.setStatusLine(statusLine);
    resp.addHeader(header1);
    resp.addHeader(header2);
    return resp;
}
Also used : Header(org.webpieces.httpparser.api.common.Header) HttpResponseStatus(org.webpieces.httpparser.api.dto.HttpResponseStatus) HttpResponse(org.webpieces.httpparser.api.dto.HttpResponse) HttpResponseStatusLine(org.webpieces.httpparser.api.dto.HttpResponseStatusLine)

Example 23 with HttpResponse

use of org.webpieces.httpparser.api.dto.HttpResponse in project webpieces by deanhiller.

the class TestResponseParsing method testWithHeadersAndBody.

@Test
public void testWithHeadersAndBody() {
    HttpResponse response = createOkResponse();
    String result1 = response.toString();
    String result2 = parser.marshalToString(response);
    String msg = "HTTP/1.1 200 OK\r\n" + "Accept: CooolValue\r\n" + "CustomerHEADER: betterValue\r\n" + "\r\n";
    Assert.assertEquals(msg, result1);
    Assert.assertEquals(msg, result2);
}
Also used : HttpResponse(org.webpieces.httpparser.api.dto.HttpResponse) Test(org.junit.Test)

Example 24 with HttpResponse

use of org.webpieces.httpparser.api.dto.HttpResponse in project webpieces by deanhiller.

the class TestResponseParsing method testHalfThenTwoHalvesNext.

/**
	 * Send in 1/2 first http message and then send in 
	 * next 1/2 AND 1/2 of second message TOGETHER in 2nd
	 * payload of bytes to make sure it is handled correctly
	 * and then finally last 1/2
	 */
@Test
public void testHalfThenTwoHalvesNext() {
    HttpResponse request = createOkResponse();
    byte[] payload = unwrap(parser.marshalToByteBuffer(state, request));
    byte[] first = new byte[20];
    byte[] second = new byte[payload.length];
    byte[] third = new byte[payload.length - first.length];
    System.arraycopy(payload, 0, first, 0, first.length);
    System.arraycopy(payload, first.length, second, 0, payload.length - first.length);
    System.arraycopy(payload, 0, second, payload.length - first.length, first.length);
    System.arraycopy(payload, first.length, third, 0, third.length);
    DataWrapper data1 = dataGen.wrapByteArray(first);
    DataWrapper data2 = dataGen.wrapByteArray(second);
    DataWrapper data3 = dataGen.wrapByteArray(third);
    Memento memento = parser.prepareToParse();
    memento = parser.parse(memento, data1);
    Assert.assertEquals(0, memento.getParsedMessages().size());
    memento = parser.parse(memento, data2);
    Assert.assertEquals(1, memento.getParsedMessages().size());
    memento = parser.parse(memento, data3);
    Assert.assertEquals(1, memento.getParsedMessages().size());
}
Also used : DataWrapper(org.webpieces.data.api.DataWrapper) HttpResponse(org.webpieces.httpparser.api.dto.HttpResponse) Test(org.junit.Test)

Example 25 with HttpResponse

use of org.webpieces.httpparser.api.dto.HttpResponse in project webpieces by deanhiller.

the class TestResponseParsing method test2AndHalfHttpMessages.

@Test
public void test2AndHalfHttpMessages() {
    HttpResponse response = createOkResponse();
    byte[] payload = unwrap(parser.marshalToByteBuffer(state, response));
    byte[] first = new byte[2 * payload.length + 20];
    byte[] second = new byte[payload.length - 20];
    System.arraycopy(payload, 0, first, 0, payload.length);
    System.arraycopy(payload, 0, first, payload.length, payload.length);
    System.arraycopy(payload, 0, first, 2 * payload.length, 20);
    System.arraycopy(payload, 20, second, 0, second.length);
    DataWrapper data1 = dataGen.wrapByteArray(first);
    DataWrapper data2 = dataGen.wrapByteArray(second);
    Memento memento = parser.prepareToParse();
    memento = parser.parse(memento, data1);
    Assert.assertEquals(2, memento.getParsedMessages().size());
    memento = parser.parse(memento, data2);
    Assert.assertEquals(1, memento.getParsedMessages().size());
}
Also used : DataWrapper(org.webpieces.data.api.DataWrapper) HttpResponse(org.webpieces.httpparser.api.dto.HttpResponse) Test(org.junit.Test)

Aggregations

HttpResponse (org.webpieces.httpparser.api.dto.HttpResponse)26 Test (org.junit.Test)17 Header (org.webpieces.httpparser.api.common.Header)15 HttpPayload (org.webpieces.httpparser.api.dto.HttpPayload)13 DataWrapper (org.webpieces.data.api.DataWrapper)12 Http2Response (com.webpieces.hpack.api.dto.Http2Response)7 HttpResponseStatus (org.webpieces.httpparser.api.dto.HttpResponseStatus)7 HttpResponseStatusLine (org.webpieces.httpparser.api.dto.HttpResponseStatusLine)7 PassedIn (org.webpieces.httpfrontend2.api.mock2.MockHttp2RequestListener.PassedIn)6 HttpChunk (org.webpieces.httpparser.api.dto.HttpChunk)6 HttpRequest (org.webpieces.httpparser.api.dto.HttpRequest)6 HttpData (org.webpieces.httpparser.api.dto.HttpData)5 StreamWriter (com.webpieces.http2engine.api.StreamWriter)4 DataFrame (com.webpieces.http2parser.api.dto.DataFrame)4 Http2Header (com.webpieces.http2parser.api.dto.lib.Http2Header)4 HttpLastChunk (org.webpieces.httpparser.api.dto.HttpLastChunk)4 CompletableFuture (java.util.concurrent.CompletableFuture)2 PushStreamHandle (com.webpieces.http2engine.api.PushStreamHandle)1 Http2HeaderName (com.webpieces.http2parser.api.dto.lib.Http2HeaderName)1 Http2Msg (com.webpieces.http2parser.api.dto.lib.Http2Msg)1