@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.")); }
@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/")); }
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 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 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 shouldReturn400BadRequestOnUnrecognizedMethod() throws IOException { Request request = new Request.Builder() .url(getFullUrl("/")) .method("ABC", 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 shouldAcceptPostWithZeroLength() throws IOException { Request request = new Request.Builder() .url(getFullUrl("/example/")) .addHeader(Headers.HEADER_CONTENT_LENGTH, "0") .post(new FormBody.Builder().build()) .build(); Response response = client.newCall(request).execute(); assertThat(response.isSuccessful(), is(true)); assertThat(response.code(), is(200)); assertThat(response.body().string(), not(isEmptyOrNullString())); }
@Test public void shouldReturn400OnMalformedRange() throws IOException { Request request = new Request.Builder() .url(getFullUrl("/staticfile.html")) .header(Headers.HEADER_RANGE, "bytes=128-abcd") .get() .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 shouldReturn416RangeNotSatisfiable() throws IOException { Request request = new Request.Builder() .url(getFullUrl("/staticfile.html")) .header(Headers.HEADER_RANGE, "bytes=128-128") .get() .build(); Response response = client.newCall(request).execute(); assertThat(response.isSuccessful(), is(false)); assertThat(response.code(), is(416)); String responseBodyString = response.body().string(); assertThat(responseBodyString, not(isEmptyOrNullString())); assertThat(responseBodyString, containsString("Range Not Satisfiable")); }
@Test public void shouldReturn405MethodNotAllowed() throws IOException { // Connect is not yet implemented Request request = new Request.Builder() .url(getFullUrl("/")) .method("CONNECT", null) .build(); Response response = client.newCall(request).execute(); assertThat(response.isSuccessful(), is(false)); assertThat(response.code(), is(405)); String responseBodyString = response.body().string(); assertThat(responseBodyString, not(isEmptyOrNullString())); assertThat(responseBodyString, containsString("Method Not Allowed")); }
@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 shouldReturn500InternalServerError() throws IOException { Request request = new Request.Builder() .url(getFullUrl("/example/InternalServerError")) .get() .build(); Response response = client.newCall(request).execute(); assertThat(response.isSuccessful(), is(false)); assertThat(response.code(), is(500)); String responseBodyString = response.body().string(); assertThat(responseBodyString, not(isEmptyOrNullString())); assertThat(responseBodyString, containsString("Error 500")); assertThat(responseBodyString, containsString("UnexpectedSituationException")); }
@Test public void shouldServeStaticFile() throws IOException { Request request = new Request.Builder() .url(getFullUrl("/staticfile.html")) .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("11")); String responseBodyString = response.body().string(); assertThat(responseBodyString, not(isEmptyOrNullString())); assertThat(responseBodyString, is("Static file")); }
@Test public void shouldReturn200ForPlainPost() throws IOException { RequestBody formBody = new FormBody.Builder() .add("someParam", "someValue") .build(); Request request = new Request.Builder() .url(getFullUrl("/example/")) .method("POST", RequestBody.create(null, new byte[0])) .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())); }
private boolean shouldServeDirectoryFile(String pathShort, String pathFull, String commonValue) throws IOException { Request request = new Request.Builder() .url(getFullUrl(pathShort)) .get() .build(); Request request2 = new Request.Builder() .url(getFullUrl(pathFull)) .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, not(isEmptyOrNullString())); assertThat(responseBodyString, containsString(commonValue)); Response response2 = client.newCall(request2).execute(); assertThat(response2.isSuccessful(), is(true)); assertThat(response2.code(), is(200)); String response2BodyString = response2.body().string(); assertThat(response2BodyString, not(isEmptyOrNullString())); assertThat(response2BodyString, containsString(commonValue)); return true; }
@Test public void shouldReturn206AndServeRangesOfStaticFileForOneRange() throws IOException { Request request = new Request.Builder() .url(getFullUrl("/staticfile.html")) .header("Range", "bytes=0-5") .get() .build(); Response response = client.newCall(request).execute(); assertThat(response.isSuccessful(), is(true)); assertThat(response.code(), is(206)); assertThat(response.header(Headers.HEADER_CONTENT_RANGE), is("bytes 0-5/11")); assertThat(response.header(Headers.HEADER_CONTENT_LENGTH), is("6")); String responseBodyString = response.body().string(); assertThat(responseBodyString, not(isEmptyOrNullString())); assertThat(responseBodyString, is("Static")); }
@Test public void shouldReturn200ForMultipartFormPost() throws IOException { // Based on https://stackoverflow.com/questions/24279563/uploading-a-large-file-in-multipart-using-okhttp RequestBody formBody = new MultipartBody.Builder() .setType(MultipartBody.FORM) .addFormDataPart("someParam", "someValue") .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 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())); }
.url(getFullUrl("/staticfile.html")) .header(Headers.HEADER_RANGE, "bytes=" + ranges) .get()