12cdde11355e7c249e83a8b7aaabae5977bbc3e49Phil Goodwinpackage com.xtremelabs.robolectric.tester.org.apache.http;
2f91a118112ae13d39251dd3c09f1d85d33283ba1Christian Williams & Phil Goodwin
3f96b2c0575b8c3efcf05420815e7297c759bef24Chris Heisterkamp & Lowell Kirshimport com.xtremelabs.robolectric.Robolectric;
4a34c6ada3c3b41e5b04ab09db7ddc346ac0b3ab9Aaron VonderHaar & Amrit Thakurimport com.xtremelabs.robolectric.shadows.HttpResponseGenerator;
5536325a5cdb8cf47bd1ea607a17970499240fbdaLenny Turetsky & Phil Planteimport org.apache.http.*;
6f91a118112ae13d39251dd3c09f1d85d33283ba1Christian Williams & Phil Goodwinimport org.apache.http.client.RequestDirector;
76ec71ebe8fc942a9a7a4a825f717969f83bcd0b3Lowell Kirshimport org.apache.http.client.methods.HttpEntityEnclosingRequestBase;
888748bc70223e557208926eca76033a38dcd044bLenny Turetsky & Tim Labeeuwimport org.apache.http.conn.ConnectTimeoutException;
988748bc70223e557208926eca76033a38dcd044bLenny Turetsky & Tim Labeeuwimport org.apache.http.params.HttpConnectionParams;
1088748bc70223e557208926eca76033a38dcd044bLenny Turetsky & Tim Labeeuwimport org.apache.http.params.HttpParams;
11f91a118112ae13d39251dd3c09f1d85d33283ba1Christian Williams & Phil Goodwinimport org.apache.http.protocol.HttpContext;
12f91a118112ae13d39251dd3c09f1d85d33283ba1Christian Williams & Phil Goodwin
134105051dc6b4e1d67766d1ad886bd3b191f8a68fChristian Williamsimport java.io.IOException;
140b54ac63d199069248799dd2a3b3aceeabcff267Ryan Richardimport java.net.URI;
15f91a118112ae13d39251dd3c09f1d85d33283ba1Christian Williams & Phil Goodwinimport java.util.ArrayList;
160b54ac63d199069248799dd2a3b3aceeabcff267Ryan Richardimport java.util.HashMap;
17f91a118112ae13d39251dd3c09f1d85d33283ba1Christian Williams & Phil Goodwinimport java.util.List;
180b54ac63d199069248799dd2a3b3aceeabcff267Ryan Richardimport java.util.Map;
19b07bf0e1d226fc883cab2df9f45f31a02fd0848cLowell Kirsh & Ryan Richardimport java.util.regex.Pattern;
20f91a118112ae13d39251dd3c09f1d85d33283ba1Christian Williams & Phil Goodwin
21f91a118112ae13d39251dd3c09f1d85d33283ba1Christian Williams & Phil Goodwinpublic class FakeHttpLayer {
22a34c6ada3c3b41e5b04ab09db7ddc346ac0b3ab9Aaron VonderHaar & Amrit Thakur    List<HttpResponseGenerator> pendingHttpResponses = new ArrayList<HttpResponseGenerator>();
23f91a118112ae13d39251dd3c09f1d85d33283ba1Christian Williams & Phil Goodwin    List<HttpRequestInfo> httpRequestInfos = new ArrayList<HttpRequestInfo>();
242cdde11355e7c249e83a8b7aaabae5977bbc3e49Phil Goodwin    List<HttpEntityStub.ResponseRule> httpResponseRules = new ArrayList<HttpEntityStub.ResponseRule>();
25f91a118112ae13d39251dd3c09f1d85d33283ba1Christian Williams & Phil Goodwin    HttpResponse defaultHttpResponse;
262cdde11355e7c249e83a8b7aaabae5977bbc3e49Phil Goodwin    private HttpResponse defaultResponse;
27ef24e85f87c30d8d1cbe6f7ca0c29a11bd7e4be4Jan Berkel    private boolean interceptHttpRequests = true;
28f91a118112ae13d39251dd3c09f1d85d33283ba1Christian Williams & Phil Goodwin
29f96b2c0575b8c3efcf05420815e7297c759bef24Chris Heisterkamp & Lowell Kirsh    public HttpRequestInfo getLastSentHttpRequestInfo() {
30f96b2c0575b8c3efcf05420815e7297c759bef24Chris Heisterkamp & Lowell Kirsh        List<HttpRequestInfo> requestInfos = Robolectric.getFakeHttpLayer().getSentHttpRequestInfos();
31f96b2c0575b8c3efcf05420815e7297c759bef24Chris Heisterkamp & Lowell Kirsh        if (requestInfos.isEmpty()) {
32f96b2c0575b8c3efcf05420815e7297c759bef24Chris Heisterkamp & Lowell Kirsh            return null;
33f96b2c0575b8c3efcf05420815e7297c759bef24Chris Heisterkamp & Lowell Kirsh        }
34f96b2c0575b8c3efcf05420815e7297c759bef24Chris Heisterkamp & Lowell Kirsh        return requestInfos.get(requestInfos.size() - 1);
35f96b2c0575b8c3efcf05420815e7297c759bef24Chris Heisterkamp & Lowell Kirsh    }
36f96b2c0575b8c3efcf05420815e7297c759bef24Chris Heisterkamp & Lowell Kirsh
3750462c4dff2d5f6aa1f5f9683935f5f0270c8a2dJan Berkel    public void addPendingHttpResponse(int statusCode, String responseBody, Header... headers) {
3850462c4dff2d5f6aa1f5f9683935f5f0270c8a2dJan Berkel        addPendingHttpResponse(new TestHttpResponse(statusCode, responseBody, headers));
396f34840c49a26f54f62ead83509e3f64bed7e0cfNeal Sanche    }
406f34840c49a26f54f62ead83509e3f64bed7e0cfNeal Sanche
41a34c6ada3c3b41e5b04ab09db7ddc346ac0b3ab9Aaron VonderHaar & Amrit Thakur    public void addPendingHttpResponse(final HttpResponse httpResponse) {
42a34c6ada3c3b41e5b04ab09db7ddc346ac0b3ab9Aaron VonderHaar & Amrit Thakur        addPendingHttpResponse(new HttpResponseGenerator() {
43a34c6ada3c3b41e5b04ab09db7ddc346ac0b3ab9Aaron VonderHaar & Amrit Thakur            @Override
44a34c6ada3c3b41e5b04ab09db7ddc346ac0b3ab9Aaron VonderHaar & Amrit Thakur            public HttpResponse getResponse(HttpRequest request) {
45a34c6ada3c3b41e5b04ab09db7ddc346ac0b3ab9Aaron VonderHaar & Amrit Thakur                return httpResponse;
46a34c6ada3c3b41e5b04ab09db7ddc346ac0b3ab9Aaron VonderHaar & Amrit Thakur            }
47a34c6ada3c3b41e5b04ab09db7ddc346ac0b3ab9Aaron VonderHaar & Amrit Thakur        });
48a34c6ada3c3b41e5b04ab09db7ddc346ac0b3ab9Aaron VonderHaar & Amrit Thakur    }
49a34c6ada3c3b41e5b04ab09db7ddc346ac0b3ab9Aaron VonderHaar & Amrit Thakur
50a34c6ada3c3b41e5b04ab09db7ddc346ac0b3ab9Aaron VonderHaar & Amrit Thakur    public void addPendingHttpResponse(HttpResponseGenerator httpResponseGenerator) {
51a34c6ada3c3b41e5b04ab09db7ddc346ac0b3ab9Aaron VonderHaar & Amrit Thakur        pendingHttpResponses.add(httpResponseGenerator);
52f91a118112ae13d39251dd3c09f1d85d33283ba1Christian Williams & Phil Goodwin    }
53f91a118112ae13d39251dd3c09f1d85d33283ba1Christian Williams & Phil Goodwin
54f91a118112ae13d39251dd3c09f1d85d33283ba1Christian Williams & Phil Goodwin    public void addHttpResponseRule(String method, String uri, HttpResponse response) {
55f91a118112ae13d39251dd3c09f1d85d33283ba1Christian Williams & Phil Goodwin        addHttpResponseRule(new DefaultRequestMatcher(method, uri), response);
56f91a118112ae13d39251dd3c09f1d85d33283ba1Christian Williams & Phil Goodwin    }
57f91a118112ae13d39251dd3c09f1d85d33283ba1Christian Williams & Phil Goodwin
58f91a118112ae13d39251dd3c09f1d85d33283ba1Christian Williams & Phil Goodwin    public void addHttpResponseRule(String uri, HttpResponse response) {
59f91a118112ae13d39251dd3c09f1d85d33283ba1Christian Williams & Phil Goodwin        addHttpResponseRule(new UriRequestMatcher(uri), response);
60f91a118112ae13d39251dd3c09f1d85d33283ba1Christian Williams & Phil Goodwin    }
61f91a118112ae13d39251dd3c09f1d85d33283ba1Christian Williams & Phil Goodwin
62f91a118112ae13d39251dd3c09f1d85d33283ba1Christian Williams & Phil Goodwin    public void addHttpResponseRule(String uri, String response) {
63f91a118112ae13d39251dd3c09f1d85d33283ba1Christian Williams & Phil Goodwin        addHttpResponseRule(new UriRequestMatcher(uri), new TestHttpResponse(200, response));
64f91a118112ae13d39251dd3c09f1d85d33283ba1Christian Williams & Phil Goodwin    }
65f91a118112ae13d39251dd3c09f1d85d33283ba1Christian Williams & Phil Goodwin
66f91a118112ae13d39251dd3c09f1d85d33283ba1Christian Williams & Phil Goodwin    public void addHttpResponseRule(RequestMatcher requestMatcher, HttpResponse response) {
674105051dc6b4e1d67766d1ad886bd3b191f8a68fChristian Williams        addHttpResponseRule(new RequestMatcherResponseRule(requestMatcher, response));
68f91a118112ae13d39251dd3c09f1d85d33283ba1Christian Williams & Phil Goodwin    }
69f91a118112ae13d39251dd3c09f1d85d33283ba1Christian Williams & Phil Goodwin
70aafab4e88efa5f28fb7bab7fd942a730fcfe8cd8David Farber & Ryan Richard    /**
71aafab4e88efa5f28fb7bab7fd942a730fcfe8cd8David Farber & Ryan Richard     * Add a response rule.
722b68d9981b16b9d721448c63e7912796d1ff40bfDavid Farber & Joe Moore     *
73aafab4e88efa5f28fb7bab7fd942a730fcfe8cd8David Farber & Ryan Richard     * @param requestMatcher Request matcher
742b68d9981b16b9d721448c63e7912796d1ff40bfDavid Farber & Joe Moore     * @param responses      A list of responses that are returned to matching requests in order from first to last.
75aafab4e88efa5f28fb7bab7fd942a730fcfe8cd8David Farber & Ryan Richard     */
762669b6bbfad4fc63460249729f6d7ae80b002a55Lowell Kirsh    public void addHttpResponseRule(RequestMatcher requestMatcher, List<? extends HttpResponse> responses) {
772669b6bbfad4fc63460249729f6d7ae80b002a55Lowell Kirsh        addHttpResponseRule(new RequestMatcherResponseRule(requestMatcher, responses));
782669b6bbfad4fc63460249729f6d7ae80b002a55Lowell Kirsh    }
792669b6bbfad4fc63460249729f6d7ae80b002a55Lowell Kirsh
802cdde11355e7c249e83a8b7aaabae5977bbc3e49Phil Goodwin    public void addHttpResponseRule(HttpEntityStub.ResponseRule responseRule) {
81536325a5cdb8cf47bd1ea607a17970499240fbdaLenny Turetsky & Phil Plante        httpResponseRules.add(0, responseRule);
82f91a118112ae13d39251dd3c09f1d85d33283ba1Christian Williams & Phil Goodwin    }
83f91a118112ae13d39251dd3c09f1d85d33283ba1Christian Williams & Phil Goodwin
84f91a118112ae13d39251dd3c09f1d85d33283ba1Christian Williams & Phil Goodwin    public void setDefaultHttpResponse(HttpResponse defaultHttpResponse) {
85f91a118112ae13d39251dd3c09f1d85d33283ba1Christian Williams & Phil Goodwin        this.defaultHttpResponse = defaultHttpResponse;
86f91a118112ae13d39251dd3c09f1d85d33283ba1Christian Williams & Phil Goodwin    }
87f91a118112ae13d39251dd3c09f1d85d33283ba1Christian Williams & Phil Goodwin
88d76cb3959b31f5def82d0476cc114cc5da06a7a1Phil Goodwin    public void setDefaultHttpResponse(int statusCode, String responseBody) {
89d76cb3959b31f5def82d0476cc114cc5da06a7a1Phil Goodwin        setDefaultHttpResponse(new TestHttpResponse(statusCode, responseBody));
90d76cb3959b31f5def82d0476cc114cc5da06a7a1Phil Goodwin    }
91d76cb3959b31f5def82d0476cc114cc5da06a7a1Phil Goodwin
924105051dc6b4e1d67766d1ad886bd3b191f8a68fChristian Williams    private HttpResponse findResponse(HttpRequest httpRequest) throws HttpException, IOException {
93f91a118112ae13d39251dd3c09f1d85d33283ba1Christian Williams & Phil Goodwin        if (!pendingHttpResponses.isEmpty()) {
94a34c6ada3c3b41e5b04ab09db7ddc346ac0b3ab9Aaron VonderHaar & Amrit Thakur            return pendingHttpResponses.remove(0).getResponse(httpRequest);
95f91a118112ae13d39251dd3c09f1d85d33283ba1Christian Williams & Phil Goodwin        }
96f91a118112ae13d39251dd3c09f1d85d33283ba1Christian Williams & Phil Goodwin
972cdde11355e7c249e83a8b7aaabae5977bbc3e49Phil Goodwin        for (HttpEntityStub.ResponseRule httpResponseRule : httpResponseRules) {
98f91a118112ae13d39251dd3c09f1d85d33283ba1Christian Williams & Phil Goodwin            if (httpResponseRule.matches(httpRequest)) {
99f91a118112ae13d39251dd3c09f1d85d33283ba1Christian Williams & Phil Goodwin                return httpResponseRule.getResponse();
100f91a118112ae13d39251dd3c09f1d85d33283ba1Christian Williams & Phil Goodwin            }
101f91a118112ae13d39251dd3c09f1d85d33283ba1Christian Williams & Phil Goodwin        }
102f91a118112ae13d39251dd3c09f1d85d33283ba1Christian Williams & Phil Goodwin
103f91a118112ae13d39251dd3c09f1d85d33283ba1Christian Williams & Phil Goodwin        return defaultHttpResponse;
104f91a118112ae13d39251dd3c09f1d85d33283ba1Christian Williams & Phil Goodwin    }
105f91a118112ae13d39251dd3c09f1d85d33283ba1Christian Williams & Phil Goodwin
1062cdde11355e7c249e83a8b7aaabae5977bbc3e49Phil Goodwin    public HttpResponse emulateRequest(HttpHost httpHost, HttpRequest httpRequest, HttpContext httpContext, RequestDirector requestDirector) throws HttpException, IOException {
107f91a118112ae13d39251dd3c09f1d85d33283ba1Christian Williams & Phil Goodwin        HttpResponse httpResponse = findResponse(httpRequest);
108f91a118112ae13d39251dd3c09f1d85d33283ba1Christian Williams & Phil Goodwin
109f91a118112ae13d39251dd3c09f1d85d33283ba1Christian Williams & Phil Goodwin        if (httpResponse == null) {
110b188febae0b24463565d9b6157bd43e0ff22a269David Farber & Ryan Richard            throw new RuntimeException("Unexpected call to execute, no pending responses are available. See Robolectric.addPendingResponse(). Request was: " +
111b188febae0b24463565d9b6157bd43e0ff22a269David Farber & Ryan Richard                    httpRequest.getRequestLine().getMethod() + " " + httpRequest.getRequestLine().getUri());
11288748bc70223e557208926eca76033a38dcd044bLenny Turetsky & Tim Labeeuw        } else {
11388748bc70223e557208926eca76033a38dcd044bLenny Turetsky & Tim Labeeuw            HttpParams params = httpResponse.getParams();
11488748bc70223e557208926eca76033a38dcd044bLenny Turetsky & Tim Labeeuw
11588748bc70223e557208926eca76033a38dcd044bLenny Turetsky & Tim Labeeuw            if (HttpConnectionParams.getConnectionTimeout(params) < 0) {
11688748bc70223e557208926eca76033a38dcd044bLenny Turetsky & Tim Labeeuw                throw new ConnectTimeoutException("Socket is not connected");
11788748bc70223e557208926eca76033a38dcd044bLenny Turetsky & Tim Labeeuw            } else if (HttpConnectionParams.getSoTimeout(params) < 0) {
11888748bc70223e557208926eca76033a38dcd044bLenny Turetsky & Tim Labeeuw                throw new ConnectTimeoutException("The operation timed out");
11988748bc70223e557208926eca76033a38dcd044bLenny Turetsky & Tim Labeeuw            }
120f91a118112ae13d39251dd3c09f1d85d33283ba1Christian Williams & Phil Goodwin        }
121f91a118112ae13d39251dd3c09f1d85d33283ba1Christian Williams & Phil Goodwin
122f91a118112ae13d39251dd3c09f1d85d33283ba1Christian Williams & Phil Goodwin        httpRequestInfos.add(new HttpRequestInfo(httpRequest, httpHost, httpContext, requestDirector));
123f91a118112ae13d39251dd3c09f1d85d33283ba1Christian Williams & Phil Goodwin
124f91a118112ae13d39251dd3c09f1d85d33283ba1Christian Williams & Phil Goodwin        return httpResponse;
125f91a118112ae13d39251dd3c09f1d85d33283ba1Christian Williams & Phil Goodwin    }
126cf9738be25f9684aeb09d0196c2c70c87c6fae01Joe Moore & Lowell Kirsh
1272cdde11355e7c249e83a8b7aaabae5977bbc3e49Phil Goodwin    public boolean hasPendingResponses() {
1282cdde11355e7c249e83a8b7aaabae5977bbc3e49Phil Goodwin        return !pendingHttpResponses.isEmpty();
1292cdde11355e7c249e83a8b7aaabae5977bbc3e49Phil Goodwin    }
1302cdde11355e7c249e83a8b7aaabae5977bbc3e49Phil Goodwin
1312cdde11355e7c249e83a8b7aaabae5977bbc3e49Phil Goodwin    public boolean hasRequestInfos() {
1322cdde11355e7c249e83a8b7aaabae5977bbc3e49Phil Goodwin        return !httpRequestInfos.isEmpty();
1332cdde11355e7c249e83a8b7aaabae5977bbc3e49Phil Goodwin    }
1342cdde11355e7c249e83a8b7aaabae5977bbc3e49Phil Goodwin
13524dfc570c08f46e39b8cd27f51a286ea6154c9daChris Heisterkamp & Ryan Richard    public void clearRequestInfos() {
13624dfc570c08f46e39b8cd27f51a286ea6154c9daChris Heisterkamp & Ryan Richard        httpRequestInfos.clear();
13724dfc570c08f46e39b8cd27f51a286ea6154c9daChris Heisterkamp & Ryan Richard    }
13824dfc570c08f46e39b8cd27f51a286ea6154c9daChris Heisterkamp & Ryan Richard
1392cdde11355e7c249e83a8b7aaabae5977bbc3e49Phil Goodwin    public boolean hasResponseRules() {
1402cdde11355e7c249e83a8b7aaabae5977bbc3e49Phil Goodwin        return !httpResponseRules.isEmpty();
1412cdde11355e7c249e83a8b7aaabae5977bbc3e49Phil Goodwin    }
1422cdde11355e7c249e83a8b7aaabae5977bbc3e49Phil Goodwin
1435ed9c8041b5f927c930d50edef58c5378d12f89cAlexander Murmann & Phil Goodwin    public boolean hasRequestMatchingRule(RequestMatcher rule) {
1445ed9c8041b5f927c930d50edef58c5378d12f89cAlexander Murmann & Phil Goodwin        for (HttpRequestInfo requestInfo : httpRequestInfos) {
1455ed9c8041b5f927c930d50edef58c5378d12f89cAlexander Murmann & Phil Goodwin            if (rule.matches(requestInfo.httpRequest)) {
1465ed9c8041b5f927c930d50edef58c5378d12f89cAlexander Murmann & Phil Goodwin                return true;
1475ed9c8041b5f927c930d50edef58c5378d12f89cAlexander Murmann & Phil Goodwin            }
1485ed9c8041b5f927c930d50edef58c5378d12f89cAlexander Murmann & Phil Goodwin        }
1495ed9c8041b5f927c930d50edef58c5378d12f89cAlexander Murmann & Phil Goodwin        return false;
1505ed9c8041b5f927c930d50edef58c5378d12f89cAlexander Murmann & Phil Goodwin    }
1515ed9c8041b5f927c930d50edef58c5378d12f89cAlexander Murmann & Phil Goodwin
1522cdde11355e7c249e83a8b7aaabae5977bbc3e49Phil Goodwin    public HttpResponse getDefaultResponse() {
1532cdde11355e7c249e83a8b7aaabae5977bbc3e49Phil Goodwin        return defaultResponse;
1542cdde11355e7c249e83a8b7aaabae5977bbc3e49Phil Goodwin    }
1552cdde11355e7c249e83a8b7aaabae5977bbc3e49Phil Goodwin
1562cdde11355e7c249e83a8b7aaabae5977bbc3e49Phil Goodwin    public HttpRequestInfo getSentHttpRequestInfo(int index) {
1572cdde11355e7c249e83a8b7aaabae5977bbc3e49Phil Goodwin        return httpRequestInfos.get(index);
1582cdde11355e7c249e83a8b7aaabae5977bbc3e49Phil Goodwin    }
1592e790fd8d977ca70d83da5bcaeb0beca290ff237Lowell Kirsh & Ryan Richard
1602e790fd8d977ca70d83da5bcaeb0beca290ff237Lowell Kirsh & Ryan Richard    public List<HttpRequestInfo> getSentHttpRequestInfos() {
1612e790fd8d977ca70d83da5bcaeb0beca290ff237Lowell Kirsh & Ryan Richard        return new ArrayList<HttpRequestInfo>(httpRequestInfos);
1622e790fd8d977ca70d83da5bcaeb0beca290ff237Lowell Kirsh & Ryan Richard    }
1632e790fd8d977ca70d83da5bcaeb0beca290ff237Lowell Kirsh & Ryan Richard
164b539795cdd8f6be1b0b01527905a31faea03242cRob Dickerson    public void clearHttpResponseRules() {
165b539795cdd8f6be1b0b01527905a31faea03242cRob Dickerson        httpResponseRules.clear();
16654c8f286408ad7e791f9468dcdc2e18cff1681e4Glenn Jahnke & Rick Kawala    }
16754c8f286408ad7e791f9468dcdc2e18cff1681e4Glenn Jahnke & Rick Kawala
16854c8f286408ad7e791f9468dcdc2e18cff1681e4Glenn Jahnke & Rick Kawala    public void clearPendingHttpResponses() {
16954c8f286408ad7e791f9468dcdc2e18cff1681e4Glenn Jahnke & Rick Kawala        pendingHttpResponses.clear();
170b539795cdd8f6be1b0b01527905a31faea03242cRob Dickerson    }
1712cdde11355e7c249e83a8b7aaabae5977bbc3e49Phil Goodwin
172ef24e85f87c30d8d1cbe6f7ca0c29a11bd7e4be4Jan Berkel    /**
173ef24e85f87c30d8d1cbe6f7ca0c29a11bd7e4be4Jan Berkel     * You can disable Robolectric's fake HTTP layer temporarily
174ef24e85f87c30d8d1cbe6f7ca0c29a11bd7e4be4Jan Berkel     * by calling this method.
175ef24e85f87c30d8d1cbe6f7ca0c29a11bd7e4be4Jan Berkel     * @param interceptHttpRequests whether all HTTP requests should be
176ef24e85f87c30d8d1cbe6f7ca0c29a11bd7e4be4Jan Berkel     *                              intercepted (true by default)
177ef24e85f87c30d8d1cbe6f7ca0c29a11bd7e4be4Jan Berkel     */
178ef24e85f87c30d8d1cbe6f7ca0c29a11bd7e4be4Jan Berkel    public void interceptHttpRequests(boolean interceptHttpRequests) {
179ef24e85f87c30d8d1cbe6f7ca0c29a11bd7e4be4Jan Berkel        this.interceptHttpRequests = interceptHttpRequests;
180ef24e85f87c30d8d1cbe6f7ca0c29a11bd7e4be4Jan Berkel    }
181ef24e85f87c30d8d1cbe6f7ca0c29a11bd7e4be4Jan Berkel
182ef24e85f87c30d8d1cbe6f7ca0c29a11bd7e4be4Jan Berkel    public boolean isInterceptingHttpRequests() {
183ef24e85f87c30d8d1cbe6f7ca0c29a11bd7e4be4Jan Berkel        return interceptHttpRequests;
184ef24e85f87c30d8d1cbe6f7ca0c29a11bd7e4be4Jan Berkel    }
185ef24e85f87c30d8d1cbe6f7ca0c29a11bd7e4be4Jan Berkel
1862cdde11355e7c249e83a8b7aaabae5977bbc3e49Phil Goodwin    public static class RequestMatcherResponseRule implements HttpEntityStub.ResponseRule {
187f91a118112ae13d39251dd3c09f1d85d33283ba1Christian Williams & Phil Goodwin        private RequestMatcher requestMatcher;
1884105051dc6b4e1d67766d1ad886bd3b191f8a68fChristian Williams        private HttpResponse responseToGive;
1894105051dc6b4e1d67766d1ad886bd3b191f8a68fChristian Williams        private IOException ioException;
190ee82c87fc647b87a7bc1641be488805016d7cbd6Lowell Kirsh        private HttpException httpException;
1912669b6bbfad4fc63460249729f6d7ae80b002a55Lowell Kirsh        private List<? extends HttpResponse> responses;
192f91a118112ae13d39251dd3c09f1d85d33283ba1Christian Williams & Phil Goodwin
1934105051dc6b4e1d67766d1ad886bd3b191f8a68fChristian Williams        public RequestMatcherResponseRule(RequestMatcher requestMatcher, HttpResponse responseToGive) {
1944105051dc6b4e1d67766d1ad886bd3b191f8a68fChristian Williams            this.requestMatcher = requestMatcher;
195f91a118112ae13d39251dd3c09f1d85d33283ba1Christian Williams & Phil Goodwin            this.responseToGive = responseToGive;
1964105051dc6b4e1d67766d1ad886bd3b191f8a68fChristian Williams        }
1974105051dc6b4e1d67766d1ad886bd3b191f8a68fChristian Williams
1984105051dc6b4e1d67766d1ad886bd3b191f8a68fChristian Williams        public RequestMatcherResponseRule(RequestMatcher requestMatcher, IOException ioException) {
1994105051dc6b4e1d67766d1ad886bd3b191f8a68fChristian Williams            this.requestMatcher = requestMatcher;
2004105051dc6b4e1d67766d1ad886bd3b191f8a68fChristian Williams            this.ioException = ioException;
2014105051dc6b4e1d67766d1ad886bd3b191f8a68fChristian Williams        }
2024105051dc6b4e1d67766d1ad886bd3b191f8a68fChristian Williams
203ee82c87fc647b87a7bc1641be488805016d7cbd6Lowell Kirsh        public RequestMatcherResponseRule(RequestMatcher requestMatcher, HttpException httpException) {
204f91a118112ae13d39251dd3c09f1d85d33283ba1Christian Williams & Phil Goodwin            this.requestMatcher = requestMatcher;
2054105051dc6b4e1d67766d1ad886bd3b191f8a68fChristian Williams            this.httpException = httpException;
206f91a118112ae13d39251dd3c09f1d85d33283ba1Christian Williams & Phil Goodwin        }
207f91a118112ae13d39251dd3c09f1d85d33283ba1Christian Williams & Phil Goodwin
2082669b6bbfad4fc63460249729f6d7ae80b002a55Lowell Kirsh        public RequestMatcherResponseRule(RequestMatcher requestMatcher, List<? extends HttpResponse> responses) {
2092669b6bbfad4fc63460249729f6d7ae80b002a55Lowell Kirsh            this.requestMatcher = requestMatcher;
2102669b6bbfad4fc63460249729f6d7ae80b002a55Lowell Kirsh            this.responses = responses;
2112669b6bbfad4fc63460249729f6d7ae80b002a55Lowell Kirsh        }
2122669b6bbfad4fc63460249729f6d7ae80b002a55Lowell Kirsh
2132b68d9981b16b9d721448c63e7912796d1ff40bfDavid Farber & Joe Moore        @Override
2142b68d9981b16b9d721448c63e7912796d1ff40bfDavid Farber & Joe Moore        public boolean matches(HttpRequest request) {
215f91a118112ae13d39251dd3c09f1d85d33283ba1Christian Williams & Phil Goodwin            return requestMatcher.matches(request);
216f91a118112ae13d39251dd3c09f1d85d33283ba1Christian Williams & Phil Goodwin        }
217f91a118112ae13d39251dd3c09f1d85d33283ba1Christian Williams & Phil Goodwin
2182b68d9981b16b9d721448c63e7912796d1ff40bfDavid Farber & Joe Moore        @Override
2192b68d9981b16b9d721448c63e7912796d1ff40bfDavid Farber & Joe Moore        public HttpResponse getResponse() throws HttpException, IOException {
2204105051dc6b4e1d67766d1ad886bd3b191f8a68fChristian Williams            if (httpException != null) throw httpException;
2214105051dc6b4e1d67766d1ad886bd3b191f8a68fChristian Williams            if (ioException != null) throw ioException;
2222669b6bbfad4fc63460249729f6d7ae80b002a55Lowell Kirsh            if (responseToGive != null) {
2232669b6bbfad4fc63460249729f6d7ae80b002a55Lowell Kirsh                return responseToGive;
2242b68d9981b16b9d721448c63e7912796d1ff40bfDavid Farber & Joe Moore            } else {
2252669b6bbfad4fc63460249729f6d7ae80b002a55Lowell Kirsh                if (responses.isEmpty()) {
2262669b6bbfad4fc63460249729f6d7ae80b002a55Lowell Kirsh                    throw new RuntimeException("No more responses left to give");
2272669b6bbfad4fc63460249729f6d7ae80b002a55Lowell Kirsh                }
2282669b6bbfad4fc63460249729f6d7ae80b002a55Lowell Kirsh                return responses.remove(0);
2292669b6bbfad4fc63460249729f6d7ae80b002a55Lowell Kirsh            }
230f91a118112ae13d39251dd3c09f1d85d33283ba1Christian Williams & Phil Goodwin        }
231f91a118112ae13d39251dd3c09f1d85d33283ba1Christian Williams & Phil Goodwin    }
232f91a118112ae13d39251dd3c09f1d85d33283ba1Christian Williams & Phil Goodwin
2334105051dc6b4e1d67766d1ad886bd3b191f8a68fChristian Williams    public static class DefaultRequestMatcher implements RequestMatcher {
234f91a118112ae13d39251dd3c09f1d85d33283ba1Christian Williams & Phil Goodwin        private String method;
235f91a118112ae13d39251dd3c09f1d85d33283ba1Christian Williams & Phil Goodwin        private String uri;
236f91a118112ae13d39251dd3c09f1d85d33283ba1Christian Williams & Phil Goodwin
2374105051dc6b4e1d67766d1ad886bd3b191f8a68fChristian Williams        public DefaultRequestMatcher(String method, String uri) {
238f91a118112ae13d39251dd3c09f1d85d33283ba1Christian Williams & Phil Goodwin            this.method = method;
239f91a118112ae13d39251dd3c09f1d85d33283ba1Christian Williams & Phil Goodwin            this.uri = uri;
240f91a118112ae13d39251dd3c09f1d85d33283ba1Christian Williams & Phil Goodwin        }
241f91a118112ae13d39251dd3c09f1d85d33283ba1Christian Williams & Phil Goodwin
2422b68d9981b16b9d721448c63e7912796d1ff40bfDavid Farber & Joe Moore        @Override
2432b68d9981b16b9d721448c63e7912796d1ff40bfDavid Farber & Joe Moore        public boolean matches(HttpRequest request) {
244f91a118112ae13d39251dd3c09f1d85d33283ba1Christian Williams & Phil Goodwin            return request.getRequestLine().getMethod().equals(method) &&
245f91a118112ae13d39251dd3c09f1d85d33283ba1Christian Williams & Phil Goodwin                    request.getRequestLine().getUri().equals(uri);
246f91a118112ae13d39251dd3c09f1d85d33283ba1Christian Williams & Phil Goodwin        }
247f91a118112ae13d39251dd3c09f1d85d33283ba1Christian Williams & Phil Goodwin    }
248f91a118112ae13d39251dd3c09f1d85d33283ba1Christian Williams & Phil Goodwin
2494105051dc6b4e1d67766d1ad886bd3b191f8a68fChristian Williams    public static class UriRequestMatcher implements RequestMatcher {
250f91a118112ae13d39251dd3c09f1d85d33283ba1Christian Williams & Phil Goodwin        private String uri;
251f91a118112ae13d39251dd3c09f1d85d33283ba1Christian Williams & Phil Goodwin
2524105051dc6b4e1d67766d1ad886bd3b191f8a68fChristian Williams        public UriRequestMatcher(String uri) {
253f91a118112ae13d39251dd3c09f1d85d33283ba1Christian Williams & Phil Goodwin            this.uri = uri;
254f91a118112ae13d39251dd3c09f1d85d33283ba1Christian Williams & Phil Goodwin        }
255f91a118112ae13d39251dd3c09f1d85d33283ba1Christian Williams & Phil Goodwin
2562b68d9981b16b9d721448c63e7912796d1ff40bfDavid Farber & Joe Moore        @Override
2572b68d9981b16b9d721448c63e7912796d1ff40bfDavid Farber & Joe Moore        public boolean matches(HttpRequest request) {
258f91a118112ae13d39251dd3c09f1d85d33283ba1Christian Williams & Phil Goodwin            return request.getRequestLine().getUri().equals(uri);
259f91a118112ae13d39251dd3c09f1d85d33283ba1Christian Williams & Phil Goodwin        }
260f91a118112ae13d39251dd3c09f1d85d33283ba1Christian Williams & Phil Goodwin    }
261b07bf0e1d226fc883cab2df9f45f31a02fd0848cLowell Kirsh & Ryan Richard
2620b54ac63d199069248799dd2a3b3aceeabcff267Ryan Richard    public static class RequestMatcherBuilder implements RequestMatcher {
2632b68d9981b16b9d721448c63e7912796d1ff40bfDavid Farber & Joe Moore        private String method, hostname, path;
2640b54ac63d199069248799dd2a3b3aceeabcff267Ryan Richard        private boolean noParams;
2650b54ac63d199069248799dd2a3b3aceeabcff267Ryan Richard        private Map<String, String> params = new HashMap<String, String>();
2662b68d9981b16b9d721448c63e7912796d1ff40bfDavid Farber & Joe Moore        private Map<String, String> headers = new HashMap<String, String>();
2676ec71ebe8fc942a9a7a4a825f717969f83bcd0b3Lowell Kirsh        private PostBodyMatcher postBodyMatcher;
2686ec71ebe8fc942a9a7a4a825f717969f83bcd0b3Lowell Kirsh
2696ec71ebe8fc942a9a7a4a825f717969f83bcd0b3Lowell Kirsh        public interface PostBodyMatcher {
2706ec71ebe8fc942a9a7a4a825f717969f83bcd0b3Lowell Kirsh            /**
2716ec71ebe8fc942a9a7a4a825f717969f83bcd0b3Lowell Kirsh             * Hint: you can use EntityUtils.toString(actualPostBody) to help you implement your matches method.
2726ec71ebe8fc942a9a7a4a825f717969f83bcd0b3Lowell Kirsh             *
2736ec71ebe8fc942a9a7a4a825f717969f83bcd0b3Lowell Kirsh             * @param actualPostBody The post body of the actual request that we are matching against.
2746ec71ebe8fc942a9a7a4a825f717969f83bcd0b3Lowell Kirsh             * @return true if you consider the body to match
2756ec71ebe8fc942a9a7a4a825f717969f83bcd0b3Lowell Kirsh             * @throws IOException Get turned into a RuntimeException to cause your test to fail.
2766ec71ebe8fc942a9a7a4a825f717969f83bcd0b3Lowell Kirsh             */
2776ec71ebe8fc942a9a7a4a825f717969f83bcd0b3Lowell Kirsh            boolean matches(HttpEntity actualPostBody) throws IOException;
2786ec71ebe8fc942a9a7a4a825f717969f83bcd0b3Lowell Kirsh        }
2790b54ac63d199069248799dd2a3b3aceeabcff267Ryan Richard
2800b54ac63d199069248799dd2a3b3aceeabcff267Ryan Richard        public RequestMatcherBuilder method(String method) {
2810b54ac63d199069248799dd2a3b3aceeabcff267Ryan Richard            this.method = method;
2820b54ac63d199069248799dd2a3b3aceeabcff267Ryan Richard            return this;
2830b54ac63d199069248799dd2a3b3aceeabcff267Ryan Richard        }
2840b54ac63d199069248799dd2a3b3aceeabcff267Ryan Richard
2850b54ac63d199069248799dd2a3b3aceeabcff267Ryan Richard        public RequestMatcherBuilder host(String hostname) {
2860b54ac63d199069248799dd2a3b3aceeabcff267Ryan Richard            this.hostname = hostname;
2870b54ac63d199069248799dd2a3b3aceeabcff267Ryan Richard            return this;
2880b54ac63d199069248799dd2a3b3aceeabcff267Ryan Richard        }
2890b54ac63d199069248799dd2a3b3aceeabcff267Ryan Richard
2900b54ac63d199069248799dd2a3b3aceeabcff267Ryan Richard        public RequestMatcherBuilder path(String path) {
291cf9738be25f9684aeb09d0196c2c70c87c6fae01Joe Moore & Lowell Kirsh            if (path.startsWith("/")) {
292cf9738be25f9684aeb09d0196c2c70c87c6fae01Joe Moore & Lowell Kirsh                throw new RuntimeException("Path should not start with '/'");
293cf9738be25f9684aeb09d0196c2c70c87c6fae01Joe Moore & Lowell Kirsh            }
2940b54ac63d199069248799dd2a3b3aceeabcff267Ryan Richard            this.path = "/" + path;
2950b54ac63d199069248799dd2a3b3aceeabcff267Ryan Richard            return this;
2960b54ac63d199069248799dd2a3b3aceeabcff267Ryan Richard        }
2970b54ac63d199069248799dd2a3b3aceeabcff267Ryan Richard
2980b54ac63d199069248799dd2a3b3aceeabcff267Ryan Richard        public RequestMatcherBuilder param(String name, String value) {
2990b54ac63d199069248799dd2a3b3aceeabcff267Ryan Richard            params.put(name, value);
3000b54ac63d199069248799dd2a3b3aceeabcff267Ryan Richard            return this;
3010b54ac63d199069248799dd2a3b3aceeabcff267Ryan Richard        }
3020b54ac63d199069248799dd2a3b3aceeabcff267Ryan Richard
3030b54ac63d199069248799dd2a3b3aceeabcff267Ryan Richard        public RequestMatcherBuilder noParams() {
3040b54ac63d199069248799dd2a3b3aceeabcff267Ryan Richard            noParams = true;
3050b54ac63d199069248799dd2a3b3aceeabcff267Ryan Richard            return this;
3060b54ac63d199069248799dd2a3b3aceeabcff267Ryan Richard        }
3070b54ac63d199069248799dd2a3b3aceeabcff267Ryan Richard
3086ec71ebe8fc942a9a7a4a825f717969f83bcd0b3Lowell Kirsh        public RequestMatcherBuilder postBody(PostBodyMatcher postBodyMatcher) {
3096ec71ebe8fc942a9a7a4a825f717969f83bcd0b3Lowell Kirsh            this.postBodyMatcher = postBodyMatcher;
3106ec71ebe8fc942a9a7a4a825f717969f83bcd0b3Lowell Kirsh            return this;
3116ec71ebe8fc942a9a7a4a825f717969f83bcd0b3Lowell Kirsh        }
3126ec71ebe8fc942a9a7a4a825f717969f83bcd0b3Lowell Kirsh
3132b68d9981b16b9d721448c63e7912796d1ff40bfDavid Farber & Joe Moore        public RequestMatcherBuilder header(String name, String value) {
3142b68d9981b16b9d721448c63e7912796d1ff40bfDavid Farber & Joe Moore            headers.put(name, value);
3152b68d9981b16b9d721448c63e7912796d1ff40bfDavid Farber & Joe Moore            return this;
3162b68d9981b16b9d721448c63e7912796d1ff40bfDavid Farber & Joe Moore        }
3172b68d9981b16b9d721448c63e7912796d1ff40bfDavid Farber & Joe Moore
3182b68d9981b16b9d721448c63e7912796d1ff40bfDavid Farber & Joe Moore        @Override
3192b68d9981b16b9d721448c63e7912796d1ff40bfDavid Farber & Joe Moore        public boolean matches(HttpRequest request) {
3200b54ac63d199069248799dd2a3b3aceeabcff267Ryan Richard            URI uri = URI.create(request.getRequestLine().getUri());
321cf9738be25f9684aeb09d0196c2c70c87c6fae01Joe Moore & Lowell Kirsh            if (method != null && !method.equals(request.getRequestLine().getMethod())) {
322cf9738be25f9684aeb09d0196c2c70c87c6fae01Joe Moore & Lowell Kirsh                return false;
323cf9738be25f9684aeb09d0196c2c70c87c6fae01Joe Moore & Lowell Kirsh            }
324cf9738be25f9684aeb09d0196c2c70c87c6fae01Joe Moore & Lowell Kirsh            if (hostname != null && !hostname.equals(uri.getHost())) {
325cf9738be25f9684aeb09d0196c2c70c87c6fae01Joe Moore & Lowell Kirsh                return false;
326cf9738be25f9684aeb09d0196c2c70c87c6fae01Joe Moore & Lowell Kirsh            }
327cf9738be25f9684aeb09d0196c2c70c87c6fae01Joe Moore & Lowell Kirsh            if (path != null && !path.equals(uri.getRawPath())) {
328cf9738be25f9684aeb09d0196c2c70c87c6fae01Joe Moore & Lowell Kirsh                return false;
329cf9738be25f9684aeb09d0196c2c70c87c6fae01Joe Moore & Lowell Kirsh            }
330cf9738be25f9684aeb09d0196c2c70c87c6fae01Joe Moore & Lowell Kirsh            if (noParams && !uri.getRawQuery().equals(null)) {
331cf9738be25f9684aeb09d0196c2c70c87c6fae01Joe Moore & Lowell Kirsh                return false;
332cf9738be25f9684aeb09d0196c2c70c87c6fae01Joe Moore & Lowell Kirsh            }
3330b54ac63d199069248799dd2a3b3aceeabcff267Ryan Richard            if (params.size() > 0) {
334f96b2c0575b8c3efcf05420815e7297c759bef24Chris Heisterkamp & Lowell Kirsh                Map<String, String> requestParams = ParamsParser.parseParams(request);
3352b68d9981b16b9d721448c63e7912796d1ff40bfDavid Farber & Joe Moore                if (!requestParams.equals(params)) {
3362b68d9981b16b9d721448c63e7912796d1ff40bfDavid Farber & Joe Moore                    return false;
3372b68d9981b16b9d721448c63e7912796d1ff40bfDavid Farber & Joe Moore                }
3382b68d9981b16b9d721448c63e7912796d1ff40bfDavid Farber & Joe Moore            }
3392b68d9981b16b9d721448c63e7912796d1ff40bfDavid Farber & Joe Moore            if (headers.size() > 0) {
3402b68d9981b16b9d721448c63e7912796d1ff40bfDavid Farber & Joe Moore                Map<String, String> actualRequestHeaders = new HashMap<String, String>();
3412b68d9981b16b9d721448c63e7912796d1ff40bfDavid Farber & Joe Moore                for (Header header : request.getAllHeaders()) {
3422b68d9981b16b9d721448c63e7912796d1ff40bfDavid Farber & Joe Moore                    actualRequestHeaders.put(header.getName(), header.getValue());
3432b68d9981b16b9d721448c63e7912796d1ff40bfDavid Farber & Joe Moore                }
3442b68d9981b16b9d721448c63e7912796d1ff40bfDavid Farber & Joe Moore                if (!headers.equals(actualRequestHeaders)) {
3452b68d9981b16b9d721448c63e7912796d1ff40bfDavid Farber & Joe Moore                    return false;
3462b68d9981b16b9d721448c63e7912796d1ff40bfDavid Farber & Joe Moore                }
347cf9738be25f9684aeb09d0196c2c70c87c6fae01Joe Moore & Lowell Kirsh            }
3486ec71ebe8fc942a9a7a4a825f717969f83bcd0b3Lowell Kirsh            if (postBodyMatcher != null) {
349536325a5cdb8cf47bd1ea607a17970499240fbdaLenny Turetsky & Phil Plante                if (!(request instanceof HttpEntityEnclosingRequestBase)) {
3506ec71ebe8fc942a9a7a4a825f717969f83bcd0b3Lowell Kirsh                    return false;
3516ec71ebe8fc942a9a7a4a825f717969f83bcd0b3Lowell Kirsh                }
3526ec71ebe8fc942a9a7a4a825f717969f83bcd0b3Lowell Kirsh                HttpEntityEnclosingRequestBase postOrPut = (HttpEntityEnclosingRequestBase) request;
3536ec71ebe8fc942a9a7a4a825f717969f83bcd0b3Lowell Kirsh                try {
3546ec71ebe8fc942a9a7a4a825f717969f83bcd0b3Lowell Kirsh                    if (!postBodyMatcher.matches(postOrPut.getEntity())) {
3556ec71ebe8fc942a9a7a4a825f717969f83bcd0b3Lowell Kirsh                        return false;
3566ec71ebe8fc942a9a7a4a825f717969f83bcd0b3Lowell Kirsh                    }
3576ec71ebe8fc942a9a7a4a825f717969f83bcd0b3Lowell Kirsh                } catch (IOException e) {
3586ec71ebe8fc942a9a7a4a825f717969f83bcd0b3Lowell Kirsh                    throw new RuntimeException(e);
3596ec71ebe8fc942a9a7a4a825f717969f83bcd0b3Lowell Kirsh                }
3606ec71ebe8fc942a9a7a4a825f717969f83bcd0b3Lowell Kirsh            }
361cf9738be25f9684aeb09d0196c2c70c87c6fae01Joe Moore & Lowell Kirsh            return true;
362cf9738be25f9684aeb09d0196c2c70c87c6fae01Joe Moore & Lowell Kirsh        }
363cf9738be25f9684aeb09d0196c2c70c87c6fae01Joe Moore & Lowell Kirsh
3642b68d9981b16b9d721448c63e7912796d1ff40bfDavid Farber & Joe Moore        String getHostname() {
3652b68d9981b16b9d721448c63e7912796d1ff40bfDavid Farber & Joe Moore            return hostname;
3662b68d9981b16b9d721448c63e7912796d1ff40bfDavid Farber & Joe Moore        }
3672b68d9981b16b9d721448c63e7912796d1ff40bfDavid Farber & Joe Moore
3682b68d9981b16b9d721448c63e7912796d1ff40bfDavid Farber & Joe Moore        String getPath() {
3692b68d9981b16b9d721448c63e7912796d1ff40bfDavid Farber & Joe Moore            return path;
3702b68d9981b16b9d721448c63e7912796d1ff40bfDavid Farber & Joe Moore        }
3712b68d9981b16b9d721448c63e7912796d1ff40bfDavid Farber & Joe Moore
3722b68d9981b16b9d721448c63e7912796d1ff40bfDavid Farber & Joe Moore        String getParam(String key) {
3732b68d9981b16b9d721448c63e7912796d1ff40bfDavid Farber & Joe Moore            return params.get(key);
3742b68d9981b16b9d721448c63e7912796d1ff40bfDavid Farber & Joe Moore        }
3752b68d9981b16b9d721448c63e7912796d1ff40bfDavid Farber & Joe Moore
3762b68d9981b16b9d721448c63e7912796d1ff40bfDavid Farber & Joe Moore        String getHeader(String key) {
3772b68d9981b16b9d721448c63e7912796d1ff40bfDavid Farber & Joe Moore            return headers.get(key);
3782b68d9981b16b9d721448c63e7912796d1ff40bfDavid Farber & Joe Moore        }
3792b68d9981b16b9d721448c63e7912796d1ff40bfDavid Farber & Joe Moore
3802b68d9981b16b9d721448c63e7912796d1ff40bfDavid Farber & Joe Moore        boolean isNoParams() {
3812b68d9981b16b9d721448c63e7912796d1ff40bfDavid Farber & Joe Moore            return noParams;
3822b68d9981b16b9d721448c63e7912796d1ff40bfDavid Farber & Joe Moore        }
3832b68d9981b16b9d721448c63e7912796d1ff40bfDavid Farber & Joe Moore
3842b68d9981b16b9d721448c63e7912796d1ff40bfDavid Farber & Joe Moore        String getMethod() {
3852b68d9981b16b9d721448c63e7912796d1ff40bfDavid Farber & Joe Moore            return method;
3862b68d9981b16b9d721448c63e7912796d1ff40bfDavid Farber & Joe Moore        }
3870b54ac63d199069248799dd2a3b3aceeabcff267Ryan Richard    }
3880b54ac63d199069248799dd2a3b3aceeabcff267Ryan Richard
389b07bf0e1d226fc883cab2df9f45f31a02fd0848cLowell Kirsh & Ryan Richard    public static class UriRegexMatcher implements RequestMatcher {
390b07bf0e1d226fc883cab2df9f45f31a02fd0848cLowell Kirsh & Ryan Richard        private String method;
391b07bf0e1d226fc883cab2df9f45f31a02fd0848cLowell Kirsh & Ryan Richard        private final Pattern uriRegex;
392b07bf0e1d226fc883cab2df9f45f31a02fd0848cLowell Kirsh & Ryan Richard
393b07bf0e1d226fc883cab2df9f45f31a02fd0848cLowell Kirsh & Ryan Richard        public UriRegexMatcher(String method, String uriRegex) {
394b07bf0e1d226fc883cab2df9f45f31a02fd0848cLowell Kirsh & Ryan Richard            this.method = method;
395b07bf0e1d226fc883cab2df9f45f31a02fd0848cLowell Kirsh & Ryan Richard            this.uriRegex = Pattern.compile(uriRegex);
396b07bf0e1d226fc883cab2df9f45f31a02fd0848cLowell Kirsh & Ryan Richard        }
397b07bf0e1d226fc883cab2df9f45f31a02fd0848cLowell Kirsh & Ryan Richard
3982b68d9981b16b9d721448c63e7912796d1ff40bfDavid Farber & Joe Moore        @Override
3992b68d9981b16b9d721448c63e7912796d1ff40bfDavid Farber & Joe Moore        public boolean matches(HttpRequest request) {
400b07bf0e1d226fc883cab2df9f45f31a02fd0848cLowell Kirsh & Ryan Richard            return request.getRequestLine().getMethod().equals(method) &&
401b07bf0e1d226fc883cab2df9f45f31a02fd0848cLowell Kirsh & Ryan Richard                    uriRegex.matcher(request.getRequestLine().getUri()).matches();
402b07bf0e1d226fc883cab2df9f45f31a02fd0848cLowell Kirsh & Ryan Richard        }
403b07bf0e1d226fc883cab2df9f45f31a02fd0848cLowell Kirsh & Ryan Richard    }
404f91a118112ae13d39251dd3c09f1d85d33283ba1Christian Williams & Phil Goodwin}
405