@Override void onBuild(Rule rule) { addRule(rule); }
/** * Adds a new mock rule to this interceptor. Use {@link Rule.Builder} to create one and * call the final statements {@link Rule.Builder#respond}. * <p> * Example: * <pre>{@code * interceptor.addRule(new Rule.Builder() * .get() * .urlStarts("https://someserver/") * .respond(HTTP_200_OK) * .header("SomeHeader", "SomeValue")); * }</pre> */ public MockInterceptor addRule(Response.Builder builder) { if (!(builder instanceof Rule.Builder.FinalRuleBuilder)) { throw new IllegalArgumentException("This response was not created with Rule.Builder!"); } addRule(((Rule.Builder.FinalRuleBuilder) builder).buildRule()); return this; }
@Test public void testResourceResponse() throws IOException { interceptor.addRule() .respond(resource("sample.json")); client.newCall(new Request.Builder() .url(TEST_URL) .get() .build()) .execute(); }
@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(); }
@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 testGetMultipleTimes() throws IOException { interceptor.addRule() .get(TEST_URL) .anyTimes() .respond(TEST_RESPONSE); String first = null; for (int i = 0; i < 10; i++) { String actual = client.newCall(new Request.Builder().url(TEST_URL).get().build()) .execute().body().string(); if (first == null) { first = actual; } else { assertEquals(first, actual); } } }
@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 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 testCustomResponse() throws IOException { final String json = "{\"succeed\":true}"; interceptor.behavior(Behavior.SEQUENTIAL).addRule() .get().or().post().or().put() .respond(json, MEDIATYPE_JSON); assertEquals(json, client.newCall(new Request.Builder() .url(TEST_URL) .get() .build()) .execute() .body() .string()); }
@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 public void testAnswer() throws IOException { interceptor.addRule() .get() .pathMatches(Pattern.compile("/aPath/(\\w+)")) .anyTimes() .answer(new RuleAnswer() { @Override public Response.Builder respond(Request request) { return new Response.Builder() .code(200) .body(ResponseBody.create(null, request.url().encodedPath())); } }); String[] paths = new String[]{"/aPath/aaa", "/aPath/bbb", "/aPath/ccc"}; for (String expectedBody : paths) { HttpUrl url = HttpUrl.parse(TEST_URL).newBuilder().encodedPath(expectedBody).build(); String body = client.newCall(new Request.Builder() .url(url) .get() .build()) .execute() .body() .string(); assertEquals(expectedBody, body); } }
@Test(expected = IllegalStateException.class) public void testWrongNotSyntax1() { interceptor.addRule() .put().not() .respond(HttpCodes.HTTP_409_CONFLICT); }
@Test(expected = IllegalStateException.class) public void testWrongOrSyntax1() { interceptor.addRule() .or().get() .respond(HttpCodes.HTTP_409_CONFLICT); }
@Test(expected = IllegalStateException.class) public void testWrongNotSyntax2() { interceptor.addRule() .not().not().put() .respond(HttpCodes.HTTP_409_CONFLICT); }
@Test(expected = IllegalStateException.class) public void testWrongOrSyntax2() { interceptor.addRule() .get().or().or().post() .respond(HttpCodes.HTTP_409_CONFLICT); }