Home | History | Annotate | Download | only in http
      1 package com.xtremelabs.robolectric.tester.org.apache.http;
      2 
      3 import org.apache.http.Header;
      4 import org.apache.http.HeaderIterator;
      5 import org.apache.http.HttpResponse;
      6 import org.apache.http.message.BasicHeader;
      7 import org.hamcrest.CoreMatchers;
      8 import org.junit.Test;
      9 
     10 import static org.hamcrest.CoreMatchers.nullValue;
     11 import static org.hamcrest.core.Is.is;
     12 import static org.hamcrest.core.IsEqual.equalTo;
     13 import static org.junit.Assert.assertThat;
     14 
     15 public class TestHttpResponseTest {
     16 
     17     @Test
     18     public void shouldSupportGetFirstHeader() throws Exception {
     19         HttpResponse resp =
     20                 new TestHttpResponse(304, "REDIRECTED",
     21                         new BasicHeader("Location", "http://bar.com"));
     22 
     23         assertThat(resp.getFirstHeader("None"), nullValue());
     24         assertThat(new TestHttpResponse(200, "OK").getFirstHeader("Foo"), nullValue());
     25 
     26         for (String l : new String[] { "location", "Location" }) {
     27             assertThat(resp.getFirstHeader(l).getValue(), equalTo("http://bar.com"));
     28         }
     29     }
     30 
     31     @Test
     32     public void shouldSupportGetLastHeader() throws Exception {
     33         HttpResponse resp =
     34                 new TestHttpResponse(304, "REDIRECTED",
     35                         new BasicHeader("Location", "http://bar.com"),
     36                         new BasicHeader("Location", "http://zombo.com"));
     37 
     38         assertThat(resp.getLastHeader("None"), nullValue());
     39 
     40         for (String l : new String[] { "location", "Location" }) {
     41             assertThat(resp.getLastHeader(l).getValue(), equalTo("http://zombo.com"));
     42         }
     43     }
     44 
     45     @Test
     46     public void shouldSupportContainsHeader() throws Exception {
     47         HttpResponse resp =
     48                 new TestHttpResponse(304, "ZOMBO",
     49                         new BasicHeader("X-Zombo-Com", "Welcome"));
     50 
     51         assertThat(resp.containsHeader("X-Zombo-Com"), is(true));
     52         assertThat(resp.containsHeader("Location"), is(false));
     53     }
     54 
     55     @Test
     56     public void shouldSupportHeaderIterator() throws Exception {
     57         HttpResponse resp =
     58                 new TestHttpResponse(304, "REDIRECTED",
     59                         new BasicHeader("Location", "http://bar.com"),
     60                         new BasicHeader("Location", "http://zombo.com"));
     61 
     62         HeaderIterator it = resp.headerIterator();
     63 
     64         assertThat(it.hasNext(), is(true));
     65         assertThat(it.nextHeader().getValue(), equalTo("http://bar.com"));
     66         assertThat(it.nextHeader().getValue(), equalTo("http://zombo.com"));
     67         assertThat(it.hasNext(), is(false));
     68     }
     69 
     70     @Test
     71     public void shouldSupportHeaderIteratorWithArg() throws Exception {
     72         HttpResponse resp =
     73                 new TestHttpResponse(304, "REDIRECTED",
     74                         new BasicHeader("Location", "http://bar.com"),
     75                         new BasicHeader("X-Zombo-Com", "http://zombo.com"),
     76                         new BasicHeader("Location", "http://foo.com"));
     77 
     78         HeaderIterator it = resp.headerIterator("Location");
     79 
     80         assertThat(it.hasNext(), is(true));
     81         assertThat(it.nextHeader().getValue(), equalTo("http://bar.com"));
     82         assertThat(it.hasNext(), is(true));
     83         assertThat(it.nextHeader().getValue(), equalTo("http://foo.com"));
     84         assertThat(it.hasNext(), is(false));
     85     }
     86 
     87 
     88     @Test
     89     public void shouldSupportGetHeadersWithArg() throws Exception {
     90         HttpResponse resp =
     91                 new TestHttpResponse(304, "REDIRECTED",
     92                         new BasicHeader("Location", "http://bar.com"),
     93                         new BasicHeader("X-Zombo-Com", "http://zombo.com"),
     94                         new BasicHeader("Location", "http://foo.com"));
     95 
     96 
     97         Header[] headers = resp.getHeaders("Location");
     98         assertThat(headers.length, is(2));
     99         assertThat(headers[0].getValue(), CoreMatchers.equalTo("http://bar.com"));
    100         assertThat(headers[1].getValue(), CoreMatchers.equalTo("http://foo.com"));
    101     }
    102 
    103     @Test
    104     public void canAddNewBasicHeader() {
    105         TestHttpResponse response = new TestHttpResponse(200, "abc");
    106         assertThat(response.getAllHeaders().length, is(0));
    107         response.addHeader(new BasicHeader("foo", "bar"));
    108         assertThat(response.getAllHeaders().length, is(1));
    109         assertThat(response.getHeaders("foo")[0].getValue(), CoreMatchers.equalTo("bar"));
    110     }
    111 
    112     @Test
    113     public void canOverrideExistingHeaderValue() {
    114         TestHttpResponse response = new TestHttpResponse(200, "abc", new BasicHeader("foo", "bar"));
    115         response.setHeader(new BasicHeader("foo", "bletch"));
    116         assertThat(response.getAllHeaders().length, is(1));
    117         assertThat(response.getHeaders("foo")[0].getValue(), CoreMatchers.equalTo("bletch"));
    118     }
    119 
    120     @Test
    121     public void onlyOverridesFirstHeaderValue() {
    122         TestHttpResponse response = new TestHttpResponse(200, "abc", new BasicHeader("foo", "bar"), new BasicHeader("foo", "baz"));
    123         response.setHeader(new BasicHeader("foo", "bletch"));
    124         assertThat(response.getAllHeaders().length, is(2));
    125         assertThat(response.getHeaders("foo")[0].getValue(), CoreMatchers.equalTo("bletch"));
    126         assertThat(response.getHeaders("foo")[1].getValue(), CoreMatchers.equalTo("baz"));
    127     }
    128 
    129 }
    130