use of cn.taketoday.web.mock.MockHttpServletRequest in project today-infrastructure by TAKETODAY.
the class ContentNegotiatingViewResolverTests method createViewResolver.
@BeforeEach
public void createViewResolver() {
wac.setServletContext(new MockServletContext());
wac.refresh();
viewResolver = new ContentNegotiatingViewResolver();
viewResolver.setApplicationContext(wac);
request = new MockHttpServletRequest("GET", "/test");
MockHttpServletResponse response = new MockHttpServletResponse();
this.requestContext = new MockServletRequestContext(wac, request, response);
RequestContextHolder.set(requestContext);
}
use of cn.taketoday.web.mock.MockHttpServletRequest in project today-infrastructure by TAKETODAY.
the class InternalResourceViewTests method forward.
@Test
public void forward() throws Exception {
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/myservlet/handler.do");
request.setContextPath("/mycontext");
request.setServletPath("/myservlet");
request.setPathInfo(";mypathinfo");
request.setQueryString("?param1=value1");
RequestContext context = new ServletRequestContext(null, request, response);
view.setUrl(url);
view.setServletContext(new MockServletContext() {
@Override
public int getMinorVersion() {
return 4;
}
});
view.render(model, context);
assertThat(response.getForwardedUrl()).isEqualTo(url);
model.forEach((key, value) -> assertThat(request.getAttribute(key)).as("Values for model key '" + key + "' must match").isEqualTo(value));
}
use of cn.taketoday.web.mock.MockHttpServletRequest in project today-infrastructure by TAKETODAY.
the class ScriptTemplateViewTests method resourceLoaderPath.
// SPR-14210
@Test
public void resourceLoaderPath() throws Exception {
MockServletContext servletContext = new MockServletContext();
this.wac.setServletContext(servletContext);
this.wac.refresh();
MockHttpServletRequest request = new MockHttpServletRequest();
MockHttpServletResponse response = new MockHttpServletResponse();
Map<String, Object> model = new HashMap<>();
InvocableScriptEngine engine = mock(InvocableScriptEngine.class);
given(engine.invokeFunction(any(), any(), any(), any())).willReturn("foo");
this.view.setEngine(engine);
this.view.setRenderFunction("render");
this.view.setApplicationContext(this.wac);
this.view.setUrl("cn/taketoday/web/servlet/view/script/empty.txt");
this.view.render(model, new ServletRequestContext(wac, request, response));
assertThat(response.getContentAsString()).isEqualTo("foo");
response = new MockHttpServletResponse();
this.view.setResourceLoaderPath("classpath:cn/taketoday/web/servlet/view/script/");
this.view.setUrl("empty.txt");
this.view.render(model, new ServletRequestContext(wac, request, response));
assertThat(response.getContentAsString()).isEqualTo("foo");
response = new MockHttpServletResponse();
this.view.setResourceLoaderPath("classpath:cn/taketoday/web/servlet/view/script");
this.view.setUrl("empty.txt");
this.view.render(model, new ServletRequestContext(wac, request, response));
assertThat(response.getContentAsString()).isEqualTo("foo");
}
use of cn.taketoday.web.mock.MockHttpServletRequest in project today-infrastructure by TAKETODAY.
the class UriComponentsBuilderTests method fromHttpRequestForwardedHeaderComma.
@Test
// gh-25737
void fromHttpRequestForwardedHeaderComma() {
MockHttpServletRequest request = new MockHttpServletRequest();
request.addHeader("Forwarded", "for=192.0.2.0,for=192.0.2.1;proto=https;host=192.0.2.3:9090");
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("192.0.2.3");
assertThat(result.getPath()).isEqualTo("/rest/mobile/users/1");
assertThat(result.getPort()).isEqualTo(9090);
assertThat(result.toUriString()).isEqualTo("https://192.0.2.3:9090/rest/mobile/users/1");
}
use of cn.taketoday.web.mock.MockHttpServletRequest in project today-infrastructure by TAKETODAY.
the class UriComponentsBuilderTests method fromHttpRequestWithForwardedInvalidIPv6Address.
@Test
// gh-26748
void fromHttpRequestWithForwardedInvalidIPv6Address() {
MockHttpServletRequest request = new MockHttpServletRequest();
request.setScheme("http");
request.setServerName("localhost");
request.setServerPort(-1);
request.setRequestURI("/mvc-showcase");
request.addHeader("X-Forwarded-Host", "2a02:918:175:ab60:45ee:c12c:dac1:808b");
HttpRequest httpRequest = new ServletServerHttpRequest(request);
assertThatIllegalArgumentException().isThrownBy(() -> UriComponentsBuilder.fromHttpRequest(httpRequest).build());
}
Aggregations