aboutsummaryrefslogtreecommitdiff
path: root/src/test/java/com/android/volley/toolbox/AdaptedHttpStackTest.java
blob: dbd6535f9a7a9dbb4bc113d5d3ee268c6bba2998 (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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package com.android.volley.toolbox;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import static org.mockito.Mockito.when;

import com.android.volley.Header;
import com.android.volley.Request;
import com.android.volley.mock.TestRequest;
import java.io.IOException;
import java.io.InputStream;
import java.net.SocketTimeoutException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.conn.ConnectTimeoutException;
import org.apache.http.message.BasicHeader;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner;

@RunWith(RobolectricTestRunner.class)
public class AdaptedHttpStackTest {
    private static final Request<?> REQUEST = new TestRequest.Get();
    private static final Map<String, String> ADDITIONAL_HEADERS = Collections.emptyMap();

    @Mock private HttpStack mHttpStack;
    @Mock private HttpResponse mHttpResponse;
    @Mock private StatusLine mStatusLine;
    @Mock private HttpEntity mHttpEntity;
    @Mock private InputStream mContent;

    private AdaptedHttpStack mAdaptedHttpStack;

    @Before
    public void setUp() {
        MockitoAnnotations.initMocks(this);
        mAdaptedHttpStack = new AdaptedHttpStack(mHttpStack);
        when(mHttpResponse.getStatusLine()).thenReturn(mStatusLine);
    }

    @Test(expected = SocketTimeoutException.class)
    public void requestTimeout() throws Exception {
        when(mHttpStack.performRequest(REQUEST, ADDITIONAL_HEADERS))
                .thenThrow(new ConnectTimeoutException());

        mAdaptedHttpStack.executeRequest(REQUEST, ADDITIONAL_HEADERS);
    }

    @Test
    public void emptyResponse() throws Exception {
        when(mHttpStack.performRequest(REQUEST, ADDITIONAL_HEADERS)).thenReturn(mHttpResponse);
        when(mStatusLine.getStatusCode()).thenReturn(12345);
        when(mHttpResponse.getAllHeaders()).thenReturn(new org.apache.http.Header[0]);

        com.android.volley.toolbox.HttpResponse response =
                mAdaptedHttpStack.executeRequest(REQUEST, ADDITIONAL_HEADERS);

        assertEquals(12345, response.getStatusCode());
        assertEquals(Collections.emptyList(), response.getHeaders());
        assertNull(response.getContent());
    }

    @Test
    public void nonEmptyResponse() throws Exception {
        when(mHttpStack.performRequest(REQUEST, ADDITIONAL_HEADERS)).thenReturn(mHttpResponse);
        when(mStatusLine.getStatusCode()).thenReturn(12345);
        when(mHttpResponse.getAllHeaders()).thenReturn(new org.apache.http.Header[0]);
        when(mHttpResponse.getEntity()).thenReturn(mHttpEntity);
        when(mHttpEntity.getContentLength()).thenReturn((long) Integer.MAX_VALUE);
        when(mHttpEntity.getContent()).thenReturn(mContent);

        com.android.volley.toolbox.HttpResponse response =
                mAdaptedHttpStack.executeRequest(REQUEST, ADDITIONAL_HEADERS);

        assertEquals(12345, response.getStatusCode());
        assertEquals(Collections.emptyList(), response.getHeaders());
        assertEquals(Integer.MAX_VALUE, response.getContentLength());
        assertSame(mContent, response.getContent());
    }

    @Test(expected = IOException.class)
    public void responseTooBig() throws Exception {
        when(mHttpStack.performRequest(REQUEST, ADDITIONAL_HEADERS)).thenReturn(mHttpResponse);
        when(mStatusLine.getStatusCode()).thenReturn(12345);
        when(mHttpResponse.getAllHeaders()).thenReturn(new org.apache.http.Header[0]);
        when(mHttpResponse.getEntity()).thenReturn(mHttpEntity);
        when(mHttpEntity.getContentLength()).thenReturn(Integer.MAX_VALUE + 1L);
        when(mHttpEntity.getContent()).thenReturn(mContent);

        mAdaptedHttpStack.executeRequest(REQUEST, ADDITIONAL_HEADERS);
    }

    @Test
    public void responseWithHeaders() throws Exception {
        when(mHttpStack.performRequest(REQUEST, ADDITIONAL_HEADERS)).thenReturn(mHttpResponse);
        when(mStatusLine.getStatusCode()).thenReturn(12345);
        when(mHttpResponse.getAllHeaders())
                .thenReturn(
                        new org.apache.http.Header[] {
                            new BasicHeader("header1", "value1_B"),
                            new BasicHeader("header3", "value3"),
                            new BasicHeader("HEADER2", "value2"),
                            new BasicHeader("header1", "value1_A")
                        });

        com.android.volley.toolbox.HttpResponse response =
                mAdaptedHttpStack.executeRequest(REQUEST, ADDITIONAL_HEADERS);

        assertEquals(12345, response.getStatusCode());
        assertNull(response.getContent());

        List<Header> expectedHeaders = new ArrayList<>();
        expectedHeaders.add(new Header("header1", "value1_B"));
        expectedHeaders.add(new Header("header3", "value3"));
        expectedHeaders.add(new Header("HEADER2", "value2"));
        expectedHeaders.add(new Header("header1", "value1_A"));
        assertEquals(expectedHeaders, response.getHeaders());
    }
}