Java Code Examples for org.springframework.mock.web.test.MockHttpServletRequest#addParameter()
The following examples show how to use
org.springframework.mock.web.test.MockHttpServletRequest#addParameter() .
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: ServletRequestUtilsTests.java From spring4-understanding with Apache License 2.0 | 6 votes |
@Test public void testIntParameters() throws ServletRequestBindingException { MockHttpServletRequest request = new MockHttpServletRequest(); request.addParameter("param", new String[] {"1", "2", "3"}); request.addParameter("param2", "1"); request.addParameter("param2", "2"); request.addParameter("param2", "bogus"); int[] array = new int[] {1, 2, 3}; int[] values = ServletRequestUtils.getRequiredIntParameters(request, "param"); assertEquals(3, values.length); for (int i = 0; i < array.length; i++) { assertEquals(array[i], values[i]); } try { ServletRequestUtils.getRequiredIntParameters(request, "param2"); fail("Should have thrown ServletRequestBindingException"); } catch (ServletRequestBindingException ex) { // expected } }
Example 2
Source File: DispatcherServletTests.java From spring-analysis-note with MIT License | 6 votes |
@Test public void themeChangeInterceptor2() throws Exception { MockHttpServletRequest request = new MockHttpServletRequest(getServletContext(), "GET", "/locale.do"); request.addPreferredLocale(Locale.CANADA); request.addUserRole("role1"); request.addParameter("theme", "mytheme"); request.addParameter("theme2", "theme"); MockHttpServletResponse response = new MockHttpServletResponse(); try { complexDispatcherServlet.service(request, response); assertTrue("Not forwarded", response.getForwardedUrl() == null); } catch (ServletException ex) { fail("Should not have thrown ServletException: " + ex.getMessage()); } }
Example 3
Source File: WebRequestDataBinderTests.java From spring-analysis-note with MIT License | 6 votes |
@Test public void testBindingWithNestedObjectCreation() throws Exception { TestBean tb = new TestBean(); WebRequestDataBinder binder = new WebRequestDataBinder(tb, "person"); binder.registerCustomEditor(ITestBean.class, new PropertyEditorSupport() { @Override public void setAsText(String text) throws IllegalArgumentException { setValue(new TestBean()); } }); MockHttpServletRequest request = new MockHttpServletRequest(); request.addParameter("spouse", "someValue"); request.addParameter("spouse.name", "test"); binder.bind(new ServletWebRequest(request)); assertNotNull(tb.getSpouse()); assertEquals("test", tb.getSpouse().getName()); }
Example 4
Source File: ServletAnnotationControllerHandlerMethodTests.java From spring4-understanding with Apache License 2.0 | 6 votes |
@Test public void modelFormController() throws Exception { initServlet(new ApplicationContextInitializer<GenericWebApplicationContext>() { @Override public void initialize(GenericWebApplicationContext wac) { wac.registerBeanDefinition("viewResolver", new RootBeanDefinition(TestViewResolver.class)); } }, MyModelFormController.class); MockHttpServletRequest request = new MockHttpServletRequest("GET", "/myPath.do"); request.addParameter("name", "name1"); request.addParameter("age", "value2"); MockHttpServletResponse response = new MockHttpServletResponse(); getServlet().service(request, response); assertEquals("myPath-name1-typeMismatch-tb1-myValue-yourValue", response.getContentAsString()); }
Example 5
Source File: ServletRequestDataBinderTests.java From java-technology-stack with MIT License | 6 votes |
@Test public void testFieldPrefixCausesFieldResetWithIgnoreUnknownFields() throws Exception { TestBean target = new TestBean(); ServletRequestDataBinder binder = new ServletRequestDataBinder(target); binder.setIgnoreUnknownFields(false); MockHttpServletRequest request = new MockHttpServletRequest(); request.addParameter("_postProcessed", "visible"); request.addParameter("postProcessed", "on"); binder.bind(request); assertTrue(target.isPostProcessed()); request.removeParameter("postProcessed"); binder.bind(request); assertFalse(target.isPostProcessed()); }
Example 6
Source File: ServletRequestDataBinderTests.java From spring-analysis-note with MIT License | 6 votes |
@Test public void testFieldDefaultPreemptsFieldMarker() throws Exception { TestBean target = new TestBean(); ServletRequestDataBinder binder = new ServletRequestDataBinder(target); MockHttpServletRequest request = new MockHttpServletRequest(); request.addParameter("!postProcessed", "on"); request.addParameter("_postProcessed", "visible"); request.addParameter("postProcessed", "on"); binder.bind(request); assertTrue(target.isPostProcessed()); request.removeParameter("postProcessed"); binder.bind(request); assertTrue(target.isPostProcessed()); request.removeParameter("!postProcessed"); binder.bind(request); assertFalse(target.isPostProcessed()); }
Example 7
Source File: WebRequestDataBinderTests.java From spring-analysis-note with MIT License | 6 votes |
@Test public void testFieldDefaultWithNestedProperty() throws Exception { TestBean target = new TestBean(); target.setSpouse(new TestBean()); WebRequestDataBinder binder = new WebRequestDataBinder(target); MockHttpServletRequest request = new MockHttpServletRequest(); request.addParameter("!spouse.postProcessed", "on"); request.addParameter("_spouse.postProcessed", "visible"); request.addParameter("spouse.postProcessed", "on"); binder.bind(new ServletWebRequest(request)); assertTrue(((TestBean) target.getSpouse()).isPostProcessed()); request.removeParameter("spouse.postProcessed"); binder.bind(new ServletWebRequest(request)); assertTrue(((TestBean) target.getSpouse()).isPostProcessed()); request.removeParameter("!spouse.postProcessed"); binder.bind(new ServletWebRequest(request)); assertFalse(((TestBean) target.getSpouse()).isPostProcessed()); }
Example 8
Source File: DispatcherServletTests.java From spring-analysis-note with MIT License | 5 votes |
@Test public void simpleMappingExceptionResolverWithAllHandlers1() throws Exception { MockHttpServletRequest request = new MockHttpServletRequest(getServletContext(), "GET", "/loc.do"); request.addPreferredLocale(Locale.CANADA); request.addUserRole("role1"); request.addParameter("access", "yes"); MockHttpServletResponse response = new MockHttpServletResponse(); complexDispatcherServlet.service(request, response); assertEquals(500, response.getStatus()); assertEquals("forwarded to failed", "failed1.jsp", response.getForwardedUrl()); assertTrue("Exception exposed", request.getAttribute("exception") instanceof IllegalAccessException); }
Example 9
Source File: ServletRequestUtilsTests.java From java-technology-stack with MIT License | 5 votes |
@Test public void testBooleanParameter() throws ServletRequestBindingException { MockHttpServletRequest request = new MockHttpServletRequest(); request.addParameter("param1", "true"); request.addParameter("param2", "e"); request.addParameter("param4", "yes"); request.addParameter("param5", "1"); request.addParameter("paramEmpty", ""); assertTrue(ServletRequestUtils.getBooleanParameter(request, "param1").equals(Boolean.TRUE)); assertTrue(ServletRequestUtils.getBooleanParameter(request, "param1", false)); assertTrue(ServletRequestUtils.getRequiredBooleanParameter(request, "param1")); assertFalse(ServletRequestUtils.getBooleanParameter(request, "param2", true)); assertFalse(ServletRequestUtils.getRequiredBooleanParameter(request, "param2")); assertTrue(ServletRequestUtils.getBooleanParameter(request, "param3") == null); assertTrue(ServletRequestUtils.getBooleanParameter(request, "param3", true)); try { ServletRequestUtils.getRequiredBooleanParameter(request, "param3"); fail("Should have thrown ServletRequestBindingException"); } catch (ServletRequestBindingException ex) { // expected } assertTrue(ServletRequestUtils.getBooleanParameter(request, "param4", false)); assertTrue(ServletRequestUtils.getRequiredBooleanParameter(request, "param4")); assertTrue(ServletRequestUtils.getBooleanParameter(request, "param5", false)); assertTrue(ServletRequestUtils.getRequiredBooleanParameter(request, "param5")); assertFalse(ServletRequestUtils.getRequiredBooleanParameter(request, "paramEmpty")); }
Example 10
Source File: DispatcherServletTests.java From spring4-understanding with Apache License 2.0 | 5 votes |
@Test public void simpleMappingExceptionResolverWithAllHandlers1() throws Exception { MockHttpServletRequest request = new MockHttpServletRequest(getServletContext(), "GET", "/loc.do"); request.addPreferredLocale(Locale.CANADA); request.addUserRole("role1"); request.addParameter("access", "yes"); MockHttpServletResponse response = new MockHttpServletResponse(); complexDispatcherServlet.service(request, response); assertEquals(500, response.getStatus()); assertEquals("forwarded to failed", "failed1.jsp", response.getForwardedUrl()); assertTrue("Exception exposed", request.getAttribute("exception") instanceof IllegalAccessException); }
Example 11
Source File: ServletAnnotationControllerHandlerMethodTests.java From spring-analysis-note with MIT License | 5 votes |
@Test public void dataClassBindingWithOptional() throws Exception { initServletWithControllers(OptionalDataClassController.class); MockHttpServletRequest request = new MockHttpServletRequest("GET", "/bind"); request.addParameter("param1", "value1"); request.addParameter("param2", "true"); request.addParameter("param3", "3"); MockHttpServletResponse response = new MockHttpServletResponse(); getServlet().service(request, response); assertEquals("value1-true-3", response.getContentAsString()); }
Example 12
Source File: MultiActionControllerTests.java From spring4-understanding with Apache License 2.0 | 5 votes |
@Test public void invokesCommandMethodNoSession() throws Exception { TestMaController mc = new TestMaController(); MockHttpServletRequest request = new MockHttpServletRequest("GET", "/commandNoSession.html"); request.addParameter("name", "rod"); request.addParameter("age", "32"); HttpServletResponse response = new MockHttpServletResponse(); ModelAndView mv = mc.handleRequest(request, response); assertTrue("Invoked commandNoSession method", mc.wasInvoked("commandNoSession")); assertTrue("view name is commandNoSession", mv.getViewName().equals("commandNoSession")); assertTrue("Only one method invoked", mc.getInvokedMethods() == 1); }
Example 13
Source File: DispatcherServletTests.java From java-technology-stack with MIT License | 5 votes |
@Test public void simpleMappingExceptionResolverWithDefaultErrorView() throws Exception { MockHttpServletRequest request = new MockHttpServletRequest(getServletContext(), "GET", "/locale.do"); request.addPreferredLocale(Locale.CANADA); request.addUserRole("role1"); request.addParameter("exception", "yes"); MockHttpServletResponse response = new MockHttpServletResponse(); complexDispatcherServlet.service(request, response); assertEquals(200, response.getStatus()); assertEquals("forwarded to failed", "failed0.jsp", response.getForwardedUrl()); assertTrue("Exception exposed", request.getAttribute("exception").getClass().equals(RuntimeException.class)); }
Example 14
Source File: ServletAnnotationControllerHandlerMethodTests.java From spring-analysis-note with MIT License | 5 votes |
@Test public void dataClassBindingWithValidationErrorAndConversionError() throws Exception { initServletWithControllers(ValidatedDataClassController.class); MockHttpServletRequest request = new MockHttpServletRequest("GET", "/bind"); request.addParameter("param2", "x"); MockHttpServletResponse response = new MockHttpServletResponse(); getServlet().service(request, response); assertEquals("2:null-x-null", response.getContentAsString()); }
Example 15
Source File: ServletAnnotationControllerHandlerMethodTests.java From spring-analysis-note with MIT License | 5 votes |
@Test public void dataClassBindingWithResult() throws Exception { initServletWithControllers(ValidatedDataClassController.class); MockHttpServletRequest request = new MockHttpServletRequest("GET", "/bind"); request.addParameter("param1", "value1"); request.addParameter("param2", "true"); request.addParameter("param3", "3"); MockHttpServletResponse response = new MockHttpServletResponse(); getServlet().service(request, response); assertEquals("value1-true-3", response.getContentAsString()); }
Example 16
Source File: ServletAnnotationControllerHandlerMethodTests.java From spring-analysis-note with MIT License | 5 votes |
@Test public void dataClassBindingWithValidationError() throws Exception { initServletWithControllers(ValidatedDataClassController.class); MockHttpServletRequest request = new MockHttpServletRequest("GET", "/bind"); request.addParameter("param2", "true"); request.addParameter("param3", "0"); MockHttpServletResponse response = new MockHttpServletResponse(); getServlet().service(request, response); assertEquals("1:null-true-0", response.getContentAsString()); }
Example 17
Source File: WebRequestDataBinderTests.java From spring-analysis-note with MIT License | 5 votes |
@Test public void testMultipleValuesForParameter() throws Exception { MockHttpServletRequest request = new MockHttpServletRequest(); String[] original = new String[] {"Tony", "Rod"}; request.addParameter("forname", original); ServletRequestParameterPropertyValues pvs = new ServletRequestParameterPropertyValues(request); assertTrue("Found 1 parameter", pvs.getPropertyValues().length == 1); assertTrue("Found array value", pvs.getPropertyValue("forname").getValue() instanceof String[]); String[] values = (String[]) pvs.getPropertyValue("forname").getValue(); assertEquals("Correct values", Arrays.asList(values), Arrays.asList(original)); }
Example 18
Source File: ServletAnnotationControllerHandlerMethodTests.java From java-technology-stack with MIT License | 5 votes |
@Test public void dataClassBindingWithMissingParameter() throws Exception { initServletWithControllers(ValidatedDataClassController.class); MockHttpServletRequest request = new MockHttpServletRequest("GET", "/bind"); request.addParameter("param1", "value1"); MockHttpServletResponse response = new MockHttpServletResponse(); getServlet().service(request, response); assertEquals("1:value1-null-null", response.getContentAsString()); }
Example 19
Source File: ServletAnnotationControllerHandlerMethodTests.java From spring-analysis-note with MIT License | 5 votes |
@Test public void dataClassBindingWithOptionalAndConversionError() throws Exception { initServletWithControllers(OptionalDataClassController.class); MockHttpServletRequest request = new MockHttpServletRequest("GET", "/bind"); request.addParameter("param1", "value1"); request.addParameter("param2", "x"); MockHttpServletResponse response = new MockHttpServletResponse(); getServlet().service(request, response); assertEquals("value1-x-null", response.getContentAsString()); }
Example 20
Source File: DispatcherServletTests.java From spring-analysis-note with MIT License | 5 votes |
@Test public void simpleMappingExceptionResolverWithSpecificHandler2() throws Exception { MockHttpServletRequest request = new MockHttpServletRequest(getServletContext(), "GET", "/locale.do"); request.addPreferredLocale(Locale.CANADA); request.addUserRole("role1"); request.addParameter("servlet", "yes"); MockHttpServletResponse response = new MockHttpServletResponse(); complexDispatcherServlet.service(request, response); assertEquals(200, response.getStatus()); assertEquals("forwarded to failed", "failed3.jsp", response.getForwardedUrl()); assertTrue("Exception exposed", request.getAttribute("exception") instanceof ServletException); }