@Test public void shouldReturn200ChunkedResponse() throws IOException { Request request = new Request.Builder() .url(getFullUrl("/example/Chunked")) .get() .build(); Response response = client.newCall(request).execute(); assertThat(response.isSuccessful(), is(true)); assertThat(response.code(), is(200)); String responseBodyString = response.body().string(); assertThat(responseBodyString, is("This is an example of chunked transfer type. Chunked transfer type can be used when the final length of the data is not known.")); }
private void assertResponsesWithHttpCode(RequestBuilder requestBuilder, int code) throws IOException { String requestBody = requestBuilder.toString(); Socket socket = getSocket(); OutputStream out = socket.getOutputStream(); out.write(requestBody.getBytes()); BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream())); String line; int numberOfLinesRead = 0; while ((line = in.readLine()) != null) { if (++numberOfLinesRead == 1) { assertThat(line, startsWith("HTTP/1.1 " + code)); break; } } if (numberOfLinesRead == 0) { fail("No server response was read"); } try { socket.close(); } catch (IOException e) { } }
@Test public void shouldReturn414URITooLong() throws IOException { Request request = new Request.Builder() .url(getFullUrl(getTooLongUri(2048))) .get() .build(); Response response = client.newCall(request).execute(); assertThat(response.isSuccessful(), is(false)); assertThat(response.code(), is(414)); String responseBodyString = response.body().string(); assertThat(responseBodyString, not(isEmptyOrNullString())); assertThat(responseBodyString, containsString("URI Too Long")); }
@Test public void shouldReturn413PayloadTooLarge() throws IOException { int length = 50 * 1024 * 1024 + 1; RequestBuilder requestBuilder = RequestBuilder.defaultBuilder() .method("POST", "/example/") .withHeader(Headers.HEADER_CONTENT_LENGTH, Integer.toString(length)) .withCloseConnection(); assertResponsesWithHttpCode(requestBuilder, 413); }
@Test public void shouldReturn414StatusTooLong() throws IOException { RequestBuilder requestBuilder = RequestBuilder.defaultBuilder() .get(getTooLongUri(2047)) .withCloseConnection() .withProtocol("HTTTTTTTTTTTTTP/4.4"); assertResponsesWithHttpCode(requestBuilder, 414); }
@Test public void shouldReturn200ForMultipartFilePost() throws IOException { File uploadFile = createRandomContentsFile(); RequestBody formBody = new MultipartBody.Builder() .setType(MultipartBody.FORM) .addFormDataPart("someParam", "someValue") .addFormDataPart("file", "somefile.dat", RequestBody.create( MediaType.parse("application/octet-stream"), uploadFile)) .build(); Request request = new Request.Builder() .url(getFullUrl("/example/")) .post(formBody) .build(); Response response = client.newCall(request).execute(); assertThat(response.isSuccessful(), is(true)); assertThat(response.code(), is(200)); String responseBodyString = response.body().string(); assertThat(responseBodyString, not(isEmptyOrNullString())); }
@Test public void shouldServeDirectoryForServletIndex() throws IOException { assertThat(shouldServeDirectoryFile("/example/", "/example/Index", "<h1>Hello World!</h1>"), is(true)); }
@Test public void shouldOpenAndCloseSession() throws IOException { assertThat(shouldOpenAndCloseSession(1), is(true)); assertThat(shouldOpenAndCloseSession(2), is(true)); }
@Test public void shouldReturn411LengthRequiredForPost() throws IOException { RequestBuilder requestBuilder = RequestBuilder.defaultBuilder() .method("POST", "/example/") .withCloseConnection(); assertResponsesWithHttpCode(requestBuilder, 411); }
@Test public void shouldServeDirectoryForFileIndex() throws IOException { assertThat(shouldServeDirectoryFile("/", "/index.html", "Index file"), is(true)); }
@Test public void shouldRedirectToDirectoryIndexOnMissingTrailingSlash() throws IOException { Request request = new Request.Builder() .url(getFullUrl("/example")) .get() .build(); Response response = client.newCall(request).execute(); assertThat(response.code(), is(301)); assertThat(response.header("Location"), is(not(nullValue()))); assertThat(response.header("Location"), is("/example/")); }
@Test public void shouldReturn403ForbiddenOnBlockedByFilter() throws IOException { RequestBuilder requestBuilder = RequestBuilder.defaultBuilder() .get("/example/secured/ForbiddenByFilter") .withCloseConnection(); assertResponsesWithHttpCode(requestBuilder, 403); }
@Test(expected = IOException.class) public void shouldCloseSocketAfterCloseConnectionRequest() throws IOException, InterruptedException { String requestBody = RequestBuilder.defaultBuilder() .get("/example/Index") .withHost(HOST + ":" + PORT) .withCloseConnection() .toString(); OutputStream out = null; try { Socket socket = getSocket(); out = socket.getOutputStream(); out.write(requestBody.getBytes()); BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream())); while (in.readLine() != null) { // Simulate reading } } catch (IOException e) { fail("The test failed too early due IOException" + e.getMessage()); } int i = 0; while (i++ < 10) { // The following code will cause error on a closed socket Thread.sleep(100); out.write("X" .getBytes()); out.flush(); } }
private boolean shouldOpenAndCloseSession(int count) throws IOException { Request request = new Request.Builder() .url(getFullUrl("/example/Session")) .get() .build(); Response response = client.newCall(request).execute(); assertThat(response.isSuccessful(), is(true)); assertThat(response.code(), is(200)); // assertThat(response.header(Headers.HEADER_CONTENT_LENGTH), is(not(nullValue()))); String responseBodyString = response.body().string(); assertThat(responseBodyString, not(isEmptyOrNullString())); assertThat(responseBodyString, containsString("Session page hits: " + count)); return true; }
@Test public void shouldReturn403ForbiddenOnIllegalPath() throws IOException { RequestBuilder requestBuilder = RequestBuilder.defaultBuilder() .get("../../../index.html") .withCloseConnection(); assertResponsesWithHttpCode(requestBuilder, 403); }
@Test public void shouldHangSilentlyOnClosingSocket() throws IOException { String requestBody = RequestBuilder.defaultBuilder() .get("/43524938257493852435/SOMEUNKNOWNURL.html") .withCloseConnection() .toString(); Socket socket = getSocket(); OutputStream out = socket.getOutputStream(); out.write(requestBody.getBytes()); BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream())); String line; int numberOfLinesRead = 0; while ((line = in.readLine()) != null) { if (++numberOfLinesRead == 1) { assertThat(line, startsWith("HTTP/1.1 404")); socket.close(); break; } } if (numberOfLinesRead == 0) { fail("No server response was read"); } }
@Test public void shouldReturn404NotFound() throws IOException { Request request = new Request.Builder() .url(getFullUrl("/43524938257493852435/SOMEUNKNOWNURL.html")) .get() .build(); Response response = client.newCall(request).execute(); assertThat(response.isSuccessful(), is(false)); assertThat(response.code(), is(404)); String responseBodyString = response.body().string(); assertThat(responseBodyString, not(isEmptyOrNullString())); assertThat(responseBodyString, containsString("File Not Found")); }
@Test public void shouldReturn400OnMalformedStatus() throws IOException { RequestBuilder requestBuilder = RequestBuilder.defaultBuilder() .get(null) .withCloseConnection(); assertResponsesWithHttpCode(requestBuilder, 400); }
@Test public void shouldReturn400BadRequestOnTooLongMethod() throws IOException { Request request = new Request.Builder() .url(getFullUrl("/")) .method("ABCABCABCABCABC", null) .build(); Response response = client.newCall(request).execute(); assertThat(response.isSuccessful(), is(false)); assertThat(response.code(), is(400)); String responseBodyString = response.body().string(); assertThat(responseBodyString, not(isEmptyOrNullString())); assertThat(responseBodyString, containsString("Bad Request")); }
@Test public void shouldReturn400WhenLengthMalformedForPost() throws IOException { RequestBuilder requestBuilder = RequestBuilder.defaultBuilder() .method("POST", "/example/") .withHeader(Headers.HEADER_CONTENT_LENGTH, "Illegal value") .withCloseConnection(); assertResponsesWithHttpCode(requestBuilder, 400); }