Skip to content

Instantly share code, notes, and snippets.

@SmartDengg
Forked from swankjesse/RetrofitBug.java
Created June 20, 2016 07:22
Show Gist options
  • Save SmartDengg/272ab92dcfb5723a81cbcc15797f6f52 to your computer and use it in GitHub Desktop.
Save SmartDengg/272ab92dcfb5723a81cbcc15797f6f52 to your computer and use it in GitHub Desktop.
import okhttp3.ResponseBody;
import okhttp3.mockwebserver.MockResponse;
import okhttp3.mockwebserver.MockWebServer;
import okhttp3.mockwebserver.RecordedRequest;
import org.junit.Rule;
import org.junit.Test;
import retrofit2.Call;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.http.GET;
import retrofit2.http.Headers;
import retrofit2.http.Query;
import static org.junit.Assert.assertEquals;
public final class RetrofitBug {
@Rule public final MockWebServer server = new MockWebServer();
interface Service {
@GET("/path")
@Headers("User-Agent: Test Case")
Call<ResponseBody> search(@Query("keyword") String keyword);
}
@Test public void test() throws Exception {
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(server.url("/"))
.build();
Service example = retrofit.create(Service.class);
server.enqueue(new MockResponse()
.addHeader("Content-Type", "text/plain")
.setBody("Hi"));
Call<ResponseBody> call = example.search("hello");
Response<ResponseBody> response = call.execute();
assertEquals("text/plain", response.headers().get("Content-Type"));
assertEquals("Hi", response.body().string());
RecordedRequest recordedRequest = server.takeRequest();
assertEquals("/path?keyword=hello", recordedRequest.getPath());
assertEquals("Test Case", recordedRequest.getHeader("User-Agent"));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment