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);
}
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");
}
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");
}
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());
}
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('`', '\"'));
}
Aggregations