List of usage examples for org.springframework.mock.web MockHttpServletRequest addHeader
public void addHeader(String name, Object value)
From source file:org.cateproject.test.functional.mockmvc.HtmlUnitRequestBuilder.java
private void headers(MockHttpServletRequest result) { for (Entry<String, String> header : webRequest.getAdditionalHeaders().entrySet()) { result.addHeader(header.getKey(), header.getValue()); }// w ww . j a v a 2 s . co m }
From source file:com.tasktop.c2c.server.common.service.tests.http.HttpProxyTest.java
@Test public void testCookieRequestCookieNoTrailingSemi() throws IOException { setupMock(HttpStatus.OK);// ww w .jav a 2 s.c o m MockHttpServletRequest request = new MockHttpServletRequest("GET", "unused"); MockHttpServletResponse response = new MockHttpServletResponse(); final String cookieHeaderValue = "$Version=\"1\"; almp.Part_Number=\"Rocket_Launcher_0001\""; request.addHeader("Cookie", cookieHeaderValue); proxy.proxyRequest("foo", request, response); List<String> proxyCookie = getProxyRequestHeaderValues("Cookie"); Assert.assertNotNull(proxyCookie); Assert.assertEquals(1, proxyCookie.size()); Assert.assertEquals("$Version=\"1\"; Part_Number=\"Rocket_Launcher_0001\";", proxyCookie.get(0)); context.assertIsSatisfied(); }
From source file:com.tasktop.c2c.server.common.service.tests.http.HttpProxyTest.java
@Test public void testBasicGet() throws IOException { setupMock(HttpStatus.OK);/*from w w w . j a v a 2 s.c o m*/ MockHttpServletRequest clientRequest = new MockHttpServletRequest("GET", "unused"); String randomRequestHeader = "RandomRequestHeader"; String randomResponseHeader = "RandomResponseHeader"; String connectionHeader = "Connection"; clientRequest.addHeader(randomRequestHeader, "RandomHeaderValue"); clientRequest.addHeader(connectionHeader, "ConnectionValue"); proxyResponseHeaders.add(new Header(randomResponseHeader, "RandomHeaderValue")); proxyResponseHeaders.add(new Header(connectionHeader, "ConnectionValue")); // Should not pass along MockHttpServletResponse clientResponse = new MockHttpServletResponse(); proxy.proxyRequest("foo", clientRequest, clientResponse); Assert.assertNotNull(getProxyRequestHeaderValues(randomRequestHeader)); Assert.assertNull(getProxyRequestHeaderValues(connectionHeader)); Assert.assertTrue(clientResponse.containsHeader(randomResponseHeader)); Assert.assertFalse(clientResponse.containsHeader(connectionHeader)); // FIXME, unsure if this req is correct context.assertIsSatisfied(); }
From source file:com.alexshabanov.springrestapi.restapitest.RestOperationsTestClient.java
private MockHttpServletRequest toMockHttpServletRequest(URI url, HttpMethod method, ClientHttpRequest clientHttpRequest) throws IOException { final MockHttpServletRequest mockHttpServletRequest = new MockHttpServletRequest(method.name(), url.getPath());/*from ww w. j a v a 2s . c om*/ // copy headers final HttpHeaders headers = clientHttpRequest.getHeaders(); for (final String headerKey : headers.toSingleValueMap().keySet()) { final List<String> headerValues = headers.get(headerKey); for (final String headerValue : headerValues) { mockHttpServletRequest.addHeader(headerKey, headerValue); } } // copy query parameters final String query = clientHttpRequest.getURI().getQuery(); if (query != null) { mockHttpServletRequest.setQueryString(query); final String[] queryParameters = query.split("&"); for (String keyValueParam : queryParameters) { final String[] components = keyValueParam.split("="); if (components.length == 1) { continue; // optional parameter } Assert.isTrue(components.length == 2, "Can't split query parameters " + keyValueParam + " by key-value pair"); mockHttpServletRequest.setParameter(components[0], components[1]); } } // copy request body // TODO: another byte copying approach here // TODO: for now we rely to the fact that request body is always presented as byte array output stream final OutputStream requestBodyStream = clientHttpRequest.getBody(); if (requestBodyStream instanceof ByteArrayOutputStream) { mockHttpServletRequest.setContent(((ByteArrayOutputStream) requestBodyStream).toByteArray()); } else { throw new AssertionError("Ooops, client http request has non-ByteArrayOutputStream body"); } return mockHttpServletRequest; }
From source file:com.tasktop.c2c.server.common.service.tests.http.HttpProxyTest.java
@Test public void testCookieRequestCookieValueUnquoted() throws IOException { setupMock(HttpStatus.OK);/*from w ww . java 2 s . co m*/ MockHttpServletRequest request = new MockHttpServletRequest("GET", "unused"); MockHttpServletResponse response = new MockHttpServletResponse(); final String cookieHeaderValue = "$Version=\"1\"; almp.Part_Number=Rocket_Launcher_0001; $Path=\"/acme2\";"; request.addHeader("Cookie", cookieHeaderValue); proxy.proxyRequest("foo", request, response); List<String> proxyCookie = getProxyRequestHeaderValues("Cookie"); Assert.assertNotNull(proxyCookie); Assert.assertEquals(1, proxyCookie.size()); Assert.assertEquals("$Version=\"1\"; Part_Number=Rocket_Launcher_0001; $Path=\"/acme2\";", proxyCookie.get(0)); context.assertIsSatisfied(); }
From source file:org.openmrs.module.atomfeed.web.AtomFeedDownloadServletTest.java
/** * @see AtomFeedDownloadServlet#doHead(javax.servlet.http.HttpServletRequest, * javax.servlet.http.HttpServletResponse) * @verifies send valid headers if atom feed has changed *//*from w ww. ja va 2 s . c o m*/ @Test public void doHead_shouldSendValidHeadersIfAtomFeedHasChanged() 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()); String etagToken = "somevalue"; Date lastModified = new Date(); // set request headers request.addHeader("If-None-Match", '"' + etagToken + '"'); request.addHeader("If-Modified-Since", lastModified); atomFeedDownloadServlet.service(request, response); // check response headers Assert.assertNotSame(0, response.getContentLength()); Assert.assertEquals("application/atom+xml", response.getContentType()); Assert.assertNotSame(HttpServletResponse.SC_NOT_MODIFIED, response.getStatus()); Assert.assertNotSame('"' + etagToken + '"', response.getHeader("Etag")); Assert.assertNotNull(response.getHeader("Last-Modified")); }
From source file:org.jasig.cas.support.oauth.web.OAuth20ProfileControllerTests.java
@Test public void verifyNoTokenAndAuthHeaderIsMalformed() throws Exception { final MockHttpServletRequest mockRequest = new MockHttpServletRequest("GET", CONTEXT + OAuthConstants.PROFILE_URL); mockRequest.addHeader("Authorization", "Let me in i am authorized"); final MockHttpServletResponse mockResponse = new MockHttpServletResponse(); final OAuth20WrapperController oauth20WrapperController = new OAuth20WrapperController(); oauth20WrapperController.afterPropertiesSet(); final ModelAndView modelAndView = oauth20WrapperController.handleRequest(mockRequest, mockResponse); assertNull(modelAndView);//from ww w. j av a 2 s. co m assertEquals(HttpStatus.SC_BAD_REQUEST, mockResponse.getStatus()); assertEquals(CONTENT_TYPE, mockResponse.getContentType()); final String expected = "{\"error\":\"" + OAuthConstants.MISSING_ACCESS_TOKEN + "\",\"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.tasktop.c2c.server.common.service.tests.http.HttpProxyTest.java
@Test public void testCookieRequestNonPrefixedCookiesNotFiltered() throws IOException { setupMock(HttpStatus.OK);// w w w . java 2s.c o m MockHttpServletRequest request = new MockHttpServletRequest("GET", "unused"); MockHttpServletResponse response = new MockHttpServletResponse(); final String cookieHeaderValue = "$Version=\"1\"; Customer=\"WILE_E_COYOTE\"; $Path=\"/acme\"; Part_Number=\"Rocket_Launcher_0001\"; $Path=\"/acme\"; Shipping=\"FedEx\"; $Path=\"/acme\";"; request.addHeader("Cookie", cookieHeaderValue); proxy.proxyRequest("foo", request, response); List<String> proxyCookie = getProxyRequestHeaderValues("Cookie"); Assert.assertNotNull(proxyCookie); Assert.assertEquals(1, proxyCookie.size()); Assert.assertEquals(cookieHeaderValue, proxyCookie.get(0)); context.assertIsSatisfied(); }
From source file:org.openmrs.module.webservices.rest.web.v1_0.controller.openmrs1_8.ConceptController1_8Test.java
@Test public void shouldGetAConceptByUuidInXML() throws Exception { MockHttpServletRequest req = request(RequestMethod.GET, getURI() + "/15f83cd6-64e9-4e06-a5f9-364d3b14a43d"); req.addHeader("Accept", "application/xml"); MockHttpServletResponse result = handle(req); String xml = result.getContentAsString(); printXML(xml);/* w w w. j av a2 s .c om*/ Assert.assertEquals("15f83cd6-64e9-4e06-a5f9-364d3b14a43d", evaluateXPath(xml, "//uuid")); Assert.assertEquals("ASPIRIN", evaluateXPath(xml, "//name/name")); }
From source file:com.tasktop.c2c.server.common.service.tests.ajp.AjpProtocolTest.java
@Test // Trying to recreate task 718 public void testGetThenPost() throws Exception { final byte[] POST_REQUEST_PAYLOAD = createData(431); GenericKeyedObjectPool uniSocketPool = new GenericKeyedObjectPool(new AjpPoolableConnectionFactory()); uniSocketPool.setLifo(true);// w ww.j a v a 2 s. co m uniSocketPool.setMaxIdle(1); uniSocketPool.setMaxTotal(1); uniSocketPool.setMinIdle(1); protocol.setSocketPool(uniSocketPool); for (int i = 0; i < 10; i++) { MockHttpServletRequest getRequest = new MockHttpServletRequest(); getRequest.setMethod("GET"); getRequest.setRequestURI("/alm/s/code2cloud/scm/test4.git/info/refs"); getRequest.setQueryString("service=git-receive-pack"); getRequest.addParameter("service", "git-receive-pack"); getRequest.addHeader("Authorization", AUTH_HEADER); getRequest.addHeader("Host", "localhost:8888"); getRequest.addHeader("User-Agent", "git/1.7.3.1"); getRequest.addHeader("Accept", "*/*"); getRequest.addHeader("Pragma", "no-cache"); Payload getPayload = new Payload(); getPayload.setResponseCode(HttpServletResponse.SC_OK); getPayload.setCharacterContent(GET_RESPONSE_PAYLOAD); MockHttpServletResponse response = new MockHttpServletResponse(); TestServlet.setResponsePayload(getPayload); protocol.forward(getRequest, response); assertRequestIsExpected(getRequest, TestServlet.getLastRequest()); assertResponseIsExpected(getPayload, response); MockHttpServletRequest postRequest = new MockHttpServletRequest(); postRequest.setMethod("POST"); postRequest.setRequestURI("/alm/s/code2cloud/scm/test4.git/git-receive-pack"); postRequest.addHeader("Authorization", AUTH_HEADER); postRequest.addHeader("Host", "localhost:8888"); postRequest.addHeader("Content-Length", "" + POST_REQUEST_PAYLOAD.length); postRequest.addHeader("Accept-Encoding", "deflate, gzip"); postRequest.addHeader("User-Agent", "git/1.7.3.1"); postRequest.addHeader("Accept", "application/x-git-receive-pack-result"); postRequest.addHeader("Content-Type", "application/x-git-receive-pack-request"); postRequest.setContent(POST_REQUEST_PAYLOAD); Payload postPayload = new Payload(); postPayload.setResponseCode(HttpServletResponse.SC_OK); postPayload.setCharacterContent(POST_RESPONSE_PAYLOAD); response = new MockHttpServletResponse(); TestServlet.setResponsePayload(postPayload); protocol.forward(postRequest, response); assertRequestIsExpected(postRequest, TestServlet.getLastRequest()); assertResponseIsExpected(postPayload, response); } }