Search in sources :

Example 11 with HttpData

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

the class TestRequestBody method testPartialHeaders.

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

Example 12 with HttpData

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

the class TestRequestBody method createPostRequestWithBody.

private HttpDummyRequest createPostRequestWithBody() {
    byte[] payload = new byte[30];
    for (int i = 0; i < payload.length; i++) {
        payload[i] = (byte) i;
    }
    HttpRequest request = TestRequestParsing.createPostRequest();
    Header header = new Header();
    header.setName(KnownHeaderName.CONTENT_LENGTH);
    header.setValue("" + payload.length);
    DataWrapper data = dataGen.wrapByteArray(payload);
    request.addHeader(header);
    HttpData httpData = new HttpData(data, true);
    return new HttpDummyRequest(request, httpData);
}
Also used : HttpRequest(org.webpieces.httpparser.api.dto.HttpRequest) DataWrapper(org.webpieces.data.api.DataWrapper) Header(org.webpieces.httpparser.api.common.Header) HttpData(org.webpieces.httpparser.api.dto.HttpData)

Example 13 with HttpData

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

the class Requests method createJsonRequest.

public static HttpDummyRequest createJsonRequest(KnownHttpMethod method, String url) {
    HttpRequest request = createRequest(method, url);
    String json = "{ `query`: `cats and dogs`, `meta`: { `numResults`: 4 } }".replace("`", "\"");
    DataWrapper body = gen.wrapByteArray(json.getBytes());
    HttpData data = new HttpData(body, true);
    request.addHeader(new Header(KnownHeaderName.CONTENT_LENGTH, body.getReadableSize() + ""));
    return new HttpDummyRequest(request, data);
}
Also used : HttpRequest(org.webpieces.httpparser.api.dto.HttpRequest) DataWrapper(org.webpieces.data.api.DataWrapper) Header(org.webpieces.httpparser.api.common.Header) HttpData(org.webpieces.httpparser.api.dto.HttpData) HttpDummyRequest(org.webpieces.webserver.test.HttpDummyRequest)

Example 14 with HttpData

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

the class Requests method createPostRequestImpl.

private static HttpDummyRequest createPostRequestImpl(String url, String... argTuples) throws UnsupportedEncodingException {
    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;
    }
    byte[] bytes = encodedParams.getBytes(StandardCharsets.UTF_8);
    DataWrapper body = gen.wrapByteArray(bytes);
    HttpData data = new HttpData(body, true);
    req.addHeader(new Header(KnownHeaderName.CONTENT_LENGTH, "" + body.getReadableSize()));
    req.addHeader(new Header(KnownHeaderName.CONTENT_TYPE, "application/x-www-form-urlencoded"));
    return new HttpDummyRequest(req, data);
}
Also used : HttpRequest(org.webpieces.httpparser.api.dto.HttpRequest) DataWrapper(org.webpieces.data.api.DataWrapper) HttpRequestLine(org.webpieces.httpparser.api.dto.HttpRequestLine) Header(org.webpieces.httpparser.api.common.Header) HttpData(org.webpieces.httpparser.api.dto.HttpData) HttpDummyRequest(org.webpieces.webserver.test.HttpDummyRequest) HttpUri(org.webpieces.httpparser.api.dto.HttpUri)

Example 15 with HttpData

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

the class Requests method createBadJsonRequest.

public static HttpDummyRequest createBadJsonRequest(KnownHttpMethod method, String url) {
    HttpRequest request = createRequest(method, url);
    String json = "{ `query `cats and dogs`, `meta`: { `numResults`: 4 } }".replace("`", "\"");
    DataWrapper body = gen.wrapByteArray(json.getBytes());
    HttpData data = new HttpData(body, true);
    request.addHeader(new Header(KnownHeaderName.CONTENT_LENGTH, body.getReadableSize() + ""));
    return new HttpDummyRequest(request, data);
}
Also used : HttpRequest(org.webpieces.httpparser.api.dto.HttpRequest) DataWrapper(org.webpieces.data.api.DataWrapper) Header(org.webpieces.httpparser.api.common.Header) HttpData(org.webpieces.httpparser.api.dto.HttpData) HttpDummyRequest(org.webpieces.webserver.test.HttpDummyRequest)

Aggregations

DataWrapper (org.webpieces.data.api.DataWrapper)15 HttpData (org.webpieces.httpparser.api.dto.HttpData)15 HttpRequest (org.webpieces.httpparser.api.dto.HttpRequest)11 Header (org.webpieces.httpparser.api.common.Header)9 Test (org.junit.Test)8 HttpPayload (org.webpieces.httpparser.api.dto.HttpPayload)7 Http2Response (com.webpieces.hpack.api.dto.Http2Response)4 PassedIn (org.webpieces.httpfrontend2.api.mock2.MockHttp2RequestListener.PassedIn)4 HttpResponse (org.webpieces.httpparser.api.dto.HttpResponse)4 HttpDummyRequest (org.webpieces.webserver.test.HttpDummyRequest)3 StreamWriter (com.webpieces.http2engine.api.StreamWriter)2 DataFrame (com.webpieces.http2parser.api.dto.DataFrame)2 HttpRequestLine (org.webpieces.httpparser.api.dto.HttpRequestLine)2 HttpUri (org.webpieces.httpparser.api.dto.HttpUri)2 Http2Header (com.webpieces.http2parser.api.dto.lib.Http2Header)1 ArrayList (java.util.ArrayList)1