Search in sources :

Example 81 with FullResponse

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

the class TestLesson1BasicRequestResponse method testAsyncControllerAndRemoteSystem.

/**
	 * It is highly suggested you step through this test in debug mode to understand the description below...
	 * 
	 * This is a single threaded test that actually allows the webserver thread to return back to the test before 
	 * the response comes.  (in production the thread would process other requests while waiting for remote system response).  
	 * Then the test simulates the response coming in from remote system and makes sure we send a response back
	 * to the ResponseSender.  In implementations like this with a remote system, one can avoid holding threads up
	 * and allow them to keep working while waiting for a response from the remote system.
	 */
@Test
public void testAsyncControllerAndRemoteSystem() {
    CompletableFuture<Integer> future = new CompletableFuture<Integer>();
    mockRemote.addValueToReturn(future);
    HttpRequest req = createRequest("/async");
    http11Socket.send(req);
    List<FullResponse> responses = http11Socket.getResponses();
    Assert.assertEquals(0, responses.size());
    //notice that the thread returned but there is no response back to browser yet such that thread can do more work.
    //next, simulate remote system returning a value..
    int value = 85;
    future.complete(value);
    List<FullResponse> responses2 = http11Socket.getResponses();
    Assert.assertEquals(1, responses2.size());
    FullResponse httpPayload = responses2.get(0);
    httpPayload.assertStatusCode(KnownStatusCode.HTTP_200_OK);
    httpPayload.assertContains("This is a page with value=" + value);
}
Also used : HttpRequest(org.webpieces.httpparser.api.dto.HttpRequest) CompletableFuture(java.util.concurrent.CompletableFuture) FullResponse(org.webpieces.webserver.test.FullResponse) AbstractWebpiecesTest(org.webpieces.webserver.test.AbstractWebpiecesTest) Test(org.junit.Test)

Example 82 with FullResponse

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

the class TestLesson2Errors method testNotFound.

/**
	 * You could also test notFound route fails with exception too...
	 */
@Test
public void testNotFound() {
    HttpRequest req = TestLesson1BasicRequestResponse.createRequest("/route/that/does/not/exist");
    http11Socket.send(req);
    List<FullResponse> responses = http11Socket.getResponses();
    Assert.assertEquals(1, responses.size());
    FullResponse httpPayload = responses.get(0);
    httpPayload.assertStatusCode(KnownStatusCode.HTTP_404_NOTFOUND);
    httpPayload.assertContains("Your page was not found");
}
Also used : HttpRequest(org.webpieces.httpparser.api.dto.HttpRequest) FullResponse(org.webpieces.webserver.test.FullResponse) Test(org.junit.Test) AbstractWebpiecesTest(org.webpieces.webserver.test.AbstractWebpiecesTest)

Example 83 with FullResponse

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

the class TestLesson2Errors method testRemoteSystemDown.

/**
	 * Tests a remote asynchronous system fails and a 500 error page is rendered
	 */
@Test
public void testRemoteSystemDown() {
    CompletableFuture<Integer> future = new CompletableFuture<Integer>();
    mockRemote.addValueToReturn(future);
    HttpRequest req = TestLesson1BasicRequestResponse.createRequest("/async");
    http11Socket.send(req);
    List<FullResponse> responses = http11Socket.getResponses();
    Assert.assertEquals(0, responses.size());
    //notice that the thread returned but there is no response back to browser yet such that thread can do more work.
    //next, simulate remote system returning a value..
    future.completeExceptionally(new RuntimeException("complete future with exception"));
    List<FullResponse> responses2 = http11Socket.getResponses();
    Assert.assertEquals(1, responses2.size());
    FullResponse httpPayload = responses2.get(0);
    httpPayload.assertStatusCode(KnownStatusCode.HTTP_500_INTERNAL_SVR_ERROR);
    httpPayload.assertContains("You encountered a 5xx in your server");
}
Also used : HttpRequest(org.webpieces.httpparser.api.dto.HttpRequest) CompletableFuture(java.util.concurrent.CompletableFuture) FullResponse(org.webpieces.webserver.test.FullResponse) Test(org.junit.Test) AbstractWebpiecesTest(org.webpieces.webserver.test.AbstractWebpiecesTest)

Example 84 with FullResponse

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

the class TestSyncHibernate method testHibernateNoUserIdParamWillSaveNewUser.

