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  */
16 package com.squareup.okhttp.mockwebserver;
17 
18 import java.io.IOException;
19 import java.net.HttpURLConnection;
20 import java.net.URL;
21 import java.util.ArrayList;
22 import java.util.List;
23 import java.util.concurrent.CountDownLatch;
24 import java.util.concurrent.atomic.AtomicInteger;
25 import org.junit.After;
26 import org.junit.Test;
27 
28 import static org.junit.Assert.assertEquals;
29 
30 public class CustomDispatcherTest {
31   private MockWebServer mockWebServer = new MockWebServer();
32 
tearDown()33   @After public void tearDown() throws Exception {
34     mockWebServer.shutdown();
35   }
36 
simpleDispatch()37   @Test public void simpleDispatch() throws Exception {
38     mockWebServer.start();
39     final List<RecordedRequest> requestsMade = new ArrayList<>();
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 
outOfOrderResponses()56   @Test public void outOfOrderResponses() throws Exception {
57     AtomicInteger firstResponseCode = new AtomicInteger();
58     AtomicInteger secondResponseCode = new AtomicInteger();
59     mockWebServer.start();
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 
buildRequestThread(final String path, final AtomicInteger responseCode)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