diff --git a/mockwebserver/README.md b/mockwebserver/README.md index 39257be60b51..9596ec163998 100644 --- a/mockwebserver/README.md +++ b/mockwebserver/README.md @@ -27,51 +27,51 @@ Use MockWebServer the same way that you use mocking frameworks like Here's a complete example: -``` - public void test() throws Exception { - // Create a MockWebServer. These are lean enough that you can create a new - // instance for every unit test. - MockWebServer server = new MockWebServer(); - - // Schedule some responses. - server.enqueue(new MockResponse().setBody("hello, world!")); - server.enqueue(new MockResponse().setBody("sup, bra?")); - server.enqueue(new MockResponse().setBody("yo dog")); - - // Start the server. - server.play(); - - // Ask the server for its URL. You'll need this to make HTTP requests. - URL baseUrl = server.getUrl("/v1/chat/"); - - // Exercise your application code, which should make those HTTP requests. - // Responses are returned in the same order that they are enqueued. - Chat chat = new Chat(baseUrl); - - chat.loadMore(); - assertEquals("hello, world!", chat.messages()); - - chat.loadMore(); - chat.loadMore(); - assertEquals("" - + "hello, world!\n" - + "sup, bra?\n" - + "yo dog", chat.messages()); - - // Optional: confirm that your app made the HTTP requests you were expecting. - RecordedRequest request1 = server.takeRequest(); - assertEquals("/v1/chat/messages/", request1.getPath()); - assertNotNull(request1.getHeader("Authorization")); - - RecordedRequest request2 = server.takeRequest(); - assertEquals("/v1/chat/messages/2", request2.getPath()); - - RecordedRequest request3 = server.takeRequest(); - assertEquals("/v1/chat/messages/3", request3.getPath()); - - // Shut down the server. Instances cannot be reused. - server.shutdown(); - } +```java +public void test() throws Exception { + // Create a MockWebServer. These are lean enough that you can create a new + // instance for every unit test. + MockWebServer server = new MockWebServer(); + + // Schedule some responses. + server.enqueue(new MockResponse().setBody("hello, world!")); + server.enqueue(new MockResponse().setBody("sup, bra?")); + server.enqueue(new MockResponse().setBody("yo dog")); + + // Start the server. + server.play(); + + // Ask the server for its URL. You'll need this to make HTTP requests. + URL baseUrl = server.getUrl("/v1/chat/"); + + // Exercise your application code, which should make those HTTP requests. + // Responses are returned in the same order that they are enqueued. + Chat chat = new Chat(baseUrl); + + chat.loadMore(); + assertEquals("hello, world!", chat.messages()); + + chat.loadMore(); + chat.loadMore(); + assertEquals("" + + "hello, world!\n" + + "sup, bra?\n" + + "yo dog", chat.messages()); + + // Optional: confirm that your app made the HTTP requests you were expecting. + RecordedRequest request1 = server.takeRequest(); + assertEquals("/v1/chat/messages/", request1.getPath()); + assertNotNull(request1.getHeader("Authorization")); + + RecordedRequest request2 = server.takeRequest(); + assertEquals("/v1/chat/messages/2", request2.getPath()); + + RecordedRequest request3 = server.takeRequest(); + assertEquals("/v1/chat/messages/3", request3.getPath()); + + // Shut down the server. Instances cannot be reused. + server.shutdown(); +} ``` Your unit tests might move the `server` into a field so you can shut it down @@ -85,18 +85,18 @@ Mock responses default to an empty response body and a `200` status code. You can set a custom body with a string, input stream or byte array. Also add headers with a fluent builder API. -``` - MockResponse response = new MockResponse() - .addHeader("Content-Type", "application/json; charset=utf-8") - .addHeader("Cache-Control", "no-cache") - .setBody("{}"); +```java +MockResponse response = new MockResponse() + .addHeader("Content-Type", "application/json; charset=utf-8") + .addHeader("Cache-Control", "no-cache") + .setBody("{}"); ``` MockResponse can be used to simulate a slow network. This is useful for testing timeouts and interactive testing. -``` - response.throttleBody(1024, 1, TimeUnit.SECONDS); +```java +response.throttleBody(1024, 1, TimeUnit.SECONDS); ``` @@ -104,11 +104,11 @@ testing timeouts and interactive testing. Verify requests by their method, path, HTTP version, body, and headers. -``` - RecordedRequest request = server.takeRequest(); - assertEquals("POST /v1/chat/send HTTP/1.1", request.getRequestLine()); - assertEquals("application/json; charset=utf-8", request.getHeader("Content-Type")); - assertEquals("{}", request.getUtf8Body()); +```java +RecordedRequest request = server.takeRequest(); +assertEquals("POST /v1/chat/send HTTP/1.1", request.getRequestLine()); +assertEquals("application/json; charset=utf-8", request.getHeader("Content-Type")); +assertEquals("{}", request.getUtf8Body()); ``` #### Dispatcher @@ -122,7 +122,7 @@ dispatch on the request path. The best way to get MockWebServer is via Maven: -``` +```xml com.squareup.okhttp mockwebserver