1/*
2 * Copyright (C) 2012 Google Inc.
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 */
16package com.google.mockwebserver;
17
18import junit.framework.TestCase;
19
20import java.io.IOException;
21import java.net.HttpURLConnection;
22import java.net.URL;
23import java.util.ArrayList;
24import java.util.List;
25import java.util.concurrent.CountDownLatch;
26import java.util.concurrent.atomic.AtomicInteger;
27
28public class CustomDispatcherTest extends TestCase {
29
30    private MockWebServer mockWebServer = new MockWebServer();
31
32    @Override
33    public void tearDown() throws Exception {
34        mockWebServer.shutdown();
35    }
36
37    public void testSimpleDispatch() throws Exception {
38        mockWebServer.play();
39        final List<RecordedRequest> requestsMade = new ArrayList<RecordedRequest>();
40        final Dispatcher dispatcher = new Dispatcher() {
41            @Override
42            public MockResponse dispatch(RecordedRequest request) throws InterruptedException {
43                requestsMade.add(request);
44                return new MockResponse();
45            }
46        };
47        assertEquals(0, requestsMade.size());
48        mockWebServer.setDispatcher(dispatcher);
49        final URL url = mockWebServer.getUrl("/");
50        final HttpURLConnection conn = (HttpURLConnection) url.openConnection();
51        conn.getResponseCode(); // Force the connection to hit the "server".
52        // Make sure our dispatcher got the request.
53        assertEquals(1, requestsMade.size());
54    }
55
56    public void testOutOfOrderResponses() throws Exception {
57        AtomicInteger firstResponseCode = new AtomicInteger();
58        AtomicInteger secondResponseCode = new AtomicInteger();
59        mockWebServer.play();
60        final String secondRequest = "/bar";
61        final String firstRequest = "/foo";
62        final CountDownLatch latch = new CountDownLatch(1);
63        final Dispatcher dispatcher = new Dispatcher() {
64            @Override
65            public MockResponse dispatch(RecordedRequest request) throws InterruptedException {
66                if (request.getPath().equals(firstRequest)) {
67                    latch.await();
68                }
69                return new MockResponse();
70            }
71        };
72        mockWebServer.setDispatcher(dispatcher);
73        final Thread startsFirst = buildRequestThread(firstRequest, firstResponseCode);
74        startsFirst.start();
75        final Thread endsFirst = buildRequestThread(secondRequest, secondResponseCode);
76        endsFirst.start();
77        endsFirst.join();
78        assertEquals(0, firstResponseCode.get()); // First response is still waiting.
79        assertEquals(200, secondResponseCode.get()); // Second response is done.
80        latch.countDown();
81        startsFirst.join();
82        assertEquals(200, firstResponseCode.get()); // And now it's done!
83        assertEquals(200, secondResponseCode.get()); // (Still done).
84    }
85
86    private Thread buildRequestThread(final String path, final AtomicInteger responseCode) {
87        return new Thread(new Runnable() {
88            @Override public void run() {
89                final URL url = mockWebServer.getUrl(path);
90                final HttpURLConnection conn;
91                try {
92                    conn = (HttpURLConnection) url.openConnection();
93                    responseCode.set(conn.getResponseCode()); // Force the connection to hit the "server".
94                } catch (IOException e) {
95                }
96            }
97        });
98    }
99
100}
101