Skip to content

Commit

Permalink
Response.isSuccessful().
Browse files Browse the repository at this point in the history
  • Loading branch information
squarejesse committed May 30, 2014
1 parent 740de60 commit b398a40
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 0 deletions.
14 changes: 14 additions & 0 deletions okhttp-tests/src/test/java/com/squareup/okhttp/CallTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ public final class CallTest {

executeSynchronously(request)
.assertCode(200)
.assertSuccessful()
.assertHeader("Content-Type", "text/plain")
.assertBody("abc");

Expand All @@ -105,6 +106,19 @@ public final class CallTest {
assertNull(recordedRequest.getHeader("Content-Length"));
}

@Test public void getReturns500() throws Exception {
server.enqueue(new MockResponse().setResponseCode(500));
server.play();

Request request = new Request.Builder()
.url(server.getUrl("/"))
.build();

executeSynchronously(request)
.assertCode(500)
.assertNotSuccessful();
}

@Test public void get_SPDY_3() throws Exception {
enableProtocol(Protocol.SPDY_3);
get();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@
import java.util.Arrays;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;

/**
* A received response or failure recorded by the response recorder.
Expand Down Expand Up @@ -53,6 +55,16 @@ public RecordedResponse assertCode(int expectedCode) {
return this;
}

public RecordedResponse assertSuccessful() {
assertTrue(response.isSuccessful());
return this;
}

public RecordedResponse assertNotSuccessful() {
assertFalse(response.isSuccessful());
return this;
}

public RecordedResponse assertHeader(String name, String... values) {
assertEquals(Arrays.asList(values), response.headers(name));
return this;
Expand Down
8 changes: 8 additions & 0 deletions okhttp/src/main/java/com/squareup/okhttp/Response.java
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,14 @@ public int code() {
return code;
}

/**
* Returns true if the code is in [200..300), which means the request was
* successfully received, understood, and accepted.
*/
public boolean isSuccessful() {
return code >= 200 && code < 300;
}

/** Returns the HTTP status message or null if it is unknown. */
public String message() {
return message;
Expand Down

0 comments on commit b398a40

Please sign in to comment.