@Override @Nullable protected URI adapt(HttpHeaders headers) throws ExecutionException { return headers.getLocation(); } };
@Override @Nullable public URI postForLocation(String url, @Nullable Object request, Object... uriVariables) throws RestClientException { RequestCallback requestCallback = httpEntityCallback(request); HttpHeaders headers = execute(url, HttpMethod.POST, requestCallback, headersExtractor(), uriVariables); return (headers != null ? headers.getLocation() : null); }
@Override @Nullable public URI postForLocation(String url, @Nullable Object request, Map<String, ?> uriVariables) throws RestClientException { RequestCallback requestCallback = httpEntityCallback(request); HttpHeaders headers = execute(url, HttpMethod.POST, requestCallback, headersExtractor(), uriVariables); return (headers != null ? headers.getLocation() : null); }
@Override @Nullable public URI postForLocation(URI url, @Nullable Object request) throws RestClientException { RequestCallback requestCallback = httpEntityCallback(request); HttpHeaders headers = execute(url, HttpMethod.POST, requestCallback, headersExtractor()); return (headers != null ? headers.getLocation() : null); }
@Test public void exchangePost() throws Exception { HttpHeaders requestHeaders = new HttpHeaders(); requestHeaders.set("MyHeader", "MyValue"); requestHeaders.setContentType(MediaType.TEXT_PLAIN); HttpEntity<String> entity = new HttpEntity<>(helloWorld, requestHeaders); HttpEntity<Void> result = template.exchange(baseUrl + "/{method}", POST, entity, Void.class, "post"); assertEquals("Invalid location", new URI(baseUrl + "/post/1"), result.getHeaders().getLocation()); assertFalse(result.hasBody()); }
@Test public void exchangePost() throws Exception { HttpHeaders requestHeaders = new HttpHeaders(); requestHeaders.set("MyHeader", "MyValue"); requestHeaders.setContentType(MediaType.TEXT_PLAIN); HttpEntity<String> requestEntity = new HttpEntity<>(helloWorld, requestHeaders); Future<ResponseEntity<Void>> resultFuture = template.exchange(baseUrl + "/{method}", HttpMethod.POST, requestEntity, Void.class, "post"); ResponseEntity<Void> result = resultFuture.get(); assertEquals("Invalid location", new URI(baseUrl + "/post/1"), result.getHeaders().getLocation()); assertFalse(result.hasBody()); }
@Test public void expandUriTemplateVariablesFromExchangeAttribute() { String url = "http://url.somewhere.com?foo={foo}"; Map<String, String> attributes = Collections.singletonMap("foo", "bar"); this.exchange.getAttributes().put(HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE, attributes); RedirectView view = new RedirectView(url); view.render(new HashMap<>(), MediaType.TEXT_HTML, exchange).block(); assertEquals(URI.create("http://url.somewhere.com?foo=bar"), this.exchange.getResponse().getHeaders().getLocation()); }
@Test public void contextRelative() { String url = "/test.html"; RedirectView view = new RedirectView(url); view.render(new HashMap<>(), MediaType.TEXT_HTML, this.exchange).block(); assertEquals(URI.create("/context/test.html"), this.exchange.getResponse().getHeaders().getLocation()); }
@Test public void expandUriTemplateVariablesFromModel() { String url = "http://url.somewhere.com?foo={foo}"; Map<String, String> model = Collections.singletonMap("foo", "bar"); RedirectView view = new RedirectView(url); view.render(model, MediaType.TEXT_HTML, this.exchange).block(); assertEquals(URI.create("http://url.somewhere.com?foo=bar"), this.exchange.getResponse().getHeaders().getLocation()); }
@Test public void contextRelativeQueryParam() { String url = "/test.html?id=1"; RedirectView view = new RedirectView(url); view.render(new HashMap<>(), MediaType.TEXT_HTML, this.exchange).block(); assertEquals(URI.create("/context/test.html?id=1"), this.exchange.getResponse().getHeaders().getLocation()); }
@Test public void location() throws URISyntaxException { URI location = new URI("http://www.example.com/hotels"); headers.setLocation(location); assertEquals("Invalid Location header", location, headers.getLocation()); assertEquals("Invalid Location header", "http://www.example.com/hotels", headers.getFirst("Location")); }
@Override public void onSuccess(ResponseEntity<Void> result) { assertEquals("Invalid location", expected, result.getHeaders().getLocation()); assertFalse(result.hasBody()); } @Override
@Test public void customStatusCode() { String url = "http://url.somewhere.com"; RedirectView view = new RedirectView(url, HttpStatus.FOUND); view.render(new HashMap<>(), MediaType.TEXT_HTML, this.exchange).block(); assertEquals(HttpStatus.FOUND, this.exchange.getResponse().getStatusCode()); assertEquals(URI.create(url), this.exchange.getResponse().getHeaders().getLocation()); }
@Test public void defaultStatusCode() { String url = "http://url.somewhere.com"; RedirectView view = new RedirectView(url); view.render(new HashMap<>(), MediaType.TEXT_HTML, this.exchange).block(); assertEquals(HttpStatus.SEE_OTHER, this.exchange.getResponse().getStatusCode()); assertEquals(URI.create(url), this.exchange.getResponse().getHeaders().getLocation()); }
@Test public void propagateQueryParams() throws Exception { RedirectView view = new RedirectView("http://url.somewhere.com?foo=bar#bazz"); view.setPropagateQuery(true); this.exchange = MockServerWebExchange.from(MockServerHttpRequest.get("http://url.somewhere.com?a=b&c=d")); view.render(new HashMap<>(), MediaType.TEXT_HTML, this.exchange).block(); assertEquals(HttpStatus.SEE_OTHER, this.exchange.getResponse().getStatusCode()); assertEquals(URI.create("http://url.somewhere.com?foo=bar&a=b&c=d#bazz"), this.exchange.getResponse().getHeaders().getLocation()); }
@Test public void created() { URI location = URI.create("http://example.com"); Mono<ServerResponse> result = ServerResponse.created(location).build(); StepVerifier.create(result) .expectNextMatches(response -> HttpStatus.CREATED.equals(response.statusCode()) && location.equals(response.headers().getLocation())) .expectComplete() .verify(); }
@Test public void seeOther() { URI location = URI.create("http://example.com"); Mono<ServerResponse> result = ServerResponse.seeOther(location).build(); StepVerifier.create(result) .expectNextMatches(response -> HttpStatus.SEE_OTHER.equals(response.statusCode()) && location.equals(response.headers().getLocation())) .expectComplete() .verify(); }
@Test public void temporaryRedirect() { URI location = URI.create("http://example.com"); Mono<ServerResponse> result = ServerResponse.temporaryRedirect(location).build(); StepVerifier.create(result) .expectNextMatches(response -> HttpStatus.TEMPORARY_REDIRECT.equals(response.statusCode()) && location.equals(response.headers().getLocation())) .expectComplete() .verify(); }
@Test public void permanentRedirect() { URI location = URI.create("http://example.com"); Mono<ServerResponse> result = ServerResponse.permanentRedirect(location).build(); StepVerifier.create(result) .expectNextMatches(response -> HttpStatus.PERMANENT_REDIRECT.equals(response.statusCode()) && location.equals(response.headers().getLocation())) .expectComplete() .verify(); }
@Test public void created() throws Exception { URI location = new URI("/foo"); DefaultResponseCreator responseCreator = MockRestResponseCreators.withCreatedEntity(location); MockClientHttpResponse response = (MockClientHttpResponse) responseCreator.createResponse(null); assertEquals(HttpStatus.CREATED, response.getStatusCode()); assertEquals(location, response.getHeaders().getLocation()); assertEquals(0, StreamUtils.copyToByteArray(response.getBody()).length); }