@Before
  public void setUp_EnsureStaticStateIsReset() {
    FakeHttpLayer fakeHttpLayer = Robolectric.getFakeHttpLayer();
    assertTrue(fakeHttpLayer.pendingHttpResponses.isEmpty());
    assertTrue(fakeHttpLayer.httpRequestInfos.isEmpty());
    assertTrue(fakeHttpLayer.httpResponseRules.isEmpty());
    assertNull(fakeHttpLayer.defaultHttpResponse);

    connectionKeepAliveStrategy =
        new ConnectionKeepAliveStrategy() {
          @Override
          public long getKeepAliveDuration(HttpResponse httpResponse, HttpContext httpContext) {
            return 0;
          }
        };
    requestDirector =
        new DefaultRequestDirector(
            null,
            null,
            null,
            connectionKeepAliveStrategy,
            null,
            null,
            null,
            null,
            null,
            null,
            null,
            null);
  }
 @Test
 public void shouldThrowUnauthorizedErrorForInvalidUsernameAndPassword() throws IOException {
   Robolectric.getFakeHttpLayer().setDefaultHttpResponse(401, "some response body");
   loginButton.performClick();
   ShadowHandler.idleMainLooper();
   assertThat(
       ShadowToast.getTextOfLatestToast(),
       equalTo(loginActivity.getString(R.string.unauthorized)));
 }
  @Test
  public void shouldLoginSuccessfullyForValidUserAndUrl() {
    Robolectric.getFakeHttpLayer().setDefaultHttpResponse(201, "some response body");

    loginButton.performClick();
    ShadowHandler.idleMainLooper();
    assertThat(
        ShadowToast.getTextOfLatestToast(),
        equalTo(loginActivity.getString(R.string.login_successful)));
  }
 @Test
 public void shouldThrowConnectionRefusedIfServerIsNotAvailable() throws IOException {
   serverUrl.setText("rapidftr.com:abcd");
   Robolectric.getFakeHttpLayer().setDefaultHttpResponse(404, "some response body");
   loginButton.performClick();
   ShadowHandler.idleMainLooper();
   assertThat(
       ShadowToast.getTextOfLatestToast(),
       equalTo(loginActivity.getString(R.string.server_not_reachable)));
 }
  @Test
  public void shouldSaveServerUrlAfterSuccessfulLogin() {
    SharedPreferences sharedPreferences =
        Robolectric.application.getSharedPreferences("RAPIDFTR_PREFERENCES", Context.MODE_PRIVATE);
    sharedPreferences.edit().putString("SERVER_URL", "").commit();

    serverUrl.setText("http://dev.rapidftr.com:3000");
    Robolectric.getFakeHttpLayer().setDefaultHttpResponse(201, "some response body");

    loginButton.performClick();
    assertThat(
        sharedPreferences.getString("SERVER_URL", ""), equalTo(serverUrl.getText().toString()));
  }
  @Test
  public void shouldSupportThrowingIOException() throws Exception {
    IOException fakeException = new IOException("fake");
    Robolectric.getFakeHttpLayer()
        .addHttpResponseRule(
            new FakeHttpLayer.RequestMatcherResponseRule(
                new FakeHttpLayer.UriRequestMatcher("http://example.com"), fakeException));

    HttpGet httpGet = new HttpGet("http://example.com");
    IOException exception = null;
    try {
      requestDirector.execute(null, httpGet, null);
      fail();
    } catch (HttpException e) {
      fail();
    } catch (IOException e) {
      exception = e;
    }
    assertThat(exception, sameInstance(fakeException));
  }