@Test public void findUserById() throws Exception { String userJson = objectMapper.writeValueAsString(testUser); mockMvc.perform( post("/user") .contentType(MediaType.APPLICATION_JSON_UTF8) .content(userJson)); mockMvc.perform(get("/user" + 1) .contentType(MediaType.APPLICATION_JSON_UTF8) .content(userJson)); }
@Test public void body() throws IOException { byte[] body = "Hello World".getBytes("UTF-8"); this.builder.content(body); MockHttpServletRequest request = this.builder.buildRequest(this.servletContext); byte[] result = FileCopyUtils.copyToByteArray(request.getInputStream()); assertArrayEquals(body, result); }
@Test public void requestParameterFromRequestBodyFormData() throws Exception { String contentType = "application/x-www-form-urlencoded;charset=UTF-8"; String body = "name+1=value+1&name+2=value+A&name+2=value+B&name+3"; MockHttpServletRequest request = new MockHttpServletRequestBuilder(HttpMethod.POST, "/foo") .contentType(contentType).content(body.getBytes(StandardCharsets.UTF_8)) .buildRequest(this.servletContext); assertArrayEquals(new String[] {"value 1"}, request.getParameterMap().get("name 1")); assertArrayEquals(new String[] {"value A", "value B"}, request.getParameterMap().get("name 2")); assertArrayEquals(new String[] {null}, request.getParameterMap().get("name 3")); }
private ClientHttpResponse getClientHttpResponse( HttpMethod httpMethod, URI uri, HttpHeaders requestHeaders, byte[] requestBody) { try { MockHttpServletResponse servletResponse = this.mockMvc .perform(request(httpMethod, uri).content(requestBody).headers(requestHeaders)) .andReturn() .getResponse(); HttpStatus status = HttpStatus.valueOf(servletResponse.getStatus()); byte[] body = servletResponse.getContentAsByteArray(); MockClientHttpResponse clientResponse = new MockClientHttpResponse(body, status); clientResponse.getHeaders().putAll(getResponseHeaders(servletResponse)); return clientResponse; } catch (Exception ex) { byte[] body = ex.toString().getBytes(StandardCharsets.UTF_8); return new MockClientHttpResponse(body, HttpStatus.INTERNAL_SERVER_ERROR); } }
@Test // SPR-15753 public void formContentIsNotDuplicated() throws Exception { MockMvc mockMvc = MockMvcBuilders.standaloneSetup(new Spr15753Controller()) .addFilter(new FormContentFilter()) .build(); mockMvc.perform(put("/").content("d1=a&d2=s").contentType(MediaType.APPLICATION_FORM_URLENCODED)) .andExpect(content().string("d1:a, d2:s.")); }
@Test public void testPrint() throws Exception { StringWriter writer = new StringWriter(); standaloneSetup(new SimpleController()) .build() .perform(get("/").content("Hello Request".getBytes())) .andDo(log()) .andDo(print()) .andDo(print(System.err)) .andDo(print(writer)) ; System.out.println(); System.out.println("==============================================================="); System.out.println(writer.toString()); }
private ResultActions createZoneScope(ScimGroup group) throws Exception { MockHttpServletRequestBuilder post = post("/Groups/zones") .accept(APPLICATION_JSON) .contentType(APPLICATION_JSON) .header("Authorization", "Bearer " + identityClientToken) .content(JsonUtils.writeValueAsBytes(group)); return getMockMvc().perform(post); }
@Test public void changeEmail_withIncorrectCode() throws Exception { when(expiringCodeStore.retrieveCode("the_secret_code", IdentityZoneHolder.get().getId())) .thenReturn(new ExpiringCode("the_secret_code", new Timestamp(System.currentTimeMillis()), "{\"userId\":\"user-id-001\",\"email\":\"new@example.com\",\"client_id\":null}", "incorrect-code")); mockMvc.perform(post("/email_changes") .contentType(APPLICATION_JSON) .content("the_secret_code") .accept(APPLICATION_JSON)) .andExpect(MockMvcResultMatchers.status().isUnprocessableEntity()); } }
private ResultActions updateAccountStatus(ScimUser user, UserAccountStatus alteredAccountStatus) throws Exception { String jsonStatus = JsonUtils.writeValueAsString(alteredAccountStatus); return mockMvc .perform( patch("/Users/" + user.getId() + "/status") .header("Authorization", "Bearer " + uaaAdminToken) .accept(APPLICATION_JSON) .contentType(APPLICATION_JSON) .content(jsonStatus) ); }
private void postUserAndExpectSame(String userJson) throws Exception { mockMvc.perform( post("/user") .contentType(MediaType.APPLICATION_JSON_UTF8) .content(userJson) ).andExpect(status().isCreated()).andExpect(content().json(userJson)); } }
private ResultActions performUpdate(ClientMetadata updatedClientMetadata) throws Exception { MockHttpServletRequestBuilder updateClientPut = put("/oauth/clients/" + updatedClientMetadata.getClientId() + "/meta") .header("Authorization", "Bearer " + adminClientTokenWithClientsWrite) .header("If-Match", "0") .accept(APPLICATION_JSON) .contentType(APPLICATION_JSON) .content(JsonUtils.writeValueAsString(updatedClientMetadata)); return mockMvc.perform(updateClientPut); }
private ResultActions createScimGroupHelper(ScimGroup scimGroup) throws Exception { MockHttpServletRequestBuilder post = post("/Groups") .header("Authorization", "Bearer " + scimWriteToken) .contentType(APPLICATION_JSON) .content(serializeWithoutMeta(scimGroup)); return mockMvc.perform(post).andExpect(status().isCreated()); }
private ResultActions performUpdate(ClientMetadata updatedClientMetadata) throws Exception { MockHttpServletRequestBuilder updateClientPut = put("/oauth/clients/" + updatedClientMetadata.getClientId() + "/meta") .header("Authorization", "Bearer " + adminClientTokenWithClientsWrite) .header("If-Match", "0") .accept(APPLICATION_JSON) .contentType(APPLICATION_JSON) .content(JsonUtils.writeValueAsString(updatedClientMetadata)); return mockMvc.perform(updateClientPut); } }
private ClientDetails createClientAdminsClient(String token) throws Exception { List<String> scopes = Arrays.asList("oauth.approvals", "clients.admin"); BaseClientDetails client = createBaseClient(null, SECRET, Arrays.asList("password", "client_credentials"), scopes, scopes); MockHttpServletRequestBuilder createClientPost = post("/oauth/clients") .header("Authorization", "Bearer " + token) .accept(APPLICATION_JSON) .contentType(APPLICATION_JSON) .content(JsonUtils.writeValueAsString(client)); mockMvc.perform(createClientPost).andExpect(status().isCreated()); return getClient(client.getClientId()); }
private ClientDetails createReadWriteClient(String token) throws Exception { List<String> scopes = Arrays.asList("oauth.approvals", "clients.read", "clients.write"); BaseClientDetails client = createBaseClient(null, SECRET, Arrays.asList("password", "client_credentials"), scopes, scopes); MockHttpServletRequestBuilder createClientPost = post("/oauth/clients") .header("Authorization", "Bearer " + token) .accept(APPLICATION_JSON) .contentType(APPLICATION_JSON) .content(JsonUtils.writeValueAsString(client)); mockMvc.perform(createClientPost).andExpect(status().isCreated()); return getClient(client.getClientId()); }
private ClientDetails createAdminClient(String token) throws Exception { List<String> scopes = Arrays.asList("uaa.admin", "oauth.approvals", "clients.read", "clients.write"); BaseClientDetails client = createBaseClient(null, SECRET, Arrays.asList("password", "client_credentials"), scopes, scopes); MockHttpServletRequestBuilder createClientPost = post("/oauth/clients") .header("Authorization", "Bearer " + token) .accept(APPLICATION_JSON) .contentType(APPLICATION_JSON) .content(JsonUtils.writeValueAsString(client)); mockMvc.perform(createClientPost).andExpect(status().isCreated()); return getClient(client.getClientId()); }
private ClientDetails createApprovalsLoginClient(String token) throws Exception { List<String> scopes = Arrays.asList("uaa.admin", "oauth.approvals", "oauth.login"); BaseClientDetails client = createBaseClient(null, SECRET, Arrays.asList("password", "client_credentials"), scopes, scopes); MockHttpServletRequestBuilder createClientPost = post("/oauth/clients") .header("Authorization", "Bearer " + token) .accept(APPLICATION_JSON) .contentType(APPLICATION_JSON) .content(JsonUtils.writeValueAsString(client)); mockMvc.perform(createClientPost).andExpect(status().isCreated()); return getClient(client.getClientId()); }
@Test public void testUpdateForbiddenNonAdmin() throws Exception { mockMvc.perform(put("/mfa-providers/invalid") .header("Authorization", "bearer " + nonAdminToken) .contentType(APPLICATION_JSON) .content(JsonUtils.writeValueAsString(new MfaProvider<>()))) .andExpect(status().isForbidden()); }
protected ClientDetails createClient(String token, String id, String clientSecret, Collection<String> grantTypes) throws Exception { BaseClientDetails client = createBaseClient(id, clientSecret, grantTypes); MockHttpServletRequestBuilder createClientPost = post("/oauth/clients") .header("Authorization", "Bearer " + token) .accept(APPLICATION_JSON) .contentType(APPLICATION_JSON) .content(toString(client)); mockMvc.perform(createClientPost).andExpect(status().isCreated()); return getClient(client.getClientId()); }
@Test public void testLimitedScopesWithoutMember() throws Exception { IdentityZone zone = MockMvcUtils.createZoneUsingWebRequest(getMockMvc(), identityClientToken); ScimGroup group = new ScimGroup("zones." + zone.getId() + ".admin"); MockHttpServletRequestBuilder post = post("/Groups/zones") .accept(APPLICATION_JSON) .contentType(APPLICATION_JSON) .header("Authorization", "Bearer " + identityClientToken) .content(JsonUtils.writeValueAsBytes(group)); getMockMvc().perform(post) .andExpect(status().isBadRequest()); }