/** * Package-private constructor. Use static factory methods in * {@link MockMvcRequestBuilders}. * <p>For other ways to initialize a {@code MockMultipartHttpServletRequest}, * see {@link #with(RequestPostProcessor)} and the * {@link RequestPostProcessor} extension point. * @param uri the URL * @since 4.0.3 */ MockMultipartHttpServletRequestBuilder(URI uri) { super(HttpMethod.POST, uri); super.contentType(MediaType.MULTIPART_FORM_DATA); }
/** * Package-private constructor. Use static factory methods in * {@link MockMvcRequestBuilders}. * <p>For other ways to initialize a {@code MockMultipartHttpServletRequest}, * see {@link #with(RequestPostProcessor)} and the * {@link RequestPostProcessor} extension point. * @param urlTemplate a URL template; the resulting URL will be encoded * @param uriVariables zero or more URI variables */ MockMultipartHttpServletRequestBuilder(String urlTemplate, Object... uriVariables) { super(HttpMethod.POST, urlTemplate, uriVariables); super.contentType(MediaType.MULTIPART_FORM_DATA); }
protected MockHttpServletRequestBuilder initDefaultSetting(MockHttpServletRequestBuilder builder) { return builder.session(session).characterEncoding("UTF-8").contentType(MediaType.APPLICATION_JSON); }
private void setContentTypeToApplicationFormUrlEncoded(MockHttpServletRequestBuilder request) { MediaType mediaType = MediaType.parseMediaType(HeaderHelper.buildApplicationFormEncodedContentType(config, APPLICATION_FORM_URLENCODED_VALUE)); request.contentType(mediaType); List<Header> newHeaders = new ArrayList<Header>(headers.asList()); newHeaders.add(new Header(CONTENT_TYPE, mediaType.toString())); headers = new Headers(newHeaders); }
@Test public void contentTypeViaString() { this.builder.contentType("text/html"); MockHttpServletRequest request = this.builder.buildRequest(this.servletContext); String contentType = request.getContentType(); List<String> contentTypes = Collections.list(request.getHeaders("Content-Type")); assertEquals("text/html", contentType); assertEquals(1, contentTypes.size()); assertEquals("text/html", contentTypes.get(0)); }
@Test public void contentType() { this.builder.contentType(MediaType.TEXT_HTML); MockHttpServletRequest request = this.builder.buildRequest(this.servletContext); String contentType = request.getContentType(); List<String> contentTypes = Collections.list(request.getHeaders("Content-Type")); assertEquals("text/html", contentType); assertEquals(1, contentTypes.size()); assertEquals("text/html", contentTypes.get(0)); }
@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")); }
@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 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()); } }
@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.")); }
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()); }
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 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); } }
@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()); }