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.mock;
18
19import com.android.volley.AuthFailureError;
20import com.android.volley.Request;
21import com.android.volley.toolbox.HttpStack;
22
23import org.apache.http.HttpResponse;
24
25import java.util.HashMap;
26import java.util.Map;
27
28public class MockHttpStack implements HttpStack {
29
30    private HttpResponse mResponseToReturn;
31
32    private String mLastUrl;
33
34    private Map<String, String> mLastHeaders;
35
36    private byte[] mLastPostBody;
37
38    public String getLastUrl() {
39        return mLastUrl;
40    }
41
42    public Map<String, String> getLastHeaders() {
43        return mLastHeaders;
44    }
45
46    public byte[] getLastPostBody() {
47        return mLastPostBody;
48    }
49
50    public void setResponseToReturn(HttpResponse response) {
51        mResponseToReturn = response;
52    }
53
54    @Override
55    public HttpResponse performRequest(Request<?> request, Map<String, String> additionalHeaders)
56            throws AuthFailureError {
57        mLastUrl = request.getUrl();
58        mLastHeaders = new HashMap<String, String>();
59        if (request.getHeaders() != null) {
60            mLastHeaders.putAll(request.getHeaders());
61        }
62        if (additionalHeaders != null) {
63            mLastHeaders.putAll(additionalHeaders);
64        }
65        try {
66            mLastPostBody = request.getPostBody();
67        } catch (AuthFailureError e) {
68            mLastPostBody = null;
69        }
70        return mResponseToReturn;
71    }
72}
73