Created
February 21, 2016 19:03
-
-
Save swankjesse/6608b4713ad80988cdc9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
Si mucho