Skip to content

Commit

Permalink
Merge pull request square#232 from square/jw/apache-thangs
Browse files Browse the repository at this point in the history
Request entity tests for the Apache client shim.
  • Loading branch information
JakeWharton committed Jul 4, 2013
2 parents b6a68e1 + 53e12cc commit 1d33ae5
Showing 1 changed file with 41 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,40 @@

import com.google.mockwebserver.MockResponse;
import com.google.mockwebserver.MockWebServer;
import com.google.mockwebserver.RecordedRequest;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.Arrays;
import org.apache.http.Header;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ByteArrayEntity;
import org.apache.http.entity.InputStreamEntity;
import org.apache.http.util.EntityUtils;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

public class OkApacheClientTest {
private MockWebServer server = new MockWebServer();
private OkApacheClient client = new OkApacheClient();
private MockWebServer server;
private OkApacheClient client;

@Before public void setUp() throws IOException {
client = new OkApacheClient();
server = new MockWebServer();
server.play();
}

@After public void tearDown() throws IOException {
server.shutdown();
}

@Test public void success() throws Exception {
server.enqueue(new MockResponse().setBody("Hello, World!"));
server.play();

HttpGet request = new HttpGet(server.getUrl("/").toURI());
HttpResponse response = client.execute(request);
Expand All @@ -34,7 +46,6 @@ public class OkApacheClientTest {
@Test public void redirect() throws Exception {
server.enqueue(new MockResponse().setResponseCode(302).addHeader("Location", "/foo"));
server.enqueue(new MockResponse().setBody("Hello, Redirect!"));
server.play();

HttpGet request = new HttpGet(server.getUrl("/").toURI());
HttpResponse response = client.execute(request);
Expand All @@ -44,7 +55,6 @@ public class OkApacheClientTest {

@Test public void sessionExpired() throws Exception {
server.enqueue(new MockResponse().setResponseCode(422));
server.play();

HttpGet request = new HttpGet(server.getUrl("/").toURI());
HttpResponse response = client.execute(request);
Expand All @@ -54,7 +64,6 @@ public class OkApacheClientTest {
@Test public void headers() throws Exception {
server.enqueue(new MockResponse().addHeader("Foo", "Bar"));
server.enqueue(new MockResponse().addHeader("Foo", "Bar").addHeader("Foo", "Baz"));
server.play();

HttpGet request1 = new HttpGet(server.getUrl("/").toURI());
HttpResponse response1 = client.execute(request1);
Expand All @@ -72,9 +81,34 @@ public class OkApacheClientTest {

@Test public void noEntity() throws Exception {
server.enqueue(new MockResponse());
server.play();

HttpPost post = new HttpPost(server.getUrl("/").toURI());
client.execute(post);
}

@Test public void postByteEntity() throws Exception {
server.enqueue(new MockResponse());

final HttpPost post = new HttpPost(server.getUrl("/").toURI());
byte[] body = "Hello, world!".getBytes("UTF-8");
post.setEntity(new ByteArrayEntity(body));
client.execute(post);

RecordedRequest request = server.takeRequest();
assertTrue(Arrays.equals(body, request.getBody()));
assertEquals(request.getHeader("Content-Length"), "13");
}

@Test public void postInputStreamEntity() throws Exception {
server.enqueue(new MockResponse());

final HttpPost post = new HttpPost(server.getUrl("/").toURI());
byte[] body = "Hello, world!".getBytes("UTF-8");
post.setEntity(new InputStreamEntity(new ByteArrayInputStream(body), body.length));
client.execute(post);

RecordedRequest request = server.takeRequest();
assertTrue(Arrays.equals(body, request.getBody()));
assertEquals(request.getHeader("Content-Length"), "13");
}
}

0 comments on commit 1d33ae5

Please sign in to comment.