Skip to content

Commit

Permalink
Merge pull request square#1134 from tomxor/mws-readme-syntax
Browse files Browse the repository at this point in the history
Enable syntax highlighting in MockWebServer README.
  • Loading branch information
JakeWharton committed Nov 5, 2014
2 parents ec1b266 + fd3f99a commit 2dc4572
Showing 1 changed file with 58 additions and 58 deletions.
116 changes: 58 additions & 58 deletions mockwebserver/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -85,30 +85,30 @@ 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);
```


#### RecordedRequest

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
Expand All @@ -122,7 +122,7 @@ dispatch on the request path.

The best way to get MockWebServer is via Maven:

```
```xml
<dependency>
<groupId>com.squareup.okhttp</groupId>
<artifactId>mockwebserver</artifactId>
Expand Down

0 comments on commit 2dc4572

Please sign in to comment.