Search in sources :

Example 26 with HttpFullRequest

use of org.webpieces.httpclient11.api.HttpFullRequest in project webpieces by deanhiller.

the class TestJsonCustomFilter method testSyncBadJsonGet.

@Test
public void testSyncBadJsonGet() {
    HttpFullRequest req = Requests.createBadJsonRequest(KnownHttpMethod.GET, "/json/45");
    XFuture<HttpFullResponse> respFuture = http11Socket.send(req);
    ResponseWrapper response = ResponseExtract.waitResponseAndWrap(respFuture);
    response.assertStatusCode(KnownStatusCode.HTTP_400_BADREQUEST);
    response.assertContains("{`error`:`invalid json in client request.  Unexpected character ('c' (code 99)): was expecting a colon to separate field name and value".replace("`", "\""));
    response.assertContentType("application/json");
}
Also used : HttpFullResponse(org.webpieces.httpclient11.api.HttpFullResponse) HttpFullRequest(org.webpieces.httpclient11.api.HttpFullRequest) ResponseWrapper(org.webpieces.webserver.test.ResponseWrapper) AbstractWebpiecesTest(org.webpieces.webserver.test.AbstractWebpiecesTest) PrivateWebserverForTest(org.webpieces.webserver.PrivateWebserverForTest) Test(org.junit.Test)

Example 27 with HttpFullRequest

use of org.webpieces.httpclient11.api.HttpFullRequest in project webpieces by deanhiller.

the class TestJsonCustomFilter method testSyncJsonPost.

@Test
public void testSyncJsonPost() {
    HttpFullRequest req = Requests.createJsonRequest(KnownHttpMethod.POST, "/json/45");
    XFuture<HttpFullResponse> respFuture = http11Socket.send(req);
    ResponseWrapper response = ResponseExtract.waitResponseAndWrap(respFuture);
    response.assertStatusCode(KnownStatusCode.HTTP_200_OK);
    response.assertContains("{`searchTime`:99,`matches`:[`match1`,`match2`]}".replace("`", "\""));
    response.assertContentType("application/json");
}
Also used : HttpFullResponse(org.webpieces.httpclient11.api.HttpFullResponse) HttpFullRequest(org.webpieces.httpclient11.api.HttpFullRequest) ResponseWrapper(org.webpieces.webserver.test.ResponseWrapper) AbstractWebpiecesTest(org.webpieces.webserver.test.AbstractWebpiecesTest) PrivateWebserverForTest(org.webpieces.webserver.PrivateWebserverForTest) Test(org.junit.Test)

Example 28 with HttpFullRequest

use of org.webpieces.httpclient11.api.HttpFullRequest in project webpieces by deanhiller.

the class TestJsonCustomFilter method testNoAttributeValueInJsonGoesToEmptyString.

@Test
public void testNoAttributeValueInJsonGoesToEmptyString() {
    // test out "something":null converts to "" in java....
    String json = "{ `meta`: { `numResults`: 4 }, `testValidation`:`notBlank` }".replace("`", "\"");
    HttpFullRequest req = Requests.createJsonRequest(KnownHttpMethod.POST, "/json/simple", json);
    XFuture<HttpFullResponse> respFuture = http11Socket.send(req);
    ResponseWrapper response = ResponseExtract.waitResponseAndWrap(respFuture);
    response.assertStatusCode(KnownStatusCode.HTTP_200_OK);
    response.assertContentType("application/json");
    SearchRequest request = mockSvc.getCachedRequest();
    Assert.assertEquals("", request.getQuery());
    Assert.assertEquals("", request.getMeta().getExtraField());
}
Also used : HttpFullResponse(org.webpieces.httpclient11.api.HttpFullResponse) SearchRequest(org.webpieces.webserver.json.app.SearchRequest) HttpFullRequest(org.webpieces.httpclient11.api.HttpFullRequest) ResponseWrapper(org.webpieces.webserver.test.ResponseWrapper) AbstractWebpiecesTest(org.webpieces.webserver.test.AbstractWebpiecesTest) PrivateWebserverForTest(org.webpieces.webserver.PrivateWebserverForTest) Test(org.junit.Test)

Example 29 with HttpFullRequest

use of org.webpieces.httpclient11.api.HttpFullRequest in project webpieces by deanhiller.

the class TestJsonCustomFilter method testAsyncBadJsonPost.

@Test
public void testAsyncBadJsonPost() {
    HttpFullRequest req = Requests.createBadJsonRequest(KnownHttpMethod.POST, "/json/async/45");
    XFuture<HttpFullResponse> respFuture = http11Socket.send(req);
    ResponseWrapper response = ResponseExtract.waitResponseAndWrap(respFuture);
    response.assertStatusCode(KnownStatusCode.HTTP_400_BADREQUEST);
    response.assertContains("{`error`:`invalid json in client request.  Unexpected character ('c' (code 99)): was expecting a colon to separate field name and value".replace("`", "\""));
    response.assertContentType("application/json");
}
Also used : HttpFullResponse(org.webpieces.httpclient11.api.HttpFullResponse) HttpFullRequest(org.webpieces.httpclient11.api.HttpFullRequest) ResponseWrapper(org.webpieces.webserver.test.ResponseWrapper) AbstractWebpiecesTest(org.webpieces.webserver.test.AbstractWebpiecesTest) PrivateWebserverForTest(org.webpieces.webserver.PrivateWebserverForTest) Test(org.junit.Test)

Example 30 with HttpFullRequest

