Jean-Baptiste Queru | d56b88a | 2012-11-07 07:48:57 -0800 | [diff] [blame] | 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 | |
| 17 | package com.android.volley.toolbox; |
| 18 | |
| 19 | import com.android.volley.NetworkResponse; |
| 20 | import com.android.volley.Request; |
| 21 | import com.android.volley.Response; |
| 22 | import com.android.volley.mock.MockHttpStack; |
| 23 | |
| 24 | import org.apache.http.ProtocolVersion; |
| 25 | import org.apache.http.entity.StringEntity; |
| 26 | import org.apache.http.message.BasicHttpResponse; |
| 27 | |
| 28 | import android.test.AndroidTestCase; |
| 29 | import android.test.suitebuilder.annotation.SmallTest; |
| 30 | |
| 31 | import java.util.HashMap; |
| 32 | import java.util.Map; |
| 33 | |
| 34 | @SmallTest |
| 35 | public 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); |
Ficus Kirkpatrick | 35d5cc3 | 2014-02-08 11:15:04 -0800 | [diff] [blame^] | 50 | Request<String> request = new Request<String>(Request.Method.GET, "http://foo", null) { |
Jean-Baptiste Queru | d56b88a | 2012-11-07 07:48:57 -0800 | [diff] [blame] | 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 |
Ficus Kirkpatrick | 35d5cc3 | 2014-02-08 11:15:04 -0800 | [diff] [blame^] | 69 | public Map<String, String> getParams() { |
Jean-Baptiste Queru | d56b88a | 2012-11-07 07:48:57 -0800 | [diff] [blame] | 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 | } |