use of com.webpieces.http2.api.dto.highlevel.Http2Request in project webpieces by deanhiller.
the class ResponseCreator method addCommonHeaders2.
public Http2Response addCommonHeaders2(Http2Request request, String responseMimeType, int statusCode, String statusReason) {
Http2Response response = new Http2Response();
response.addHeader(new Http2Header(Http2HeaderName.STATUS, statusCode + ""));
if (statusReason != null)
response.addHeader(new Http2Header("reason", statusReason));
String connHeader = request.getSingleHeaderValue(Http2HeaderName.CONNECTION);
String dateStr = DateTimeFormatter.RFC_1123_DATE_TIME.format(ZonedDateTime.now(ZoneOffset.UTC));
Http2Header versionHeader = new Http2Header(Http2HeaderName.SERVER, version);
response.addHeader(versionHeader);
// in general, nearly all these headers are desired..
Http2Header date = new Http2Header(Http2HeaderName.DATE, dateStr);
response.addHeader(date);
if (responseMimeType != null)
response.addHeader(new Http2Header(Http2HeaderName.CONTENT_TYPE, responseMimeType));
if (connHeader == null)
return response;
else if (!"keep-alive".equals(connHeader))
return response;
// just re-use the connHeader from the request...
response.addHeader(request.getHeaderLookupStruct().getHeader(Http2HeaderName.CONNECTION));
return response;
}
use of com.webpieces.http2.api.dto.highlevel.Http2Request in project webpieces by deanhiller.
the class TestHttp2Errors method testFarEndClosedSocket.
@Test
public void testFarEndClosedSocket() throws InterruptedException, ExecutionException {
MockStreamWriter mockSw = new MockStreamWriter();
MockStreamRef ref1 = new MockStreamRef(mockSw);
mockListener.addMockStreamToReturn(ref1);
MockStreamWriter mockSw2 = new MockStreamWriter();
MockStreamRef ref2 = new MockStreamRef(mockSw2);
mockListener.addMockStreamToReturn(ref2);
Http2Request request1 = Http2Requests.createRequest(1, true);
Http2Request request2 = Http2Requests.createRequest(3, true);
mockChannel.send(request1);
PassedIn in1 = mockListener.getSingleRequest();
mockChannel.send(request2);
PassedIn in2 = mockListener.getSingleRequest();
mockChannel.close();
Assert.assertTrue(ref1.isCancelled());
Assert.assertTrue(ref2.isCancelled());
}
use of com.webpieces.http2.api.dto.highlevel.Http2Request in project webpieces by deanhiller.
the class TestS5x1StreamStates method testSection5_1BadFrameReceivedInReservedRemoteState.
/**
* reserved local
*
* A PRIORITY or WINDOW_UPDATE frame MAY be received in this state. Receiving any
* type of frame other than RST_STREAM, PRIORITY, or WINDOW_UPDATE on a stream
* in this state MUST be treated as a connection error (Section 5.4.1) of type PROTOCOL_ERROR.
*/
@Test
public void testSection5_1BadFrameReceivedInReservedRemoteState() {
MockStreamWriter mockWriter = new MockStreamWriter();
XFuture<StreamWriter> futA = XFuture.completedFuture(mockWriter);
MockStreamRef mockStream = new MockStreamRef(futA);
mockListener.addMockStreamToReturn(mockStream);
Http2Request request = Http2Requests.createRequest(1, true);
mockChannel.send(request);
PassedIn in = mockListener.getSingleRequest();
ResponseStream stream = in.stream;
Http2Push push = Http2Requests.createPush(request.getStreamId());
stream.openPushStream().process(push);
Http2Msg pushMsg = mockChannel.getFrameAndClear();
Assert.assertEquals(push, pushMsg);
// send bad frame in this state
DataFrame data = Http2Requests.createData1(push.getPromisedStreamId(), false);
mockChannel.send(data);
Assert.assertTrue(mockStream.isCancelled());
// remote receives goAway
GoAwayFrame goAway = (GoAwayFrame) mockChannel.getFrameAndClear();
Assert.assertEquals(Http2ErrorCode.PROTOCOL_ERROR, goAway.getKnownErrorCode());
DataWrapper debugData = goAway.getDebugData();
String msg = debugData.createStringFromUtf8(0, debugData.getReadableSize());
Assert.assertEquals("ConnectionException: HttpSocket[Http2ChannelCache1]:stream2:(BAD_FRAME_RECEIVED_FOR_THIS_STATE) " + "No transition defined on statemachine for event=RECV_DATA when in state=Reserved(local)", msg);
Assert.assertTrue(mockChannel.isClosed());
}
use of com.webpieces.http2.api.dto.highlevel.Http2Request in project webpieces by deanhiller.
the class TestS5x1StreamStates method testSection5_1_1BadEvenStreamId.
/**
* The identifier of a newly established stream MUST be numerically
* greater than all streams that the initiating endpoint has opened
* or reserved. This governs streams that are opened using a HEADERS
* frame and streams that are reserved using PUSH_PROMISE. An endpoint
* that receives an unexpected stream identifier MUST respond with
* a connection error (Section 5.4.1) of type PROTOCOL_ERROR.
*/
@Test
public void testSection5_1_1BadEvenStreamId() {
Http2Request request = Http2Requests.createRequest(2, true);
mockChannel.send(request);
// no request comes in
Assert.assertEquals(0, mockListener.getNumRequestsThatCameIn());
Assert.assertTrue(mockListener.isClosed());
// remote receives goAway
GoAwayFrame goAway = (GoAwayFrame) mockChannel.getFrameAndClear();
Assert.assertEquals(Http2ErrorCode.PROTOCOL_ERROR, goAway.getKnownErrorCode());
DataWrapper debugData = goAway.getDebugData();
String msg = debugData.createStringFromUtf8(0, debugData.getReadableSize());
Assert.assertTrue(msg.contains("Bad stream id. Even stream ids not allowed in requests to a server request="));
Assert.assertTrue(mockChannel.isClosed());
}
use of com.webpieces.http2.api.dto.highlevel.Http2Request in project webpieces by deanhiller.
the class TestSBasicRequestResponse method testWithDataAndTrailingHeaders.
@Test
public void testWithDataAndTrailingHeaders() throws InterruptedException, ExecutionException, TimeoutException {
MockStreamWriter mockSw = new MockStreamWriter();
mockSw.setDefaultRetValToThis();
mockListener.addMockStreamToReturn(mockSw);
Http2Request request1 = Http2Requests.createRequest(1, false);
DataFrame data1 = Http2Requests.createData1(request1.getStreamId(), false);
Http2Trailers trailing = Http2Requests.createTrailers(request1.getStreamId());
mockChannel.send(request1);
PassedIn incoming1 = mockListener.getSingleRequest();
Assert.assertEquals(request1, incoming1.request);
mockChannel.send(data1);
DataFrame incoming2 = (DataFrame) mockSw.getSingleFrame();
Assert.assertEquals(3, incoming2.getData().getReadableSize());
// clear window update frames
Assert.assertEquals(2, mockChannel.getFramesAndClear().size());
mockChannel.send(trailing);
Http2Headers incoming = (Http2Headers) mockSw.getSingleFrame();
Assert.assertEquals(trailing, incoming);
Http2Response resp = Http2Requests.createResponse(request1.getStreamId(), false);
XFuture<StreamWriter> future = incoming1.stream.process(resp);
Http2Msg response = mockChannel.getFrameAndClear();
Assert.assertEquals(resp, response);
StreamWriter writer = future.get(2, TimeUnit.SECONDS);
DataFrame data2 = Http2Requests.createData2(request1.getStreamId(), false);
writer.processPiece(data2);
DataFrame dataResp = (DataFrame) mockChannel.getFrameAndClear();
Assert.assertEquals(1, dataResp.getData().getReadableSize());
Http2Trailers trailingResp = Http2Requests.createTrailers(request1.getStreamId());
writer.processPiece(trailingResp);
Http2Headers trailers = (Http2Headers) mockChannel.getFrameAndClear();
Assert.assertEquals(trailingResp, trailers);
}
Aggregations