Search in sources :

Example 56 with Header

use of org.webpieces.httpparser.api.common.Header 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 57 with Header

use of org.webpieces.httpparser.api.common.Header 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 58 with Header

use of org.webpieces.httpparser.api.common.Header in project webpieces by deanhiller.

the class Requests method createGetRequest.

public static HttpRequest createGetRequest(String domain, String url) {
    HttpUri httpUri = new HttpUri(url);
    HttpRequestLine requestLine = new HttpRequestLine();
    requestLine.setMethod(KnownHttpMethod.GET);
    requestLine.setUri(httpUri);
    HttpRequest req = new HttpRequest();
    req.setRequestLine(requestLine);
    req.addHeader(new Header(KnownHeaderName.HOST, domain));
    return req;
}
Also used : HttpRequest(org.webpieces.httpparser.api.dto.HttpRequest) HttpRequestLine(org.webpieces.httpparser.api.dto.HttpRequestLine) Header(org.webpieces.httpparser.api.common.Header) HttpUri(org.webpieces.httpparser.api.dto.HttpUri)

Example 59 with Header

use of org.webpieces.httpparser.api.common.Header in project webpieces by deanhiller.

the class TestSyncWebServer method testBasic.

@Test
public void testBasic() {
    HttpRequest req = Requests.createRequest(KnownHttpMethod.GET, "/myroute");
    http11Socket.send(req);
    FullResponse response = ResponseExtract.assertSingleResponse(http11Socket);
    response.assertStatusCode(KnownStatusCode.HTTP_200_OK);
    response.assertContains("This is the first raw html page");
    response.assertContentType("text/html; charset=utf-8");
    List<Header> headers = response.getResponse().getHeaderLookupStruct().getHeaders(KnownHeaderName.CONTENT_TYPE);
    Assert.assertEquals(1, headers.size());
}
Also used : HttpRequest(org.webpieces.httpparser.api.dto.HttpRequest) FullResponse(org.webpieces.webserver.test.FullResponse) Header(org.webpieces.httpparser.api.common.Header) Test(org.junit.Test) AbstractWebpiecesTest(org.webpieces.webserver.test.AbstractWebpiecesTest) WebserverForTest(org.webpieces.webserver.WebserverForTest)

Example 60 with Header

use of org.webpieces.httpparser.api.common.Header in project webpieces by deanhiller.

the class TestI18n method testChineseText.

@Test
public void testChineseText() {
    HttpRequest req = Requests.createRequest(KnownHttpMethod.GET, "/i18nBasic");
    req.addHeader(new Header(KnownHeaderName.ACCEPT_LANGUAGE, "zh-CN"));
    http11Socket.send(req);
    FullResponse response = ResponseExtract.assertSingleResponse(http11Socket);
    response.assertStatusCode(KnownStatusCode.HTTP_200_OK);
    response.assertContains("你好, 这个是一个比较长的一个东西 我可以写比较多。  我在北京师范大学学了中文。 我喜欢完冰球");
    response.assertContains("你好Dean,我们要去Italy");
}
Also used : HttpRequest(org.webpieces.httpparser.api.dto.HttpRequest) Header(org.webpieces.httpparser.api.common.Header) FullResponse(org.webpieces.webserver.test.FullResponse) Test(org.junit.Test) AbstractWebpiecesTest(org.webpieces.webserver.test.AbstractWebpiecesTest) WebserverForTest(org.webpieces.webserver.WebserverForTest)

Aggregations

Header (org.webpieces.httpparser.api.common.Header)64 HttpRequest (org.webpieces.httpparser.api.dto.HttpRequest)32 Test (org.junit.Test)24 DataWrapper (org.webpieces.data.api.DataWrapper)18 HttpResponse (org.webpieces.httpparser.api.dto.HttpResponse)16 FullResponse (org.webpieces.webserver.test.FullResponse)16 AbstractWebpiecesTest (org.webpieces.webserver.test.AbstractWebpiecesTest)13 WebserverForTest (org.webpieces.webserver.WebserverForTest)12 HttpData (org.webpieces.httpparser.api.dto.HttpData)11 Http2Header (com.webpieces.http2parser.api.dto.lib.Http2Header)10 HttpPayload (org.webpieces.httpparser.api.dto.HttpPayload)10 HttpRequestLine (org.webpieces.httpparser.api.dto.HttpRequestLine)10 HttpUri (org.webpieces.httpparser.api.dto.HttpUri)9 HttpDummyRequest (org.webpieces.webserver.test.HttpDummyRequest)9 Http2Response (com.webpieces.hpack.api.dto.Http2Response)8 PassedIn (org.webpieces.httpfrontend2.api.mock2.MockHttp2RequestListener.PassedIn)7 HttpChunk (org.webpieces.httpparser.api.dto.HttpChunk)7 DataFrame (com.webpieces.http2parser.api.dto.DataFrame)5 HttpResponseStatus (org.webpieces.httpparser.api.dto.HttpResponseStatus)5 StreamWriter (com.webpieces.http2engine.api.StreamWriter)4