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 org.apache.http.HttpEntity;
20import org.apache.http.HttpHost;
21import org.apache.http.HttpRequest;
22import org.apache.http.HttpResponse;
23import org.apache.http.HttpStatus;
24import org.apache.http.ProtocolVersion;
25import org.apache.http.StatusLine;
26import org.apache.http.client.HttpClient;
27import org.apache.http.client.ResponseHandler;
28import org.apache.http.client.methods.HttpUriRequest;
29import org.apache.http.conn.ClientConnectionManager;
30import org.apache.http.message.BasicHttpResponse;
31import org.apache.http.message.BasicStatusLine;
32import org.apache.http.params.HttpParams;
33import org.apache.http.protocol.HttpContext;
34
35
36public class MockHttpClient implements HttpClient {
37    private int mStatusCode = HttpStatus.SC_OK;
38    private HttpEntity mResponseEntity = null;
39
40    public void setResponseData(HttpEntity entity) {
41        mStatusCode = HttpStatus.SC_OK;
42        mResponseEntity = entity;
43    }
44
45    public void setErrorCode(int statusCode) {
46        if (statusCode == HttpStatus.SC_OK) {
47            throw new IllegalArgumentException("statusCode cannot be 200 for an error");
48        }
49        mStatusCode = statusCode;
50    }
51
52    public HttpUriRequest requestExecuted = null;
53
54    // This is the only one we actually use.
55    @Override
56    public HttpResponse execute(HttpUriRequest request, HttpContext context) {
57        requestExecuted = request;
58        StatusLine statusLine = new BasicStatusLine(
59                new ProtocolVersion("HTTP", 1, 1), mStatusCode, "");
60        HttpResponse response = new BasicHttpResponse(statusLine);
61        response.setEntity(mResponseEntity);
62
63        return response;
64    }
65
66
67    // Unimplemented methods ahoy
68
69    @Override
70    public HttpResponse execute(HttpUriRequest request) {
71        throw new UnsupportedOperationException();
72    }
73
74    @Override
75    public HttpResponse execute(HttpHost target, HttpRequest request) {
76        throw new UnsupportedOperationException();
77    }
78
79    @Override
80    public <T> T execute(HttpUriRequest arg0, ResponseHandler<? extends T> arg1) {
81        throw new UnsupportedOperationException();
82    }
83
84    @Override
85    public HttpResponse execute(HttpHost target, HttpRequest request, HttpContext context) {
86        throw new UnsupportedOperationException();
87    }
88
89    @Override
90    public <T> T execute(HttpUriRequest arg0, ResponseHandler<? extends T> arg1, HttpContext arg2) {
91        throw new UnsupportedOperationException();
92    }
93
94    @Override
95    public <T> T execute(HttpHost arg0, HttpRequest arg1, ResponseHandler<? extends T> arg2) {
96        throw new UnsupportedOperationException();
97    }
98
99    @Override
100    public <T> T execute(HttpHost arg0, HttpRequest arg1, ResponseHandler<? extends T> arg2,
101            HttpContext arg3) {
102        throw new UnsupportedOperationException();
103    }
104
105    @Override
106    public ClientConnectionManager getConnectionManager() {
107        throw new UnsupportedOperationException();
108    }
109
110    @Override
111    public HttpParams getParams() {
112        throw new UnsupportedOperationException();
113    }
114}
115