public Response.Builder respond(@NonNull InputStream body, @NonNull MediaType mediaType) { return respond(-1, body, mediaType); }
public Response.Builder respond(long contentLength, @NonNull InputStream body, @NonNull MediaType mediaType) { try { return respond(ResponseBody.create(mediaType, contentLength, new Buffer().readFrom(body))); } catch (IOException e) { throw new RuntimeException(e); } }
public Response.Builder respond(@Nullable ResponseBody body) { return respond(HTTP_200_OK, body); }
public Response.Builder respond(long contentLength, @NonNull InputStream body) { return respond(contentLength, body, MEDIATYPE_RAW_DATA); }
public Response.Builder respond(@NonNull byte[] body, @NonNull MediaType mediaType) { return respond(ResponseBody.create(mediaType, body)); }
public Response.Builder respond(@NonNull byte[] body) { return respond(body, MEDIATYPE_RAW_DATA); }
public Response.Builder respond(@NonNull InputStream body) { return respond(-1, body); }
public Response.Builder respond(@NonNull String body) { return respond(body, MEDIATYPE_TEXT); }
public Response.Builder respond(int code) { return respond(code, (ResponseBody) null); }
public Response.Builder respond(@NonNull String body, @NonNull MediaType mediaType) { return respond(ResponseBody.create(mediaType, body)); }
@Test(expected = AssertionError.class) public void testURLStartsWith_Fail() throws IOException { interceptor.addRule() .get() .urlStarts("http://") .respond(TEST_RESPONSE) .code(401); client.newCall(new Request.Builder() .url(TEST_URL) .get() .build()) .execute(); }
@Test(expected = AssertionError.class) public void testFailReasonSequential() throws IOException { interceptor.behavior(Behavior.SEQUENTIAL) .addRule() .get().or().post().or().put() .respond("OK"); client.newCall(new Request.Builder() .url(TEST_URL) .delete() .build()) .execute(); }
@Test(expected = IllegalStateException.class) public void testWrongOrSyntax2() { interceptor.addRule() .get().or().or().post() .respond(HttpCodes.HTTP_409_CONFLICT); }
@Test(expected = IllegalStateException.class) public void testWrongNotSyntax1() { interceptor.addRule() .put().not() .respond(HttpCodes.HTTP_409_CONFLICT); }
@Test public void testResourceResponse() throws IOException { interceptor.addRule() .respond(resource("sample.json")); client.newCall(new Request.Builder() .url(TEST_URL) .get() .build()) .execute(); }
@Test(expected = IllegalStateException.class) public void testWrongNotSyntax2() { interceptor.addRule() .not().not().put() .respond(HttpCodes.HTTP_409_CONFLICT); }
@Test(expected = IllegalStateException.class) public void testWrongOrSyntax1() { interceptor.addRule() .or().get() .respond(HttpCodes.HTTP_409_CONFLICT); }
@Test public void testURLStartsWith() throws IOException { interceptor.addRule() .get() .urlStarts("https://") .respond(TEST_RESPONSE); client.newCall(new Request.Builder() .url(TEST_URL) .get() .build()) .execute(); }
@Test public void testCustomizeResponse() throws IOException { final ResponseBody body = ResponseBody.create(MediaTypes.MEDIATYPE_XML, "<html/>"); interceptor.addRule().respond(TEST_RESPONSE).body(body).addHeader("Test", "aValue"); Response response = client.newCall(new Request.Builder().url(TEST_URL).get().build()).execute(); assertEquals(MediaTypes.MEDIATYPE_XML.type(), response.body().contentType().type()); assertEquals(MediaTypes.MEDIATYPE_XML.subtype(), response.body().contentType().subtype()); assertEquals("aValue", response.header("Test")); assertEquals("<html/>", response.body().string()); }
@Test public void testGet() throws IOException { interceptor.addRule() .get(TEST_URL) .respond(TEST_RESPONSE); client.newCall(new Request.Builder() .url(TEST_URL) .get() .build()) .execute(); }