BasicNetworkTest.java revision b9b8dc3d98fb1a8c3f02c2c2fcc18cbd344c05cb
1/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.volley.toolbox;
18
19import com.android.volley.NetworkResponse;
20import com.android.volley.Request;
21import com.android.volley.Response;
22import com.android.volley.mock.MockHttpStack;
23
24import org.apache.http.ProtocolVersion;
25import org.apache.http.entity.StringEntity;
26import org.apache.http.message.BasicHttpResponse;
27
28import org.junit.After;
29import org.junit.Before;
30import org.junit.Test;
31import org.junit.runner.RunWith;
32import org.robolectric.RobolectricTestRunner;
33
34import static org.junit.Assert.*;
35
36import java.util.HashMap;
37import java.util.Map;
38
39@RunWith(RobolectricTestRunner.class)
40public class BasicNetworkTest {
41
42    @Test public void headersAndPostParams() throws Exception {
43        MockHttpStack mockHttpStack = new MockHttpStack();
44        BasicHttpResponse fakeResponse = new BasicHttpResponse(new ProtocolVersion("HTTP", 1, 1),
45                200, "OK");
46        fakeResponse.setEntity(new StringEntity("foobar"));
47        mockHttpStack.setResponseToReturn(fakeResponse);
48        BasicNetwork httpNetwork = new BasicNetwork(mockHttpStack);
49        Request<String> request = new Request<String>(Request.Method.GET, "http://foo", null) {
50
51            @Override
52            protected Response<String> parseNetworkResponse(NetworkResponse response) {
53                return null;
54            }
55
56            @Override
57            protected void deliverResponse(String response) {
58            }
59
60            @Override
61            public Map<String, String> getHeaders() {
62                Map<String, String> result = new HashMap<String, String>();
63                result.put("requestheader", "foo");
64                return result;
65            }
66
67            @Override
68            public Map<String, String> getParams() {
69                Map<String, String> result = new HashMap<String, String>();
70                result.put("requestpost", "foo");
71                return result;
72            }
73        };
74        httpNetwork.performRequest(request);
75        assertEquals("foo", mockHttpStack.getLastHeaders().get("requestheader"));
76        assertEquals("requestpost=foo&", new String(mockHttpStack.getLastPostBody()));
77    }
78}
79