use of org.webpieces.httpparser.api.common.Header in project webpieces by deanhiller.
the class TestHttp11Basic method testSendTwoRequests.
@Test
public void testSendTwoRequests() throws InterruptedException, ExecutionException {
HttpRequest req = Requests.createRequest(KnownHttpMethod.GET, "/xxxx");
HttpRequest req2 = Requests.createRequest(KnownHttpMethod.GET, "/xxxx");
mockChannel.write(req);
PassedIn in1 = mockListener.getSingleRequest();
mockChannel.write(req2);
Assert.assertEquals(0, mockListener.getNumRequestsThatCameIn());
//send back request2's response first!!!! BUT verify it does not go to client per http11 pipelining rules
HttpResponse resp1 = Requests.createResponse(1);
resp1.addHeader(new Header(KnownHeaderName.CONTENT_LENGTH, "0"));
Http2Response headers1 = Http2Translations.responseToHeaders(resp1);
in1.stream.sendResponse(headers1);
HttpPayload payload = mockChannel.getFrameAndClear();
Assert.assertEquals(resp1, payload);
PassedIn in2 = mockListener.getSingleRequest();
HttpResponse resp2 = Requests.createResponse(2);
resp2.addHeader(new Header(KnownHeaderName.CONTENT_LENGTH, "0"));
Http2Response headers2 = Http2Translations.responseToHeaders(resp2);
in2.stream.sendResponse(headers2);
HttpPayload payload2 = mockChannel.getFrameAndClear();
Assert.assertEquals(resp2, payload2);
}
use of org.webpieces.httpparser.api.common.Header in project webpieces by deanhiller.
the class Http2Translations method requestToHeaders.
private static Http2Request requestToHeaders(HttpRequest request, boolean fromSslChannel) {
HttpRequestLine requestLine = request.getRequestLine();
List<Header> requestHeaders = request.getHeaders();
LinkedList<Http2Header> headerList = new LinkedList<>();
// add special headers
headerList.add(new Http2Header(":method", requestLine.getMethod().getMethodAsString()));
UrlInfo urlInfo = requestLine.getUri().getUriBreakdown();
headerList.add(new Http2Header(":path", urlInfo.getFullPath()));
// Figure out scheme
if (urlInfo.getPrefix() != null) {
headerList.add(new Http2Header(":scheme", urlInfo.getPrefix()));
} else if (fromSslChannel) {
headerList.add(new Http2Header(":scheme", "https"));
} else {
headerList.add(new Http2Header(":scheme", "http"));
}
// Figure out authority
Header hostHeader = request.getHeaderLookupStruct().getHeader(KnownHeaderName.HOST);
if (hostHeader == null)
throw new IllegalArgumentException("Host header is required in http1.1");
// Add regular headers
for (Header header : requestHeaders) {
if (header.getKnownName() == KnownHeaderName.HOST) {
//keeps headers in order of http1 headers
String h = hostHeader.getValue();
headerList.add(new Http2Header(":authority", h));
continue;
}
headerList.add(new Http2Header(header.getName().toLowerCase(), header.getValue()));
}
Http2Request headers = new Http2Request(headerList);
Header contentLen = request.getHeaderLookupStruct().getHeader(KnownHeaderName.CONTENT_LENGTH);
if (request.isHasChunkedTransferHeader()) {
headers.setEndOfStream(false);
} else if (contentLenGreaterThanZero(contentLen)) {
headers.setEndOfStream(false);
} else
headers.setEndOfStream(true);
return headers;
}
use of org.webpieces.httpparser.api.common.Header in project webpieces by deanhiller.
the class TestHttps method simulateLogin.
private Header simulateLogin() {
HttpRequest req1 = Requests.createRequest(KnownHttpMethod.POST, "/postLogin");
httpsSocket.send(req1);
FullResponse response1 = ResponseExtract.assertSingleResponse(httpsSocket);
Header header = response1.getResponse().getHeaderLookupStruct().getHeader(KnownHeaderName.SET_COOKIE);
String value = header.getValue();
value = value.replace("; path=/; HttpOnly", "");
Header cookie = new Header(KnownHeaderName.COOKIE, value);
return cookie;
}
use of org.webpieces.httpparser.api.common.Header in project webpieces by deanhiller.
the class TestScopes method testGetStaticFileDoesNotClearFlashMessage.
@Test
public void testGetStaticFileDoesNotClearFlashMessage() {
HttpRequest req = Requests.createRequest(KnownHttpMethod.GET, "/flashmessage");
http11Socket.send(req);
FullResponse response = ResponseExtract.assertSingleResponse(http11Socket);
response.assertStatusCode(KnownStatusCode.HTTP_200_OK);
Header header = response.createCookieRequestHeader();
HttpRequest req2 = Requests.createRequest(KnownHttpMethod.GET, "/public/fonts.css");
req2.addHeader(header);
http11Socket.send(req2);
FullResponse response2 = ResponseExtract.assertSingleResponse(http11Socket);
response2.assertStatusCode(KnownStatusCode.HTTP_200_OK);
Header cookie = response2.getResponse().getHeaderLookupStruct().getHeader(KnownHeaderName.SET_COOKIE);
Assert.assertNull("static routes should not be clearing cookies or things go south", cookie);
}
use of org.webpieces.httpparser.api.common.Header in project webpieces by deanhiller.
the class TestScopes method testSessionScopeModificationByClient.
@Test
public void testSessionScopeModificationByClient() {
HttpRequest req = Requests.createRequest(KnownHttpMethod.GET, "/home");
http11Socket.send(req);
FullResponse response = ResponseExtract.assertSingleResponse(http11Socket);
response.assertStatusCode(KnownStatusCode.HTTP_200_OK);
response.assertContains("age=30");
Header cookie = response.getResponse().getHeaderLookupStruct().getHeader(KnownHeaderName.SET_COOKIE);
String value = cookie.getValue();
value = value.replace("age=30", "age=60");
value = value.replace("; path=/; HttpOnly", "");
//modify cookie and rerequest...
HttpRequest req2 = Requests.createRequest(KnownHttpMethod.GET, "/displaySession");
req2.addHeader(new Header(KnownHeaderName.COOKIE, value));
http11Socket.send(req2);
response = ResponseExtract.assertSingleResponse(http11Socket);
response.assertStatusCode(KnownStatusCode.HTTP_303_SEEOTHER);
Header cookie2 = response.getResponse().getHeaderLookupStruct().getHeader(KnownHeaderName.SET_COOKIE);
String deleteCookie = cookie2.getValue();
Assert.assertEquals("webSession=; Max-Age=0; path=/; HttpOnly", deleteCookie);
Header header = response.getResponse().getHeaderLookupStruct().getHeader(KnownHeaderName.LOCATION);
String url = header.getValue();
Assert.assertEquals("http://myhost.com/displaySession", url);
}
Aggregations