@Test public void headerContainsWithMissingHeader() throws Exception { AssertionError error = assertThrows(AssertionError.class, () -> MockRestRequestMatchers.header("foo", containsString("baz")).match(this.request)); assertThat(error.getMessage(), containsString("but was null")); }
@Test public void headersWithMissingHeader() throws Exception { AssertionError error = assertThrows(AssertionError.class, () -> MockRestRequestMatchers.header("foo", "bar").match(this.request)); assertThat(error.getMessage(), containsString("but was null")); }
@Test public void headerMissing() throws Exception { AssertionError error = assertThrows(AssertionError.class, () -> MockRestRequestMatchers.header("foo", "bar").match(this.request)); assertThat(error.getMessage(), containsString("was null")); }
@Test public void headerContainsWithMissingValue() throws Exception { this.request.getHeaders().put("foo", Arrays.asList("bar", "baz")); AssertionError error = assertThrows(AssertionError.class, () -> MockRestRequestMatchers.header("foo", containsString("bx")).match(this.request)); assertThat(error.getMessage(), containsString("was \"bar\"")); }
@Test public void headerMissingValue() throws Exception { this.request.getHeaders().put("foo", Arrays.asList("bar", "baz")); AssertionError error = assertThrows(AssertionError.class, () -> MockRestRequestMatchers.header("foo", "bad").match(this.request)); assertThat(error.getMessage(), containsString("expected:<bad> but was:<bar>")); }
@Test public void headersWithMissingValue() throws Exception { this.request.getHeaders().put("foo", Collections.singletonList("bar")); AssertionError error = assertThrows(AssertionError.class, () -> MockRestRequestMatchers.header("foo", "bar", "baz").match(this.request)); assertThat(error.getMessage(), containsString("to have at least <2> values")); }
@Test public void headerContains() throws Exception { this.request.getHeaders().put("foo", Arrays.asList("bar", "baz")); MockRestRequestMatchers.header("foo", containsString("ba")).match(this.request); }
@Test public void header() throws Exception { this.request.getHeaders().put("foo", Arrays.asList("bar", "baz")); MockRestRequestMatchers.header("foo", "bar", "baz").match(this.request); }
@Test public void headers() throws Exception { this.request.getHeaders().put("foo", Arrays.asList("bar", "baz")); MockRestRequestMatchers.header("foo", "bar", "baz").match(this.request); }
@Test public void testStringContains() throws Exception { this.mockServer.expect(requestTo("/person/1")) .andExpect(header("Accept", containsString("json"))) .andRespond(withSuccess(RESPONSE_BODY, MediaType.APPLICATION_JSON)); executeAndVerify(); }
private void configureTokenKeyResponse(String keyUrl, String response) throws MalformedURLException { config.setTokenKey(null); config.setTokenKeyUrl(new URL(keyUrl)); mockToken(); mockUaaServer.expect(requestTo(keyUrl)) .andExpect(header("Authorization", "Basic " + new String(Base64.encodeBase64("identity:identitysecret".getBytes())))) .andExpect(header("Accept", "application/json")) .andRespond(withStatus(OK).contentType(APPLICATION_JSON).body(response)); }
@Test public void testString() throws Exception { this.mockServer.expect(requestTo("/person/1")) .andExpect(header("Accept", "application/json, application/*+json")) .andRespond(withSuccess(RESPONSE_BODY, MediaType.APPLICATION_JSON)); executeAndVerify(); }
private void mockToken() { String response = getIdTokenResponse(); mockUaaServer.expect(requestTo("http://localhost/oauth/token")) .andExpect(header("Authorization", "Basic " + new String(Base64.encodeBase64("identity:identitysecret".getBytes())))) .andExpect(header("Accept", "application/json")) .andExpect(content().string(containsString("grant_type=authorization_code"))) .andExpect(content().string(containsString("code=the_code"))) .andExpect(content().string(containsString("redirect_uri=http%3A%2F%2Flocalhost%2Fcallback%2Fthe_origin"))) .andExpect(content().string(containsString(("response_type=id_token")))) .andRespond(withStatus(OK).contentType(APPLICATION_JSON).body(response)); }
@Test public void loginAndValidateSignatureUsingTokenKeyEndpoint() throws Exception { config.setTokenKeyUrl(new URL("http://localhost/token_key")); config.setTokenKey(null); KeyInfo key = KeyInfoBuilder.build("correctKey", PRIVATE_KEY, UAA_ISSUER_URL); VerificationKeyResponse verificationKeyResponse = TokenKeyEndpoint.getVerificationKeyResponse(key); String response = JsonUtils.writeValueAsString(verificationKeyResponse); mockToken(); mockUaaServer.expect(requestTo("http://localhost/token_key")) .andExpect(header("Authorization", "Basic " + new String(Base64.encodeBase64("identity:identitysecret".getBytes())))) .andExpect(header("Accept", "application/json")) .andRespond(withStatus(OK).contentType(APPLICATION_JSON).body(response)); mockToken(); UaaUser existingShadowUser = new UaaUser(new UaaUserPrototype() .withUsername("12345") .withPassword("") .withEmail("marissa_old@bloggs.com") .withGivenName("Marissa_Old") .withFamilyName("Bloggs_Old") .withId("user-id") .withOrigin("the_origin") .withZoneId("uaa") .withAuthorities(UaaAuthority.USER_AUTHORITIES)); userDatabase.addUser(existingShadowUser); xoAuthAuthenticationManager.authenticate(xCodeToken); }
@Test public void testContentTypePropagation() throws Exception { this.mockServer.expect(requestTo("/testApps/outboundResponse")) .andExpect(method(HttpMethod.POST)) .andExpect(header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON.toString())) .andRespond(withSuccess(HttpMethod.POST.name(), MediaType.TEXT_PLAIN)); this.contentTypePropagationChannel .send(new GenericMessage<Map<String, String>>(Collections.singletonMap("foo", "bar"))); this.mockServer.verify(); }
@Test public void getAchievementTypes() throws Exception { appFacebookMockServer.expect(requestTo(fbUrl("app/achievements"))) .andExpect(method(GET)) .andExpect(header("Authorization", "OAuth " + APP_ACCESS_TOKEN)) .andRespond(withSuccess(jsonResource("achievement-types"), MediaType.APPLICATION_JSON)); List<AchievementType> achievementTypes = appFacebook.achievementOperations().getAchievementTypes(); assertEquals(2, achievementTypes.size()); assertWaldoAchievement(achievementTypes.get(0), null); assertTiedShoesAchievement(achievementTypes.get(1)); }
@Test(expected=ServerException.class) public void code2Service() throws Exception { mockServer.expect(requestTo(fbUrl("me"))) .andExpect(method(GET)) .andExpect(header("Authorization", "OAuth someAccessToken")) .andRespond(withStatus(HttpStatus.BAD_REQUEST).body(jsonResource("error-2-serviceUnavailable")).contentType(MediaType.APPLICATION_JSON)); facebook.fetchObject("me", User.class); fail(); }
@Test(expected=org.springframework.social.InsufficientPermissionException.class) public void code10PermissionDenied() throws Exception { mockServer.expect(requestTo(fbUrl("me"))) .andExpect(method(GET)) .andExpect(header("Authorization", "OAuth someAccessToken")) .andRespond(withStatus(HttpStatus.BAD_REQUEST).body(jsonResource("error-10-permissionDenied")).contentType(MediaType.APPLICATION_JSON)); facebook.fetchObject("me", User.class); fail(); }
@Test public void deleteComment() { mockServer.expect(requestTo(fbUrl("1533260333_122829644452184_587062"))) .andExpect(method(POST)) .andExpect(content().string("method=delete")) .andExpect(header("Authorization", "OAuth someAccessToken")) .andRespond(withSuccess("{}", MediaType.APPLICATION_JSON)); facebook.commentOperations().deleteComment("1533260333_122829644452184_587062"); mockServer.verify(); }
@Test public void readBook() throws Exception { mockServer.expect(requestTo(fbUrl("me/book.reads"))) .andExpect(method(POST)) .andExpect(content().string("book=http%3A%2F%2Fsamples.ogp.me%2F226075010839791&progress%3Atimestamp=1378241299&progress%3Apercent_complete=3.1415927")) .andExpect(header("Authorization", "OAuth someAccessToken")) .andRespond(withSuccess("{\"id\": \"123456789080\"}", MediaType.APPLICATION_JSON)); String actionId = facebook.openGraphOperations().bookActions().readBook("http://samples.ogp.me/226075010839791", 1378241299, 3.1415926535f); assertEquals("123456789080", actionId); }