### Question:
AuthenticationGateway extends BaseApiGateway { public void signIn(LoginParams login, Callback responseCallback) { apiGateway.makeRequest(new AuthenticationRequest(login), new AuthenticationApiResponseCallback(responseCallback, this, douban)); } AuthenticationGateway(Douban douban, ApiGateway apiGateway); void signOut(); void signIn(LoginParams login, Callback responseCallback); static final Gson GSON; }### Answer:
@Test public void shouldMakeARemoteCallWhenSigningInWithCaptchaCode() { authenticationGateway.signIn(loginParams, new Callback()); String urlString = apiGateway.getLatestRequest().getUrlString(); assertThat(urlString, equalTo("http: }
@Test public void shouldSendLoginParams() throws Exception { authenticationGateway.signIn(loginParams, new Callback()); AuthenticationRequest authenticationRequest = (AuthenticationRequest) apiGateway.getLatestRequest(); assertThat(authenticationRequest, equalTo(new AuthenticationRequest(loginParams))); HttpEntity postEntity = authenticationRequest.getPostEntity(); assertThat(postEntity.getContentType().getValue(), equalTo("application/x-www-form-urlencoded; charset=UTF-8")); String content = HiUtil.dump(postEntity); }
@Test public void shouldReturnTrueSignedIn() throws Exception { assertThat(Douban.isLogged(), equalTo(false)); authenticationGateway.signIn(loginParams, new Callback()); apiGateway.simulateTextResponse(200, TestResponses.AUTH_SUCCESS, null); assertThat(Douban.isLogged(), equalTo(true)); }
@Test public void shouldReturnFalseWhenSignedInWithWrongCaptchaCode() throws Exception { assertThat(Douban.isLogged(), equalTo(false)); authenticationGateway.signIn(loginParams, new Callback()); Header[] header = new Header[0]; apiGateway.simulateTextResponse(200, TestResponses.AUTH_ERROR, null); assertThat(Douban.isLogged(), equalTo(false)); assertThat(douban.mApiRespErrorCode.getCode(), equalTo("1")); assertThat(douban.mApiRespErrorCode.getMsg(), equalTo("验证码不正确")); }
@Test public void shouldCallOnFailureWhenParseRespError() throws Exception { authenticationGateway.signIn(loginParams, new Callback()); apiGateway.simulateTextResponse(200, TestResponses.NULL_RESP, null); assertNotNull(authenticationGateway.failureResponse); assertThat(douban.mApiRespErrorCode.getCode(), equalTo(ApiInternalError.INTERNAL_ERROR.getCode())); }
@Test public void shouldCallOnFailureWhenCallerError() throws Exception { authenticationGateway.signIn(loginParams, badCallback); apiGateway.simulateTextResponse(200, TestResponses.AUTH_SUCCESS, null); assertNotNull(authenticationGateway.failureResponse); assertThat(douban.mApiRespErrorCode.getCode(), equalTo(ApiInternalError.CALLER_ERROR.getCode())); }