List of usage examples for org.springframework.mock.web MockHttpServletRequest addHeader
public void addHeader(String name, Object value)
From source file:com.tasktop.c2c.server.common.service.tests.ajp.AjpProxyWebTest.java
@Test public void testProxyHandlesCookies() throws Exception { final String ajpBaseUri = String.format("ajp://localhost:%s", container.getAjpPort()); Payload payload = new Payload(HttpServletResponse.SC_OK, "some content\none two three\n\nfour"); payload.getResponseHeaders().put("foo", "bar"); payload.getSessionVariables().put("s1", "v1"); TestServlet.setResponsePayload(payload); MockHttpServletRequest request = new MockHttpServletRequest(); request.setMethod("GET"); request.setRequestURI("/test"); request.setQueryString("a=b"); request.setParameter("a", new String[] { "b" }); request.addHeader("c", "d ef"); MockHttpServletResponse response = new MockHttpServletResponse(); proxy.proxyRequest(ajpBaseUri + "/foo", request, response); Request firstRequest = null;//w ww . ja v a 2 s.co m for (int i = 0; i < 100; i++) { firstRequest = TestServlet.getLastRequest(); // If our request is not yet there, then pause and retry shortly - proxying is an async process, and this // request was sometimes coming back as null which was causing test failures on the first assert below. if (firstRequest == null) { Thread.sleep(10); } else { // Got our request, so break now. break; } } Assert.assertTrue(firstRequest.isNewSession()); Assert.assertEquals("v1", firstRequest.getSessionAttributes().get("s1")); List<org.apache.commons.httpclient.Cookie> cookies = new ArrayList<org.apache.commons.httpclient.Cookie>(); for (String headerName : response.getHeaderNames()) { if (headerName.equalsIgnoreCase("set-cookie") || headerName.equalsIgnoreCase("set-cookie2")) { cookies.addAll(Arrays.asList(new RFC2965Spec().parse("localhost", container.getPort(), "/", false, response.getHeader(headerName).toString()))); } } Assert.assertEquals(1, cookies.size()); Cookie cookie = cookies.get(0); Assert.assertEquals("almp.JSESSIONID", cookie.getName()); MockHttpServletRequest request2 = new MockHttpServletRequest(); request2.setMethod("GET"); request2.setRequestURI("/test"); request2.addHeader("Cookie", cookie.toExternalForm()); MockHttpServletResponse response2 = new MockHttpServletResponse(); payload = new Payload(HttpServletResponse.SC_OK, "test"); TestServlet.setResponsePayload(payload); proxy.proxyRequest(ajpBaseUri + "/foo", request2, response2); Request secondRequest = TestServlet.getLastRequest(); Assert.assertFalse(secondRequest.isNewSession()); Assert.assertEquals(firstRequest.getSessionId(), secondRequest.getSessionId()); Assert.assertEquals("v1", secondRequest.getSessionAttributes().get("s1")); }
From source file:nl.dtls.fairdatapoint.api.controller.DataAccessorControllerTest.java
/** * Check supported accept headers.//from w w w.j a v a 2 s . c om * * @throws Exception */ @Test public void supportedAcceptHeaders() throws Exception { MockHttpServletRequest request; MockHttpServletResponse response; Object handler; request = new MockHttpServletRequest(); response = new MockHttpServletResponse(); request.setMethod("GET"); request.addHeader(HttpHeaders.ACCEPT, "text/turtle"); request.setRequestURI("/textmining/gene-disease-association_lumc/sparql"); handler = handlerMapping.getHandler(request).getHandler(); handlerAdapter.handle(request, response, handler); assertEquals(HttpServletResponse.SC_OK, response.getStatus()); request.addHeader(HttpHeaders.ACCEPT, "text/n3"); handler = handlerMapping.getHandler(request).getHandler(); handlerAdapter.handle(request, response, handler); assertEquals(HttpServletResponse.SC_OK, response.getStatus()); request.addHeader(HttpHeaders.ACCEPT, "application/ld+json"); handler = handlerMapping.getHandler(request).getHandler(); handlerAdapter.handle(request, response, handler); assertEquals(HttpServletResponse.SC_OK, response.getStatus()); request.addHeader(HttpHeaders.ACCEPT, "application/rdf+xml"); handler = handlerMapping.getHandler(request).getHandler(); handlerAdapter.handle(request, response, handler); assertEquals(HttpServletResponse.SC_OK, response.getStatus()); }
From source file:com.xyxy.platform.modules.core.web.ServletsTest.java
@Test public void checkIfNoneMatch() { MockHttpServletRequest request = new MockHttpServletRequest(); MockHttpServletResponse response = new MockHttpServletResponse(); // Header,true,? assertThat(Servlets.checkIfNoneMatchEtag(request, response, "V1.0")).isTrue(); // If-None-Match Header request.addHeader("If-None-Match", "V1.0,V1.1"); // Etag//ww w. j a va2 s . co m assertThat(Servlets.checkIfNoneMatchEtag(request, response, "V1.0")).isFalse(); // ?Etag assertThat(Servlets.checkIfNoneMatchEtag(request, response, "V2.0")).isTrue(); }
From source file:org.n52.sos.service.it.MockHttpClient.java
private MockHttpServletRequest build() { try {// ww w .j a v a 2s. c o m final MockHttpServletRequest req = new MockHttpServletRequest(context); req.setMethod(method); for (String header : headers.keySet()) { for (String value : headers.get(header)) { req.addHeader(header, value); } } final StringBuilder queryString = new StringBuilder(); if (query != null && !query.isEmpty()) { boolean first = true; for (String key : query.keySet()) { final Set<String> values = query.get(key); req.addParameter(key, values.toArray(new String[values.size()])); if (first) { queryString.append("?"); first = false; } else { queryString.append("&"); } queryString.append(key).append("="); Iterator<String> i = values.iterator(); queryString.append(i.next()); while (i.hasNext()) { queryString.append(",").append(i.next()); } } req.setQueryString(queryString.toString()); } req.setRequestURI(path + queryString.toString()); if (path == null) { path = "/"; } req.setPathInfo(path); if (content != null) { req.setContent(content.getBytes(MockHttpExecutor.ENCODING)); } return req; } catch (UnsupportedEncodingException ex) { throw new RuntimeException(ex); } }
From source file:org.openmrs.module.hl7query.web.controller.HL7QueryControllerTest.java
/** * @see {@link HL7QueryController#getEncounters(String,String,String,Date,Date,HttpServletRequest)} *///www. j av a 2 s . c om @Test @Verifies(value = "should return the expected hl7 output as xml if the xml header exists", method = "getEncounters(String,String,String,Date,Date,HttpServletRequest)") public void getEncounters_shouldReturnTheExpectedHl7OutputAsXmlIfTheXmlHeaderExists() throws Exception { //TODO Add the ORU_R01.ORDER_OBSERVATION tags(obs) to the test file 'expectedORUR01Output.xml' //When https://tickets.openmrs.org/browse/HLQRY-38 is completed String expectedOutput = IOUtils .toString(getClass().getClassLoader().getResourceAsStream("expectedORUR01XmlOutput.xml")); expectedOutput = StringUtils.deleteWhitespace(expectedOutput); MockHttpServletRequest request = new MockHttpServletRequest(); MockHttpServletResponse response = new MockHttpServletResponse(); request.addHeader("Accept", "text/xml"); String hl7Output = new HL7QueryController() .getEncounters(null, null, ENCOUNTER_1_UUID, null, null, request, response).toString(); hl7Output = StringUtils.deleteWhitespace(hl7Output); //Ignore timestamp by removing it hl7Output = StringUtils.replace(hl7Output, StringUtils.substringBetween(hl7Output, "<TS.1>", "</TS.1>"), ""); //Ignore the uuid of the message hl7Output = StringUtils.replace(hl7Output, StringUtils.substringBetween(hl7Output, "<MSH.10>", "</MSH.10>"), ""); Assert.assertEquals(expectedOutput, hl7Output); }
From source file:org.openmrs.module.hl7query.web.controller.HL7QueryControllerTest.java
@Test @Verifies(value = "should return appopriately formed error message if encounter uuid is not found", method = "getEncounters(String,String,String,Date,Date,HttpServletRequest)") public void getEncounters_shouldReturnAppopriatelyFormedErrorMessageIfEncounterUuidIsNotFound() throws Exception { //Test the xml formatted error message String expectedOutput = IOUtils .toString(getClass().getClassLoader().getResourceAsStream("missingEncounterUuidXmlError.xml")); expectedOutput = StringUtils.deleteWhitespace(expectedOutput); MockHttpServletRequest request = new MockHttpServletRequest(); MockHttpServletResponse response = new MockHttpServletResponse(); request.addHeader("Accept", "text/xml"); String hl7Output = new HL7QueryController().getEncounters(null, null, "0", null, null, request, response) .toString();/*from ww w . j a va 2 s. c o m*/ hl7Output = StringUtils.deleteWhitespace(hl7Output); Assert.assertEquals(hl7Output, expectedOutput); //Test the json error message String expectedPipeOutput = IOUtils .toString(getClass().getClassLoader().getResourceAsStream("missingEncounterUuidJsonError.json")); MockHttpServletRequest pipeRequest = new MockHttpServletRequest(); MockHttpServletResponse pipeResponse = new MockHttpServletResponse(); String hl7PipeOutput = new HL7QueryController() .getEncounters(null, null, "0", null, null, pipeRequest, pipeResponse).toString(); Assert.assertEquals(hl7PipeOutput, expectedPipeOutput); }
From source file:org.openmrs.module.hl7query.web.controller.HL7QueryControllerTest.java
@Test @Verifies(value = "should return appopriately formed error message if patient id and encounter uuid are null", method = "getEncounters(String,String,String,Date,Date,HttpServletRequest)") public void getEncounters_shouldReturnAppopriatelyFormedErrorMessageIfPatientIdAndEncounterUuidAreNull() throws Exception { //Test the xml formatted error message String expectedOutput = IOUtils .toString(getClass().getClassLoader().getResourceAsStream("missingIdentifiersXmlError.xml")); expectedOutput = StringUtils.deleteWhitespace(expectedOutput); MockHttpServletRequest request = new MockHttpServletRequest(); MockHttpServletResponse response = new MockHttpServletResponse(); request.addHeader("Accept", "text/xml"); String hl7Output = new HL7QueryController().getEncounters(null, null, null, null, null, request, response) .toString();//from w ww . ja v a2 s. co m hl7Output = StringUtils.deleteWhitespace(hl7Output); Assert.assertEquals(hl7Output, expectedOutput); //Test the json error message String expectedPipeOutput = IOUtils .toString(getClass().getClassLoader().getResourceAsStream("missingIdentifiersJsonError.json")); expectedPipeOutput = StringUtils.deleteWhitespace(expectedPipeOutput); MockHttpServletRequest pipeRequest = new MockHttpServletRequest(); MockHttpServletResponse pipeResponse = new MockHttpServletResponse(); String hl7PipeOutput = new HL7QueryController() .getEncounters(null, null, null, null, null, pipeRequest, pipeResponse).toString(); hl7Output = StringUtils.deleteWhitespace(hl7PipeOutput); Assert.assertEquals(hl7PipeOutput, expectedPipeOutput); }
From source file:io.pivotal.cla.mvc.github.GitHubHooksControllerTests.java
private MockHttpServletRequestBuilder hookRequest() { MockHttpServletRequestBuilder post = post("/github/hooks/pull_request/pivotal") .with(new RequestPostProcessor() { @Override/*from w ww .j a va 2 s. c o m*/ public MockHttpServletRequest postProcessRequest(MockHttpServletRequest request) { if (accessToken != null) { try { String signature = getSignature(request); request.addHeader("X-Hub-Signature", signature); } catch (Exception e) { throw new RuntimeException(e); } } return request; } private String getSignature(MockHttpServletRequest request) throws IOException, UnsupportedEncodingException, Exception { String body = IOUtils.toString(request.getReader()); String signature = oauth.create(body, accessToken.getToken()); return signature; } }); return post; }
From source file:org.openmrs.module.hl7query.web.controller.HL7QueryControllerTest.java
/** * @see {@link HL7QueryController#getEncounters(String,String,String,Date,Date,HttpServletRequest)} *//*from w w w. j a v a 2s .c o m*/ @Test @Verifies(value = "should return the patient encounters given the patient identifier and id type", method = "getEncounters(String,String,String,Date,Date,HttpServletRequest)") public void getEncounters_shouldReturnThePatientEncountersGivenThePatientIdentifierAndIdType() throws Exception { //TODO Add the ORU_R01.ORDER_OBSERVATION tags(obs) to the test file 'expectedOutput_encountersByPatient.xml' //When https://tickets.openmrs.org/browse/HLQRY-38 is completed String expectedOutput = IOUtils.toString( getClass().getClassLoader().getResourceAsStream("expectedOutput_encountersByPatient.xml")); expectedOutput = StringUtils.deleteWhitespace(expectedOutput); final String identifier = "6TS-4"; final String identifierTypeUuid = "1a339fe9-38bc-4ab3-b180-320988c0b968"; final int expectedEncounterCount = 3; Patient patient = Context.getPatientService().getPatient(7); //sanity checks Assert.assertEquals(identifier, patient.getPatientIdentifier().getIdentifier()); Assert.assertEquals(identifierTypeUuid, patient.getPatientIdentifier().getIdentifierType().getUuid()); Assert.assertEquals(expectedEncounterCount, Context.getEncounterService().getEncountersByPatient(patient).size()); MockHttpServletRequest request = new MockHttpServletRequest(); MockHttpServletResponse response = new MockHttpServletResponse(); request.addHeader("Accept", "text/xml"); String hl7Output = new HL7QueryController() .getEncounters(identifier, identifierTypeUuid, null, null, null, request, response).toString(); hl7Output = StringUtils.deleteWhitespace(hl7Output); //Ignore timestamp by removing it hl7Output = StringUtils.replace(hl7Output, StringUtils.substringBetween(hl7Output, "<TS.1>", "</TS.1>"), ""); //Ignore the uuid of the message hl7Output = StringUtils.replace(hl7Output, StringUtils.substringBetween(hl7Output, "<MSH.10>", "</MSH.10>"), ""); Assert.assertEquals(expectedOutput, hl7Output); }
From source file:org.openmrs.module.hl7query.web.controller.HL7QueryControllerTest.java
/** * @see {@link HL7QueryController#getEncounters(String,String,String,Date,Date,HttpServletRequest)} *///from w w w . j a v a 2 s .c o m @Test @Verifies(value = "should return the patient encounters matching specified start and end encounter dates", method = "getEncounters(String,String,String,Date,Date,HttpServletRequest)") public void getEncounters_shouldReturnThePatientEncountersMatchingSpecifiedStartAndEndEncounterDates() throws Exception { //TODO Add the ORU_R01.ORDER_OBSERVATION tags(obs) to the test file 'expectedOutput_encountersByStartAndEndDate.xml' //When https://tickets.openmrs.org/browse/HLQRY-38 is completed String expectedOutput = IOUtils.toString( getClass().getClassLoader().getResourceAsStream("expectedOutput_encountersByStartAndEndDate.xml")); expectedOutput = StringUtils.deleteWhitespace(expectedOutput); final String identifier = "6TS-4"; final String identifierTypeUuid = "1a339fe9-38bc-4ab3-b180-320988c0b968"; Patient patient = Context.getPatientService().getPatient(7); //sanity checks Assert.assertEquals(identifier, patient.getPatientIdentifier().getIdentifier()); Assert.assertEquals(identifierTypeUuid, patient.getPatientIdentifier().getIdentifierType().getUuid()); MockHttpServletRequest request = new MockHttpServletRequest(); MockHttpServletResponse response = new MockHttpServletResponse(); request.addHeader("Accept", "text/xml"); Encounter expectedEncounter = Context.getEncounterService().getEncounter(4); String hl7Output = new HL7QueryController() .getEncounters(identifier, identifierTypeUuid, null, expectedEncounter.getEncounterDatetime(), expectedEncounter.getEncounterDatetime(), request, response) .toString(); hl7Output = StringUtils.deleteWhitespace(hl7Output); //Ignore timestamp by removing it hl7Output = StringUtils.replace(hl7Output, StringUtils.substringBetween(hl7Output, "<TS.1>", "</TS.1>"), ""); //Ignore the uuid of the message hl7Output = StringUtils.replace(hl7Output, StringUtils.substringBetween(hl7Output, "<MSH.10>", "</MSH.10>"), ""); Assert.assertEquals(expectedOutput, hl7Output); }