1*ab625e41SAndroid Build Coastguard WorkerMockWebServer 2*ab625e41SAndroid Build Coastguard Worker============= 3*ab625e41SAndroid Build Coastguard Worker 4*ab625e41SAndroid Build Coastguard WorkerA scriptable web server for testing HTTP clients 5*ab625e41SAndroid Build Coastguard Worker 6*ab625e41SAndroid Build Coastguard Worker 7*ab625e41SAndroid Build Coastguard Worker### Motivation 8*ab625e41SAndroid Build Coastguard Worker 9*ab625e41SAndroid Build Coastguard WorkerThis library makes it easy to test that your app Does The Right Thing when it 10*ab625e41SAndroid Build Coastguard Workermakes HTTP and HTTPS calls. It lets you specify which responses to return and 11*ab625e41SAndroid Build Coastguard Workerthen verify that requests were made as expected. 12*ab625e41SAndroid Build Coastguard Worker 13*ab625e41SAndroid Build Coastguard WorkerBecause it exercises your full HTTP stack, you can be confident that you're 14*ab625e41SAndroid Build Coastguard Workertesting everything. You can even copy & paste HTTP responses from your real web 15*ab625e41SAndroid Build Coastguard Workerserver to create representative test cases. Or test that your code survives in 16*ab625e41SAndroid Build Coastguard Workerawkward-to-reproduce situations like 500 errors or slow-loading responses. 17*ab625e41SAndroid Build Coastguard Worker 18*ab625e41SAndroid Build Coastguard Worker 19*ab625e41SAndroid Build Coastguard Worker### Example 20*ab625e41SAndroid Build Coastguard Worker 21*ab625e41SAndroid Build Coastguard WorkerUse MockWebServer the same way that you use mocking frameworks like 22*ab625e41SAndroid Build Coastguard Worker[Mockito](https://code.google.com/p/mockito/): 23*ab625e41SAndroid Build Coastguard Worker 24*ab625e41SAndroid Build Coastguard Worker1. Script the mocks. 25*ab625e41SAndroid Build Coastguard Worker2. Run application code. 26*ab625e41SAndroid Build Coastguard Worker3. Verify that the expected requests were made. 27*ab625e41SAndroid Build Coastguard Worker 28*ab625e41SAndroid Build Coastguard WorkerHere's a complete example: 29*ab625e41SAndroid Build Coastguard Worker 30*ab625e41SAndroid Build Coastguard Worker```java 31*ab625e41SAndroid Build Coastguard Workerpublic void test() throws Exception { 32*ab625e41SAndroid Build Coastguard Worker // Create a MockWebServer. These are lean enough that you can create a new 33*ab625e41SAndroid Build Coastguard Worker // instance for every unit test. 34*ab625e41SAndroid Build Coastguard Worker MockWebServer server = new MockWebServer(); 35*ab625e41SAndroid Build Coastguard Worker 36*ab625e41SAndroid Build Coastguard Worker // Schedule some responses. 37*ab625e41SAndroid Build Coastguard Worker server.enqueue(new MockResponse().setBody("hello, world!")); 38*ab625e41SAndroid Build Coastguard Worker server.enqueue(new MockResponse().setBody("sup, bra?")); 39*ab625e41SAndroid Build Coastguard Worker server.enqueue(new MockResponse().setBody("yo dog")); 40*ab625e41SAndroid Build Coastguard Worker 41*ab625e41SAndroid Build Coastguard Worker // Start the server. 42*ab625e41SAndroid Build Coastguard Worker server.start(); 43*ab625e41SAndroid Build Coastguard Worker 44*ab625e41SAndroid Build Coastguard Worker // Ask the server for its URL. You'll need this to make HTTP requests. 45*ab625e41SAndroid Build Coastguard Worker HttpUrl baseUrl = server.url("/v1/chat/"); 46*ab625e41SAndroid Build Coastguard Worker 47*ab625e41SAndroid Build Coastguard Worker // Exercise your application code, which should make those HTTP requests. 48*ab625e41SAndroid Build Coastguard Worker // Responses are returned in the same order that they are enqueued. 49*ab625e41SAndroid Build Coastguard Worker Chat chat = new Chat(baseUrl); 50*ab625e41SAndroid Build Coastguard Worker 51*ab625e41SAndroid Build Coastguard Worker chat.loadMore(); 52*ab625e41SAndroid Build Coastguard Worker assertEquals("hello, world!", chat.messages()); 53*ab625e41SAndroid Build Coastguard Worker 54*ab625e41SAndroid Build Coastguard Worker chat.loadMore(); 55*ab625e41SAndroid Build Coastguard Worker chat.loadMore(); 56*ab625e41SAndroid Build Coastguard Worker assertEquals("" 57*ab625e41SAndroid Build Coastguard Worker + "hello, world!\n" 58*ab625e41SAndroid Build Coastguard Worker + "sup, bra?\n" 59*ab625e41SAndroid Build Coastguard Worker + "yo dog", chat.messages()); 60*ab625e41SAndroid Build Coastguard Worker 61*ab625e41SAndroid Build Coastguard Worker // Optional: confirm that your app made the HTTP requests you were expecting. 62*ab625e41SAndroid Build Coastguard Worker RecordedRequest request1 = server.takeRequest(); 63*ab625e41SAndroid Build Coastguard Worker assertEquals("/v1/chat/messages/", request1.getPath()); 64*ab625e41SAndroid Build Coastguard Worker assertNotNull(request1.getHeader("Authorization")); 65*ab625e41SAndroid Build Coastguard Worker 66*ab625e41SAndroid Build Coastguard Worker RecordedRequest request2 = server.takeRequest(); 67*ab625e41SAndroid Build Coastguard Worker assertEquals("/v1/chat/messages/2", request2.getPath()); 68*ab625e41SAndroid Build Coastguard Worker 69*ab625e41SAndroid Build Coastguard Worker RecordedRequest request3 = server.takeRequest(); 70*ab625e41SAndroid Build Coastguard Worker assertEquals("/v1/chat/messages/3", request3.getPath()); 71*ab625e41SAndroid Build Coastguard Worker 72*ab625e41SAndroid Build Coastguard Worker // Shut down the server. Instances cannot be reused. 73*ab625e41SAndroid Build Coastguard Worker server.shutdown(); 74*ab625e41SAndroid Build Coastguard Worker} 75*ab625e41SAndroid Build Coastguard Worker``` 76*ab625e41SAndroid Build Coastguard Worker 77*ab625e41SAndroid Build Coastguard WorkerYour unit tests might move the `server` into a field so you can shut it down 78*ab625e41SAndroid Build Coastguard Workerfrom your test's `tearDown()`. 79*ab625e41SAndroid Build Coastguard Worker 80*ab625e41SAndroid Build Coastguard Worker### API 81*ab625e41SAndroid Build Coastguard Worker 82*ab625e41SAndroid Build Coastguard Worker#### MockResponse 83*ab625e41SAndroid Build Coastguard Worker 84*ab625e41SAndroid Build Coastguard WorkerMock responses default to an empty response body and a `200` status code. 85*ab625e41SAndroid Build Coastguard WorkerYou can set a custom body with a string, input stream or byte array. Also 86*ab625e41SAndroid Build Coastguard Workeradd headers with a fluent builder API. 87*ab625e41SAndroid Build Coastguard Worker 88*ab625e41SAndroid Build Coastguard Worker```java 89*ab625e41SAndroid Build Coastguard WorkerMockResponse response = new MockResponse() 90*ab625e41SAndroid Build Coastguard Worker .addHeader("Content-Type", "application/json; charset=utf-8") 91*ab625e41SAndroid Build Coastguard Worker .addHeader("Cache-Control", "no-cache") 92*ab625e41SAndroid Build Coastguard Worker .setBody("{}"); 93*ab625e41SAndroid Build Coastguard Worker``` 94*ab625e41SAndroid Build Coastguard Worker 95*ab625e41SAndroid Build Coastguard WorkerMockResponse can be used to simulate a slow network. This is useful for 96*ab625e41SAndroid Build Coastguard Workertesting timeouts and interactive testing. 97*ab625e41SAndroid Build Coastguard Worker 98*ab625e41SAndroid Build Coastguard Worker```java 99*ab625e41SAndroid Build Coastguard Workerresponse.throttleBody(1024, 1, TimeUnit.SECONDS); 100*ab625e41SAndroid Build Coastguard Worker``` 101*ab625e41SAndroid Build Coastguard Worker 102*ab625e41SAndroid Build Coastguard Worker 103*ab625e41SAndroid Build Coastguard Worker#### RecordedRequest 104*ab625e41SAndroid Build Coastguard Worker 105*ab625e41SAndroid Build Coastguard WorkerVerify requests by their method, path, HTTP version, body, and headers. 106*ab625e41SAndroid Build Coastguard Worker 107*ab625e41SAndroid Build Coastguard Worker```java 108*ab625e41SAndroid Build Coastguard WorkerRecordedRequest request = server.takeRequest(); 109*ab625e41SAndroid Build Coastguard WorkerassertEquals("POST /v1/chat/send HTTP/1.1", request.getRequestLine()); 110*ab625e41SAndroid Build Coastguard WorkerassertEquals("application/json; charset=utf-8", request.getHeader("Content-Type")); 111*ab625e41SAndroid Build Coastguard WorkerassertEquals("{}", request.getUtf8Body()); 112*ab625e41SAndroid Build Coastguard Worker``` 113*ab625e41SAndroid Build Coastguard Worker 114*ab625e41SAndroid Build Coastguard Worker#### Dispatcher 115*ab625e41SAndroid Build Coastguard Worker 116*ab625e41SAndroid Build Coastguard WorkerBy default MockWebServer uses a queue to specify a series of responses. Use a 117*ab625e41SAndroid Build Coastguard WorkerDispatcher to handle requests using another policy. One natural policy is to 118*ab625e41SAndroid Build Coastguard Workerdispatch on the request path. 119*ab625e41SAndroid Build Coastguard WorkerYou can, for example, filter the request instead of using `server.enqueue()`. 120*ab625e41SAndroid Build Coastguard Worker 121*ab625e41SAndroid Build Coastguard Worker```java 122*ab625e41SAndroid Build Coastguard Workerfinal Dispatcher dispatcher = new Dispatcher() { 123*ab625e41SAndroid Build Coastguard Worker 124*ab625e41SAndroid Build Coastguard Worker @Override 125*ab625e41SAndroid Build Coastguard Worker public MockResponse dispatch(RecordedRequest request) throws InterruptedException { 126*ab625e41SAndroid Build Coastguard Worker 127*ab625e41SAndroid Build Coastguard Worker if (request.getPath().equals("/v1/login/auth/")){ 128*ab625e41SAndroid Build Coastguard Worker return new MockResponse().setResponseCode(200); 129*ab625e41SAndroid Build Coastguard Worker } else if (request.getPath().equals("v1/check/version/")){ 130*ab625e41SAndroid Build Coastguard Worker return new MockResponse().setResponseCode(200).setBody("version=9"); 131*ab625e41SAndroid Build Coastguard Worker } else if (request.getPath().equals("/v1/profile/info")) { 132*ab625e41SAndroid Build Coastguard Worker return new MockResponse().setResponseCode(200).setBody("{\\\"info\\\":{\\\"name\":\"Lucas Albuquerque\",\"age\":\"21\",\"gender\":\"male\"}}"); 133*ab625e41SAndroid Build Coastguard Worker } 134*ab625e41SAndroid Build Coastguard Worker return new MockResponse().setResponseCode(404); 135*ab625e41SAndroid Build Coastguard Worker } 136*ab625e41SAndroid Build Coastguard Worker}; 137*ab625e41SAndroid Build Coastguard Workerserver.setDispatcher(dispatcher); 138*ab625e41SAndroid Build Coastguard Worker``` 139*ab625e41SAndroid Build Coastguard Worker 140*ab625e41SAndroid Build Coastguard Worker 141*ab625e41SAndroid Build Coastguard Worker### Download 142*ab625e41SAndroid Build Coastguard Worker 143*ab625e41SAndroid Build Coastguard WorkerGet MockWebServer via Maven: 144*ab625e41SAndroid Build Coastguard Worker```xml 145*ab625e41SAndroid Build Coastguard Worker<dependency> 146*ab625e41SAndroid Build Coastguard Worker <groupId>com.squareup.okhttp</groupId> 147*ab625e41SAndroid Build Coastguard Worker <artifactId>mockwebserver</artifactId> 148*ab625e41SAndroid Build Coastguard Worker <version>(insert latest version)</version> 149*ab625e41SAndroid Build Coastguard Worker <scope>test</scope> 150*ab625e41SAndroid Build Coastguard Worker</dependency> 151*ab625e41SAndroid Build Coastguard Worker``` 152*ab625e41SAndroid Build Coastguard Worker 153*ab625e41SAndroid Build Coastguard Workeror via Gradle 154*ab625e41SAndroid Build Coastguard Worker```groovy 155*ab625e41SAndroid Build Coastguard WorkertestCompile 'com.squareup.okhttp:mockwebserver:(insert latest version)' 156*ab625e41SAndroid Build Coastguard Worker``` 157*ab625e41SAndroid Build Coastguard Worker 158*ab625e41SAndroid Build Coastguard Worker### License 159*ab625e41SAndroid Build Coastguard Worker 160*ab625e41SAndroid Build Coastguard Worker Licensed under the Apache License, Version 2.0 (the "License"); 161*ab625e41SAndroid Build Coastguard Worker you may not use this file except in compliance with the License. 162*ab625e41SAndroid Build Coastguard Worker You may obtain a copy of the License at 163*ab625e41SAndroid Build Coastguard Worker 164*ab625e41SAndroid Build Coastguard Worker http://www.apache.org/licenses/LICENSE-2.0 165*ab625e41SAndroid Build Coastguard Worker 166*ab625e41SAndroid Build Coastguard Worker Unless required by applicable law or agreed to in writing, software 167*ab625e41SAndroid Build Coastguard Worker distributed under the License is distributed on an "AS IS" BASIS, 168*ab625e41SAndroid Build Coastguard Worker WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 169*ab625e41SAndroid Build Coastguard Worker See the License for the specific language governing permissions and 170*ab625e41SAndroid Build Coastguard Worker limitations under the License. 171