/** * Set the given, single header value under the given name. * @param headerName the header name * @param headerValue the header value * @throws UnsupportedOperationException if adding headers is not supported * @see #put(String, List) * @see #add(String, String) */ @Override public void set(String headerName, @Nullable String headerValue) { this.headers.set(headerName, headerValue); }
public void set(String key, String value) { parameters.set(key, value); }
/** * Set the 'Accept' header to the given media type(s). * @param mediaTypes one or more media types */ public MockHttpServletRequestBuilder accept(MediaType... mediaTypes) { Assert.notEmpty(mediaTypes, "'mediaTypes' must not be empty"); this.headers.set("Accept", MediaType.toString(Arrays.asList(mediaTypes))); return this; }
/** * Set the given, single header value under the given name. * @param headerName the header name * @param headerValue the header value * @throws UnsupportedOperationException if adding headers is not supported * @see #put(String, List) * @see #add(String, String) */ @Override public void set(String headerName, @Nullable String headerValue) { this.headers.set(headerName, headerValue); }
/** * Set the 'Accept' header to the given media type(s). * @param mediaTypes one or more media types */ public MockHttpServletRequestBuilder accept(String... mediaTypes) { Assert.notEmpty(mediaTypes, "'mediaTypes' must not be empty"); List<MediaType> result = new ArrayList<>(mediaTypes.length); for (String mediaType : mediaTypes) { result.add(MediaType.parseMediaType(mediaType)); } this.headers.set("Accept", MediaType.toString(result)); return this; }
@Override public void setSessionId(ServerWebExchange exchange, String id) { Assert.notNull(id, "'id' is required"); ResponseCookie cookie = initSessionCookie(exchange, id, getCookieMaxAge()); exchange.getResponse().getCookies().set(this.cookieName, cookie); }
@Override public void expireSession(ServerWebExchange exchange) { ResponseCookie cookie = initSessionCookie(exchange, "", Duration.ZERO); exchange.getResponse().getCookies().set(this.cookieName, cookie); }
@Override public void enhance(AccessTokenRequest request, OAuth2ProtectedResourceDetails resource, MultiValueMap<String, String> form, HttpHeaders headers) { for (String include : parameterIncludes) { if (request.containsKey(include)) { form.set(include, request.getFirst(include)); } } }
@Override public void expireSession(ServerWebExchange exchange) { ResponseCookie cookie = initSessionCookie(exchange, "", Duration.ZERO); exchange.getResponse().getCookies().set(this.cookieName, cookie); }
@Override public void setSessionId(ServerWebExchange exchange, String id) { Assert.notNull(id, "'id' is required"); ResponseCookie cookie = initSessionCookie(exchange, id, getCookieMaxAge()); exchange.getResponse().getCookies().set(this.cookieName, cookie); }
@Test public void testEquals() { MultiValueMap<String, String> map1 = new LinkedMultiValueMap<>(); map1.set("Content-Type", "text/plain"); MultiValueMap<String, String> map2 = new LinkedMultiValueMap<>(); map2.set("Content-Type", "application/json"); assertTrue(new HttpEntity<>().equals(new HttpEntity<Object>())); assertFalse(new HttpEntity<>(map1).equals(new HttpEntity<Object>())); assertFalse(new HttpEntity<>().equals(new HttpEntity<Object>(map2))); assertTrue(new HttpEntity<>(map1).equals(new HttpEntity<Object>(map1))); assertFalse(new HttpEntity<>(map1).equals(new HttpEntity<Object>(map2))); assertTrue(new HttpEntity<String>(null, null).equals(new HttpEntity<String>(null, null))); assertFalse(new HttpEntity<>("foo", null).equals(new HttpEntity<String>(null, null))); assertFalse(new HttpEntity<String>(null, null).equals(new HttpEntity<>("bar", null))); assertTrue(new HttpEntity<>("foo", map1).equals(new HttpEntity<String>("foo", map1))); assertFalse(new HttpEntity<>("foo", map1).equals(new HttpEntity<String>("bar", map1))); }
@Test public void equals() { map.set("key1", "value1"); assertEquals(map, map); MultiValueMap<String, String> o1 = new LinkedMultiValueMap<>(); o1.set("key1", "value1"); assertEquals(map, o1); assertEquals(o1, map); Map<String, List<String>> o2 = new HashMap<>(); o2.put("key1", Collections.singletonList("value1")); assertEquals(map, o2); assertEquals(o2, map); }
@Test public void multiValueMap() { MultiValueMap<String, String> map = new LinkedMultiValueMap<>(); map.set("Content-Type", "text/plain"); String body = "foo"; HttpEntity<String> entity = new HttpEntity<>(body, map); assertEquals(body, entity.getBody()); assertEquals(MediaType.TEXT_PLAIN, entity.getHeaders().getContentType()); assertEquals("text/plain", entity.getHeaders().getFirst("Content-Type")); }
@Test public void writeForm() { MultiValueMap<String, String> body = new LinkedMultiValueMap<>(); body.set("name 1", "value 1"); body.add("name 2", "value 2+1"); body.add("name 2", "value 2+2"); body.add("name 3", null); MockServerHttpResponse response = new MockServerHttpResponse(this.bufferFactory); this.writer.write(Mono.just(body), null, MediaType.APPLICATION_FORM_URLENCODED, response, null).block(); String expected = "name+1=value+1&name+2=value+2%2B1&name+2=value+2%2B2&name+3"; StepVerifier.create(response.getBody()) .consumeNextWith(stringConsumer( expected)) .expectComplete() .verify(); HttpHeaders headers = response.getHeaders(); assertEquals("application/x-www-form-urlencoded;charset=UTF-8", headers.getContentType().toString()); assertEquals(expected.length(), headers.getContentLength()); }
@Test public void fromMultipartData() { MultiValueMap<String, Object> map = new LinkedMultiValueMap<>(); map.set("name 3", "value 3"); BodyInserters.FormInserter<Object> inserter = BodyInserters.fromMultipartData("name 1", "value 1") .withPublisher("name 2", Flux.just("foo", "bar", "baz"), String.class) .with(map); MockClientHttpRequest request = new MockClientHttpRequest(HttpMethod.GET, URI.create("http://example.com")); Mono<Void> result = inserter.insert(request, this.context); StepVerifier.create(result).expectComplete().verify(); }
@Test public void writeForm() throws IOException { MultiValueMap<String, String> body = new LinkedMultiValueMap<>(); body.set("name 1", "value 1"); body.add("name 2", "value 2+1"); body.add("name 2", "value 2+2"); body.add("name 3", null); MockHttpOutputMessage outputMessage = new MockHttpOutputMessage(); this.converter.write(body, MediaType.APPLICATION_FORM_URLENCODED, outputMessage); assertEquals("Invalid result", "name+1=value+1&name+2=value+2%2B1&name+2=value+2%2B2&name+3", outputMessage.getBodyAsString(StandardCharsets.UTF_8)); assertEquals("Invalid content-type", "application/x-www-form-urlencoded;charset=UTF-8", outputMessage.getHeaders().getContentType().toString()); assertEquals("Invalid content-length", outputMessage.getBodyAsBytes().length, outputMessage.getHeaders().getContentLength()); }
@Test public void fromFormDataMap() { MultiValueMap<String, String> body = new LinkedMultiValueMap<>(); body.set("name 1", "value 1"); body.add("name 2", "value 2+1"); body.add("name 2", "value 2+2"); body.add("name 3", null); BodyInserter<MultiValueMap<String, String>, ClientHttpRequest> inserter = BodyInserters.fromFormData(body); MockClientHttpRequest request = new MockClientHttpRequest(HttpMethod.GET, URI.create("http://example.com")); Mono<Void> result = inserter.insert(request, this.context); StepVerifier.create(result).expectComplete().verify(); StepVerifier.create(request.getBody()) .consumeNextWith(dataBuffer -> { byte[] resultBytes = new byte[dataBuffer.readableByteCount()]; dataBuffer.read(resultBytes); DataBufferUtils.release(dataBuffer); assertArrayEquals("name+1=value+1&name+2=value+2%2B1&name+2=value+2%2B2&name+3".getBytes(StandardCharsets.UTF_8), resultBytes); }) .expectComplete() .verify(); }
@Test public void from() throws URISyntaxException { ClientRequest other = ClientRequest.create(GET, URI.create("http://example.com")) .header("foo", "bar") .cookie("baz", "qux").build(); ClientRequest result = ClientRequest.from(other) .headers(httpHeaders -> httpHeaders.set("foo", "baar")) .cookies(cookies -> cookies.set("baz", "quux")) .build(); assertEquals(new URI("http://example.com"), result.url()); assertEquals(GET, result.method()); assertEquals(1, result.headers().size()); assertEquals("baar", result.headers().getFirst("foo")); assertEquals(1, result.cookies().size()); assertEquals("quux", result.cookies().getFirst("baz")); }
@Test public void from() { Flux<DataBuffer> otherBody = Flux.just("foo", "bar") .map(s -> s.getBytes(StandardCharsets.UTF_8)) .map(dataBufferFactory::wrap); ClientResponse other = ClientResponse.create(HttpStatus.BAD_REQUEST, ExchangeStrategies.withDefaults()) .header("foo", "bar") .cookie("baz", "qux") .body(otherBody) .build(); Flux<DataBuffer> body = Flux.just("baz") .map(s -> s.getBytes(StandardCharsets.UTF_8)) .map(dataBufferFactory::wrap); ClientResponse result = ClientResponse.from(other) .headers(httpHeaders -> httpHeaders.set("foo", "baar")) .cookies(cookies -> cookies.set("baz", ResponseCookie.from("baz", "quux").build())) .body(body) .build(); assertEquals(HttpStatus.BAD_REQUEST, result.statusCode()); assertEquals(1, result.headers().asHttpHeaders().size()); assertEquals("baar", result.headers().asHttpHeaders().getFirst("foo")); assertEquals(1, result.cookies().size()); assertEquals("quux", result.cookies().getFirst("baz").getValue()); StepVerifier.create(result.bodyToFlux(String.class)) .expectNext("baz") .verifyComplete(); }
@Test public void from() { MockServerHttpRequest request = MockServerHttpRequest.post("http://example.com") .header("foo", "bar") .build(); MockServerWebExchange exchange = MockServerWebExchange.from(request); ServerRequest other = ServerRequest.create(exchange, HandlerStrategies.withDefaults().messageReaders()); Flux<DataBuffer> body = Flux.just("baz") .map(s -> s.getBytes(StandardCharsets.UTF_8)) .map(dataBufferFactory::wrap); ServerRequest result = ServerRequest.from(other) .method(HttpMethod.HEAD) .headers(httpHeaders -> httpHeaders.set("foo", "baar")) .cookies(cookies -> cookies.set("baz", ResponseCookie.from("baz", "quux").build())) .body(body) .build(); assertEquals(HttpMethod.HEAD, result.method()); assertEquals(1, result.headers().asHttpHeaders().size()); assertEquals("baar", result.headers().asHttpHeaders().getFirst("foo")); assertEquals(1, result.cookies().size()); assertEquals("quux", result.cookies().getFirst("baz").getValue()); StepVerifier.create(result.bodyToFlux(String.class)) .expectNext("baz") .verifyComplete(); }