use of org.webpieces.httpclient11.api.HttpFullRequest in project webpieces by deanhiller.

the class TestStreamingRaw method testAsyncJsonGet.

@Test
public void testAsyncJsonGet() throws InterruptedException, ExecutionException {
    MockChannel channel = new MockChannel();
    XFuture<DataListener> future = mgr.simulateHttpConnect(channel);
    DataListener dataListener = future.get();
    HttpFullRequest fullRequest = Requests.createJsonRequest(KnownHttpMethod.GET, "/json/streaming");
    ByteBuffer buffer = parser.marshalToByteBuffer(fullRequest.getRequest());
    DataWrapper data = fullRequest.getData();
    byte[] headers = new byte[buffer.remaining()];
    buffer.get(headers);
    data.getReadableSize();
    byte[] part1 = data.readBytesAt(0, 10);
    String str = new String(part1);
    byte[] part2 = data.readBytesAt(10, data.getReadableSize() - 10);
    String str2 = new String(part2);
    ByteBuffer firstPacket = ByteBuffer.allocate(headers.length + part1.length);
    firstPacket.put(headers);
    firstPacket.put(part1);
    firstPacket.flip();
    XFuture<Boolean> authFuture = new XFuture<Boolean>();
    mockAuth.addValueToReturn(authFuture);
    Assert.assertEquals(0, Context.getContext().size());
    // Feed in request with content-length AND part of the body as well...
    XFuture<Void> fut1 = dataListener.incomingData(channel, firstPacket);
    Assert.assertEquals(0, Context.getContext().size());
    // Feed in more BEFORE authFuture is complete(this was the bug, ie. race condition)
    ByteBuffer buf2 = ByteBuffer.allocate(part2.length);
    buf2.put(part2);
    buf2.flip();
    XFuture<Void> fut2 = dataListener.incomingData(channel, buf2);
    Assert.assertEquals(0, Context.getContext().size());
    XFuture<StreamWriter> streamWriterFuture = new XFuture<StreamWriter>();
    mockStreamClient.addStreamWriter(streamWriterFuture);
    // complete it
    authFuture.complete(true);
    Assert.assertFalse(fut1.isDone());
    Assert.assertFalse(fut2.isDone());
    MockStreamWriter2 mockStreamWriter = new MockStreamWriter2();
    streamWriterFuture.complete(mockStreamWriter);
    Assert.assertEquals(0, Context.getContext().size());
    Assert.assertTrue(fut1.isDone());
    Assert.assertTrue(fut2.isDone());
    List<StreamMsg> frames = mockStreamWriter.getFrames();
    Assert.assertEquals(2, frames.size());
    DataFrame f1 = (DataFrame) frames.get(0);
    DataFrame f2 = (DataFrame) frames.get(1);
    String s1 = f1.getData().createStringFromUtf8(0, f1.getData().getReadableSize());
    Assert.assertEquals(str, s1);
    String s2 = f2.getData().createStringFromUtf8(0, f2.getData().getReadableSize());
    Assert.assertEquals(str2, s2);
    Assert.assertEquals(0, Context.getContext().size());
}
Also used : StreamMsg(com.webpieces.http2.api.dto.lowlevel.lib.StreamMsg) MockChannel(org.webpieces.webserver.test.sockets.MockChannel) XFuture(org.webpieces.util.futures.XFuture) StreamWriter(com.webpieces.http2.api.streaming.StreamWriter) DataFrame(com.webpieces.http2.api.dto.lowlevel.DataFrame) ByteBuffer(java.nio.ByteBuffer) DataWrapper(org.webpieces.data.api.DataWrapper) HttpFullRequest(org.webpieces.httpclient11.api.HttpFullRequest) DataListener(org.webpieces.nio.api.handlers.DataListener) AbstractWebpiecesTest(org.webpieces.webserver.test.sockets.AbstractWebpiecesTest) PrivateWebserverForTest(org.webpieces.webserver.PrivateWebserverForTest) Test(org.junit.Test)

Aggregations

HttpFullRequest (org.webpieces.httpclient11.api.HttpFullRequest)222 HttpFullResponse (org.webpieces.httpclient11.api.HttpFullResponse)205 ResponseWrapper (org.webpieces.webserver.test.ResponseWrapper)204 Test (org.junit.Test)194 AbstractWebpiecesTest (org.webpieces.webserver.test.AbstractWebpiecesTest)193 PrivateWebserverForTest (org.webpieces.webserver.PrivateWebserverForTest)180 Header (org.webpieces.httpparser.api.common.Header)50 XFuture (org.webpieces.util.futures.XFuture)13 HttpRequest (org.webpieces.httpparser.api.dto.HttpRequest)12 Http2Header (com.webpieces.http2.api.dto.lowlevel.lib.Http2Header)9 HttpRequestLine (org.webpieces.httpparser.api.dto.HttpRequestLine)9 HttpUri (org.webpieces.httpparser.api.dto.HttpUri)9 WebserverForTest (org.webpieces.plugins.fortesting.WebserverForTest)8 DataWrapper (org.webpieces.data.api.DataWrapper)6 UserDto (org.webpieces.webserver.basic.app.biz.UserDto)4 URL (java.net.URL)3 UserTestDbo (org.webpieces.plugins.hibernate.app.dbo.UserTestDbo)3 URI (java.net.URI)2 ByteBuffer (java.nio.ByteBuffer)2 NotFoundException (org.webpieces.http.exception.NotFoundException)2