Java Code Examples for org.springframework.mock.web.test.MockHttpServletRequest#setMethod()

The following examples show how to use org.springframework.mock.web.test.MockHttpServletRequest#setMethod() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MvcNamespaceTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void testDefaultServletHandlerWithOptionalAttributes() throws Exception {
	loadBeanDefinitions("mvc-config-default-servlet-optional-attrs.xml");

	HttpRequestHandlerAdapter adapter = appContext.getBean(HttpRequestHandlerAdapter.class);
	assertNotNull(adapter);

	DefaultServletHttpRequestHandler handler = appContext.getBean(DefaultServletHttpRequestHandler.class);
	assertNotNull(handler);

	SimpleUrlHandlerMapping mapping = appContext.getBean(SimpleUrlHandlerMapping.class);
	assertNotNull(mapping);
	assertEquals(Ordered.LOWEST_PRECEDENCE, mapping.getOrder());

	MockHttpServletRequest request = new MockHttpServletRequest();
	request.setRequestURI("/foo.css");
	request.setMethod("GET");

	HandlerExecutionChain chain = mapping.getHandler(request);
	assertTrue(chain.getHandler() instanceof DefaultServletHttpRequestHandler);

	MockHttpServletResponse response = new MockHttpServletResponse();
	ModelAndView mv = adapter.handle(request, response, chain.getHandler());
	assertNull(mv);
}
 
Example 2
Source File: RequestPartMethodArgumentResolverTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void resolvePartArrayArgument() throws Exception {
	MockHttpServletRequest request = new MockHttpServletRequest();
	request.setMethod("POST");
	request.setContentType("multipart/form-data");
	MockPart part1 = new MockPart("requestPart", "Hello World 1".getBytes());
	MockPart part2 = new MockPart("requestPart", "Hello World 2".getBytes());
	request.addPart(part1);
	request.addPart(part2);
	request.addPart(new MockPart("otherPart", "Hello World".getBytes()));
	webRequest = new ServletWebRequest(request);

	Object result = resolver.resolveArgument(paramPartArray, null, webRequest, null);
	assertTrue(result instanceof Part[]);
	Part[] parts = (Part[]) result;
	assertEquals(2, parts.length);
	assertEquals(parts[0], part1);
	assertEquals(parts[1], part2);
}
 
Example 3
Source File: RequestMappingInfoTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test // SPR-14383
public void compareToWithHttpHeadMapping() {
	MockHttpServletRequest request = new MockHttpServletRequest();
	request.setMethod("HEAD");
	request.addHeader("Accept", "application/json");

	RequestMappingInfo noMethods = paths().build();
	RequestMappingInfo getMethod = paths().methods(GET).produces("application/json").build();
	RequestMappingInfo headMethod = paths().methods(HEAD).build();

	Comparator<RequestMappingInfo> comparator = (info, otherInfo) -> info.compareTo(otherInfo, request);

	List<RequestMappingInfo> list = asList(noMethods, getMethod, headMethod);
	Collections.shuffle(list);
	Collections.sort(list, comparator);

	assertEquals(headMethod, list.get(0));
	assertEquals(getMethod, list.get(1));
	assertEquals(noMethods, list.get(2));
}
 
Example 4
Source File: ServletAnnotationControllerHandlerMethodTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void parameterCsvAsStringArray() throws Exception {
	initServlet(wac -> {
		RootBeanDefinition csDef = new RootBeanDefinition(FormattingConversionServiceFactoryBean.class);
		RootBeanDefinition wbiDef = new RootBeanDefinition(ConfigurableWebBindingInitializer.class);
		wbiDef.getPropertyValues().add("conversionService", csDef);
		RootBeanDefinition adapterDef = new RootBeanDefinition(RequestMappingHandlerAdapter.class);
		adapterDef.getPropertyValues().add("webBindingInitializer", wbiDef);
		wac.registerBeanDefinition("handlerAdapter", adapterDef);
	}, CsvController.class);

	MockHttpServletRequest request = new MockHttpServletRequest();
	request.setRequestURI("/integerArray");
	request.setMethod("POST");
	request.addParameter("content", "1,2");
	MockHttpServletResponse response = new MockHttpServletResponse();
	getServlet().service(request, response);
	assertEquals("1-2", response.getContentAsString());
}
 
