Search in sources :

Example 21 with DataWrapper

use of org.webpieces.data.api.DataWrapper in project webpieces by deanhiller.

the class TestS6_5SettingsFrameErrors method testSection6_5AckNonEmptyPayload.

@Test
public void testSection6_5AckNonEmptyPayload() {
    String badAckFrame = // length
    "00 00 01" + // type
    "04" + // flags (ack)
    "01" + // R + streamid
    "00 00 00 00" + //payload 
    "00";
    //ack client frame
    mockChannel.sendHexBack(badAckFrame);
    //remote receives goAway
    GoAwayFrame goAway = (GoAwayFrame) mockChannel.getFrameAndClear();
    DataWrapper debugData = goAway.getDebugData();
    String msg = debugData.createStringFromUtf8(0, debugData.getReadableSize());
    Assert.assertEquals("ConnectionException: stream0:(FRAME_SIZE_INCORRECT) size of payload of a settings frame ack must be 0 but was=1", msg);
    Assert.assertTrue(mockChannel.isClosed());
}
Also used : DataWrapper(org.webpieces.data.api.DataWrapper) GoAwayFrame(com.webpieces.http2parser.api.dto.GoAwayFrame) Test(org.junit.Test)

Example 22 with DataWrapper

use of org.webpieces.data.api.DataWrapper in project webpieces by deanhiller.

the class TestRequestBody method testPartialBody.

@Test
public void testPartialBody() {
    HttpDummyRequest request = createPostRequestWithBody();
    byte[] expected = request.getHttpData().getBody().createByteArray();
    byte[] data = unwrap(request);
    byte[] first = new byte[data.length - 20];
    byte[] second = new byte[data.length - first.length];
    System.arraycopy(data, 0, first, 0, first.length);
    System.arraycopy(data, first.length, second, 0, second.length);
    DataWrapper wrap1 = dataGen.wrapByteArray(first);
    DataWrapper wrap2 = dataGen.wrapByteArray(second);
    Memento memento = parser.prepareToParse();
    memento = parser.parse(memento, wrap1);
    Assert.assertEquals(ParsingState.BODY, memento.getUnParsedState().getCurrentlyParsing());
    Assert.assertEquals(0, memento.getUnParsedState().getCurrentUnparsedSize());
    HttpRequest httpRequest = memento.getParsedMessages().get(0).getHttpRequest();
    Assert.assertEquals(request.getRequest(), httpRequest);
    HttpData data1 = memento.getParsedMessages().get(1).getHttpData();
    Assert.assertEquals(2, memento.getParsedMessages().size());
    memento = parser.parse(memento, wrap2);
    Assert.assertEquals(1, memento.getParsedMessages().size());
    HttpData data2 = memento.getParsedMessages().get(0).getHttpData();
    DataWrapper body = dataGen.chainDataWrappers(data1.getBody(), data2.getBody());
    byte[] bodyBytesActual = body.createByteArray();
    Assert.assertArrayEquals(expected, bodyBytesActual);
}
Also used : DataWrapper(org.webpieces.data.api.DataWrapper) HttpRequest(org.webpieces.httpparser.api.dto.HttpRequest) HttpData(org.webpieces.httpparser.api.dto.HttpData) Test(org.junit.Test)

Example 23 with DataWrapper

use of org.webpieces.data.api.DataWrapper in project webpieces by deanhiller.

the class TestBeans method testIncomingDataAndDataSeperate.

@Test
public void testIncomingDataAndDataSeperate() {
    HttpDummyRequest req = Requests.createPostRequest("/postArray2", "user.accounts[1].name", "Account2Name", "user.accounts[1].color", "green", "user.accounts[2].addresses[0].number", "56", "user.firstName", "D&D", "user.lastName", "Hiller", "user.fullName", "Dean Hiller");
    DataWrapperGenerator dataGen = DataWrapperGeneratorFactory.createDataWrapperGenerator();
    HttpParser parser = HttpParserFactory.createParser(new BufferCreationPool());
    MarshalState state = parser.prepareToMarshal();
    ByteBuffer buffer = parser.marshalToByteBuffer(state, req.getRequest());
    DataWrapper d1 = dataGen.wrapByteBuffer(buffer);
    ByteBuffer buf2 = parser.marshalToByteBuffer(state, req.getData());
    DataWrapper data = dataGen.chainDataWrappers(d1, dataGen.wrapByteBuffer(buf2));
    // Split the body in half
    List<? extends DataWrapper> split = dataGen.split(data, data.getReadableSize() - 20);
    http11Socket.sendBytes(split.get(0));
    http11Socket.sendBytes(split.get(1));
    FullResponse response = ResponseExtract.assertSingleResponse(http11Socket);
    response.assertStatusCode(KnownStatusCode.HTTP_303_SEEOTHER);
    UserDto user = mockSomeLib.getUser();
    Assert.assertEquals("D&D", user.getFirstName());
    Assert.assertEquals(3, user.getAccounts().size());
    Assert.assertEquals("Account2Name", user.getAccounts().get(1).getName());
    Assert.assertEquals(56, user.getAccounts().get(2).getAddresses().get(0).getNumber());
}
Also used : DataWrapper(org.webpieces.data.api.DataWrapper) DataWrapperGenerator(org.webpieces.data.api.DataWrapperGenerator) MarshalState(org.webpieces.httpparser.api.MarshalState) FullResponse(org.webpieces.webserver.test.FullResponse) HttpDummyRequest(org.webpieces.webserver.test.HttpDummyRequest) UserDto(org.webpieces.webserver.basic.app.biz.UserDto) HttpParser(org.webpieces.httpparser.api.HttpParser) ByteBuffer(java.nio.ByteBuffer) BufferCreationPool(org.webpieces.data.api.BufferCreationPool) AbstractWebpiecesTest(org.webpieces.webserver.test.AbstractWebpiecesTest) Test(org.junit.Test) WebserverForTest(org.webpieces.webserver.WebserverForTest)

