Search in sources :

Example 31 with HttpPayload

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

the class TestReceiveHttp2 method testReceivePreface.

@Test
public void testReceivePreface() {
    // All the 1's must be ignored and in leftover data
    String preface = "PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n1111";
    byte[] bytes = preface.getBytes(StandardCharsets.UTF_8);
    DataWrapper moreData = dataGen.wrapByteArray(bytes);
    Memento state = parser.prepareToParse();
    state = parser.parse(state, moreData);
    Assert.assertEquals(1, state.getParsedMessages().size());
    Assert.assertEquals(4, state.getLeftOverData().getReadableSize());
    HttpPayload httpPayload = state.getParsedMessages().get(0);
    Assert.assertEquals(HttpMessageType.HTTP2_MARKER_MSG, httpPayload.getMessageType());
}
Also used : DataWrapper(org.webpieces.data.api.DataWrapper) HttpPayload(org.webpieces.httpparser.api.dto.HttpPayload) Test(org.junit.Test)

Example 32 with HttpPayload

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

the class TestRequestParsing method testPartialHttpMessage.

@Test
public void testPartialHttpMessage() {
    HttpRequest request = createPostRequest();
    byte[] payload = unwrap(parser.marshalToByteBuffer(state, request));
    byte[] firstPart = new byte[10];
    byte[] secondPart = new byte[payload.length - firstPart.length];
    // let's split the payload up into two pieces
    System.arraycopy(payload, 0, firstPart, 0, firstPart.length);
    System.arraycopy(payload, firstPart.length, secondPart, 0, secondPart.length);
    DataWrapper data1 = dataGen.wrapByteArray(firstPart);
    DataWrapper data2 = dataGen.wrapByteArray(secondPart);
    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());
    HttpPayload httpMessage = memento.getParsedMessages().get(0);
    HttpRequest req = (HttpRequest) httpMessage;
    Assert.assertEquals(request.getRequestLine(), req.getRequestLine());
    Assert.assertEquals(request.getHeaders(), req.getHeaders());
    Assert.assertEquals(request, httpMessage);
}
Also used : HttpRequest(org.webpieces.httpparser.api.dto.HttpRequest) DataWrapper(org.webpieces.data.api.DataWrapper) HttpPayload(org.webpieces.httpparser.api.dto.HttpPayload) Test(org.junit.Test)

Example 33 with HttpPayload

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

the class TestGooglePlayback method testHttpsGooglePlayback.

@Test
public void testHttpsGooglePlayback() {
    List<HttpPayload> results = runPlayback("https.google.com.recording");
    int size = 0;
    for (HttpPayload result : results) {
        if (result.getMessageType() != HttpMessageType.DATA)
            continue;
        size += result.getHttpData().getBodyNonNull().getReadableSize();
    }
    Assert.assertEquals(10396, size);
    HttpResponse resp = (HttpResponse) results.get(0);
    HttpLastData lastChunk = (HttpLastData) results.get(results.size() - 1);
    Assert.assertEquals("chunked", resp.getHeaderLookupStruct().getHeader(KnownHeaderName.TRANSFER_ENCODING).getValue());
    Assert.assertTrue(lastChunk.isEndOfData());
}
Also used : HttpPayload(org.webpieces.httpparser.api.dto.HttpPayload) HttpResponse(org.webpieces.httpparser.api.dto.HttpResponse) HttpLastData(org.webpieces.httpparser.api.dto.HttpLastData) Test(org.junit.Test)

Example 34 with HttpPayload

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

the class TestGooglePlayback method runPlayback.

private List<HttpPayload> runPlayback(String name) {
    Memento mem = parser.prepareToParse();
    int counter = 0;
    ClassLoader cl = getClass().getClassLoader();
    InputStream in = cl.getResourceAsStream(name);
    // This loads relative to this test class package(while the above does not).
    // InputStream in = getClass().getResourceAsStream(name);
    Playback playback = RecordingPlaybackFactory.createPlayback(in, 1);
    List<HttpPayload> results = new ArrayList<>();
    while (true) {
        counter++;
        if (counter > 1000)
            throw new IllegalArgumentException("Is your simulation really this long...1000+ buffers?");
        ByteBuffer buffer = playback.getNextPacket();
        if (buffer == null)
            return results;
        DataWrapper data = dataGen.wrapByteBuffer(buffer);
        mem = parser.parse(mem, data);
        List<HttpPayload> parsedMessages = mem.getParsedMessages();
        results.addAll(parsedMessages);
    }
}
Also used : DataWrapper(org.webpieces.data.api.DataWrapper) Memento(org.webpieces.httpparser.api.Memento) HttpPayload(org.webpieces.httpparser.api.dto.HttpPayload) InputStream(java.io.InputStream) Playback(org.webpieces.recording.api.Playback) ArrayList(java.util.ArrayList) ByteBuffer(java.nio.ByteBuffer)

Example 35 with HttpPayload

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

the class TestGooglePlayback method testGooglePlayback.

@Test
public void testGooglePlayback() {
    List<HttpPayload> results = runPlayback("google.com.11015.recording");
    int size = 0;
    for (HttpPayload result : results) {
        if (result.getMessageType() != HttpMessageType.DATA)
            continue;
        size += result.getHttpData().getBodyNonNull().getReadableSize();
    }
    Assert.assertEquals(10349, size);
    HttpResponse resp = (HttpResponse) results.get(0);
    HttpLastData lastChunk = (HttpLastData) results.get(results.size() - 1);
    Assert.assertEquals("chunked", resp.getHeaderLookupStruct().getHeader(KnownHeaderName.TRANSFER_ENCODING).getValue());
    Assert.assertTrue(lastChunk.isEndOfData());
}
Also used : HttpPayload(org.webpieces.httpparser.api.dto.HttpPayload) HttpResponse(org.webpieces.httpparser.api.dto.HttpResponse) HttpLastData(org.webpieces.httpparser.api.dto.HttpLastData) Test(org.junit.Test)

Aggregations

HttpPayload (org.webpieces.httpparser.api.dto.HttpPayload)35 DataWrapper (org.webpieces.data.api.DataWrapper)30 Test (org.junit.Test)24 HttpResponse (org.webpieces.httpparser.api.dto.HttpResponse)23 Header (org.webpieces.httpparser.api.common.Header)18 HttpData (org.webpieces.httpparser.api.dto.HttpData)14 HttpRequest (org.webpieces.httpparser.api.dto.HttpRequest)13 Http2Response (com.webpieces.http2.api.dto.highlevel.Http2Response)8 PassedIn (org.webpieces.httpfrontend2.api.mock2.MockHttp2RequestListener.PassedIn)8 StreamWriter (com.webpieces.http2.api.streaming.StreamWriter)6 DataFrame (com.webpieces.http2.api.dto.lowlevel.DataFrame)4 HttpChunk (org.webpieces.httpparser.api.dto.HttpChunk)4 Http2Msg (com.webpieces.http2.api.dto.lowlevel.lib.Http2Msg)3 ByteBuffer (java.nio.ByteBuffer)3 Memento (org.webpieces.httpparser.api.Memento)3 HttpLastData (org.webpieces.httpparser.api.dto.HttpLastData)3 XFuture (org.webpieces.util.futures.XFuture)3 InputStream (java.io.InputStream)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2