Example 5
Source File: RequestParamMethodArgumentResolverTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void resolvePartList() throws Exception {
	MockHttpServletRequest request = new MockHttpServletRequest();
	request.setMethod("POST");
	request.setContentType("multipart/form-data");
	MockPart expected1 = new MockPart("pfilelist", "Hello World 1".getBytes());
	MockPart expected2 = new MockPart("pfilelist", "Hello World 2".getBytes());
	request.addPart(expected1);
	request.addPart(expected2);
	request.addPart(new MockPart("other", "Hello World 3".getBytes()));
	webRequest = new ServletWebRequest(request);

	MethodParameter param = this.testMethod.annotPresent(RequestParam.class).arg(List.class, Part.class);
	Object result = resolver.resolveArgument(param, null, webRequest, null);

	assertTrue(result instanceof List);
	assertEquals(Arrays.asList(expected1, expected2), result);
}
 
Example 6
Source File: RequestPartMethodArgumentResolverTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void resolvePartListArgument() throws Exception {
	MockHttpServletRequest request = new MockHttpServletRequest();
	request.setMethod("POST");
	request.setContentType("multipart/form-data");
	MockPart part1 = new MockPart("requestPart", "Hello World 1".getBytes());
	MockPart part2 = new MockPart("requestPart", "Hello World 2".getBytes());
	request.addPart(part1);
	request.addPart(part2);
	request.addPart(new MockPart("otherPart", "Hello World".getBytes()));
	webRequest = new ServletWebRequest(request);

	Object result = resolver.resolveArgument(paramPartList, null, webRequest, null);
	assertTrue(result instanceof List);
	assertEquals(Arrays.asList(part1, part2), result);
}
 
Example 7
Source File: MvcNamespaceTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void testDefaultServletHandlerWithOptionalAttributes() throws Exception {
	loadBeanDefinitions("mvc-config-default-servlet-optional-attrs.xml");

	HttpRequestHandlerAdapter adapter = appContext.getBean(HttpRequestHandlerAdapter.class);
	assertNotNull(adapter);

	DefaultServletHttpRequestHandler handler = appContext.getBean(DefaultServletHttpRequestHandler.class);
	assertNotNull(handler);

	SimpleUrlHandlerMapping mapping = appContext.getBean(SimpleUrlHandlerMapping.class);
	assertNotNull(mapping);
	assertEquals(Ordered.LOWEST_PRECEDENCE, mapping.getOrder());

	MockHttpServletRequest request = new MockHttpServletRequest();
	request.setRequestURI("/foo.css");
	request.setMethod("GET");

	HandlerExecutionChain chain = mapping.getHandler(request);
	assertTrue(chain.getHandler() instanceof DefaultServletHttpRequestHandler);

	MockHttpServletResponse response = new MockHttpServletResponse();
	ModelAndView mv = adapter.handle(request, response, chain.getHandler());
	assertNull(mv);
}
 
Example 8
Source File: CorsUtilsTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void isPreFlightRequest() {
	MockHttpServletRequest request = new MockHttpServletRequest();
	request.setMethod(HttpMethod.OPTIONS.name());
	request.addHeader(HttpHeaders.ORIGIN, "https://domain.com");
	request.addHeader(HttpHeaders.ACCESS_CONTROL_REQUEST_METHOD, "GET");
	assertTrue(CorsUtils.isPreFlightRequest(request));
}
 
Example 9
Source File: SimpleMappingExceptionResolverTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Before
public void setUp() throws Exception {
	exceptionResolver = new SimpleMappingExceptionResolver();
	handler1 = new String();
	handler2 = new Object();
	request = new MockHttpServletRequest();
	response = new MockHttpServletResponse();
	request.setMethod("GET");
	genericException = new Exception();
}
 
