List of usage examples for org.springframework.mock.web MockHttpServletRequest MockHttpServletRequest
public MockHttpServletRequest(@Nullable String method, @Nullable String requestURI)
From source file:ar.com.zauber.commons.web.proxy.URLRequestMapperEditorTest.java
/** @throws MalformedURLException on error */ public final void testFactory() throws MalformedURLException { final PropertyEditor pe = new URLRequestMapperEditor(); pe.setAsText("^/public/(.*)$=http://localhost:9095/nexus/content/" + "repositories/public/$1\n" + "^/nexus/(.*)$=http://localhost:9095/nexus/$1\n"); final URLRequestMapper c = (URLRequestMapper) pe.getValue(); assertEquals(new URL("http://localhost:9095/nexus/foo/bar"), c.getProxiedURLFromRequest(new MockHttpServletRequest("GET", "/nexus/foo/bar")).getURL()); }
From source file:nz.net.catalyst.mobile.dds.CapabilitySerivceControllerTest.java
@Test public void testRequiredParams() throws Exception { MockHttpServletRequest request = new MockHttpServletRequest("GET", "/services/v1/get_capabilities"); MockHttpServletResponse response = new MockHttpServletResponse(); HashMap<String, String> headers = new HashMap<String, String>(); headers.put("user-agent", "Mozilla/5.0 (SymbianOS/9.2; U; Series60/3.1 NokiaE71-1/100.07.57; Profile/MIDP-2.0 Configuration/CLDC-1.1 ) AppleWebKit/413 (KHTML, like Gecko) Safari/413"); String headersStr = mapper.writeValueAsString(headers); request.addParameter("headers", headersStr); try {/*from w ww . j a v a 2s . c om*/ handlerAdapter.handle(request, response, csController); fail("exception expected"); } catch (MissingServletRequestParameterException e) { assertEquals("Required String[] parameter 'capability' is not present", e.getMessage()); } request = new MockHttpServletRequest("GET", "/services/v1/get_capabilities"); request.addParameter("capability", "device_id"); try { handlerAdapter.handle(request, response, csController); fail("exception expected"); } catch (MissingServletRequestParameterException e) { assertEquals("Required String parameter 'headers' is not present", e.getMessage()); csController.handleMissingParameters(e, response); assertEquals(400, response.getStatus()); } }
From source file:org.openmrs.contrib.metadatarepository.webapp.controller.BaseControllerTestCase.java
/** * Convenience methods to make tests simpler * * @param url the URL to post to/*from w w w . j av a 2 s.co m*/ * @return a MockHttpServletRequest with a POST to the specified URL */ public MockHttpServletRequest newPost(String url) { return new MockHttpServletRequest("POST", url); }
From source file:org.openmrs.web.controller.encounter.LocationFormControllerTest.java
/** * @see LocationFormController#onSubmit(HttpServletRequest,HttpServletResponse,Object,BindException) *//*from w w w . ja v a 2 s . c o m*/ @Test @Verifies(value = "should not retire location if reason is empty", method = "onSubmit(HttpServletRequest,HttpServletResponse,Object,BindException)") public void onSubmit_shouldNotRetireLocationIfReasonIsEmpty() throws Exception { MockHttpServletRequest request = new MockHttpServletRequest("POST", ""); request.setParameter("locationId", "1"); request.setParameter("retireReason", ""); request.setParameter("retired", "true"); request.setParameter("retireLocation", "true"); HttpServletResponse response = new MockHttpServletResponse(); LocationFormController controller = getLocationFormController(); ModelAndView modelAndView = controller.handleRequest(request, response); // make sure an error is returned because of the empty retire reason BeanPropertyBindingResult bindingResult = (BeanPropertyBindingResult) modelAndView.getModel() .get("org.springframework.validation.BindingResult.location"); Assert.assertTrue(bindingResult.hasFieldErrors("retireReason")); }
From source file:org.openmrs.module.webservices.rest.web.v1_0.controller.BaseCrudControllerTest.java
/** * Creates a request from the given parameters. * <p>// w w w . jav a2 s .c o m * The requestURI is automatically preceded with "/rest/" + RestConstants.VERSION_1. * * @param method * @param requestURI * @return */ public MockHttpServletRequest request(RequestMethod method, String requestURI) { MockHttpServletRequest request = new MockHttpServletRequest(method.toString(), "/rest/" + RestConstants.VERSION_1 + "/" + requestURI); request.addHeader("content-type", "application/json"); return request; }
From source file:org.jasig.cas.support.oauth.web.OAuth20RevokeTokenControllerTests.java
@Test public void verifyNoToken() throws Exception { final CentralOAuthService centralOAuthService = mock(CentralOAuthService.class); when(centralOAuthService.getToken(null)).thenThrow(new InvalidTokenException("error")); final MockHttpServletRequest mockRequest = new MockHttpServletRequest("POST", CONTEXT + OAuthConstants.REVOKE_URL); final MockHttpServletResponse mockResponse = new MockHttpServletResponse(); final OAuth20WrapperController oauth20WrapperController = new OAuth20WrapperController(); oauth20WrapperController.setCentralOAuthService(centralOAuthService); oauth20WrapperController.afterPropertiesSet(); final ModelAndView modelAndView = oauth20WrapperController.handleRequest(mockRequest, mockResponse); assertNull(modelAndView);/*www.j a v a2 s. co m*/ assertEquals(HttpStatus.SC_BAD_REQUEST, mockResponse.getStatus()); assertEquals(CONTENT_TYPE, mockResponse.getContentType()); final ObjectMapper mapper = new ObjectMapper(); final String expected = "{\"error\":\"" + OAuthConstants.INVALID_REQUEST + "\",\"error_description\":\"Invalid Token\"}"; final JsonNode expectedObj = mapper.readTree(expected); final JsonNode receivedObj = mapper.readTree(mockResponse.getContentAsString()); assertEquals(expectedObj.get("error").asText(), receivedObj.get("error").asText()); assertEquals(expectedObj.get("error_description").asText(), receivedObj.get("error_description").asText()); }
From source file:org.jasig.cas.support.oauth.web.OAuth20RevokeClientPrincipalTokensControllerTests.java
@Test public void verifyNoTokenOrAuthHeader() throws Exception { final MockHttpServletRequest mockRequest = new MockHttpServletRequest("POST", CONTEXT + OAuthConstants.REVOKE_URL); mockRequest.setParameter(OAuthConstants.CLIENT_ID, CLIENT_ID); final MockHttpServletResponse mockResponse = new MockHttpServletResponse(); final OAuth20WrapperController oauth20WrapperController = new OAuth20WrapperController(); oauth20WrapperController.afterPropertiesSet(); final ModelAndView modelAndView = oauth20WrapperController.handleRequest(mockRequest, mockResponse); assertNull(modelAndView);// ww w . j a v a2s. c o m assertEquals(HttpStatus.SC_BAD_REQUEST, mockResponse.getStatus()); assertEquals(CONTENT_TYPE, mockResponse.getContentType()); final String expected = "{\"error\":\"" + OAuthConstants.INVALID_REQUEST + "\",\"error_description\":\"" + OAuthConstants.MISSING_ACCESS_TOKEN_DESCRIPTION + "\"}"; final ObjectMapper mapper = new ObjectMapper(); final JsonNode expectedObj = mapper.readTree(expected); final JsonNode receivedObj = mapper.readTree(mockResponse.getContentAsString()); assertEquals(expectedObj.get("error").asText(), receivedObj.get("error").asText()); assertEquals(expectedObj.get("error_description").asText(), receivedObj.get("error_description").asText()); }
From source file:com.navercorp.lucy.security.xss.servletfilter.XssEscapeServletFilterWrapperTest.java
@Test public void testPostMethodGetParameter() { request = new MockHttpServletRequest("POST", "/notExistUrl.do"); request.addParameter("title", "<b>Text</b>"); request.addParameter("globalParameter", "<b>Text</b>"); wrapper = new XssEscapeServletFilterWrapper(request, filter); assertThat(wrapper.getParameter("title"), is("<b>Text</b>")); assertThat(wrapper.getParameter("globalParameter"), is("<b>Text</b>")); request = new MockHttpServletRequest("POST", "/url1.do"); request.addParameter("title", "<b>Text</b>"); request.addParameter("mode", "<script>Text</script>"); request.addParameter("globalParameter", "<script>Text</script>"); wrapper = new XssEscapeServletFilterWrapper(request, filter); assertThat(wrapper.getParameter("title"), is("<b>Text</b>")); assertThat(wrapper.getParameter("mode"), is("<script>Text</script>")); assertThat(wrapper.getParameter("globalParameter"), is("<script>Text</script>")); }
From source file:org.openmrs.module.atomfeed.web.AtomFeedDownloadServletTest.java
/** * @see AtomFeedDownloadServlet#doHead(javax.servlet.http.HttpServletRequest, * javax.servlet.http.HttpServletResponse) * @verifies send not modified error if atom feed has not changed *//*from w w w . j a va2 s . c om*/ @Test public void doHead_shouldSendNotModifiedErrorIfAtomFeedHasNotChanged() throws Exception { // create servlet and corresponding request and response object to be sent AtomFeedDownloadServlet atomFeedDownloadServlet = new AtomFeedDownloadServlet(); MockHttpServletRequest request = new MockHttpServletRequest("HEAD", "/atomfeed"); request.setContextPath("/somecontextpath"); MockHttpServletResponse response = new MockHttpServletResponse(); // intentionally change atom feed in order to not depend from other tests AtomFeedUtil.objectCreated(new Encounter()); // read atom feed header specific information from the header file String headerFileContent = AtomFeedUtil.readFeedHeaderFile(); int contentLength = 0; String etagToken = ""; Date lastModified = null; if (StringUtils.isNotBlank(headerFileContent)) { contentLength = headerFileContent.length() + Integer .valueOf(StringUtils.substringBetween(headerFileContent, "<entriesSize>", "</entriesSize>")); etagToken = StringUtils.substringBetween(headerFileContent, "<versionId>", "</versionId>"); try { lastModified = new SimpleDateFormat(AtomFeedUtil.RFC_3339_DATE_FORMAT) .parse(StringUtils.substringBetween(headerFileContent, "<updated>", "</updated>")); } catch (ParseException e) { // ignore it here } } // set request headers request.addHeader("If-None-Match", '"' + etagToken + '"'); request.addHeader("If-Modified-Since", lastModified); atomFeedDownloadServlet.service(request, response); // check response headers Assert.assertEquals(contentLength, response.getContentLength()); Assert.assertEquals("application/atom+xml", response.getContentType()); Assert.assertEquals(HttpServletResponse.SC_NOT_MODIFIED, response.getStatus()); Assert.assertNotNull(response.getHeader("Last-Modified")); }
From source file:org.ngrinder.script.controller.MyHttpServletRequestWrapperTest.java
@Test public void testHandleRequest2() { HttpServletRequest req = new MockHttpServletRequest("GET", "http://127.0.0.1:80/hello/svnadmin/admin"); wrapper = new MyHttpServletRequestWrapper(req) { public String getRequestURI() { return "/hello/svnadmin/admin"; }/*from w w w . ja v a 2 s. c o m*/ @Override public String getContextPath() { return "/hello"; } }; String path = wrapper.getPathInfo(); assertThat(path, is("admin/admin")); }