1package com.xtremelabs.robolectric.tester.org.apache.http; 2 3import com.xtremelabs.robolectric.WithTestDefaultsRunner; 4import org.apache.http.HttpEntity; 5import org.apache.http.client.methods.HttpGet; 6import org.apache.http.client.methods.HttpPost; 7import org.apache.http.client.methods.HttpPut; 8import org.apache.http.entity.StringEntity; 9import org.apache.http.message.BasicHeader; 10import org.apache.http.util.EntityUtils; 11import org.junit.Before; 12import org.junit.Test; 13import org.junit.runner.RunWith; 14 15import java.io.IOException; 16 17import static org.hamcrest.core.IsEqual.equalTo; 18import static org.junit.Assert.assertFalse; 19import static org.junit.Assert.assertThat; 20import static org.junit.Assert.assertTrue; 21 22@RunWith(WithTestDefaultsRunner.class) 23public class FakeHttpLayerTest { 24 25 private FakeHttpLayer.RequestMatcherBuilder requestMatcherBuilder; 26 27 @Before 28 public void setUp() throws Exception { 29 requestMatcherBuilder = new FakeHttpLayer.RequestMatcherBuilder(); 30 } 31 32 @Test 33 public void requestMatcherBuilder_shouldAddHost() throws Exception { 34 requestMatcherBuilder.host("example.com"); 35 assertThat("example.com", equalTo(requestMatcherBuilder.getHostname())); 36 } 37 38 @Test 39 public void requestMatcherBuilder_shouldAddMethod() throws Exception { 40 requestMatcherBuilder.method("POST"); 41 assertThat("POST", equalTo(requestMatcherBuilder.getMethod())); 42 } 43 44 @Test 45 public void requestMatcherBuilder_shouldAddPath() throws Exception { 46 requestMatcherBuilder.path("foo/bar"); 47 assertThat("/foo/bar", equalTo(requestMatcherBuilder.getPath())); 48 } 49 50 @Test 51 public void requestMatcherBuilder_shouldAddParams() throws Exception { 52 requestMatcherBuilder.param("param1", "param one"); 53 assertThat("param one", equalTo(requestMatcherBuilder.getParam("param1"))); 54 } 55 56 @Test 57 public void requestMatcherBuilder_shouldAddHeaders() throws Exception { 58 requestMatcherBuilder.header("header1", "header one"); 59 assertThat("header one", equalTo(requestMatcherBuilder.getHeader("header1"))); 60 } 61 62 @Test 63 public void matches__shouldMatchHeaders() throws Exception { 64 requestMatcherBuilder.header("header1", "header one"); 65 HttpGet match = new HttpGet("example.com"); 66 HttpGet noMatch = new HttpGet("example.com"); 67 match.setHeader(new BasicHeader("header1", "header one")); 68 noMatch.setHeader(new BasicHeader("header1", "header not a match")); 69 70 assertFalse(requestMatcherBuilder.matches(new HttpGet("example.com"))); 71 assertFalse(requestMatcherBuilder.matches(noMatch)); 72 assertTrue(requestMatcherBuilder.matches(match)); 73 } 74 75 @Test 76 public void matches__shouldMatchPostBody() throws Exception { 77 final String expectedText = "some post body text"; 78 79 requestMatcherBuilder.postBody(new FakeHttpLayer.RequestMatcherBuilder.PostBodyMatcher() { 80 @Override 81 public boolean matches(HttpEntity actualPostBody) throws IOException { 82 return EntityUtils.toString(actualPostBody).equals(expectedText); 83 } 84 }); 85 86 HttpPut match = new HttpPut("example.com"); 87 match.setEntity(new StringEntity(expectedText)); 88 89 HttpPost noMatch = new HttpPost("example.com"); 90 noMatch.setEntity(new StringEntity("some text that does not match")); 91 92 assertFalse(requestMatcherBuilder.matches(new HttpGet("example.com"))); 93 assertFalse(requestMatcherBuilder.matches(noMatch)); 94 assertTrue(requestMatcherBuilder.matches(match)); 95 } 96} 97