Example 10
Source File: ServletAnnotationControllerHandlerMethodTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void regularParameterAsStringArray() throws Exception {
	initServletWithControllers(MultipartController.class);

	MockHttpServletRequest request = new MockHttpServletRequest();
	request.setRequestURI("/stringArray");
	request.setMethod("POST");
	request.addParameter("content", "Juergen");
	MockHttpServletResponse response = new MockHttpServletResponse();
	getServlet().service(request, response);
	assertEquals("Juergen", response.getContentAsString());
}
 
Example 11
Source File: HttpEntityMethodProcessorTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Before
public void setUp() throws Exception {
	Method method = getClass().getMethod("handle", HttpEntity.class, HttpEntity.class);
	paramList = new MethodParameter(method, 0);
	paramSimpleBean = new MethodParameter(method, 1);

	mavContainer = new ModelAndViewContainer();
	binderFactory = new ValidatingBinderFactory();
	servletRequest = new MockHttpServletRequest();
	servletResponse = new MockHttpServletResponse();
	servletRequest.setMethod("POST");
	webRequest = new ServletWebRequest(servletRequest, servletResponse);

}
 
Example 12
Source File: RequestPartMethodArgumentResolverTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void resolveOptionalRequestPartNotPresent() throws Exception {
	MockHttpServletRequest request = new MockHttpServletRequest();
	request.setMethod("POST");
	request.setContentType("multipart/form-data");
	webRequest = new ServletWebRequest(request);

	Object actualValue = resolver.resolveArgument(optionalRequestPart, null, webRequest, null);
	assertEquals("Invalid argument value", Optional.empty(), actualValue);

	actualValue = resolver.resolveArgument(optionalRequestPart, null, webRequest, null);
	assertEquals("Invalid argument value", Optional.empty(), actualValue);
}
 
Example 13
Source File: RequestParamMethodArgumentResolverTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void resolvePartNotAnnot() throws Exception {
	MockPart expected = new MockPart("part", "Hello World".getBytes());
	MockHttpServletRequest request = new MockHttpServletRequest();
	request.setMethod("POST");
	request.setContentType("multipart/form-data");
	request.addPart(expected);
	webRequest = new ServletWebRequest(request);

	MethodParameter param = this.testMethod.annotNotPresent(RequestParam.class).arg(Part.class);
	Object result = resolver.resolveArgument(param, null, webRequest, null);
	assertTrue(result instanceof Part);
	assertEquals("Invalid result", expected, result);
}
 
Example 14
Source File: RequestPartMethodArgumentResolverTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void resolveOptionalRequestPartNotPresent() throws Exception {
	MockHttpServletRequest request = new MockHttpServletRequest();
	request.setMethod("POST");
	request.setContentType("multipart/form-data");
	webRequest = new ServletWebRequest(request);

	Object actualValue = resolver.resolveArgument(optionalRequestPart, null, webRequest, null);
	assertEquals("Invalid argument value", Optional.empty(), actualValue);

	actualValue = resolver.resolveArgument(optionalRequestPart, null, webRequest, null);
	assertEquals("Invalid argument value", Optional.empty(), actualValue);
}
 
Example 15
Source File: RequestPartMethodArgumentResolverTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void resolvePartArgument() throws Exception {
	MockHttpServletRequest request = new MockHttpServletRequest();
	request.setMethod("POST");
	request.setContentType("multipart/form-data");
	MockPart expected = new MockPart("part", "Hello World".getBytes());
	request.addPart(expected);
	request.addPart(new MockPart("otherPart", "Hello World".getBytes()));
	webRequest = new ServletWebRequest(request);

	Object result = resolver.resolveArgument(paramPart, null, webRequest, null);
	assertTrue(result instanceof Part);
	assertEquals("Invalid result", expected, result);
}
 
Example 16
Source File: ServletAnnotationControllerTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void regularParametersAsStringArray() throws Exception {
	initServlet(MultipartController.class);

	MockHttpServletRequest request = new MockHttpServletRequest();
	request.setRequestURI("/stringArray");
	request.setMethod("POST");
	request.addParameter("content", "Juergen");
	request.addParameter("content", "Eva");
	MockHttpServletResponse response = new MockHttpServletResponse();
	servlet.service(request, response);
	assertEquals("Juergen-Eva", response.getContentAsString());
}
 
