Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
21478 rajender 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
import org.junit.Test;
28
import org.junit.runner.RunWith;
29
import org.robolectric.RobolectricTestRunner;
30
 
31
import java.util.HashMap;
32
import java.util.Map;
33
 
34
import static org.junit.Assert.assertEquals;
35
 
36
@RunWith(RobolectricTestRunner.class)
37
public class BasicNetworkTest {
38
 
39
    @Test public void headersAndPostParams() throws Exception {
40
        MockHttpStack mockHttpStack = new MockHttpStack();
41
        BasicHttpResponse fakeResponse = new BasicHttpResponse(new ProtocolVersion("HTTP", 1, 1),
42
                200, "OK");
43
        fakeResponse.setEntity(new StringEntity("foobar"));
44
        mockHttpStack.setResponseToReturn(fakeResponse);
45
        BasicNetwork httpNetwork = new BasicNetwork(mockHttpStack);
46
        Request<String> request = new Request<String>(Request.Method.GET, "http://foo", null) {
47
 
48
            @Override
49
            protected Response<String> parseNetworkResponse(NetworkResponse response) {
50
                return null;
51
            }
52
 
53
            @Override
54
            protected void deliverResponse(String response) {
55
            }
56
 
57
            @Override
58
            public Map<String, String> getHeaders() {
59
                Map<String, String> result = new HashMap<String, String>();
60
                result.put("requestheader", "foo");
61
                return result;
62
            }
63
 
64
            @Override
65
            public Map<String, String> getParams() {
66
                Map<String, String> result = new HashMap<String, String>();
67
                result.put("requestpost", "foo");
68
                return result;
69
            }
70
        };
71
        httpNetwork.performRequest(request);
72
        assertEquals("foo", mockHttpStack.getLastHeaders().get("requestheader"));
73
        assertEquals("requestpost=foo&", new String(mockHttpStack.getLastPostBody()));
74
    }
75
}