aboutsummaryrefslogtreecommitdiff
path: root/src/test/java/com/xtremelabs/robolectric/tester/org/apache/http/FakeHttpLayerTest.java
blob: 88534f08fc56f67cdb20c014be69693ed736ce31 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package com.xtremelabs.robolectric.tester.org.apache.http;

import com.xtremelabs.robolectric.WithTestDefaultsRunner;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.entity.StringEntity;
import org.apache.http.message.BasicHeader;
import org.apache.http.util.EntityUtils;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;

import java.io.IOException;

import static org.hamcrest.core.IsEqual.equalTo;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;

@RunWith(WithTestDefaultsRunner.class)
public class FakeHttpLayerTest {

    private FakeHttpLayer.RequestMatcherBuilder requestMatcherBuilder;

    @Before
    public void setUp() throws Exception {
        requestMatcherBuilder = new FakeHttpLayer.RequestMatcherBuilder();
    }

    @Test
    public void requestMatcherBuilder_shouldAddHost() throws Exception {
        requestMatcherBuilder.host("example.com");
        assertThat("example.com", equalTo(requestMatcherBuilder.getHostname()));
    }

    @Test
    public void requestMatcherBuilder_shouldAddMethod() throws Exception {
        requestMatcherBuilder.method("POST");
        assertThat("POST", equalTo(requestMatcherBuilder.getMethod()));
    }

    @Test
    public void requestMatcherBuilder_shouldAddPath() throws Exception {
        requestMatcherBuilder.path("foo/bar");
        assertThat("/foo/bar", equalTo(requestMatcherBuilder.getPath()));
    }

    @Test
    public void requestMatcherBuilder_shouldAddParams() throws Exception {
        requestMatcherBuilder.param("param1", "param one");
        assertThat("param one", equalTo(requestMatcherBuilder.getParam("param1")));
    }

    @Test
    public void requestMatcherBuilder_shouldAddHeaders() throws Exception {
        requestMatcherBuilder.header("header1", "header one");
        assertThat("header one", equalTo(requestMatcherBuilder.getHeader("header1")));
    }

    @Test
    public void matches__shouldMatchHeaders() throws Exception {
        requestMatcherBuilder.header("header1", "header one");
        HttpGet match = new HttpGet("example.com");
        HttpGet noMatch = new HttpGet("example.com");
        match.setHeader(new BasicHeader("header1", "header one"));
        noMatch.setHeader(new BasicHeader("header1", "header not a match"));

        assertFalse(requestMatcherBuilder.matches(new HttpGet("example.com")));
        assertFalse(requestMatcherBuilder.matches(noMatch));
        assertTrue(requestMatcherBuilder.matches(match));
    }

    @Test
    public void matches__shouldMatchPostBody() throws Exception {
        final String expectedText = "some post body text";

        requestMatcherBuilder.postBody(new FakeHttpLayer.RequestMatcherBuilder.PostBodyMatcher() {
            @Override
            public boolean matches(HttpEntity actualPostBody) throws IOException {
                return EntityUtils.toString(actualPostBody).equals(expectedText);
            }
        });

        HttpPut match = new HttpPut("example.com");
        match.setEntity(new StringEntity(expectedText));

        HttpPost noMatch = new HttpPost("example.com");
        noMatch.setEntity(new StringEntity("some text that does not match"));

        assertFalse(requestMatcherBuilder.matches(new HttpGet("example.com")));
        assertFalse(requestMatcherBuilder.matches(noMatch));
        assertTrue(requestMatcherBuilder.matches(match));
    }
}