Skip to content

Commit

Permalink
add test scenario for Issue 3276
Browse files Browse the repository at this point in the history
  • Loading branch information
zjc17 committed May 30, 2020
1 parent c6f21c3 commit 92ca604
Showing 1 changed file with 58 additions and 0 deletions.
58 changes: 58 additions & 0 deletions retrofit/src/test/java/retrofit2/CallTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,64 @@ public void onFailure(Call<String> call, Throwable t) {
assertThat(response.body()).isEqualTo("Hi");
}

@Test
public void http202Sync() throws IOException {
Retrofit retrofit =
new Retrofit.Builder()
.baseUrl(server.url("/"))
.addConverterFactory(new ToStringConverterFactory())
.build();
Service example = retrofit.create(Service.class);

server.enqueue(new MockResponse().setResponseCode(202).setBody(""));
server.enqueue(new MockResponse().setResponseCode(200).setBody("Finish"));

Response<String> response = example.getString().execute();
assertThat(response.isSuccessful()).isTrue();
assertThat(response.code()).isEqualTo(202);
assertThat(response.body()).isEqualTo("");
response = example.getString().execute();
assertThat(response.isSuccessful()).isTrue();
assertThat(response.code()).isEqualTo(200);
assertThat(response.body()).isEqualTo("Finish");
}

@Test
public void http202Async() throws InterruptedException, IOException {
Retrofit retrofit =
new Retrofit.Builder()
.baseUrl(server.url("/"))
.addConverterFactory(new ToStringConverterFactory())
.build();
Service example = retrofit.create(Service.class);
final AtomicReference<Response<String>> responseRef = new AtomicReference<>();
final CountDownLatch latch = new CountDownLatch(1);
Response<String> response;

server.enqueue(new MockResponse().setResponseCode(202).setBody(""));
example
.getString()
.enqueue(
new Callback<String>() {
@Override
public void onResponse(Call<String> call, Response<String> response) {
responseRef.set(response);
latch.countDown();
}

@Override
public void onFailure(Call<String> call, Throwable t) {
t.printStackTrace();
}
});
assertTrue(latch.await(10, SECONDS));

response = responseRef.get();
assertThat(response.isSuccessful()).isTrue();
assertThat(response.code()).isEqualTo(202);
assertThat(response.body()).isEqualTo("");
}

@Test
public void http404Sync() throws IOException {
Retrofit retrofit =
Expand Down

0 comments on commit 92ca604

Please sign in to comment.