List of usage examples for org.springframework.mock.web MockHttpServletResponse getContentAsString
public String getContentAsString() throws UnsupportedEncodingException
From source file:com.github.woonsan.katharsis.servlet.KatharsisServletTest.java
@Test public void testUnacceptableRequestContentType() throws Exception { MockHttpServletRequest request = new MockHttpServletRequest(servletContext); request.setMethod("GET"); request.setContextPath(""); request.setServletPath("/api"); request.setPathInfo("/tasks"); request.setRequestURI("/api/tasks"); request.setContentType(JsonApiMediaType.APPLICATION_JSON_API); request.addHeader("Accept", "application/xml"); request.addParameter("filter", "{\"name\":\"John\"}"); MockHttpServletResponse response = new MockHttpServletResponse(); katharsisServlet.service(request, response); assertEquals(HttpServletResponse.SC_UNSUPPORTED_MEDIA_TYPE, response.getStatus()); String responseContent = response.getContentAsString(); assertTrue(responseContent == null || "".equals(responseContent.trim())); }
From source file:test.com.tsc9526.monalisa.service.actions.AbstractActionTest.java
protected Response getRespone(MockHttpServletRequest req) throws Exception { MockHttpServletResponse resp = new MockHttpServletResponse(); req.addHeader("DEV_TEST", "true"); DbQueryHttpServlet ms = new DbQueryHttpServlet(); ms.service(req, resp);/* ww w . j a v a 2 s .com*/ String type = resp.getContentType(); Assert.assertTrue(type.indexOf("json") > 0 && type.indexOf("utf-8") > 0); String body = resp.getContentAsString(); Response r = Response.fromJson(body); if (req.getParameter("page") != null && r.getStatus() == 200) { int total = Integer.parseInt(resp.getHeader("X-Total-Page")); Assert.assertTrue(total >= 0); } return r; }
From source file:org.springsource.restbucks.payment.web.PaymentProcessIntegrationTest.java
/** * Cancels the order by issuing a delete request. Verifies the resource being inavailable after that. * /*from ww w . j a va 2s . c o m*/ * @param response the response that retrieved an order resource * @throws Exception */ private void cancelOrder(MockHttpServletResponse response) throws Exception { String content = response.getContentAsString(); LinkDiscoverer discoverer = getDiscovererFor(response); Link selfLink = discoverer.findLinkWithRel(Link.REL_SELF, content).expand(); Link cancellationLink = discoverer.findLinkWithRel(CANCEL_REL, content); mvc.perform(delete(cancellationLink.getHref())).andExpect(status().isNoContent()); mvc.perform(get(selfLink.getHref())).andExpect(status().isNotFound()); }
From source file:org.springsource.restbucks.payment.web.PaymentProcessIntegrationTest.java
/** * Follows the {@code orders} link returns the {@link Order}s found. * /*from w ww . j a v a 2 s.co m*/ * @param source * @return * @throws Exception */ private MockHttpServletResponse discoverOrdersResource(MockHttpServletResponse source) throws Exception { String content = source.getContentAsString(); Link ordersLink = getDiscovererFor(source).findLinkWithRel(ORDERS_REL, content); LOG.info("Root resource returned: " + content); LOG.info(String.format("Found orders link pointing to %s Following", ordersLink)); MockHttpServletResponse response = mvc.perform(get(ordersLink.expand().getHref())). // andExpect(status().isOk()). // andReturn().getResponse(); LOG.info("Found orders: " + response.getContentAsString()); return response; }
From source file:org.springsource.restbucks.payment.web.PaymentProcessIntegrationTest.java
/** * Creates a new {@link Order} by looking up the orders link from the source and posting the content of * {@code orders.json} to it. Verifies we get receive a {@code 201 Created} and a {@code Location} header. Follows the * location header to retrieve the {@link Order} just created. * //from w ww. j a v a2s. c o m * @param source * @return * @throws Exception */ private MockHttpServletResponse createNewOrder(MockHttpServletResponse source) throws Exception { String content = source.getContentAsString(); Link ordersLink = getDiscovererFor(source).findLinkWithRel(ORDERS_REL, content); ClassPathResource resource = new ClassPathResource("order.json"); byte[] data = Files.readAllBytes(resource.getFile().toPath()); MockHttpServletResponse result = mvc .perform(post(ordersLink.expand().getHref()).contentType(MediaType.APPLICATION_JSON).content(data)). // andExpect(status().isCreated()). // andExpect(header().string("Location", is(notNullValue()))). // andReturn().getResponse(); return mvc.perform(get(result.getHeader("Location"))).andReturn().getResponse(); }
From source file:org.springsource.restbucks.payment.web.PaymentProcessIntegrationTest.java
/** * Looks up the first {@link Order} from the orders representation using a JSONPath expression of * {@value #FIRST_ORDER_EXPRESSION}. Looks up the {@value Link#REL_SELF} link from the nested object and follows it to * lookup the representation. Verifies the {@code self}, {@code cancel}, and {@code update} link to be present. * /*from w ww .jav a 2 s .c o m*/ * @param source * @return * @throws Exception */ private MockHttpServletResponse accessFirstOrder(MockHttpServletResponse source) throws Exception { String content = source.getContentAsString(); String order = JsonPath.parse(content).read(JsonPath.compile(FIRST_ORDER_EXPRESSION), JSONObject.class) .toString(); Link orderLink = getDiscovererFor(source).findLinkWithRel("self", order).expand(); LOG.info(String.format("Picking first order using JSONPath expression %s", FIRST_ORDER_EXPRESSION)); LOG.info(String.format("Discovered self link pointing to %s Following", orderLink)); return mvc.perform(get(orderLink.getHref())). // andExpect(linkWithRelIsPresent(Link.REL_SELF)). // andExpect(linkWithRelIsPresent(CANCEL_REL)). // andExpect(linkWithRelIsPresent(UPDATE_REL)). // andExpect(linkWithRelIsPresent(PAYMENT_REL)).// andReturn().getResponse(); }
From source file:com.google.api.server.spi.response.ServletResponseResultWriterTest.java
@Test public void testWriteNull() throws Exception { MockHttpServletResponse response = new MockHttpServletResponse(); ServletResponseResultWriter writer = new ServletResponseResultWriter(response, null); writer.write(null);// w w w. j a v a2 s .co m assertEquals("", response.getContentAsString()); assertEquals(HttpServletResponse.SC_NO_CONTENT, response.getStatus()); }
From source file:newcontroller.handler.impl.DefaultResponseTest.java
@Test public void testBodyText() throws Exception { MockHttpServletResponse response = new MockHttpServletResponse(); Response res = new DefaultResponse(response, Arrays.asList(new StringHttpMessageConverter(), new GsonHttpMessageConverter())); HandlerBridge handlerBridge = res.body("Hello"); handlerBridge.bridge(new DefaultRequest(new MockHttpServletRequest()), res); assertThat(response.getContentAsString(), is("Hello")); }
From source file:org.springframework.data.rest.tests.mongodb.MongoWebTests.java
/** * @see DATAREST-247//www . ja v a 2 s.co m */ @Test public void executeQueryMethodWithPrimitiveReturnType() throws Exception { Link profiles = client.discoverUnique("profiles"); Link profileSearches = client.discoverUnique(profiles, "search"); Link countByTypeLink = client.discoverUnique(profileSearches, "countByType"); assertThat(countByTypeLink.isTemplated(), is(true)); assertThat(countByTypeLink.getVariableNames(), hasItem("type")); MockHttpServletResponse response = client.request(countByTypeLink.expand("Twitter")); assertThat(response.getContentAsString(), is("1")); }
From source file:org.springsource.restbucks.training.payment.web.PaymentProcessIntegrationTest.java
/** * Triggers the payment of an {@link Order} by following the {@code payment} link and submitting a credit card number * to it. Verifies that we get a {@code 201 Created} and the response contains an {@code order} link. After the * payment has been triggered we fake a cancellation to make sure it is rejected with a {@code 404 Not found}. * /*from ww w . j a v a 2s .c om*/ * @param response * @return * @throws Exception */ private MockHttpServletResponse triggerPayment(MockHttpServletResponse response) throws Exception { String content = response.getContentAsString(); LinkDiscoverer discoverer = getDiscovererFor(response); Link paymentLink = discoverer.findLinkWithRel(PAYMENT_REL, content); log.info(String.format("Discovered payment link pointing to %s", paymentLink)); assertThat(paymentLink, not(nullValue())); log.info("Triggering payment"); ResultActions action = mvc.perform( put(paymentLink.getHref()).content("\"1234123412341234\"").contentType(MediaType.APPLICATION_JSON)); MockHttpServletResponse result = action.andExpect(status().isCreated()). // andExpect(linkWithRelIsPresent(ORDER_REL)). // andReturn().getResponse(); log.info("Payment triggered"); // Make sure we cannot cheat and cancel the order after it has been payed log.info("Faking a cancel request to make sure it's forbidden"); Link selfLink = discoverer.findLinkWithRel(Link.REL_SELF, content); mvc.perform(delete(selfLink.getHref())).andExpect(status().isMethodNotAllowed()); return result; }