@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 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 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()); }
public Builder get(String url) { get(); return url(url); }
@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 = 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 = IllegalStateException.class) public void testWrongOrSyntax1() { interceptor.addRule() .or().get() .respond(HttpCodes.HTTP_409_CONFLICT); }