Search in sources :

Example 26 with HttpDummyRequest

use of org.webpieces.webserver.test.HttpDummyRequest 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 27 with HttpDummyRequest

use of org.webpieces.webserver.test.HttpDummyRequest 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 28 with HttpDummyRequest

use of org.webpieces.webserver.test.HttpDummyRequest 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)

Example 29 with HttpDummyRequest

use of org.webpieces.webserver.test.HttpDummyRequest in project webpieces by deanhiller.

the class TestBeans method testDeveloperMistypesBeanNameVsFormNamesButNullableIsUsed.

@Test
public void testDeveloperMistypesBeanNameVsFormNamesButNullableIsUsed() {
    HttpDummyRequest req = Requests.createPostRequest("/postusernullable", "entity.firstName", "D&D", "entity.lastName", "Hiller", "entity.fullName", "Dean Hiller", "password", "hi");
    http11Socket.send(req);
    FullResponse response = ResponseExtract.assertSingleResponse(http11Socket);
    response.assertStatusCode(KnownStatusCode.HTTP_303_SEEOTHER);
}
Also used : FullResponse(org.webpieces.webserver.test.FullResponse) HttpDummyRequest(org.webpieces.webserver.test.HttpDummyRequest) AbstractWebpiecesTest(org.webpieces.webserver.test.AbstractWebpiecesTest) Test(org.junit.Test) WebserverForTest(org.webpieces.webserver.WebserverForTest)

Example 30 with HttpDummyRequest

use of org.webpieces.webserver.test.HttpDummyRequest in project webpieces by deanhiller.

the class TestJson method testAsyncWriteOnlyPost.

@Test
public void testAsyncWriteOnlyPost() {
    HttpDummyRequest req = Requests.createJsonRequest(KnownHttpMethod.POST, "/json/writeasync");
    http11Socket.send(req);
    FullResponse response = ResponseExtract.assertSingleResponse(http11Socket);
    response.assertStatusCode(KnownStatusCode.HTTP_200_OK);
    Assert.assertEquals("", response.getBodyAsString());
}
Also used : FullResponse(org.webpieces.webserver.test.FullResponse) HttpDummyRequest(org.webpieces.webserver.test.HttpDummyRequest) Test(org.junit.Test) AbstractWebpiecesTest(org.webpieces.webserver.test.AbstractWebpiecesTest) WebserverForTest(org.webpieces.webserver.WebserverForTest)

Aggregations

HttpDummyRequest (org.webpieces.webserver.test.HttpDummyRequest)35 FullResponse (org.webpieces.webserver.test.FullResponse)32 Test (org.junit.Test)31 WebserverForTest (org.webpieces.webserver.WebserverForTest)31 AbstractWebpiecesTest (org.webpieces.webserver.test.AbstractWebpiecesTest)31 Header (org.webpieces.httpparser.api.common.Header)9 HttpRequest (org.webpieces.httpparser.api.dto.HttpRequest)6 DataWrapper (org.webpieces.data.api.DataWrapper)4 UserDto (org.webpieces.webserver.basic.app.biz.UserDto)4 HttpData (org.webpieces.httpparser.api.dto.HttpData)3 UserTestDbo (org.webpieces.plugins.hibernate.app.dbo.UserTestDbo)3 ByteBuffer (java.nio.ByteBuffer)1 BufferCreationPool (org.webpieces.data.api.BufferCreationPool)1 DataWrapperGenerator (org.webpieces.data.api.DataWrapperGenerator)1 HttpParser (org.webpieces.httpparser.api.HttpParser)1 MarshalState (org.webpieces.httpparser.api.MarshalState)1 HttpRequestLine (org.webpieces.httpparser.api.dto.HttpRequestLine)1 HttpUri (org.webpieces.httpparser.api.dto.HttpUri)1