Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
18084 manas 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 org.junit.After;
29
import org.junit.Before;
30
import org.junit.Test;
31
import org.junit.runner.RunWith;
32
import org.robolectric.RobolectricTestRunner;
33
 
34
import static org.junit.Assert.*;
35
 
36
import java.util.HashMap;
37
import java.util.Map;
38
 
39
@RunWith(RobolectricTestRunner.class)
40
public 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
}