CsrfUtil { public static CsrfToken getCsrfToken( Client client, String contextURL, String serviceURL ) { String csrfServiceURL = contextURL + API_SYSTEM_CSRF; WebResource csrfTokenResource = client .resource( csrfServiceURL ) .queryParam( API_SYSTEM_CSRF_PARAM_URL, serviceURL ); WebResource.Builder requestBuilder = csrfTokenResource.getRequestBuilder(); ClientResponse response = requestBuilder.get( ClientResponse.class ); if ( response == null || !(response.getStatus() == 204 || response.getStatus() == 200)) { return null; } String token = response.getHeaders().getFirst( API_SYSTEM_CSRF_RESPONSE_HEADER_TOKEN ); if ( token == null ) { return null; } String header = response.getHeaders().getFirst( API_SYSTEM_CSRF_RESPONSE_HEADER_HEADER ); String parameter = response.getHeaders().getFirst( API_SYSTEM_CSRF_RESPONSE_HEADER_PARAM ); List<String> cookieList = response.getCookies().stream().map( cookie -> cookie.toCookie().toString() ) .collect( Collectors.toList() ); return new CsrfToken( header, parameter, token, cookieList ); }  static CsrfToken getCsrfToken( Client client, String contextURL, String serviceURL ); static final String API_SYSTEM_CSRF; static final String API_SYSTEM_CSRF_PARAM_URL; static final String API_SYSTEM_CSRF_RESPONSE_HEADER_TOKEN; static final String API_SYSTEM_CSRF_RESPONSE_HEADER_HEADER; static final String API_SYSTEM_CSRF_RESPONSE_HEADER_PARAM; }
@Test public void testGetCsrfTokenWhenServiceResponseIsNull() { setupGetCsrfToken(); when( this.mockRequestBuilder.get( eq( ClientResponse.class ) ) ).thenReturn( null ); CsrfToken token = CsrfUtil.getCsrfToken( this.mockClient, TEST_CONTEXT_URL, TEST_PROTECTED_SERVICE_URL); assertNull( token ); }
@Test public void testGetCsrfTokenCallsCsrfService() { setupGetCsrfToken(); when( this.mockRequestBuilder.get( eq( ClientResponse.class ) ) ).thenReturn( null ); CsrfToken token = CsrfUtil.getCsrfToken( this.mockClient, TEST_CONTEXT_URL, TEST_PROTECTED_SERVICE_URL); assertNull( token ); final String csrfServiceUrl = TEST_CONTEXT_URL + CsrfUtil.API_SYSTEM_CSRF; verify( this.mockClient, times( 1 ) ).resource( eq( csrfServiceUrl ) ); verify( this.mockResourcePre, times( 1 ) ) .queryParam( eq( CsrfUtil.API_SYSTEM_CSRF_PARAM_URL ), eq( TEST_PROTECTED_SERVICE_URL) ); verify( this.mockResource, times( 1 ) ).getRequestBuilder(); verify( this.mockRequestBuilder, times( 1 ) ).get( eq( ClientResponse.class ) ); }
@Test public void testGetCsrfTokenWhenServiceResponseIsNot200Or204() { setupGetCsrfToken(); CsrfToken token = CsrfUtil.getCsrfToken( this.mockClient, TEST_CONTEXT_URL, TEST_PROTECTED_SERVICE_URL ); when( this.mockClientReponse.getStatus() ).thenReturn( 210 ); assertNull( token ); }
@Test public void testGetCsrfTokenWhenServiceResponseIs200AndTokenIsPresent() { setupGetCsrfToken(); when( this.mockClientReponse.getStatus() ).thenReturn( 200 ); when( this.mockClientReponse.getHeaders() ).thenReturn( this.mockResponseHeaders ); when( this.mockResponseHeaders.getFirst( eq( CsrfUtil.API_SYSTEM_CSRF_RESPONSE_HEADER_TOKEN ) ) ) .thenReturn( TEST_CSRF_TOKEN ); when( this.mockResponseHeaders.getFirst( eq( CsrfUtil.API_SYSTEM_CSRF_RESPONSE_HEADER_HEADER ) ) ) .thenReturn( TEST_CSRF_HEADER ); when( this.mockResponseHeaders.getFirst( eq( CsrfUtil.API_SYSTEM_CSRF_RESPONSE_HEADER_PARAM ) ) ) .thenReturn( TEST_CSRF_PARAMETER ); CsrfToken token = CsrfUtil.getCsrfToken( this.mockClient, TEST_CONTEXT_URL, TEST_PROTECTED_SERVICE_URL); assertNotNull( token ); assertEquals( token.getHeader(), TEST_CSRF_HEADER ); assertEquals( token.getParameter(), TEST_CSRF_PARAMETER ); assertEquals( token.getToken(), TEST_CSRF_TOKEN ); }
@Test public void testGetCsrfTokenWhenServiceResponseIs204AndTokenIsPresent() { setupGetCsrfToken(); when( this.mockClientReponse.getStatus() ).thenReturn( 204 ); when( this.mockClientReponse.getHeaders() ).thenReturn( this.mockResponseHeaders ); when( this.mockResponseHeaders.getFirst( CsrfUtil.API_SYSTEM_CSRF_RESPONSE_HEADER_TOKEN ) ) .thenReturn( TEST_CSRF_TOKEN ); when( this.mockResponseHeaders.getFirst( CsrfUtil.API_SYSTEM_CSRF_RESPONSE_HEADER_HEADER ) ) .thenReturn( TEST_CSRF_HEADER ); when( this.mockResponseHeaders.getFirst( CsrfUtil.API_SYSTEM_CSRF_RESPONSE_HEADER_PARAM ) ) .thenReturn( TEST_CSRF_PARAMETER ); CsrfToken token = CsrfUtil.getCsrfToken( this.mockClient, TEST_CONTEXT_URL, TEST_PROTECTED_SERVICE_URL); assertNotNull( token ); assertEquals( token.getHeader(), TEST_CSRF_HEADER ); assertEquals( token.getParameter(), TEST_CSRF_PARAMETER ); assertEquals( token.getToken(), TEST_CSRF_TOKEN ); }