use of cn.taketoday.web.mock.MockHttpServletRequest in project today-infrastructure by TAKETODAY.
the class WebUtilsTests method testWithXForwardedHeaders.
private void testWithXForwardedHeaders(String serverName, int port, String forwardedProto, String forwardedHost, int forwardedPort, String originHeader) throws Exception {
MockHttpServletRequest request = new MockHttpServletRequest();
request.setServerName(serverName);
if (port != -1) {
request.setServerPort(port);
}
if (forwardedProto != null) {
request.addHeader("X-Forwarded-Proto", forwardedProto);
}
if (forwardedHost != null) {
request.addHeader("X-Forwarded-Host", forwardedHost);
}
if (forwardedPort != -1) {
request.addHeader("X-Forwarded-Port", String.valueOf(forwardedPort));
}
request.addHeader(HttpHeaders.ORIGIN, originHeader);
// HttpServletRequest requestToUse = adaptFromForwardedHeaders(request);
// ServerHttpRequest httpRequest = new ServletServerHttpRequest(requestToUse);
//
// assertThat(WebUtils.isSameOrigin(httpRequest)).isTrue();
}
use of cn.taketoday.web.mock.MockHttpServletRequest in project today-infrastructure by TAKETODAY.
the class BaseViewTests method renderWithStaticAttributesNoCollision.
/**
* Test attribute passing, NOT CSV parsing.
*/
@Test
public void renderWithStaticAttributesNoCollision() throws Exception {
WebServletApplicationContext wac = mock(WebServletApplicationContext.class);
given(wac.getServletContext()).willReturn(new MockServletContext());
HttpServletRequest request = new MockHttpServletRequest();
HttpServletResponse response = new MockHttpServletResponse();
TestView tv = new TestView(wac);
tv.setApplicationContext(wac);
Properties p = new Properties();
p.setProperty("foo", "bar");
p.setProperty("something", "else");
tv.setAttributes(p);
Map<String, Object> model = new HashMap<>();
model.put("one", new HashMap<>());
model.put("two", new Object());
RequestContext requestContext = ServletUtils.getRequestContext(request, response);
tv.render(model, requestContext);
checkContainsAll(model, tv.model);
checkContainsAll(p, tv.model);
assertThat(tv.initialized).isTrue();
}
use of cn.taketoday.web.mock.MockHttpServletRequest in project today-infrastructure by TAKETODAY.
the class BaseViewTests method dynamicModelOverridesStaticAttributesIfCollision.
@Test
public void dynamicModelOverridesStaticAttributesIfCollision() throws Exception {
WebServletApplicationContext wac = mock(WebServletApplicationContext.class);
given(wac.getServletContext()).willReturn(new MockServletContext());
HttpServletRequest request = new MockHttpServletRequest();
HttpServletResponse response = new MockHttpServletResponse();
TestView tv = new TestView(wac);
tv.setApplicationContext(wac);
Properties p = new Properties();
p.setProperty("one", "bar");
p.setProperty("something", "else");
tv.setAttributes(p);
Map<String, Object> model = new HashMap<>();
model.put("one", new HashMap<>());
model.put("two", new Object());
RequestContext requestContext = ServletUtils.getRequestContext(request, response);
tv.render(model, requestContext);
// Check it contains all
checkContainsAll(model, tv.model);
assertThat(tv.model.size()).isEqualTo(3);
assertThat(tv.model.get("something")).isEqualTo("else");
assertThat(tv.initialized).isTrue();
}
use of cn.taketoday.web.mock.MockHttpServletRequest in project today-infrastructure by TAKETODAY.
the class UriComponentsBuilderTests method fromHttpRequestForwardedHeaderWithoutHostPortAndWithServerPort.
@Test
void fromHttpRequestForwardedHeaderWithoutHostPortAndWithServerPort() {
MockHttpServletRequest request = new MockHttpServletRequest();
request.addHeader("Forwarded", "proto=https; host=84.198.58.199");
request.setScheme("http");
request.setServerPort(8080);
request.setServerName("example.com");
request.setRequestURI("/rest/mobile/users/1");
HttpRequest httpRequest = new ServletServerHttpRequest(request);
UriComponents result = UriComponentsBuilder.fromHttpRequest(httpRequest).build();
assertThat(result.getScheme()).isEqualTo("https");
assertThat(result.getHost()).isEqualTo("84.198.58.199");
assertThat(result.getPath()).isEqualTo("/rest/mobile/users/1");
assertThat(result.getPort()).isEqualTo(-1);
assertThat(result.toUriString()).isEqualTo("https://84.198.58.199/rest/mobile/users/1");
}
use of cn.taketoday.web.mock.MockHttpServletRequest in project today-infrastructure by TAKETODAY.
the class UriComponentsBuilderTests method fromHttpRequestWithForwardedProtoMultiValueHeader.
@Test
// SPR-12816
void fromHttpRequestWithForwardedProtoMultiValueHeader() {
MockHttpServletRequest request = new MockHttpServletRequest();
request.setScheme("http");
request.setServerName("localhost");
request.setServerPort(8080);
request.setRequestURI("/mvc-showcase");
request.addHeader("X-Forwarded-Host", "a.example.org");
request.addHeader("X-Forwarded-Port", "443");
request.addHeader("X-Forwarded-Proto", "https,https");
HttpRequest httpRequest = new ServletServerHttpRequest(request);
UriComponents result = UriComponentsBuilder.fromHttpRequest(httpRequest).build();
assertThat(result.toString()).isEqualTo("https://a.example.org/mvc-showcase");
}
Aggregations