@Test
public void testHibernateNoUserIdParamWillSaveNewUser() {
    String email = "test1";
    HttpDummyRequest req = Requests.createPostRequest("/testmerge", "user.email", email, "user.name", "blah1", "user.firstName", "blah2");
    http11Socket.send(req);
    FullResponse response = ResponseExtract.assertSingleResponse(http11Socket);
    response.assertStatusCode(KnownStatusCode.HTTP_303_SEEOTHER);
    UserTestDbo user2 = loadByEmail(email);
    //name changed
    Assert.assertEquals("blah1", user2.getName());
    //firstname changed
    Assert.assertEquals("blah2", user2.getFirstName());
}
Also used : FullResponse(org.webpieces.webserver.test.FullResponse) UserTestDbo(org.webpieces.plugins.hibernate.app.dbo.UserTestDbo) HttpDummyRequest(org.webpieces.webserver.test.HttpDummyRequest) AbstractWebpiecesTest(org.webpieces.webserver.test.AbstractWebpiecesTest) Test(org.junit.Test) WebserverForTest(org.webpieces.webserver.WebserverForTest)

Example 85 with FullResponse

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

the class TestFlashAndSelect method testMultiSelectSingleSelection.

@Test
public void testMultiSelectSingleSelection() {
    HttpDummyRequest req1 = Requests.createPostRequest("/multiselect", "entity.id", user.getId() + "", //invalid first name
    "entity.firstName", //invalid first name
    "NextName", "entity.email", "dean@zz.com", "entity.lastName", "", "entity.password", "", "entity.levelOfEducation", "", "selectedRoles", "j");
    http11Socket.send(req1);
    FullResponse response1 = ResponseExtract.assertSingleResponse(http11Socket);
    response1.assertStatusCode(KnownStatusCode.HTTP_303_SEEOTHER);
    String urlPath = "/multiselect/" + user.getId();
    Assert.assertEquals("http://myhost.com" + urlPath, response1.getRedirectUrl());
    HttpRequest req = Requests.createRequest(KnownHttpMethod.GET, urlPath);
    Header cookieHeader = response1.createCookieRequestHeader();
    req.addHeader(cookieHeader);
    http11Socket.send(req);
    FullResponse response = ResponseExtract.assertSingleResponse(http11Socket);
    response.assertStatusCode(KnownStatusCode.HTTP_200_OK);
    response.assertContains("<option value=`b` >Badass</script>".replace('`', '\"'));
    response.assertContains("<option value=`j` selected=`selected`>Jerk</script>".replace('`', '\"'));
    response.assertContains("<option value=`d` >Delinquint</script>".replace('`', '\"'));
}
Also used : HttpRequest(org.webpieces.httpparser.api.dto.HttpRequest) FullResponse(org.webpieces.webserver.test.FullResponse) Header(org.webpieces.httpparser.api.common.Header) HttpDummyRequest(org.webpieces.webserver.test.HttpDummyRequest) AbstractWebpiecesTest(org.webpieces.webserver.test.AbstractWebpiecesTest) Test(org.junit.Test) WebserverForTest(org.webpieces.webserver.WebserverForTest)

Aggregations

FullResponse (org.webpieces.webserver.test.FullResponse)154 Test (org.junit.Test)142 AbstractWebpiecesTest (org.webpieces.webserver.test.AbstractWebpiecesTest)142 WebserverForTest (org.webpieces.webserver.WebserverForTest)136 HttpRequest (org.webpieces.httpparser.api.dto.HttpRequest)120 HttpDummyRequest (org.webpieces.webserver.test.HttpDummyRequest)32 Header (org.webpieces.httpparser.api.common.Header)16 CompletableFuture (java.util.concurrent.CompletableFuture)11 PlatformOverridesForTest (org.webpieces.webserver.test.PlatformOverridesForTest)9 UserDto (org.webpieces.webserver.basic.app.biz.UserDto)4 UserTestDbo (org.webpieces.plugins.hibernate.app.dbo.UserTestDbo)3 NotFoundException (org.webpieces.router.api.exceptions.NotFoundException)2 ByteBuffer (java.nio.ByteBuffer)1 BufferCreationPool (org.webpieces.data.api.BufferCreationPool)1 DataWrapper (org.webpieces.data.api.DataWrapper)1 DataWrapperGenerator (org.webpieces.data.api.DataWrapperGenerator)1 HttpParser (org.webpieces.httpparser.api.HttpParser)1 MarshalState (org.webpieces.httpparser.api.MarshalState)1