Refactor tests to use toExchange() shortcuts
Issue: SPR-15350
This commit is contained in:
@@ -27,13 +27,11 @@ import org.junit.Test;
|
||||
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.mock.http.server.reactive.test.MockServerHttpRequest;
|
||||
import org.springframework.mock.http.server.reactive.test.MockServerHttpResponse;
|
||||
import org.springframework.tests.sample.beans.ITestBean;
|
||||
import org.springframework.tests.sample.beans.TestBean;
|
||||
import org.springframework.util.LinkedMultiValueMap;
|
||||
import org.springframework.util.MultiValueMap;
|
||||
import org.springframework.web.server.ServerWebExchange;
|
||||
import org.springframework.web.server.adapter.DefaultServerWebExchange;
|
||||
|
||||
import static junit.framework.TestCase.assertFalse;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
@@ -173,8 +171,7 @@ public class WebExchangeDataBinderTests {
|
||||
public void testBindingWithQueryParams() throws Exception {
|
||||
String url = "/path?spouse=someValue&spouse.name=test";
|
||||
MockServerHttpRequest request = MockServerHttpRequest.post(url).build();
|
||||
ServerWebExchange exchange = new DefaultServerWebExchange(request, new MockServerHttpResponse());
|
||||
this.binder.bind(exchange).block(Duration.ofMillis(5000));
|
||||
this.binder.bind(request.toExchange()).block(Duration.ofSeconds(5));
|
||||
|
||||
assertNotNull(this.testBean.getSpouse());
|
||||
assertEquals("test", this.testBean.getSpouse().getName());
|
||||
@@ -208,13 +205,11 @@ public class WebExchangeDataBinderTests {
|
||||
}
|
||||
|
||||
private ServerWebExchange exchange(MultiValueMap<String, String> formData) {
|
||||
|
||||
MockServerHttpRequest request = MockServerHttpRequest
|
||||
return MockServerHttpRequest
|
||||
.post("/")
|
||||
.contentType(MediaType.APPLICATION_FORM_URLENCODED)
|
||||
.body(generateForm(formData));
|
||||
|
||||
return new DefaultServerWebExchange(request, new MockServerHttpResponse());
|
||||
.body(generateForm(formData))
|
||||
.toExchange();
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -22,13 +22,19 @@ import org.junit.Test;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.server.reactive.ServerHttpResponse;
|
||||
import org.springframework.mock.http.server.reactive.test.MockServerHttpRequest;
|
||||
import org.springframework.mock.http.server.reactive.test.MockServerHttpResponse;
|
||||
import org.springframework.web.cors.CorsConfiguration;
|
||||
import org.springframework.web.server.adapter.DefaultServerWebExchange;
|
||||
import org.springframework.web.server.ServerWebExchange;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
import static org.springframework.http.HttpHeaders.*;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.springframework.http.HttpHeaders.ACCESS_CONTROL_ALLOW_HEADERS;
|
||||
import static org.springframework.http.HttpHeaders.ACCESS_CONTROL_ALLOW_ORIGIN;
|
||||
import static org.springframework.http.HttpHeaders.ACCESS_CONTROL_REQUEST_HEADERS;
|
||||
import static org.springframework.http.HttpHeaders.ACCESS_CONTROL_REQUEST_METHOD;
|
||||
|
||||
/**
|
||||
* {@link DefaultCorsProcessor} tests with simple or pre-flight CORS request.
|
||||
@@ -37,10 +43,6 @@ import static org.springframework.http.HttpHeaders.*;
|
||||
*/
|
||||
public class DefaultCorsProcessorTests {
|
||||
|
||||
private MockServerHttpRequest request;
|
||||
|
||||
private MockServerHttpResponse response;
|
||||
|
||||
private DefaultCorsProcessor processor;
|
||||
|
||||
private CorsConfiguration conf;
|
||||
@@ -49,297 +51,322 @@ public class DefaultCorsProcessorTests {
|
||||
@Before
|
||||
public void setup() {
|
||||
this.conf = new CorsConfiguration();
|
||||
this.response = new MockServerHttpResponse();
|
||||
this.response.setStatusCode(HttpStatus.OK);
|
||||
this.processor = new DefaultCorsProcessor();
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void actualRequestWithOriginHeader() throws Exception {
|
||||
this.request = actualRequest().build();
|
||||
this.processor.processRequest(this.conf, createExchange());
|
||||
assertFalse(this.response.getHeaders().containsKey(ACCESS_CONTROL_ALLOW_ORIGIN));
|
||||
assertEquals(HttpStatus.FORBIDDEN, this.response.getStatusCode());
|
||||
ServerWebExchange exchange = actualRequest();
|
||||
this.processor.processRequest(this.conf, exchange);
|
||||
|
||||
ServerHttpResponse response = exchange.getResponse();
|
||||
assertFalse(response.getHeaders().containsKey(ACCESS_CONTROL_ALLOW_ORIGIN));
|
||||
assertEquals(HttpStatus.FORBIDDEN, response.getStatusCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void actualRequestWithOriginHeaderAndNullConfig() throws Exception {
|
||||
this.request = actualRequest().build();
|
||||
this.processor.processRequest(null, createExchange());
|
||||
assertFalse(this.response.getHeaders().containsKey(ACCESS_CONTROL_ALLOW_ORIGIN));
|
||||
assertEquals(HttpStatus.OK, this.response.getStatusCode());
|
||||
ServerWebExchange exchange = actualRequest();
|
||||
this.processor.processRequest(null, exchange);
|
||||
|
||||
ServerHttpResponse response = exchange.getResponse();
|
||||
assertFalse(response.getHeaders().containsKey(ACCESS_CONTROL_ALLOW_ORIGIN));
|
||||
assertNull(response.getStatusCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void actualRequestWithOriginHeaderAndAllowedOrigin() throws Exception {
|
||||
this.request = actualRequest().build();
|
||||
ServerWebExchange exchange = actualRequest();
|
||||
this.conf.addAllowedOrigin("*");
|
||||
this.processor.processRequest(this.conf, exchange);
|
||||
|
||||
this.processor.processRequest(this.conf, createExchange());
|
||||
assertTrue(this.response.getHeaders().containsKey(ACCESS_CONTROL_ALLOW_ORIGIN));
|
||||
assertEquals("*", this.response.getHeaders().getFirst(ACCESS_CONTROL_ALLOW_ORIGIN));
|
||||
assertFalse(this.response.getHeaders().containsKey(HttpHeaders.ACCESS_CONTROL_MAX_AGE));
|
||||
assertFalse(this.response.getHeaders().containsKey(HttpHeaders.ACCESS_CONTROL_EXPOSE_HEADERS));
|
||||
assertEquals(HttpStatus.OK, this.response.getStatusCode());
|
||||
ServerHttpResponse response = exchange.getResponse();
|
||||
assertTrue(response.getHeaders().containsKey(ACCESS_CONTROL_ALLOW_ORIGIN));
|
||||
assertEquals("*", response.getHeaders().getFirst(ACCESS_CONTROL_ALLOW_ORIGIN));
|
||||
assertFalse(response.getHeaders().containsKey(HttpHeaders.ACCESS_CONTROL_MAX_AGE));
|
||||
assertFalse(response.getHeaders().containsKey(HttpHeaders.ACCESS_CONTROL_EXPOSE_HEADERS));
|
||||
assertNull(response.getStatusCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void actualRequestCredentials() throws Exception {
|
||||
this.request = actualRequest().build();
|
||||
ServerWebExchange exchange = actualRequest();
|
||||
this.conf.addAllowedOrigin("http://domain1.com");
|
||||
this.conf.addAllowedOrigin("http://domain2.com");
|
||||
this.conf.addAllowedOrigin("http://domain3.com");
|
||||
this.conf.setAllowCredentials(true);
|
||||
this.processor.processRequest(this.conf, exchange);
|
||||
|
||||
this.processor.processRequest(this.conf, createExchange());
|
||||
assertTrue(this.response.getHeaders().containsKey(ACCESS_CONTROL_ALLOW_ORIGIN));
|
||||
assertEquals("http://domain2.com", this.response.getHeaders().getFirst(ACCESS_CONTROL_ALLOW_ORIGIN));
|
||||
assertTrue(this.response.getHeaders().containsKey(HttpHeaders.ACCESS_CONTROL_ALLOW_CREDENTIALS));
|
||||
assertEquals("true", this.response.getHeaders().getFirst(HttpHeaders.ACCESS_CONTROL_ALLOW_CREDENTIALS));
|
||||
assertEquals(HttpStatus.OK, this.response.getStatusCode());
|
||||
ServerHttpResponse response = exchange.getResponse();
|
||||
assertTrue(response.getHeaders().containsKey(ACCESS_CONTROL_ALLOW_ORIGIN));
|
||||
assertEquals("http://domain2.com", response.getHeaders().getFirst(ACCESS_CONTROL_ALLOW_ORIGIN));
|
||||
assertTrue(response.getHeaders().containsKey(HttpHeaders.ACCESS_CONTROL_ALLOW_CREDENTIALS));
|
||||
assertEquals("true", response.getHeaders().getFirst(HttpHeaders.ACCESS_CONTROL_ALLOW_CREDENTIALS));
|
||||
assertNull(response.getStatusCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void actualRequestCredentialsWithOriginWildcard() throws Exception {
|
||||
this.request = actualRequest().build();
|
||||
ServerWebExchange exchange = actualRequest();
|
||||
this.conf.addAllowedOrigin("*");
|
||||
this.conf.setAllowCredentials(true);
|
||||
this.processor.processRequest(this.conf, exchange);
|
||||
|
||||
this.processor.processRequest(this.conf, createExchange());
|
||||
assertTrue(this.response.getHeaders().containsKey(ACCESS_CONTROL_ALLOW_ORIGIN));
|
||||
assertEquals("http://domain2.com", this.response.getHeaders().getFirst(ACCESS_CONTROL_ALLOW_ORIGIN));
|
||||
assertTrue(this.response.getHeaders().containsKey(HttpHeaders.ACCESS_CONTROL_ALLOW_CREDENTIALS));
|
||||
assertEquals("true", this.response.getHeaders().getFirst(HttpHeaders.ACCESS_CONTROL_ALLOW_CREDENTIALS));
|
||||
assertEquals(HttpStatus.OK, this.response.getStatusCode());
|
||||
ServerHttpResponse response = exchange.getResponse();
|
||||
assertTrue(response.getHeaders().containsKey(ACCESS_CONTROL_ALLOW_ORIGIN));
|
||||
assertEquals("http://domain2.com", response.getHeaders().getFirst(ACCESS_CONTROL_ALLOW_ORIGIN));
|
||||
assertTrue(response.getHeaders().containsKey(HttpHeaders.ACCESS_CONTROL_ALLOW_CREDENTIALS));
|
||||
assertEquals("true", response.getHeaders().getFirst(HttpHeaders.ACCESS_CONTROL_ALLOW_CREDENTIALS));
|
||||
assertNull(response.getStatusCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void actualRequestCaseInsensitiveOriginMatch() throws Exception {
|
||||
this.request = actualRequest().build();
|
||||
ServerWebExchange exchange = actualRequest();
|
||||
this.conf.addAllowedOrigin("http://DOMAIN2.com");
|
||||
this.processor.processRequest(this.conf, exchange);
|
||||
|
||||
this.processor.processRequest(this.conf, createExchange());
|
||||
assertTrue(this.response.getHeaders().containsKey(ACCESS_CONTROL_ALLOW_ORIGIN));
|
||||
assertEquals(HttpStatus.OK, this.response.getStatusCode());
|
||||
ServerHttpResponse response = exchange.getResponse();
|
||||
assertTrue(response.getHeaders().containsKey(ACCESS_CONTROL_ALLOW_ORIGIN));
|
||||
assertNull(response.getStatusCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void actualRequestExposedHeaders() throws Exception {
|
||||
this.request = actualRequest().build();
|
||||
ServerWebExchange exchange = actualRequest();
|
||||
this.conf.addExposedHeader("header1");
|
||||
this.conf.addExposedHeader("header2");
|
||||
this.conf.addAllowedOrigin("http://domain2.com");
|
||||
this.processor.processRequest(this.conf, exchange);
|
||||
|
||||
this.processor.processRequest(this.conf, createExchange());
|
||||
assertTrue(this.response.getHeaders().containsKey(ACCESS_CONTROL_ALLOW_ORIGIN));
|
||||
assertEquals("http://domain2.com", this.response.getHeaders().getFirst(ACCESS_CONTROL_ALLOW_ORIGIN));
|
||||
assertTrue(this.response.getHeaders().containsKey(HttpHeaders.ACCESS_CONTROL_EXPOSE_HEADERS));
|
||||
assertTrue(this.response.getHeaders().getFirst(HttpHeaders.ACCESS_CONTROL_EXPOSE_HEADERS).contains("header1"));
|
||||
assertTrue(this.response.getHeaders().getFirst(HttpHeaders.ACCESS_CONTROL_EXPOSE_HEADERS).contains("header2"));
|
||||
assertEquals(HttpStatus.OK, this.response.getStatusCode());
|
||||
ServerHttpResponse response = exchange.getResponse();
|
||||
assertTrue(response.getHeaders().containsKey(ACCESS_CONTROL_ALLOW_ORIGIN));
|
||||
assertEquals("http://domain2.com", response.getHeaders().getFirst(ACCESS_CONTROL_ALLOW_ORIGIN));
|
||||
assertTrue(response.getHeaders().containsKey(HttpHeaders.ACCESS_CONTROL_EXPOSE_HEADERS));
|
||||
assertTrue(response.getHeaders().getFirst(HttpHeaders.ACCESS_CONTROL_EXPOSE_HEADERS).contains("header1"));
|
||||
assertTrue(response.getHeaders().getFirst(HttpHeaders.ACCESS_CONTROL_EXPOSE_HEADERS).contains("header2"));
|
||||
assertNull(response.getStatusCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void preflightRequestAllOriginsAllowed() throws Exception {
|
||||
this.request = preFlightRequest().header(ACCESS_CONTROL_REQUEST_METHOD, "GET").build();
|
||||
ServerWebExchange exchange = preFlightRequest().header(ACCESS_CONTROL_REQUEST_METHOD, "GET").toExchange();
|
||||
this.conf.addAllowedOrigin("*");
|
||||
this.processor.processRequest(this.conf, exchange);
|
||||
|
||||
this.processor.processRequest(this.conf, createExchange());
|
||||
assertEquals(HttpStatus.OK, this.response.getStatusCode());
|
||||
assertNull(exchange.getResponse().getStatusCode());
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void preflightRequestWrongAllowedMethod() throws Exception {
|
||||
this.request = preFlightRequest().header(ACCESS_CONTROL_REQUEST_METHOD, "DELETE").build();
|
||||
ServerWebExchange exchange = preFlightRequest().header(ACCESS_CONTROL_REQUEST_METHOD, "DELETE").toExchange();
|
||||
this.conf.addAllowedOrigin("*");
|
||||
this.processor.processRequest(this.conf, exchange);
|
||||
|
||||
this.processor.processRequest(this.conf, createExchange());
|
||||
assertEquals(HttpStatus.FORBIDDEN, this.response.getStatusCode());
|
||||
assertEquals(HttpStatus.FORBIDDEN, exchange.getResponse().getStatusCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void preflightRequestMatchedAllowedMethod() throws Exception {
|
||||
this.request = preFlightRequest().header(ACCESS_CONTROL_REQUEST_METHOD, "GET").build();
|
||||
ServerWebExchange exchange = preFlightRequest().header(ACCESS_CONTROL_REQUEST_METHOD, "GET").toExchange();
|
||||
this.conf.addAllowedOrigin("*");
|
||||
this.processor.processRequest(this.conf, exchange);
|
||||
|
||||
this.processor.processRequest(this.conf, createExchange());
|
||||
assertEquals(HttpStatus.OK, this.response.getStatusCode());
|
||||
assertEquals("GET,HEAD", this.response.getHeaders().getFirst(HttpHeaders.ACCESS_CONTROL_ALLOW_METHODS));
|
||||
ServerHttpResponse response = exchange.getResponse();
|
||||
assertNull(response.getStatusCode());
|
||||
assertEquals("GET,HEAD", response.getHeaders().getFirst(HttpHeaders.ACCESS_CONTROL_ALLOW_METHODS));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void preflightRequestTestWithOriginButWithoutOtherHeaders() throws Exception {
|
||||
this.request = preFlightRequest().build();
|
||||
ServerWebExchange exchange = preFlightRequest().toExchange();
|
||||
this.processor.processRequest(this.conf, exchange);
|
||||
|
||||
this.processor.processRequest(this.conf, createExchange());
|
||||
assertFalse(this.response.getHeaders().containsKey(ACCESS_CONTROL_ALLOW_ORIGIN));
|
||||
assertEquals(HttpStatus.FORBIDDEN, this.response.getStatusCode());
|
||||
ServerHttpResponse response = exchange.getResponse();
|
||||
assertFalse(response.getHeaders().containsKey(ACCESS_CONTROL_ALLOW_ORIGIN));
|
||||
assertEquals(HttpStatus.FORBIDDEN, response.getStatusCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void preflightRequestWithoutRequestMethod() throws Exception {
|
||||
this.request = preFlightRequest().header(ACCESS_CONTROL_REQUEST_HEADERS, "Header1").build();
|
||||
ServerWebExchange exchange = preFlightRequest().header(ACCESS_CONTROL_REQUEST_HEADERS, "Header1").toExchange();
|
||||
this.processor.processRequest(this.conf, exchange);
|
||||
|
||||
this.processor.processRequest(this.conf, createExchange());
|
||||
assertFalse(this.response.getHeaders().containsKey(ACCESS_CONTROL_ALLOW_ORIGIN));
|
||||
assertEquals(HttpStatus.FORBIDDEN, this.response.getStatusCode());
|
||||
ServerHttpResponse response = exchange.getResponse();
|
||||
assertFalse(response.getHeaders().containsKey(ACCESS_CONTROL_ALLOW_ORIGIN));
|
||||
assertEquals(HttpStatus.FORBIDDEN, response.getStatusCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void preflightRequestWithRequestAndMethodHeaderButNoConfig() throws Exception {
|
||||
this.request = preFlightRequest()
|
||||
ServerWebExchange exchange = preFlightRequest()
|
||||
.header(ACCESS_CONTROL_REQUEST_METHOD, "GET")
|
||||
.header(ACCESS_CONTROL_REQUEST_HEADERS, "Header1")
|
||||
.build();
|
||||
.toExchange();
|
||||
|
||||
this.processor.processRequest(this.conf, createExchange());
|
||||
assertFalse(this.response.getHeaders().containsKey(ACCESS_CONTROL_ALLOW_ORIGIN));
|
||||
assertEquals(HttpStatus.FORBIDDEN, this.response.getStatusCode());
|
||||
this.processor.processRequest(this.conf, exchange);
|
||||
|
||||
ServerHttpResponse response = exchange.getResponse();
|
||||
assertFalse(response.getHeaders().containsKey(ACCESS_CONTROL_ALLOW_ORIGIN));
|
||||
assertEquals(HttpStatus.FORBIDDEN, response.getStatusCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void preflightRequestValidRequestAndConfig() throws Exception {
|
||||
this.request = preFlightRequest()
|
||||
ServerWebExchange exchange = preFlightRequest()
|
||||
.header(ACCESS_CONTROL_REQUEST_METHOD, "GET")
|
||||
.header(ACCESS_CONTROL_REQUEST_HEADERS, "Header1")
|
||||
.build();
|
||||
.toExchange();
|
||||
|
||||
this.conf.addAllowedOrigin("*");
|
||||
this.conf.addAllowedMethod("GET");
|
||||
this.conf.addAllowedMethod("PUT");
|
||||
this.conf.addAllowedHeader("header1");
|
||||
this.conf.addAllowedHeader("header2");
|
||||
|
||||
this.processor.processRequest(this.conf, createExchange());
|
||||
assertTrue(this.response.getHeaders().containsKey(ACCESS_CONTROL_ALLOW_ORIGIN));
|
||||
assertEquals("*", this.response.getHeaders().getFirst(ACCESS_CONTROL_ALLOW_ORIGIN));
|
||||
assertTrue(this.response.getHeaders().containsKey(HttpHeaders.ACCESS_CONTROL_ALLOW_METHODS));
|
||||
assertEquals("GET,PUT", this.response.getHeaders().getFirst(HttpHeaders.ACCESS_CONTROL_ALLOW_METHODS));
|
||||
assertFalse(this.response.getHeaders().containsKey(HttpHeaders.ACCESS_CONTROL_MAX_AGE));
|
||||
assertEquals(HttpStatus.OK, this.response.getStatusCode());
|
||||
this.processor.processRequest(this.conf, exchange);
|
||||
|
||||
ServerHttpResponse response = exchange.getResponse();
|
||||
assertTrue(response.getHeaders().containsKey(ACCESS_CONTROL_ALLOW_ORIGIN));
|
||||
assertEquals("*", response.getHeaders().getFirst(ACCESS_CONTROL_ALLOW_ORIGIN));
|
||||
assertTrue(response.getHeaders().containsKey(HttpHeaders.ACCESS_CONTROL_ALLOW_METHODS));
|
||||
assertEquals("GET,PUT", response.getHeaders().getFirst(HttpHeaders.ACCESS_CONTROL_ALLOW_METHODS));
|
||||
assertFalse(response.getHeaders().containsKey(HttpHeaders.ACCESS_CONTROL_MAX_AGE));
|
||||
assertNull(response.getStatusCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void preflightRequestCredentials() throws Exception {
|
||||
this.request = preFlightRequest()
|
||||
ServerWebExchange exchange = preFlightRequest()
|
||||
.header(ACCESS_CONTROL_REQUEST_METHOD, "GET")
|
||||
.header(ACCESS_CONTROL_REQUEST_HEADERS, "Header1")
|
||||
.build();
|
||||
.toExchange();
|
||||
|
||||
this.conf.addAllowedOrigin("http://domain1.com");
|
||||
this.conf.addAllowedOrigin("http://domain2.com");
|
||||
this.conf.addAllowedOrigin("http://domain3.com");
|
||||
this.conf.addAllowedHeader("Header1");
|
||||
this.conf.setAllowCredentials(true);
|
||||
|
||||
this.processor.processRequest(this.conf, createExchange());
|
||||
assertTrue(this.response.getHeaders().containsKey(ACCESS_CONTROL_ALLOW_ORIGIN));
|
||||
assertEquals("http://domain2.com", this.response.getHeaders().getFirst(ACCESS_CONTROL_ALLOW_ORIGIN));
|
||||
assertTrue(this.response.getHeaders().containsKey(HttpHeaders.ACCESS_CONTROL_ALLOW_CREDENTIALS));
|
||||
assertEquals("true", this.response.getHeaders().getFirst(HttpHeaders.ACCESS_CONTROL_ALLOW_CREDENTIALS));
|
||||
assertEquals(HttpStatus.OK, this.response.getStatusCode());
|
||||
this.processor.processRequest(this.conf, exchange);
|
||||
|
||||
ServerHttpResponse response = exchange.getResponse();
|
||||
assertTrue(response.getHeaders().containsKey(ACCESS_CONTROL_ALLOW_ORIGIN));
|
||||
assertEquals("http://domain2.com", response.getHeaders().getFirst(ACCESS_CONTROL_ALLOW_ORIGIN));
|
||||
assertTrue(response.getHeaders().containsKey(HttpHeaders.ACCESS_CONTROL_ALLOW_CREDENTIALS));
|
||||
assertEquals("true", response.getHeaders().getFirst(HttpHeaders.ACCESS_CONTROL_ALLOW_CREDENTIALS));
|
||||
assertNull(response.getStatusCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void preflightRequestCredentialsWithOriginWildcard() throws Exception {
|
||||
this.request = preFlightRequest()
|
||||
ServerWebExchange exchange = preFlightRequest()
|
||||
.header(ACCESS_CONTROL_REQUEST_METHOD, "GET")
|
||||
.header(ACCESS_CONTROL_REQUEST_HEADERS, "Header1")
|
||||
.build();
|
||||
.toExchange();
|
||||
|
||||
this.conf.addAllowedOrigin("http://domain1.com");
|
||||
this.conf.addAllowedOrigin("*");
|
||||
this.conf.addAllowedOrigin("http://domain3.com");
|
||||
this.conf.addAllowedHeader("Header1");
|
||||
this.conf.setAllowCredentials(true);
|
||||
|
||||
this.processor.processRequest(this.conf, createExchange());
|
||||
assertTrue(this.response.getHeaders().containsKey(ACCESS_CONTROL_ALLOW_ORIGIN));
|
||||
assertEquals("http://domain2.com", this.response.getHeaders().getFirst(ACCESS_CONTROL_ALLOW_ORIGIN));
|
||||
assertEquals(HttpStatus.OK, this.response.getStatusCode());
|
||||
this.processor.processRequest(this.conf, exchange);
|
||||
|
||||
ServerHttpResponse response = exchange.getResponse();
|
||||
assertTrue(response.getHeaders().containsKey(ACCESS_CONTROL_ALLOW_ORIGIN));
|
||||
assertEquals("http://domain2.com", response.getHeaders().getFirst(ACCESS_CONTROL_ALLOW_ORIGIN));
|
||||
assertNull(response.getStatusCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void preflightRequestAllowedHeaders() throws Exception {
|
||||
this.request = preFlightRequest()
|
||||
ServerWebExchange exchange = preFlightRequest()
|
||||
.header(ACCESS_CONTROL_REQUEST_METHOD, "GET")
|
||||
.header(ACCESS_CONTROL_REQUEST_HEADERS, "Header1, Header2")
|
||||
.build();
|
||||
.toExchange();
|
||||
|
||||
this.conf.addAllowedHeader("Header1");
|
||||
this.conf.addAllowedHeader("Header2");
|
||||
this.conf.addAllowedHeader("Header3");
|
||||
this.conf.addAllowedOrigin("http://domain2.com");
|
||||
|
||||
this.processor.processRequest(this.conf, createExchange());
|
||||
assertTrue(this.response.getHeaders().containsKey(ACCESS_CONTROL_ALLOW_ORIGIN));
|
||||
assertTrue(this.response.getHeaders().containsKey(ACCESS_CONTROL_ALLOW_HEADERS));
|
||||
assertTrue(this.response.getHeaders().getFirst(ACCESS_CONTROL_ALLOW_HEADERS).contains("Header1"));
|
||||
assertTrue(this.response.getHeaders().getFirst(ACCESS_CONTROL_ALLOW_HEADERS).contains("Header2"));
|
||||
assertFalse(this.response.getHeaders().getFirst(ACCESS_CONTROL_ALLOW_HEADERS).contains("Header3"));
|
||||
assertEquals(HttpStatus.OK, this.response.getStatusCode());
|
||||
this.processor.processRequest(this.conf, exchange);
|
||||
|
||||
ServerHttpResponse response = exchange.getResponse();
|
||||
assertTrue(response.getHeaders().containsKey(ACCESS_CONTROL_ALLOW_ORIGIN));
|
||||
assertTrue(response.getHeaders().containsKey(ACCESS_CONTROL_ALLOW_HEADERS));
|
||||
assertTrue(response.getHeaders().getFirst(ACCESS_CONTROL_ALLOW_HEADERS).contains("Header1"));
|
||||
assertTrue(response.getHeaders().getFirst(ACCESS_CONTROL_ALLOW_HEADERS).contains("Header2"));
|
||||
assertFalse(response.getHeaders().getFirst(ACCESS_CONTROL_ALLOW_HEADERS).contains("Header3"));
|
||||
assertNull(response.getStatusCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void preflightRequestAllowsAllHeaders() throws Exception {
|
||||
this.request = preFlightRequest()
|
||||
ServerWebExchange exchange = preFlightRequest()
|
||||
.header(ACCESS_CONTROL_REQUEST_METHOD, "GET")
|
||||
.header(ACCESS_CONTROL_REQUEST_HEADERS, "Header1, Header2")
|
||||
.build();
|
||||
.toExchange();
|
||||
|
||||
this.conf.addAllowedHeader("*");
|
||||
this.conf.addAllowedOrigin("http://domain2.com");
|
||||
|
||||
this.processor.processRequest(this.conf, createExchange());
|
||||
assertTrue(this.response.getHeaders().containsKey(ACCESS_CONTROL_ALLOW_ORIGIN));
|
||||
assertTrue(this.response.getHeaders().containsKey(ACCESS_CONTROL_ALLOW_HEADERS));
|
||||
assertTrue(this.response.getHeaders().getFirst(ACCESS_CONTROL_ALLOW_HEADERS).contains("Header1"));
|
||||
assertTrue(this.response.getHeaders().getFirst(ACCESS_CONTROL_ALLOW_HEADERS).contains("Header2"));
|
||||
assertFalse(this.response.getHeaders().getFirst(ACCESS_CONTROL_ALLOW_HEADERS).contains("*"));
|
||||
assertEquals(HttpStatus.OK, this.response.getStatusCode());
|
||||
this.processor.processRequest(this.conf, exchange);
|
||||
|
||||
ServerHttpResponse response = exchange.getResponse();
|
||||
assertTrue(response.getHeaders().containsKey(ACCESS_CONTROL_ALLOW_ORIGIN));
|
||||
assertTrue(response.getHeaders().containsKey(ACCESS_CONTROL_ALLOW_HEADERS));
|
||||
assertTrue(response.getHeaders().getFirst(ACCESS_CONTROL_ALLOW_HEADERS).contains("Header1"));
|
||||
assertTrue(response.getHeaders().getFirst(ACCESS_CONTROL_ALLOW_HEADERS).contains("Header2"));
|
||||
assertFalse(response.getHeaders().getFirst(ACCESS_CONTROL_ALLOW_HEADERS).contains("*"));
|
||||
assertNull(response.getStatusCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void preflightRequestWithEmptyHeaders() throws Exception {
|
||||
this.request = preFlightRequest()
|
||||
ServerWebExchange exchange = preFlightRequest()
|
||||
.header(ACCESS_CONTROL_REQUEST_METHOD, "GET")
|
||||
.header(ACCESS_CONTROL_REQUEST_HEADERS, "")
|
||||
.build();
|
||||
.toExchange();
|
||||
|
||||
this.conf.addAllowedHeader("*");
|
||||
this.conf.addAllowedOrigin("http://domain2.com");
|
||||
|
||||
this.processor.processRequest(this.conf, createExchange());
|
||||
assertTrue(this.response.getHeaders().containsKey(ACCESS_CONTROL_ALLOW_ORIGIN));
|
||||
assertFalse(this.response.getHeaders().containsKey(ACCESS_CONTROL_ALLOW_HEADERS));
|
||||
assertEquals(HttpStatus.OK, this.response.getStatusCode());
|
||||
this.processor.processRequest(this.conf, exchange);
|
||||
|
||||
ServerHttpResponse response = exchange.getResponse();
|
||||
assertTrue(response.getHeaders().containsKey(ACCESS_CONTROL_ALLOW_ORIGIN));
|
||||
assertFalse(response.getHeaders().containsKey(ACCESS_CONTROL_ALLOW_HEADERS));
|
||||
assertNull(response.getStatusCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void preflightRequestWithNullConfig() throws Exception {
|
||||
this.request = preFlightRequest()
|
||||
.header(ACCESS_CONTROL_REQUEST_METHOD, "GET")
|
||||
.build();
|
||||
ServerWebExchange exchange = preFlightRequest().header(ACCESS_CONTROL_REQUEST_METHOD, "GET").toExchange();
|
||||
this.conf.addAllowedOrigin("*");
|
||||
this.processor.processRequest(null, exchange);
|
||||
|
||||
this.processor.processRequest(null, createExchange());
|
||||
assertFalse(this.response.getHeaders().containsKey(ACCESS_CONTROL_ALLOW_ORIGIN));
|
||||
assertEquals(HttpStatus.FORBIDDEN, this.response.getStatusCode());
|
||||
ServerHttpResponse response = exchange.getResponse();
|
||||
assertFalse(response.getHeaders().containsKey(ACCESS_CONTROL_ALLOW_ORIGIN));
|
||||
assertEquals(HttpStatus.FORBIDDEN, response.getStatusCode());
|
||||
}
|
||||
|
||||
|
||||
private MockServerHttpRequest.BaseBuilder<?> actualRequest() {
|
||||
return corsRequest(HttpMethod.GET);
|
||||
private ServerWebExchange actualRequest() {
|
||||
return corsRequest(HttpMethod.GET).toExchange();
|
||||
}
|
||||
|
||||
private MockServerHttpRequest.BaseBuilder<?> preFlightRequest() {
|
||||
return corsRequest(HttpMethod.OPTIONS);
|
||||
}
|
||||
|
||||
private MockServerHttpRequest.BaseBuilder<?> corsRequest(HttpMethod httpMethod) {
|
||||
private MockServerHttpRequest.BaseBuilder<?> corsRequest(HttpMethod method) {
|
||||
return MockServerHttpRequest
|
||||
.method(httpMethod, "http://localhost/test.html")
|
||||
.method(method, "http://localhost/test.html")
|
||||
.header(HttpHeaders.ORIGIN, "http://domain2.com");
|
||||
}
|
||||
|
||||
private DefaultServerWebExchange createExchange() {
|
||||
return new DefaultServerWebExchange(this.request, this.response);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -18,13 +18,9 @@ package org.springframework.web.cors.reactive;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.http.server.reactive.ServerHttpRequest;
|
||||
import org.springframework.mock.http.server.reactive.test.MockServerHttpRequest;
|
||||
import org.springframework.mock.http.server.reactive.test.MockServerHttpResponse;
|
||||
import org.springframework.web.cors.CorsConfiguration;
|
||||
import org.springframework.web.server.ServerWebExchange;
|
||||
import org.springframework.web.server.adapter.DefaultServerWebExchange;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNull;
|
||||
@@ -42,7 +38,7 @@ public class UrlBasedCorsConfigurationSourceTests {
|
||||
|
||||
@Test
|
||||
public void empty() {
|
||||
ServerWebExchange exchange = createExchange(HttpMethod.GET, "/bar/test.html");
|
||||
ServerWebExchange exchange = MockServerHttpRequest.get("/bar/test.html").toExchange();
|
||||
assertNull(this.configSource.getCorsConfiguration(exchange));
|
||||
}
|
||||
|
||||
@@ -51,10 +47,10 @@ public class UrlBasedCorsConfigurationSourceTests {
|
||||
CorsConfiguration config = new CorsConfiguration();
|
||||
this.configSource.registerCorsConfiguration("/bar/**", config);
|
||||
|
||||
ServerWebExchange exchange = createExchange(HttpMethod.GET, "/foo/test.html");
|
||||
ServerWebExchange exchange = MockServerHttpRequest.get("/foo/test.html").toExchange();
|
||||
assertNull(this.configSource.getCorsConfiguration(exchange));
|
||||
|
||||
exchange = createExchange(HttpMethod.GET, "/bar/test.html");
|
||||
exchange = MockServerHttpRequest.get("/bar/test.html").toExchange();
|
||||
assertEquals(config, this.configSource.getCorsConfiguration(exchange));
|
||||
}
|
||||
|
||||
@@ -63,10 +59,4 @@ public class UrlBasedCorsConfigurationSourceTests {
|
||||
this.configSource.getCorsConfigurations().put("/**", new CorsConfiguration());
|
||||
}
|
||||
|
||||
private ServerWebExchange createExchange(HttpMethod httpMethod, String url) {
|
||||
ServerHttpRequest request = MockServerHttpRequest.method(httpMethod, url).build();
|
||||
MockServerHttpResponse response = new MockServerHttpResponse();
|
||||
return new DefaultServerWebExchange(request, response);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -27,10 +27,8 @@ import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.mock.http.server.reactive.test.MockServerHttpRequest;
|
||||
import org.springframework.mock.http.server.reactive.test.MockServerHttpResponse;
|
||||
import org.springframework.web.server.ServerWebExchange;
|
||||
import org.springframework.web.server.WebFilterChain;
|
||||
import org.springframework.web.server.adapter.DefaultServerWebExchange;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertThat;
|
||||
@@ -145,9 +143,7 @@ public class HiddenHttpMethodFilterTests {
|
||||
.map(method -> builder.body(methodName + "=" + method))
|
||||
.orElse(builder.build());
|
||||
|
||||
MockServerHttpResponse response = new MockServerHttpResponse();
|
||||
|
||||
return new DefaultServerWebExchange(request, response);
|
||||
return request.toExchange();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -34,9 +34,13 @@ import org.junit.runners.Parameterized.Parameters;
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.mock.http.server.reactive.test.MockServerHttpRequest;
|
||||
import org.springframework.mock.http.server.reactive.test.MockServerHttpResponse;
|
||||
import org.springframework.mock.http.server.reactive.test.MockServerWebExchange;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.springframework.mock.http.server.reactive.test.MockServerHttpRequest.get;
|
||||
|
||||
/**
|
||||
* "checkNotModified" unit tests for {@link DefaultServerWebExchange}.
|
||||
@@ -51,10 +55,6 @@ public class DefaultServerWebExchangeCheckNotModifiedTests {
|
||||
|
||||
private SimpleDateFormat dateFormat;
|
||||
|
||||
private MockServerHttpRequest request;
|
||||
|
||||
private MockServerHttpResponse response = new MockServerHttpResponse();
|
||||
|
||||
private Instant currentDate;
|
||||
|
||||
@Parameter
|
||||
@@ -79,75 +79,75 @@ public class DefaultServerWebExchangeCheckNotModifiedTests {
|
||||
|
||||
@Test
|
||||
public void checkNotModifiedNon2xxStatus() {
|
||||
this.request = request().ifModifiedSince(this.currentDate.toEpochMilli()).build();
|
||||
this.response.setStatusCode(HttpStatus.NOT_MODIFIED);
|
||||
MockServerWebExchange exchange = get("/").ifModifiedSince(this.currentDate.toEpochMilli()).toExchange();
|
||||
exchange.getResponse().setStatusCode(HttpStatus.NOT_MODIFIED);
|
||||
|
||||
assertFalse(createExchange().checkNotModified(this.currentDate));
|
||||
assertEquals(304, response.getStatusCode().value());
|
||||
assertEquals(-1, response.getHeaders().getLastModified());
|
||||
assertFalse(exchange.checkNotModified(this.currentDate));
|
||||
assertEquals(304, exchange.getResponse().getStatusCode().value());
|
||||
assertEquals(-1, exchange.getResponse().getHeaders().getLastModified());
|
||||
}
|
||||
|
||||
@Test // SPR-14559
|
||||
public void checkNotModifiedInvalidIfNoneMatchHeader() {
|
||||
String eTag = "\"etagvalue\"";
|
||||
this.request = request().ifNoneMatch("missingquotes").build();
|
||||
assertFalse(createExchange().checkNotModified(eTag));
|
||||
assertNull(response.getStatusCode());
|
||||
assertEquals(eTag, response.getHeaders().getETag());
|
||||
MockServerWebExchange exchange = get("/").ifNoneMatch("missingquotes").toExchange();
|
||||
assertFalse(exchange.checkNotModified(eTag));
|
||||
assertNull(exchange.getResponse().getStatusCode());
|
||||
assertEquals(eTag, exchange.getResponse().getHeaders().getETag());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void checkNotModifiedHeaderAlreadySet() {
|
||||
this.request = request().ifModifiedSince(currentDate.toEpochMilli()).build();
|
||||
this.response.getHeaders().add("Last-Modified", CURRENT_TIME);
|
||||
MockServerWebExchange exchange = get("/").ifModifiedSince(currentDate.toEpochMilli()).toExchange();
|
||||
exchange.getResponse().getHeaders().add("Last-Modified", CURRENT_TIME);
|
||||
|
||||
assertTrue(createExchange().checkNotModified(currentDate));
|
||||
assertEquals(304, response.getStatusCode().value());
|
||||
assertEquals(1, response.getHeaders().get("Last-Modified").size());
|
||||
assertEquals(CURRENT_TIME, response.getHeaders().getFirst("Last-Modified"));
|
||||
assertTrue(exchange.checkNotModified(currentDate));
|
||||
assertEquals(304, exchange.getResponse().getStatusCode().value());
|
||||
assertEquals(1, exchange.getResponse().getHeaders().get("Last-Modified").size());
|
||||
assertEquals(CURRENT_TIME, exchange.getResponse().getHeaders().getFirst("Last-Modified"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void checkNotModifiedTimestamp() throws Exception {
|
||||
this.request = request().ifModifiedSince(currentDate.toEpochMilli()).build();
|
||||
MockServerWebExchange exchange = get("/").ifModifiedSince(currentDate.toEpochMilli()).toExchange();
|
||||
|
||||
assertTrue(createExchange().checkNotModified(currentDate));
|
||||
assertTrue(exchange.checkNotModified(currentDate));
|
||||
|
||||
assertEquals(304, response.getStatusCode().value());
|
||||
assertEquals(currentDate.toEpochMilli(), response.getHeaders().getLastModified());
|
||||
assertEquals(304, exchange.getResponse().getStatusCode().value());
|
||||
assertEquals(currentDate.toEpochMilli(), exchange.getResponse().getHeaders().getLastModified());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void checkModifiedTimestamp() {
|
||||
Instant oneMinuteAgo = currentDate.minusSeconds(60);
|
||||
this.request = request().ifModifiedSince(oneMinuteAgo.toEpochMilli()).build();
|
||||
MockServerWebExchange exchange = get("/").ifModifiedSince(oneMinuteAgo.toEpochMilli()).toExchange();
|
||||
|
||||
assertFalse(createExchange().checkNotModified(currentDate));
|
||||
assertFalse(exchange.checkNotModified(currentDate));
|
||||
|
||||
assertNull(response.getStatusCode());
|
||||
assertEquals(currentDate.toEpochMilli(), response.getHeaders().getLastModified());
|
||||
assertNull(exchange.getResponse().getStatusCode());
|
||||
assertEquals(currentDate.toEpochMilli(), exchange.getResponse().getHeaders().getLastModified());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void checkNotModifiedETag() {
|
||||
String eTag = "\"Foo\"";
|
||||
this.request = request().ifNoneMatch(eTag).build();
|
||||
MockServerWebExchange exchange = get("/").ifNoneMatch(eTag).toExchange();
|
||||
|
||||
assertTrue(createExchange().checkNotModified(eTag));
|
||||
assertTrue(exchange.checkNotModified(eTag));
|
||||
|
||||
assertEquals(304, response.getStatusCode().value());
|
||||
assertEquals(eTag, response.getHeaders().getETag());
|
||||
assertEquals(304, exchange.getResponse().getStatusCode().value());
|
||||
assertEquals(eTag, exchange.getResponse().getHeaders().getETag());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void checkNotModifiedETagWithSeparatorChars() {
|
||||
String eTag = "\"Foo, Bar\"";
|
||||
this.request = request().ifNoneMatch(eTag).build();
|
||||
MockServerWebExchange exchange = get("/").ifNoneMatch(eTag).toExchange();
|
||||
|
||||
assertTrue(createExchange().checkNotModified(eTag));
|
||||
assertTrue(exchange.checkNotModified(eTag));
|
||||
|
||||
assertEquals(304, response.getStatusCode().value());
|
||||
assertEquals(eTag, response.getHeaders().getETag());
|
||||
assertEquals(304, exchange.getResponse().getStatusCode().value());
|
||||
assertEquals(eTag, exchange.getResponse().getHeaders().getETag());
|
||||
}
|
||||
|
||||
|
||||
@@ -155,58 +155,59 @@ public class DefaultServerWebExchangeCheckNotModifiedTests {
|
||||
public void checkModifiedETag() {
|
||||
String currentETag = "\"Foo\"";
|
||||
String oldEtag = "Bar";
|
||||
this.request = request().ifNoneMatch(oldEtag).build();
|
||||
MockServerWebExchange exchange = get("/").ifNoneMatch(oldEtag).toExchange();
|
||||
|
||||
assertFalse(createExchange().checkNotModified(currentETag));
|
||||
assertFalse(exchange.checkNotModified(currentETag));
|
||||
|
||||
assertNull(response.getStatusCode());
|
||||
assertEquals(currentETag, response.getHeaders().getETag());
|
||||
assertNull(exchange.getResponse().getStatusCode());
|
||||
assertEquals(currentETag, exchange.getResponse().getHeaders().getETag());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void checkNotModifiedUnpaddedETag() {
|
||||
String eTag = "Foo";
|
||||
String paddedEtag = String.format("\"%s\"", eTag);
|
||||
this.request = request().ifNoneMatch(paddedEtag).build();
|
||||
MockServerWebExchange exchange = get("/").ifNoneMatch(paddedEtag).toExchange();
|
||||
|
||||
assertTrue(createExchange().checkNotModified(eTag));
|
||||
assertTrue(exchange.checkNotModified(eTag));
|
||||
|
||||
assertEquals(304, response.getStatusCode().value());
|
||||
assertEquals(paddedEtag, response.getHeaders().getETag());
|
||||
assertEquals(304, exchange.getResponse().getStatusCode().value());
|
||||
assertEquals(paddedEtag, exchange.getResponse().getHeaders().getETag());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void checkModifiedUnpaddedETag() {
|
||||
String currentETag = "Foo";
|
||||
String oldEtag = "Bar";
|
||||
this.request = request().ifNoneMatch(oldEtag).build();
|
||||
MockServerWebExchange exchange = get("/").ifNoneMatch(oldEtag).toExchange();
|
||||
|
||||
assertFalse(createExchange().checkNotModified(currentETag));
|
||||
assertFalse(exchange.checkNotModified(currentETag));
|
||||
|
||||
assertNull(response.getStatusCode());
|
||||
assertEquals(String.format("\"%s\"", currentETag), response.getHeaders().getETag());
|
||||
assertNull(exchange.getResponse().getStatusCode());
|
||||
assertEquals(String.format("\"%s\"", currentETag), exchange.getResponse().getHeaders().getETag());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void checkNotModifiedWildcardIsIgnored() {
|
||||
String eTag = "\"Foo\"";
|
||||
this.request = request().ifNoneMatch("*").build();
|
||||
assertFalse(createExchange().checkNotModified(eTag));
|
||||
MockServerWebExchange exchange = get("/").ifNoneMatch("*").toExchange();
|
||||
assertFalse(exchange.checkNotModified(eTag));
|
||||
|
||||
assertNull(response.getStatusCode());
|
||||
assertEquals(eTag, response.getHeaders().getETag());
|
||||
assertNull(exchange.getResponse().getStatusCode());
|
||||
assertEquals(eTag, exchange.getResponse().getHeaders().getETag());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void checkNotModifiedETagAndTimestamp() {
|
||||
String eTag = "\"Foo\"";
|
||||
this.request = request().ifNoneMatch(eTag).ifModifiedSince(currentDate.toEpochMilli()).build();
|
||||
long time = currentDate.toEpochMilli();
|
||||
MockServerWebExchange exchange = get("/").ifNoneMatch(eTag).ifModifiedSince(time).toExchange();
|
||||
|
||||
assertTrue(createExchange().checkNotModified(eTag, currentDate));
|
||||
assertTrue(exchange.checkNotModified(eTag, currentDate));
|
||||
|
||||
assertEquals(304, response.getStatusCode().value());
|
||||
assertEquals(eTag, response.getHeaders().getETag());
|
||||
assertEquals(currentDate.toEpochMilli(), response.getHeaders().getLastModified());
|
||||
assertEquals(304, exchange.getResponse().getStatusCode().value());
|
||||
assertEquals(eTag, exchange.getResponse().getHeaders().getETag());
|
||||
assertEquals(time, exchange.getResponse().getHeaders().getLastModified());
|
||||
}
|
||||
|
||||
// SPR-14224
|
||||
@@ -214,114 +215,111 @@ public class DefaultServerWebExchangeCheckNotModifiedTests {
|
||||
public void checkNotModifiedETagAndModifiedTimestamp() {
|
||||
String eTag = "\"Foo\"";
|
||||
Instant oneMinuteAgo = currentDate.minusSeconds(60);
|
||||
this.request = request().ifNoneMatch(eTag).ifModifiedSince(oneMinuteAgo.toEpochMilli()).build();
|
||||
MockServerWebExchange exchange = get("/")
|
||||
.ifNoneMatch(eTag)
|
||||
.ifModifiedSince(oneMinuteAgo.toEpochMilli())
|
||||
.toExchange();
|
||||
|
||||
assertTrue(createExchange().checkNotModified(eTag, currentDate));
|
||||
assertTrue(exchange.checkNotModified(eTag, currentDate));
|
||||
|
||||
assertEquals(304, response.getStatusCode().value());
|
||||
assertEquals(eTag, response.getHeaders().getETag());
|
||||
assertEquals(currentDate.toEpochMilli(), response.getHeaders().getLastModified());
|
||||
assertEquals(304, exchange.getResponse().getStatusCode().value());
|
||||
assertEquals(eTag, exchange.getResponse().getHeaders().getETag());
|
||||
assertEquals(currentDate.toEpochMilli(), exchange.getResponse().getHeaders().getLastModified());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void checkModifiedETagAndNotModifiedTimestamp() throws Exception {
|
||||
String currentETag = "\"Foo\"";
|
||||
String oldEtag = "\"Bar\"";
|
||||
this.request = request().ifNoneMatch(oldEtag).ifModifiedSince(currentDate.toEpochMilli()).build();
|
||||
long time = currentDate.toEpochMilli();
|
||||
MockServerWebExchange exchange = get("/").ifNoneMatch(oldEtag).ifModifiedSince(time).toExchange();
|
||||
|
||||
assertFalse(createExchange().checkNotModified(currentETag, currentDate));
|
||||
assertFalse(exchange.checkNotModified(currentETag, currentDate));
|
||||
|
||||
assertNull(response.getStatusCode());
|
||||
assertEquals(currentETag, response.getHeaders().getETag());
|
||||
assertEquals(currentDate.toEpochMilli(), response.getHeaders().getLastModified());
|
||||
assertNull(exchange.getResponse().getStatusCode());
|
||||
assertEquals(currentETag, exchange.getResponse().getHeaders().getETag());
|
||||
assertEquals(time, exchange.getResponse().getHeaders().getLastModified());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void checkNotModifiedETagWeakStrong() {
|
||||
String eTag = "\"Foo\"";
|
||||
String weakEtag = String.format("W/%s", eTag);
|
||||
this.request = request().ifNoneMatch(eTag).build();
|
||||
MockServerWebExchange exchange = get("/").ifNoneMatch(eTag).toExchange();
|
||||
|
||||
assertTrue(createExchange().checkNotModified(weakEtag));
|
||||
assertTrue(exchange.checkNotModified(weakEtag));
|
||||
|
||||
assertEquals(304, response.getStatusCode().value());
|
||||
assertEquals(weakEtag, response.getHeaders().getETag());
|
||||
assertEquals(304, exchange.getResponse().getStatusCode().value());
|
||||
assertEquals(weakEtag, exchange.getResponse().getHeaders().getETag());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void checkNotModifiedETagStrongWeak() {
|
||||
String eTag = "\"Foo\"";
|
||||
this.request = request().ifNoneMatch(String.format("W/%s", eTag)).build();
|
||||
MockServerWebExchange exchange = get("/").ifNoneMatch(String.format("W/%s", eTag)).toExchange();
|
||||
|
||||
assertTrue(createExchange().checkNotModified(eTag));
|
||||
assertTrue(exchange.checkNotModified(eTag));
|
||||
|
||||
assertEquals(304, response.getStatusCode().value());
|
||||
assertEquals(eTag, response.getHeaders().getETag());
|
||||
assertEquals(304, exchange.getResponse().getStatusCode().value());
|
||||
assertEquals(eTag, exchange.getResponse().getHeaders().getETag());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void checkNotModifiedMultipleETags() {
|
||||
String eTag = "\"Bar\"";
|
||||
String multipleETags = String.format("\"Foo\", %s", eTag);
|
||||
this.request = request().ifNoneMatch(multipleETags).build();
|
||||
MockServerWebExchange exchange = get("/").ifNoneMatch(multipleETags).toExchange();
|
||||
|
||||
assertTrue(createExchange().checkNotModified(eTag));
|
||||
assertTrue(exchange.checkNotModified(eTag));
|
||||
|
||||
assertEquals(304, response.getStatusCode().value());
|
||||
assertEquals(eTag, response.getHeaders().getETag());
|
||||
assertEquals(304, exchange.getResponse().getStatusCode().value());
|
||||
assertEquals(eTag, exchange.getResponse().getHeaders().getETag());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void checkNotModifiedTimestampWithLengthPart() throws Exception {
|
||||
long epochTime = dateFormat.parse(CURRENT_TIME).getTime();
|
||||
this.request = request().header("If-Modified-Since", "Wed, 09 Apr 2014 09:57:42 GMT; length=13774").build();
|
||||
String header = "Wed, 09 Apr 2014 09:57:42 GMT; length=13774";
|
||||
MockServerWebExchange exchange = get("/").header("If-Modified-Since", header).toExchange();
|
||||
|
||||
assertTrue(createExchange().checkNotModified(Instant.ofEpochMilli(epochTime)));
|
||||
assertTrue(exchange.checkNotModified(Instant.ofEpochMilli(epochTime)));
|
||||
|
||||
assertEquals(304, response.getStatusCode().value());
|
||||
assertEquals(epochTime, response.getHeaders().getLastModified());
|
||||
assertEquals(304, exchange.getResponse().getStatusCode().value());
|
||||
assertEquals(epochTime, exchange.getResponse().getHeaders().getLastModified());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void checkModifiedTimestampWithLengthPart() throws Exception {
|
||||
long epochTime = dateFormat.parse(CURRENT_TIME).getTime();
|
||||
this.request = request().header("If-Modified-Since", "Tue, 08 Apr 2014 09:57:42 GMT; length=13774").build();
|
||||
String header = "Tue, 08 Apr 2014 09:57:42 GMT; length=13774";
|
||||
MockServerWebExchange exchange = get("/").header("If-Modified-Since", header).toExchange();
|
||||
|
||||
assertFalse(createExchange().checkNotModified(Instant.ofEpochMilli(epochTime)));
|
||||
assertFalse(exchange.checkNotModified(Instant.ofEpochMilli(epochTime)));
|
||||
|
||||
assertNull(response.getStatusCode());
|
||||
assertEquals(epochTime, response.getHeaders().getLastModified());
|
||||
assertNull(exchange.getResponse().getStatusCode());
|
||||
assertEquals(epochTime, exchange.getResponse().getHeaders().getLastModified());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void checkNotModifiedTimestampConditionalPut() throws Exception {
|
||||
Instant oneMinuteAgo = currentDate.minusSeconds(60);
|
||||
long millis = currentDate.toEpochMilli();
|
||||
this.request = MockServerHttpRequest.put("http://example.org").ifUnmodifiedSince(millis).build();
|
||||
MockServerWebExchange exchange = MockServerHttpRequest.put("/").ifUnmodifiedSince(millis).toExchange();
|
||||
|
||||
assertFalse(createExchange().checkNotModified(oneMinuteAgo));
|
||||
assertNull(response.getStatusCode());
|
||||
assertEquals(-1, response.getHeaders().getLastModified());
|
||||
assertFalse(exchange.checkNotModified(oneMinuteAgo));
|
||||
assertNull(exchange.getResponse().getStatusCode());
|
||||
assertEquals(-1, exchange.getResponse().getHeaders().getLastModified());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void checkNotModifiedTimestampConditionalPutConflict() throws Exception {
|
||||
Instant oneMinuteAgo = currentDate.minusSeconds(60);
|
||||
long millis = oneMinuteAgo.toEpochMilli();
|
||||
this.request = MockServerHttpRequest.put("http://example.org").ifUnmodifiedSince(millis).build();
|
||||
MockServerWebExchange exchange = MockServerHttpRequest.put("/").ifUnmodifiedSince(millis).toExchange();
|
||||
|
||||
assertTrue(createExchange().checkNotModified(currentDate));
|
||||
assertEquals(412, response.getStatusCode().value());
|
||||
assertEquals(-1, response.getHeaders().getLastModified());
|
||||
}
|
||||
|
||||
|
||||
private MockServerHttpRequest.BaseBuilder<?> request() {
|
||||
return MockServerHttpRequest.get("http://example.org");
|
||||
}
|
||||
|
||||
private DefaultServerWebExchange createExchange() {
|
||||
return new DefaultServerWebExchange(this.request, this.response);
|
||||
assertTrue(exchange.checkNotModified(currentDate));
|
||||
assertEquals(412, exchange.getResponse().getStatusCode().value());
|
||||
assertEquals(-1, exchange.getResponse().getHeaders().getLastModified());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -18,17 +18,15 @@ package org.springframework.web.server.handler;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.mock.http.server.reactive.test.MockServerHttpRequest;
|
||||
import org.springframework.mock.http.server.reactive.test.MockServerHttpResponse;
|
||||
import org.springframework.mock.http.server.reactive.test.MockServerWebExchange;
|
||||
import org.springframework.web.server.ServerWebExchange;
|
||||
import org.springframework.web.server.WebExceptionHandler;
|
||||
import org.springframework.web.server.WebHandler;
|
||||
import org.springframework.web.server.adapter.DefaultServerWebExchange;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
@@ -38,20 +36,9 @@ import static org.junit.Assert.assertEquals;
|
||||
*/
|
||||
public class ExceptionHandlingHttpHandlerTests {
|
||||
|
||||
private MockServerHttpResponse response;
|
||||
private final MockServerWebExchange exchange = MockServerHttpRequest.get("http://localhost:8080").toExchange();
|
||||
|
||||
private ServerWebExchange exchange;
|
||||
|
||||
private WebHandler targetHandler;
|
||||
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
MockServerHttpRequest request = MockServerHttpRequest.get("http://localhost:8080").build();
|
||||
this.response = new MockServerHttpResponse();
|
||||
this.exchange = new DefaultServerWebExchange(request, this.response);
|
||||
this.targetHandler = new StubWebHandler(new IllegalStateException("boo"));
|
||||
}
|
||||
private final WebHandler targetHandler = new StubWebHandler(new IllegalStateException("boo"));
|
||||
|
||||
|
||||
@Test
|
||||
@@ -59,7 +46,7 @@ public class ExceptionHandlingHttpHandlerTests {
|
||||
WebExceptionHandler exceptionHandler = new BadRequestExceptionHandler();
|
||||
createWebHandler(exceptionHandler).handle(this.exchange).block();
|
||||
|
||||
assertEquals(HttpStatus.BAD_REQUEST, this.response.getStatusCode());
|
||||
assertEquals(HttpStatus.BAD_REQUEST, this.exchange.getResponse().getStatusCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -72,7 +59,7 @@ public class ExceptionHandlingHttpHandlerTests {
|
||||
};
|
||||
createWebHandler(exceptionHandlers).handle(this.exchange).block();
|
||||
|
||||
assertEquals(HttpStatus.BAD_REQUEST, this.response.getStatusCode());
|
||||
assertEquals(HttpStatus.BAD_REQUEST, this.exchange.getResponse().getStatusCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -80,7 +67,7 @@ public class ExceptionHandlingHttpHandlerTests {
|
||||
WebExceptionHandler exceptionHandler = new UnresolvedExceptionHandler();
|
||||
createWebHandler(exceptionHandler).handle(this.exchange).block();
|
||||
|
||||
assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, this.response.getStatusCode());
|
||||
assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, this.exchange.getResponse().getStatusCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -88,7 +75,7 @@ public class ExceptionHandlingHttpHandlerTests {
|
||||
WebExceptionHandler exceptionHandler = new BadRequestExceptionHandler();
|
||||
createWebHandler(exceptionHandler).handle(this.exchange).block();
|
||||
|
||||
assertEquals(HttpStatus.BAD_REQUEST, this.response.getStatusCode());
|
||||
assertEquals(HttpStatus.BAD_REQUEST, this.exchange.getResponse().getStatusCode());
|
||||
}
|
||||
|
||||
private WebHandler createWebHandler(WebExceptionHandler... handlers) {
|
||||
|
||||
@@ -18,18 +18,17 @@ package org.springframework.web.server.handler;
|
||||
|
||||
import java.time.Duration;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import reactor.core.publisher.Mono;
|
||||
import reactor.test.StepVerifier;
|
||||
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.mock.http.server.reactive.test.MockServerHttpRequest;
|
||||
import org.springframework.mock.http.server.reactive.test.MockServerHttpResponse;
|
||||
import org.springframework.mock.http.server.reactive.test.MockServerWebExchange;
|
||||
import org.springframework.web.server.ResponseStatusException;
|
||||
import org.springframework.web.server.adapter.DefaultServerWebExchange;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertSame;
|
||||
|
||||
/**
|
||||
* Unit tests for {@link ResponseStatusExceptionHandler}.
|
||||
@@ -38,38 +37,23 @@ import static org.junit.Assert.*;
|
||||
*/
|
||||
public class ResponseStatusExceptionHandlerTests {
|
||||
|
||||
private ResponseStatusExceptionHandler handler;
|
||||
private final ResponseStatusExceptionHandler handler = new ResponseStatusExceptionHandler();
|
||||
|
||||
private MockServerHttpRequest request;
|
||||
|
||||
private MockServerHttpResponse response;
|
||||
|
||||
|
||||
@Before
|
||||
public void setup() throws Exception {
|
||||
this.handler = new ResponseStatusExceptionHandler();
|
||||
this.request = MockServerHttpRequest.get("/").build();
|
||||
this.response = new MockServerHttpResponse();
|
||||
}
|
||||
private final MockServerWebExchange exchange = MockServerHttpRequest.get("/").toExchange();
|
||||
|
||||
|
||||
@Test
|
||||
public void handleException() throws Exception {
|
||||
Throwable ex = new ResponseStatusException(HttpStatus.BAD_REQUEST, "");
|
||||
this.handler.handle(createExchange(), ex).block(Duration.ofSeconds(5));
|
||||
assertEquals(HttpStatus.BAD_REQUEST, this.response.getStatusCode());
|
||||
this.handler.handle(this.exchange, ex).block(Duration.ofSeconds(5));
|
||||
assertEquals(HttpStatus.BAD_REQUEST, this.exchange.getResponse().getStatusCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void unresolvedException() throws Exception {
|
||||
Throwable expected = new IllegalStateException();
|
||||
Mono<Void> mono = this.handler.handle(createExchange(), expected);
|
||||
Mono<Void> mono = this.handler.handle(this.exchange, expected);
|
||||
StepVerifier.create(mono).consumeErrorWith(actual -> assertSame(expected, actual)).verify();
|
||||
}
|
||||
|
||||
|
||||
private DefaultServerWebExchange createExchange() {
|
||||
return new DefaultServerWebExchange(this.request, this.response);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -30,10 +30,6 @@ public class MockWebSessionManager implements WebSessionManager {
|
||||
private final Mono<WebSession> session;
|
||||
|
||||
|
||||
public MockWebSessionManager() {
|
||||
this(Mono.empty());
|
||||
}
|
||||
|
||||
public MockWebSessionManager(WebSession session) {
|
||||
this(Mono.just(session));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user