AuthenticatedRestTemplate { public <T> T postForObject(String resourcePath, Object request, Class<T> responseType) throws RestClientException { return restTemplate.postForObject(getURIForResource(resourcePath), request, responseType); }  String getURIForResource(String resourcePath); T getForObject(String resourcePath, Class<T> responseType); List<T> getForObjectAsList(String resourcePath, Class<T[]> responseType); List<T> getForObjectAsListWithQueryStringParams(String resourcePath, Class<T[]> responseType, Map<String, ?> queryStringParams); ResponseEntity<T> getForEntity(String resourcePath, Class<T> responseType); ResponseEntity<T> getForEntityWithQueryParams(String resourcePath, ParameterizedTypeReference<T> responseType, Map<String, ?> queryStringParams); T postForObject(String resourcePath, Object request, Class<T> responseType); T putForObject(String resourcePath, Object request, Class<T> responseType); ResponseEntity<T> postForEntity(String resourcePath, Object request, Class<T> responseType); T deleteForObject(String resourcePath, HttpEntity request, Class<T> responseType); void delete(String resourcePath, HttpEntity request); void delete(String resourcePath); void patch(String resourcePath, Object request); void put(String resourcePath, Object request); @VisibleForTesting CookieStoreRestTemplate getRestTemplate();  }
@Test public void testAuthRestTemplateForSessionTimeoutWithPostForObject() { initialAuthenticationMock(); mockCsrfTokenEndpoint(); logger.debug("Mock returns 403 for POST request when session timeout"); WireMock.stubFor( WireMock.post(WireMock.urlEqualTo("/random-403-endpoint")) .willReturn(WireMock.aResponse().withStatus(HttpStatus.FORBIDDEN.value())) ); String response = null; try { response = authenticatedRestTemplate.postForObject("random-403-endpoint", new HashMap<String, String>(), String.class); } catch (RestClientException e) { logger.debug("Expecting this to fail because the response for /random-403-endpoint has been stubbed to be always returning a 403"); Assert.assertEquals("Tried to re-authenticate but the response remains to be unauthenticated", e.getMessage()); } Assert.assertNull(response); WireMock.verify(2, WireMock.getRequestedFor(WireMock.urlMatching("/login")).withHeader("Accept", WireMock.matching("text/plain.*"))); WireMock.verify(2, WireMock.postRequestedFor(WireMock.urlMatching("/login")).withHeader("Accept", WireMock.matching("text/plain.*"))); WireMock.verify(2, WireMock.postRequestedFor(WireMock.urlMatching("/random-403-endpoint")).withHeader("Accept", WireMock.matching("text/plain.*")).withHeader("X-CSRF-TOKEN", WireMock.matching("madeup-csrf-value"))); }
@Test public void testAuthRestTemplateFor401() { initialAuthenticationMock(); mockCsrfTokenEndpoint(); logger.debug("Mock returns 401 for POST request when session timeout"); WireMock.stubFor( WireMock.post(WireMock.urlEqualTo("/random-401-endpoint")) .willReturn(WireMock.aResponse().withStatus(HttpStatus.UNAUTHORIZED.value())) ); String response = null; try { response = authenticatedRestTemplate.postForObject("random-401-endpoint", new HashMap<String, String>(), String.class); } catch (RestClientException e) { logger.debug("Expecting this to fail because the response for /random-401-endpoint has been stubbed to be always returning a 401"); Assert.assertEquals("Tried to re-authenticate but the response remains to be unauthenticated", e.getMessage()); } Assert.assertNull(response); WireMock.verify(2, WireMock.getRequestedFor(WireMock.urlMatching("/login")).withHeader("Accept", WireMock.matching("text/plain.*"))); WireMock.verify(2, WireMock.postRequestedFor(WireMock.urlMatching("/login")).withHeader("Accept", WireMock.matching("text/plain.*"))); WireMock.verify(2, WireMock.postRequestedFor(WireMock.urlMatching("/random-401-endpoint")).withHeader("Accept", WireMock.matching("text/plain.*")).withHeader("X-CSRF-TOKEN", WireMock.matching("madeup-csrf-value"))); }