FakeHttpLayer.java revision 5ed9c8041b5f927c930d50edef58c5378d12f89c
12cdde11355e7c249e83a8b7aaabae5977bbc3e49Phil Goodwinpackage com.xtremelabs.robolectric.tester.org.apache.http;
2f91a118112ae13d39251dd3c09f1d85d33283ba1Christian Williams & Phil Goodwin
36f34840c49a26f54f62ead83509e3f64bed7e0cfNeal Sancheimport org.apache.http.Header;
44105051dc6b4e1d67766d1ad886bd3b191f8a68fChristian Williamsimport org.apache.http.HttpException;
5f91a118112ae13d39251dd3c09f1d85d33283ba1Christian Williams & Phil Goodwinimport org.apache.http.HttpHost;
6f91a118112ae13d39251dd3c09f1d85d33283ba1Christian Williams & Phil Goodwinimport org.apache.http.HttpRequest;
7f91a118112ae13d39251dd3c09f1d85d33283ba1Christian Williams & Phil Goodwinimport org.apache.http.HttpResponse;
8f91a118112ae13d39251dd3c09f1d85d33283ba1Christian Williams & Phil Goodwinimport org.apache.http.client.RequestDirector;
9f91a118112ae13d39251dd3c09f1d85d33283ba1Christian Williams & Phil Goodwinimport org.apache.http.protocol.HttpContext;
10f91a118112ae13d39251dd3c09f1d85d33283ba1Christian Williams & Phil Goodwin
114105051dc6b4e1d67766d1ad886bd3b191f8a68fChristian Williamsimport javax.xml.ws.http.HTTPException;
124105051dc6b4e1d67766d1ad886bd3b191f8a68fChristian Williamsimport java.io.IOException;
13f91a118112ae13d39251dd3c09f1d85d33283ba1Christian Williams & Phil Goodwinimport java.util.ArrayList;
14f91a118112ae13d39251dd3c09f1d85d33283ba1Christian Williams & Phil Goodwinimport java.util.List;
15f91a118112ae13d39251dd3c09f1d85d33283ba1Christian Williams & Phil Goodwin
16f91a118112ae13d39251dd3c09f1d85d33283ba1Christian Williams & Phil Goodwinpublic class FakeHttpLayer {
17f91a118112ae13d39251dd3c09f1d85d33283ba1Christian Williams & Phil Goodwin    List<HttpResponse> pendingHttpResponses = new ArrayList<HttpResponse>();
18f91a118112ae13d39251dd3c09f1d85d33283ba1Christian Williams & Phil Goodwin    List<HttpRequestInfo> httpRequestInfos = new ArrayList<HttpRequestInfo>();
192cdde11355e7c249e83a8b7aaabae5977bbc3e49Phil Goodwin    List<HttpEntityStub.ResponseRule> httpResponseRules = new ArrayList<HttpEntityStub.ResponseRule>();
20f91a118112ae13d39251dd3c09f1d85d33283ba1Christian Williams & Phil Goodwin    HttpResponse defaultHttpResponse;
212cdde11355e7c249e83a8b7aaabae5977bbc3e49Phil Goodwin    private HttpResponse defaultResponse;
22f91a118112ae13d39251dd3c09f1d85d33283ba1Christian Williams & Phil Goodwin
23f91a118112ae13d39251dd3c09f1d85d33283ba1Christian Williams & Phil Goodwin    public void addPendingHttpResponse(int statusCode, String responseBody) {
24f91a118112ae13d39251dd3c09f1d85d33283ba1Christian Williams & Phil Goodwin        addPendingHttpResponse(new TestHttpResponse(statusCode, responseBody));
25f91a118112ae13d39251dd3c09f1d85d33283ba1Christian Williams & Phil Goodwin    }
26f91a118112ae13d39251dd3c09f1d85d33283ba1Christian Williams & Phil Goodwin
2739a416145e85c7008805ef493b2d61f66d669158Tyler Schultz    public void addPendingHttpResponseWithContentType(int statusCode, String responseBody, Header contentType) {
286f34840c49a26f54f62ead83509e3f64bed7e0cfNeal Sanche        addPendingHttpResponse(new TestHttpResponse(statusCode, responseBody, contentType));
296f34840c49a26f54f62ead83509e3f64bed7e0cfNeal Sanche    }
306f34840c49a26f54f62ead83509e3f64bed7e0cfNeal Sanche
31f91a118112ae13d39251dd3c09f1d85d33283ba1Christian Williams & Phil Goodwin    public void addPendingHttpResponse(HttpResponse httpResponse) {
32f91a118112ae13d39251dd3c09f1d85d33283ba1Christian Williams & Phil Goodwin        pendingHttpResponses.add(httpResponse);
33f91a118112ae13d39251dd3c09f1d85d33283ba1Christian Williams & Phil Goodwin    }
34f91a118112ae13d39251dd3c09f1d85d33283ba1Christian Williams & Phil Goodwin
35f91a118112ae13d39251dd3c09f1d85d33283ba1Christian Williams & Phil Goodwin    public void addHttpResponseRule(String method, String uri, HttpResponse response) {
36f91a118112ae13d39251dd3c09f1d85d33283ba1Christian Williams & Phil Goodwin        addHttpResponseRule(new DefaultRequestMatcher(method, uri), response);
37f91a118112ae13d39251dd3c09f1d85d33283ba1Christian Williams & Phil Goodwin    }
38f91a118112ae13d39251dd3c09f1d85d33283ba1Christian Williams & Phil Goodwin
39f91a118112ae13d39251dd3c09f1d85d33283ba1Christian Williams & Phil Goodwin    public void addHttpResponseRule(String uri, HttpResponse response) {
40f91a118112ae13d39251dd3c09f1d85d33283ba1Christian Williams & Phil Goodwin        addHttpResponseRule(new UriRequestMatcher(uri), response);
41f91a118112ae13d39251dd3c09f1d85d33283ba1Christian Williams & Phil Goodwin    }
42f91a118112ae13d39251dd3c09f1d85d33283ba1Christian Williams & Phil Goodwin
43f91a118112ae13d39251dd3c09f1d85d33283ba1Christian Williams & Phil Goodwin    public void addHttpResponseRule(String uri, String response) {
44f91a118112ae13d39251dd3c09f1d85d33283ba1Christian Williams & Phil Goodwin        addHttpResponseRule(new UriRequestMatcher(uri), new TestHttpResponse(200, response));
45f91a118112ae13d39251dd3c09f1d85d33283ba1Christian Williams & Phil Goodwin    }
46f91a118112ae13d39251dd3c09f1d85d33283ba1Christian Williams & Phil Goodwin
47f91a118112ae13d39251dd3c09f1d85d33283ba1Christian Williams & Phil Goodwin    public void addHttpResponseRule(RequestMatcher requestMatcher, HttpResponse response) {
484105051dc6b4e1d67766d1ad886bd3b191f8a68fChristian Williams        addHttpResponseRule(new RequestMatcherResponseRule(requestMatcher, response));
49f91a118112ae13d39251dd3c09f1d85d33283ba1Christian Williams & Phil Goodwin    }
50f91a118112ae13d39251dd3c09f1d85d33283ba1Christian Williams & Phil Goodwin
512cdde11355e7c249e83a8b7aaabae5977bbc3e49Phil Goodwin    public void addHttpResponseRule(HttpEntityStub.ResponseRule responseRule) {
52f91a118112ae13d39251dd3c09f1d85d33283ba1Christian Williams & Phil Goodwin        httpResponseRules.add(responseRule);
53f91a118112ae13d39251dd3c09f1d85d33283ba1Christian Williams & Phil Goodwin    }
54f91a118112ae13d39251dd3c09f1d85d33283ba1Christian Williams & Phil Goodwin
55f91a118112ae13d39251dd3c09f1d85d33283ba1Christian Williams & Phil Goodwin    public void setDefaultHttpResponse(HttpResponse defaultHttpResponse) {
56f91a118112ae13d39251dd3c09f1d85d33283ba1Christian Williams & Phil Goodwin        this.defaultHttpResponse = defaultHttpResponse;
57f91a118112ae13d39251dd3c09f1d85d33283ba1Christian Williams & Phil Goodwin    }
58f91a118112ae13d39251dd3c09f1d85d33283ba1Christian Williams & Phil Goodwin
59d76cb3959b31f5def82d0476cc114cc5da06a7a1Phil Goodwin    public void setDefaultHttpResponse(int statusCode, String responseBody) {
60d76cb3959b31f5def82d0476cc114cc5da06a7a1Phil Goodwin        setDefaultHttpResponse(new TestHttpResponse(statusCode, responseBody));
61d76cb3959b31f5def82d0476cc114cc5da06a7a1Phil Goodwin    }
62d76cb3959b31f5def82d0476cc114cc5da06a7a1Phil Goodwin
634105051dc6b4e1d67766d1ad886bd3b191f8a68fChristian Williams    private HttpResponse findResponse(HttpRequest httpRequest) throws HttpException, IOException {
64f91a118112ae13d39251dd3c09f1d85d33283ba1Christian Williams & Phil Goodwin        if (!pendingHttpResponses.isEmpty()) {
65f91a118112ae13d39251dd3c09f1d85d33283ba1Christian Williams & Phil Goodwin            return pendingHttpResponses.remove(0);
66f91a118112ae13d39251dd3c09f1d85d33283ba1Christian Williams & Phil Goodwin        }
67f91a118112ae13d39251dd3c09f1d85d33283ba1Christian Williams & Phil Goodwin
682cdde11355e7c249e83a8b7aaabae5977bbc3e49Phil Goodwin        for (HttpEntityStub.ResponseRule httpResponseRule : httpResponseRules) {
69f91a118112ae13d39251dd3c09f1d85d33283ba1Christian Williams & Phil Goodwin            if (httpResponseRule.matches(httpRequest)) {
70f91a118112ae13d39251dd3c09f1d85d33283ba1Christian Williams & Phil Goodwin                return httpResponseRule.getResponse();
71f91a118112ae13d39251dd3c09f1d85d33283ba1Christian Williams & Phil Goodwin            }
72f91a118112ae13d39251dd3c09f1d85d33283ba1Christian Williams & Phil Goodwin        }
73f91a118112ae13d39251dd3c09f1d85d33283ba1Christian Williams & Phil Goodwin
74f91a118112ae13d39251dd3c09f1d85d33283ba1Christian Williams & Phil Goodwin        return defaultHttpResponse;
75f91a118112ae13d39251dd3c09f1d85d33283ba1Christian Williams & Phil Goodwin    }
76f91a118112ae13d39251dd3c09f1d85d33283ba1Christian Williams & Phil Goodwin
772cdde11355e7c249e83a8b7aaabae5977bbc3e49Phil Goodwin    public HttpResponse emulateRequest(HttpHost httpHost, HttpRequest httpRequest, HttpContext httpContext, RequestDirector requestDirector) throws HttpException, IOException {
78f91a118112ae13d39251dd3c09f1d85d33283ba1Christian Williams & Phil Goodwin        HttpResponse httpResponse = findResponse(httpRequest);
79f91a118112ae13d39251dd3c09f1d85d33283ba1Christian Williams & Phil Goodwin
80f91a118112ae13d39251dd3c09f1d85d33283ba1Christian Williams & Phil Goodwin        if (httpResponse == null) {
81f91a118112ae13d39251dd3c09f1d85d33283ba1Christian Williams & Phil Goodwin            throw new RuntimeException("Unexpected call to execute, no pending responses are available. See Robolectric.addPendingResponse().");
82f91a118112ae13d39251dd3c09f1d85d33283ba1Christian Williams & Phil Goodwin        }
83f91a118112ae13d39251dd3c09f1d85d33283ba1Christian Williams & Phil Goodwin
84f91a118112ae13d39251dd3c09f1d85d33283ba1Christian Williams & Phil Goodwin        httpRequestInfos.add(new HttpRequestInfo(httpRequest, httpHost, httpContext, requestDirector));
85f91a118112ae13d39251dd3c09f1d85d33283ba1Christian Williams & Phil Goodwin
86f91a118112ae13d39251dd3c09f1d85d33283ba1Christian Williams & Phil Goodwin        return httpResponse;
87f91a118112ae13d39251dd3c09f1d85d33283ba1Christian Williams & Phil Goodwin    }
882cdde11355e7c249e83a8b7aaabae5977bbc3e49Phil Goodwin    public boolean hasPendingResponses() {
892cdde11355e7c249e83a8b7aaabae5977bbc3e49Phil Goodwin        return !pendingHttpResponses.isEmpty();
902cdde11355e7c249e83a8b7aaabae5977bbc3e49Phil Goodwin    }
912cdde11355e7c249e83a8b7aaabae5977bbc3e49Phil Goodwin
922cdde11355e7c249e83a8b7aaabae5977bbc3e49Phil Goodwin    public boolean hasRequestInfos() {
932cdde11355e7c249e83a8b7aaabae5977bbc3e49Phil Goodwin        return !httpRequestInfos.isEmpty();
942cdde11355e7c249e83a8b7aaabae5977bbc3e49Phil Goodwin    }
952cdde11355e7c249e83a8b7aaabae5977bbc3e49Phil Goodwin
962cdde11355e7c249e83a8b7aaabae5977bbc3e49Phil Goodwin    public boolean hasResponseRules() {
972cdde11355e7c249e83a8b7aaabae5977bbc3e49Phil Goodwin        return !httpResponseRules.isEmpty();
982cdde11355e7c249e83a8b7aaabae5977bbc3e49Phil Goodwin    }
992cdde11355e7c249e83a8b7aaabae5977bbc3e49Phil Goodwin
1005ed9c8041b5f927c930d50edef58c5378d12f89cAlexander Murmann & Phil Goodwin    public boolean hasRequestMatchingRule(RequestMatcher rule) {
1015ed9c8041b5f927c930d50edef58c5378d12f89cAlexander Murmann & Phil Goodwin        for (HttpRequestInfo requestInfo : httpRequestInfos) {
1025ed9c8041b5f927c930d50edef58c5378d12f89cAlexander Murmann & Phil Goodwin            if (rule.matches(requestInfo.httpRequest)) {
1035ed9c8041b5f927c930d50edef58c5378d12f89cAlexander Murmann & Phil Goodwin                return true;
1045ed9c8041b5f927c930d50edef58c5378d12f89cAlexander Murmann & Phil Goodwin            }
1055ed9c8041b5f927c930d50edef58c5378d12f89cAlexander Murmann & Phil Goodwin        }
1065ed9c8041b5f927c930d50edef58c5378d12f89cAlexander Murmann & Phil Goodwin        return false;
1075ed9c8041b5f927c930d50edef58c5378d12f89cAlexander Murmann & Phil Goodwin    }
1085ed9c8041b5f927c930d50edef58c5378d12f89cAlexander Murmann & Phil Goodwin
1092cdde11355e7c249e83a8b7aaabae5977bbc3e49Phil Goodwin    public HttpResponse getDefaultResponse() {
1102cdde11355e7c249e83a8b7aaabae5977bbc3e49Phil Goodwin        return defaultResponse;
1112cdde11355e7c249e83a8b7aaabae5977bbc3e49Phil Goodwin    }
1122cdde11355e7c249e83a8b7aaabae5977bbc3e49Phil Goodwin
1132cdde11355e7c249e83a8b7aaabae5977bbc3e49Phil Goodwin    public HttpRequestInfo getSentHttpRequestInfo(int index) {
1142cdde11355e7c249e83a8b7aaabae5977bbc3e49Phil Goodwin        return httpRequestInfos.get(index);
1152cdde11355e7c249e83a8b7aaabae5977bbc3e49Phil Goodwin    }
116b539795cdd8f6be1b0b01527905a31faea03242cRob Dickerson    public void clearHttpResponseRules() {
117b539795cdd8f6be1b0b01527905a31faea03242cRob Dickerson        httpResponseRules.clear();
118b539795cdd8f6be1b0b01527905a31faea03242cRob Dickerson    }
1192cdde11355e7c249e83a8b7aaabae5977bbc3e49Phil Goodwin
1202cdde11355e7c249e83a8b7aaabae5977bbc3e49Phil Goodwin    public static class RequestMatcherResponseRule implements HttpEntityStub.ResponseRule {
121f91a118112ae13d39251dd3c09f1d85d33283ba1Christian Williams & Phil Goodwin        private RequestMatcher requestMatcher;
1224105051dc6b4e1d67766d1ad886bd3b191f8a68fChristian Williams        private HttpResponse responseToGive;
1234105051dc6b4e1d67766d1ad886bd3b191f8a68fChristian Williams        private IOException ioException;
1244105051dc6b4e1d67766d1ad886bd3b191f8a68fChristian Williams        private HTTPException httpException;
125f91a118112ae13d39251dd3c09f1d85d33283ba1Christian Williams & Phil Goodwin
1264105051dc6b4e1d67766d1ad886bd3b191f8a68fChristian Williams        public RequestMatcherResponseRule(RequestMatcher requestMatcher, HttpResponse responseToGive) {
1274105051dc6b4e1d67766d1ad886bd3b191f8a68fChristian Williams            this.requestMatcher = requestMatcher;
128f91a118112ae13d39251dd3c09f1d85d33283ba1Christian Williams & Phil Goodwin            this.responseToGive = responseToGive;
1294105051dc6b4e1d67766d1ad886bd3b191f8a68fChristian Williams        }
1304105051dc6b4e1d67766d1ad886bd3b191f8a68fChristian Williams
1314105051dc6b4e1d67766d1ad886bd3b191f8a68fChristian Williams        public RequestMatcherResponseRule(RequestMatcher requestMatcher, IOException ioException) {
1324105051dc6b4e1d67766d1ad886bd3b191f8a68fChristian Williams            this.requestMatcher = requestMatcher;
1334105051dc6b4e1d67766d1ad886bd3b191f8a68fChristian Williams            this.ioException = ioException;
1344105051dc6b4e1d67766d1ad886bd3b191f8a68fChristian Williams        }
1354105051dc6b4e1d67766d1ad886bd3b191f8a68fChristian Williams
1364105051dc6b4e1d67766d1ad886bd3b191f8a68fChristian Williams        public RequestMatcherResponseRule(RequestMatcher requestMatcher, HTTPException httpException) {
137f91a118112ae13d39251dd3c09f1d85d33283ba1Christian Williams & Phil Goodwin            this.requestMatcher = requestMatcher;
1384105051dc6b4e1d67766d1ad886bd3b191f8a68fChristian Williams            this.httpException = httpException;
139f91a118112ae13d39251dd3c09f1d85d33283ba1Christian Williams & Phil Goodwin        }
140f91a118112ae13d39251dd3c09f1d85d33283ba1Christian Williams & Phil Goodwin
1414105051dc6b4e1d67766d1ad886bd3b191f8a68fChristian Williams        @Override public boolean matches(HttpRequest request) {
142f91a118112ae13d39251dd3c09f1d85d33283ba1Christian Williams & Phil Goodwin            return requestMatcher.matches(request);
143f91a118112ae13d39251dd3c09f1d85d33283ba1Christian Williams & Phil Goodwin        }
144f91a118112ae13d39251dd3c09f1d85d33283ba1Christian Williams & Phil Goodwin
1454105051dc6b4e1d67766d1ad886bd3b191f8a68fChristian Williams        @Override public HttpResponse getResponse() throws HttpException, IOException {
1464105051dc6b4e1d67766d1ad886bd3b191f8a68fChristian Williams            if (httpException != null) throw httpException;
1474105051dc6b4e1d67766d1ad886bd3b191f8a68fChristian Williams            if (ioException != null) throw ioException;
148f91a118112ae13d39251dd3c09f1d85d33283ba1Christian Williams & Phil Goodwin            return responseToGive;
149f91a118112ae13d39251dd3c09f1d85d33283ba1Christian Williams & Phil Goodwin        }
150f91a118112ae13d39251dd3c09f1d85d33283ba1Christian Williams & Phil Goodwin    }
151f91a118112ae13d39251dd3c09f1d85d33283ba1Christian Williams & Phil Goodwin
1524105051dc6b4e1d67766d1ad886bd3b191f8a68fChristian Williams    public static class DefaultRequestMatcher implements RequestMatcher {
153f91a118112ae13d39251dd3c09f1d85d33283ba1Christian Williams & Phil Goodwin        private String method;
154f91a118112ae13d39251dd3c09f1d85d33283ba1Christian Williams & Phil Goodwin        private String uri;
155f91a118112ae13d39251dd3c09f1d85d33283ba1Christian Williams & Phil Goodwin
1564105051dc6b4e1d67766d1ad886bd3b191f8a68fChristian Williams        public DefaultRequestMatcher(String method, String uri) {
157f91a118112ae13d39251dd3c09f1d85d33283ba1Christian Williams & Phil Goodwin            this.method = method;
158f91a118112ae13d39251dd3c09f1d85d33283ba1Christian Williams & Phil Goodwin            this.uri = uri;
159f91a118112ae13d39251dd3c09f1d85d33283ba1Christian Williams & Phil Goodwin        }
160f91a118112ae13d39251dd3c09f1d85d33283ba1Christian Williams & Phil Goodwin
161f91a118112ae13d39251dd3c09f1d85d33283ba1Christian Williams & Phil Goodwin        @Override public boolean matches(HttpRequest request) {
162f91a118112ae13d39251dd3c09f1d85d33283ba1Christian Williams & Phil Goodwin            return request.getRequestLine().getMethod().equals(method) &&
163f91a118112ae13d39251dd3c09f1d85d33283ba1Christian Williams & Phil Goodwin                    request.getRequestLine().getUri().equals(uri);
164f91a118112ae13d39251dd3c09f1d85d33283ba1Christian Williams & Phil Goodwin        }
165f91a118112ae13d39251dd3c09f1d85d33283ba1Christian Williams & Phil Goodwin    }
166f91a118112ae13d39251dd3c09f1d85d33283ba1Christian Williams & Phil Goodwin
1674105051dc6b4e1d67766d1ad886bd3b191f8a68fChristian Williams    public static class UriRequestMatcher implements RequestMatcher {
168f91a118112ae13d39251dd3c09f1d85d33283ba1Christian Williams & Phil Goodwin        private String uri;
169f91a118112ae13d39251dd3c09f1d85d33283ba1Christian Williams & Phil Goodwin
1704105051dc6b4e1d67766d1ad886bd3b191f8a68fChristian Williams        public UriRequestMatcher(String uri) {
171f91a118112ae13d39251dd3c09f1d85d33283ba1Christian Williams & Phil Goodwin            this.uri = uri;
172f91a118112ae13d39251dd3c09f1d85d33283ba1Christian Williams & Phil Goodwin        }
173f91a118112ae13d39251dd3c09f1d85d33283ba1Christian Williams & Phil Goodwin
174f91a118112ae13d39251dd3c09f1d85d33283ba1Christian Williams & Phil Goodwin        @Override public boolean matches(HttpRequest request) {
175f91a118112ae13d39251dd3c09f1d85d33283ba1Christian Williams & Phil Goodwin            return request.getRequestLine().getUri().equals(uri);
176f91a118112ae13d39251dd3c09f1d85d33283ba1Christian Williams & Phil Goodwin        }
177f91a118112ae13d39251dd3c09f1d85d33283ba1Christian Williams & Phil Goodwin    }
178f91a118112ae13d39251dd3c09f1d85d33283ba1Christian Williams & Phil Goodwin}
179