title: How to test service calls using mockwebserver that contains redirection?
body: <p>I am using mockwebserver to mock request and response for my android app. I am testing a login feature which goes through a series of 4 service calls. </p>
  
  <ol>
  <li>Get access token </li>
  <li>Re-direct </li>
  <li>Get user info (different base url)</li>
  <li>Get some other stuff (original base url)</li>
  </ol>
  
  <p>I am trying to mock the response of the redirected call. Here is my code:</p>
  
  <pre><code>@Test
  public void testSuccessfulLogin() throws Exception {
      // Post
      server.enqueue(new MockResponse()
              .setResponseCode(HTTP_OK)
              .setBody(getStringFromFile(getInstrumentation().getContext(), "access_token.json")));
  
      // Redirect
      server.enqueue(new MockResponse().setResponseCode(HTTP_MOVED_TEMP));
  
      // GET user info
      server.enqueue(new MockResponse().setResponseCode(HTTP_OK).setBody(getStringFromFile(getInstrumentation().getContext(), "userinfo.json")));
  
      // GET some other stuff
      server.enqueue(new MockResponse().setResponseCode(HTTP_OK)
              .setBody(getStringFromFile(getInstrumentation().getContext(), "sts.json")));
  
      // Init call
      loginWithoutWaiting(Data.serviceLoginUsername, Data.serviceLoginPassword);
  
      // Debug (need to loop 4 times to get all 4 call paths)
      RecordedRequest request = server.takeRequest();
      request.getPath();
  }
  </code></pre>
  
  <p>My test fails at the Redirect code. I cannot login. I have found some hints <a href="https://github.com/square/okhttp/blob/f91f97795b6fc3ccfec3d2ec4ea46abe733012df/okhttp-tests/src/test/java/okhttp3/CookiesTest.java#L214" rel="noreferrer">here</a> but I do not fully understand what is going on, thus can't make it work at the moment.</p>