Skip to content

Instantly share code, notes, and snippets.

@JerzyPuchalski
Created March 21, 2016 13:05
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JerzyPuchalski/1d8a4890389f3602ae71 to your computer and use it in GitHub Desktop.
Save JerzyPuchalski/1d8a4890389f3602ae71 to your computer and use it in GitHub Desktop.
Retrifit 2 redirect problem with changing POST into GET
package net.nerrd.alcoquiz;
import org.junit.Test;
import okhttp3.ResponseBody;
import okhttp3.mockwebserver.MockResponse;
import okhttp3.mockwebserver.MockWebServer;
import okhttp3.mockwebserver.RecordedRequest;
import retrofit2.Call;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.http.Field;
import retrofit2.http.FormUrlEncoded;
import retrofit2.http.POST;
import static org.junit.Assert.assertEquals;
public class Retrofit2RedirectProblem {
public final MockWebServer server = new MockWebServer();
@Test
public void test() throws Exception {
final Retrofit retrofit = new Retrofit.Builder()
.baseUrl(server.url("/"))
.build();
final Service example = retrofit.create(Service.class);
server.enqueue(new MockResponse()
.setResponseCode(301)
.addHeader("Location", "/device/register/"));
server.enqueue(new MockResponse()
.setResponseCode(200)
.setBody("ok"));
final Call<ResponseBody> call = example.search("xyz");
final Response<ResponseBody> response = call.execute();
final RecordedRequest firstRequest = server.takeRequest();
final RecordedRequest redirectedRequest = server.takeRequest();
assertEquals("/device/register/", redirectedRequest.getPath());
assertEquals("POST", redirectedRequest.getMethod());
}
interface Service {
@FormUrlEncoded
@POST("device/register")
Call<ResponseBody> search(@Field("token") String token);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment