Skip to content

Instantly share code, notes, and snippets.

@swankjesse
Created February 21, 2016 19:03
Show Gist options
  • Save swankjesse/6608b4713ad80988cdc9 to your computer and use it in GitHub Desktop.
Save swankjesse/6608b4713ad80988cdc9 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"));
}
}
@blanca92
Copy link

blanca92 commented Nov 7, 2017

Si mucho

@MianoMen1
Copy link

#``

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment