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 android.test.AndroidTestCase;
29import android.test.suitebuilder.annotation.SmallTest;
30
31import java.util.HashMap;
32import java.util.Map;
33
34@SmallTest
35public class BasicNetworkTest extends AndroidTestCase {
36
37    @Override
38    protected void setUp() throws Exception {
39        super.setUp();
40        mContext = getContext();
41    }
42
43    public void testHeadersAndPostParams() throws Exception {
44        MockHttpStack mockHttpStack = new MockHttpStack();
45        BasicHttpResponse fakeResponse = new BasicHttpResponse(new ProtocolVersion("HTTP", 1, 1),
46                200, "OK");
47        fakeResponse.setEntity(new StringEntity("foobar"));
48        mockHttpStack.setResponseToReturn(fakeResponse);
49        BasicNetwork httpNetwork = new BasicNetwork(mockHttpStack);
50        Request<String> request = new Request<String>("http://foo", null) {
51
52            @Override
53            protected Response<String> parseNetworkResponse(NetworkResponse response) {
54                return null;
55            }
56
57            @Override
58            protected void deliverResponse(String response) {
59            }
60
61            @Override
62            public Map<String, String> getHeaders() {
63                Map<String, String> result = new HashMap<String, String>();
64                result.put("requestheader", "foo");
65                return result;
66            }
67
68            @Override
69            public Map<String, String> getPostParams() {
70                Map<String, String> result = new HashMap<String, String>();
71                result.put("requestpost", "foo");
72                return result;
73            }
74        };
75        httpNetwork.performRequest(request);
76        assertEquals("foo", mockHttpStack.getLastHeaders().get("requestheader"));
77        assertEquals("requestpost=foo&", new String(mockHttpStack.getLastPostBody()));
78    }
79}
80