Example 24 with DataWrapper

use of org.webpieces.data.api.DataWrapper in project webpieces by deanhiller.

the class MockTcpChannel method write.

@Override
public CompletableFuture<Channel> write(ByteBuffer b) {
    DataWrapper data = dataGen.wrapByteBuffer(b);
    memento = parser.parse(memento, data);
    List<HttpPayload> parsedMessages = memento.getParsedMessages();
    for (HttpPayload payload : parsedMessages) {
        if (payload instanceof HttpResponse) {
            sendResponse((HttpResponse) payload);
        } else {
            sendData((HttpData) payload);
        }
    }
    return CompletableFuture.completedFuture(this);
}
Also used : DataWrapper(org.webpieces.data.api.DataWrapper) HttpPayload(org.webpieces.httpparser.api.dto.HttpPayload) HttpResponse(org.webpieces.httpparser.api.dto.HttpResponse)

Example 25 with DataWrapper

use of org.webpieces.data.api.DataWrapper in project webpieces by deanhiller.

the class FullResponse method uncompressBodyAndAssertContainsString.

public void uncompressBodyAndAssertContainsString(String text) {
    Header header = getResponse().getHeaderLookupStruct().getHeader(KnownHeaderName.CONTENT_ENCODING);
    if (header == null)
        throw new IllegalStateException("Body is not compressed as no CONTENT_ENCODING header field exists");
    else if (!"gzip".equals(header.getValue()))
        throw new IllegalStateException("Body has wrong compression type=" + header.getValue() + " in CONTENT_ENCODING header field");
    DataWrapper wrapper = getBody();
    byte[] compressed = wrapper.createByteArray();
    ByteArrayInputStream in = new ByteArrayInputStream(compressed);
    byte[] out = new byte[10000];
    DataWrapper output = dataGen.emptyWrapper();
    try (GZIPInputStream str = new GZIPInputStream(in)) {
        int read = 0;
        while ((read = str.read(out)) > 0) {
            ByteBuffer buffer = ByteBuffer.wrap(out, 0, read);
            DataWrapper byteWrapper = dataGen.wrapByteBuffer(buffer);
            output = dataGen.chainDataWrappers(output, byteWrapper);
            out = new byte[10000];
        }
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    Charset charset = extractCharset();
    String bodyAsString = output.createStringFrom(0, output.getReadableSize(), charset);
    if (!bodyAsString.contains(text))
        throw new IllegalStateException("Expected compressed body to contain='" + text + "' but body was=" + bodyAsString);
}
Also used : DataWrapper(org.webpieces.data.api.DataWrapper) GZIPInputStream(java.util.zip.GZIPInputStream) Header(org.webpieces.httpparser.api.common.Header) ByteArrayInputStream(java.io.ByteArrayInputStream) Charset(java.nio.charset.Charset) IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer)

Aggregations

DataWrapper (org.webpieces.data.api.DataWrapper)115 Test (org.junit.Test)47 DataFrame (com.webpieces.http2parser.api.dto.DataFrame)22 ByteBuffer (java.nio.ByteBuffer)20 GoAwayFrame (com.webpieces.http2parser.api.dto.GoAwayFrame)18 Header (org.webpieces.httpparser.api.common.Header)17 HttpPayload (org.webpieces.httpparser.api.dto.HttpPayload)17 HttpRequest (org.webpieces.httpparser.api.dto.HttpRequest)16 HttpData (org.webpieces.httpparser.api.dto.HttpData)15 HttpResponse (org.webpieces.httpparser.api.dto.HttpResponse)12 Http2Request (com.webpieces.hpack.api.dto.Http2Request)10 StreamWriter (com.webpieces.http2engine.api.StreamWriter)8 PassedIn (org.webpieces.httpfrontend2.api.mock2.MockHttp2RequestListener.PassedIn)8 MockResponseListener (org.webpieces.http2client.mock.MockResponseListener)7 HttpChunk (org.webpieces.httpparser.api.dto.HttpChunk)7 Http2Response (com.webpieces.hpack.api.dto.Http2Response)6 ConnectionClosedException (com.webpieces.http2engine.api.ConnectionClosedException)6 ArrayList (java.util.ArrayList)6 ShutdownStream (com.webpieces.http2engine.api.error.ShutdownStream)5 ConnectionException (com.webpieces.http2parser.api.dto.error.ConnectionException)5