OltuOAuth2ClientFacade implements OAuth2ClientFacade { @Override public void applyAccessToken(OAuth2Profile profile, HttpRequestBase request, String requestContent) { String uri = request.getURI().getPath(); OAuthBearerClientRequest oAuthClientRequest = new OAuthBearerClientRequest(uri).setAccessToken(profile.getAccessToken()); try { switch (profile.getAccessTokenPosition()) { case QUERY: appendAccessTokenToQuery(request, oAuthClientRequest); break; case BODY: appendAccessTokenToBody(request, oAuthClientRequest); break; case HEADER: default: appendAccessTokenToHeader(request, oAuthClientRequest); break; } } catch (OAuthSystemException e) { SoapUI.logError(e); } }  @Override void requestAccessToken(OAuth2Profile profile); @Override void refreshAccessToken(OAuth2Profile profile); @Override void applyAccessToken(OAuth2Profile profile, HttpRequestBase request, String requestContent);  }
@Test public void appendsAccessTokenToHeader() throws Exception { profileWithOnlyAccessToken.setAccessTokenPosition(OAuth2Profile.AccessTokenPosition.HEADER); String expectedAccessTokenValue = "Bearer " + profileWithOnlyAccessToken.getAccessToken(); oltuClientFacade.applyAccessToken(profileWithOnlyAccessToken, httpRequest, ""); assertThat(httpRequest.getHeaders(OAuth.HeaderType.AUTHORIZATION)[0].getValue(), is(expectedAccessTokenValue)); }
@Test public void appendsAccessTokenToHeaderByDefault() throws Exception { String expectedAccessTokenValue = "Bearer " + profileWithOnlyAccessToken.getAccessToken(); oltuClientFacade.applyAccessToken(profileWithOnlyAccessToken, httpRequest, ""); assertThat(httpRequest.getHeaders(OAuth.HeaderType.AUTHORIZATION)[0].getValue(), is(expectedAccessTokenValue)); }
@Test public void appendsAccessTokenToQuery() throws Exception { profileWithOnlyAccessToken.setAccessTokenPosition(OAuth2Profile.AccessTokenPosition.QUERY); oltuClientFacade.applyAccessToken(profileWithOnlyAccessToken, httpRequest, ""); assertThat(httpRequest.getURI().getQuery(), is("access_token=" + profileWithOnlyAccessToken.getAccessToken())); }
@Test public void appendsAccessTokenToBody() throws OAuth2Exception, IOException { String expectedBodyContent = "access_token=" + profileWithOnlyAccessToken.getAccessToken(); profileWithOnlyAccessToken.setAccessTokenPosition(OAuth2Profile.AccessTokenPosition.BODY); oltuClientFacade.applyAccessToken(profileWithOnlyAccessToken, httpRequest, ""); StringWriter writer = new StringWriter(); IOUtils.copy(httpRequest.getEntity().getContent(), writer, "UTF-8"); String actualContent = writer.toString(); assertThat(actualContent, is(expectedBodyContent)); }