### Question:
Fetcher { PostLoginResponse postLogin( final MultiValueMap<String, String> formData, final MultiValueMap<String, String> cookies ) throws InterruptedException { final ClientResponse clientResponse = post(LOGIN_PATH, formData, cookies); final HttpStatus statusCode = clientResponse.statusCode(); MonoUtils.unwrap(clientResponse.bodyToMono(String.class), TIMEOUT); if (!statusCode.is3xxRedirection()) { throw new RuntimeException( String.format("Illegal status code received. :: statusCode=%s", statusCode.value())); } final String location = clientResponse.headers().asHttpHeaders().getFirst(HttpHeaders.LOCATION); if (StringUtils.isEmpty(location)) { throw new RuntimeException("An empty `Location` header received."); } if (location.contains(LOGIN_PATH)) { throw new RuntimeException("Incorrect email address or password"); } return PostLoginResponse.builder() .cookies(CookieUtils.normalize(clientResponse.cookies())) .build(); } Fetcher(
        final WebClient.Builder webClientBuilder
    );   }### Answer:
@Test @SneakyThrows public void postLoginTest_illegalStatus() { expectedException.expect(RuntimeException.class); expectedException.expectMessage("Illegal status code received. :: statusCode=200"); final MockResponse mockResponse = new MockResponse(); mockWebServer.enqueue(mockResponse); final MultiValueMap<String, String> formData = new LinkedMultiValueMap<>(); final MultiValueMap<String, String> cookies = new LinkedMultiValueMap<>(); fetcher.postLogin(formData, cookies); }
@Test @SneakyThrows public void postLoginTest_emptyLocationHeader() { expectedException.expect(RuntimeException.class); expectedException.expectMessage("An empty `Location` header received."); final MockResponse mockResponse = new MockResponse() .setResponseCode(HttpStatus.FOUND.value()); mockWebServer.enqueue(mockResponse); final MultiValueMap<String, String> formData = new LinkedMultiValueMap<>(); final MultiValueMap<String, String> cookies = new LinkedMultiValueMap<>(); fetcher.postLogin(formData, cookies); }
@Test @SneakyThrows public void postLoginTest() { final MockResponse mockResponse = new MockResponse() .setResponseCode(HttpStatus.FOUND.value()) .addHeader(HttpHeaders.LOCATION, Fetcher.BASE_URL) .addHeader(HttpHeaders.SET_COOKIE, "response-cookie=qwerty"); mockWebServer.enqueue(mockResponse); final MultiValueMap<String, String> formData = new LinkedMultiValueMap<>() {{ set("form-data", "abc"); }}; final MultiValueMap<String, String> requestCookies = new LinkedMultiValueMap<>() {{ set("request-cookie", "12345"); }}; final MultiValueMap<String, String> responseCookies = new LinkedMultiValueMap<>() {{ set("response-cookie", "qwerty"); }}; final Fetcher.PostLoginResponse expected = Fetcher.PostLoginResponse.builder() .cookies(responseCookies) .build(); final Fetcher.PostLoginResponse actual = fetcher.postLogin(formData, requestCookies); assertEquals(expected, actual); final RecordedRequest recordedRequest = mockWebServer.takeRequest(); assertEquals(1, mockWebServer.getRequestCount()); assertEquals(HttpMethod.POST.name(), recordedRequest.getMethod()); assertEquals(Fetcher.LOGIN_PATH, recordedRequest.getPath()); assertEquals(USER_AGENT, recordedRequest.getHeader(HttpHeaders.USER_AGENT)); assertEquals("request-cookie=12345", recordedRequest.getHeader(HttpHeaders.COOKIE)); assertEquals("form-data=abc", recordedRequest.getBody().readUtf8()); }
@Test @SneakyThrows public void postLoginTest_incorrectEmailOrPassword() { expectedException.expect(RuntimeException.class); final MockResponse mockResponse = new MockResponse() .setResponseCode(HttpStatus.FOUND.value()) .addHeader(HttpHeaders.LOCATION, Fetcher.LOGIN_PATH); mockWebServer.enqueue(mockResponse); final MultiValueMap<String, String> formData = new LinkedMultiValueMap<>(); final MultiValueMap<String, String> cookies = new LinkedMultiValueMap<>(); fetcher.postLogin(formData, cookies); }