Example 17
Source File: HttpEntityMethodProcessorTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Before
public void setup() throws Exception {
	Method method = getClass().getDeclaredMethod("handle", HttpEntity.class, HttpEntity.class);
	paramList = new MethodParameter(method, 0);
	paramSimpleBean = new MethodParameter(method, 1);

	mavContainer = new ModelAndViewContainer();
	binderFactory = new ValidatingBinderFactory();
	servletRequest = new MockHttpServletRequest();
	servletResponse = new MockHttpServletResponse();
	servletRequest.setMethod("POST");
	webRequest = new ServletWebRequest(servletRequest, servletResponse);
}
 
Example 18
Source File: ServletAnnotationControllerTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void regularParameterAsStringArray() throws Exception {
	initServlet(MultipartController.class);

	MockHttpServletRequest request = new MockHttpServletRequest();
	request.setRequestURI("/stringArray");
	request.setMethod("POST");
	request.addParameter("content", "Juergen");
	MockHttpServletResponse response = new MockHttpServletResponse();
	servlet.service(request, response);
	assertEquals("Juergen", response.getContentAsString());
}
 
Example 19
Source File: MvcNamespaceTests.java    From spring-analysis-note with MIT License 4 votes vote down vote up
/** WebSphere gives trailing servlet path slashes by default!! */
@Test
public void testViewControllersOnWebSphere() throws Exception {
	loadBeanDefinitions("mvc-config-view-controllers.xml");

	SimpleUrlHandlerMapping mapping2 = appContext.getBean(SimpleUrlHandlerMapping.class);
	SimpleControllerHandlerAdapter adapter = appContext.getBean(SimpleControllerHandlerAdapter.class);

	MockHttpServletRequest request = new MockHttpServletRequest();
	request.setMethod("GET");
	request.setRequestURI("/myapp/app/bar");
	request.setContextPath("/myapp");
	request.setServletPath("/app/");
	request.setAttribute("com.ibm.websphere.servlet.uri_non_decoded", "/myapp/app/bar");
	HandlerExecutionChain chain = mapping2.getHandler(request);
	assertEquals(4, chain.getInterceptors().length);
	assertTrue(chain.getInterceptors()[1] instanceof ConversionServiceExposingInterceptor);
	assertTrue(chain.getInterceptors()[2] instanceof LocaleChangeInterceptor);
	assertTrue(chain.getInterceptors()[3] instanceof ThemeChangeInterceptor);
	ModelAndView mv2 = adapter.handle(request, new MockHttpServletResponse(), chain.getHandler());
	assertEquals("baz", mv2.getViewName());

	request.setRequestURI("/myapp/app/");
	request.setContextPath("/myapp");
	request.setServletPath("/app/");
	chain = mapping2.getHandler(request);
	assertEquals(4, chain.getInterceptors().length);
	assertTrue(chain.getInterceptors()[1] instanceof ConversionServiceExposingInterceptor);
	assertTrue(chain.getInterceptors()[2] instanceof LocaleChangeInterceptor);
	assertTrue(chain.getInterceptors()[3] instanceof ThemeChangeInterceptor);
	ModelAndView mv3 = adapter.handle(request, new MockHttpServletResponse(), chain.getHandler());
	assertEquals("root", mv3.getViewName());

	request.setRequestURI("/myapp/");
	request.setContextPath("/myapp");
	request.setServletPath("/");
	chain = mapping2.getHandler(request);
	assertEquals(4, chain.getInterceptors().length);
	assertTrue(chain.getInterceptors()[1] instanceof ConversionServiceExposingInterceptor);
	assertTrue(chain.getInterceptors()[2] instanceof LocaleChangeInterceptor);
	assertTrue(chain.getInterceptors()[3] instanceof ThemeChangeInterceptor);
	mv3 = adapter.handle(request, new MockHttpServletResponse(), chain.getHandler());
	assertEquals("root", mv3.getViewName());
}
 
Example 20
Source File: WebContentInterceptorTests.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
@Before
public void setUp() throws Exception {
	request = new MockHttpServletRequest();
	request.setMethod(WebContentGenerator.METHOD_GET);
	response = new MockHttpServletResponse();
}