/** * @return The collected so far pushed data in the suspended response */ public String getPushedResponse() { return suspendedResponse != null ? suspendedResponse.getDocument() : null; }
/** * Delegate to initialize method. */ @Override public void reset() { initialize(); }
/** * @return binary response */ public String getBinaryResponse() { String ctheader = getHeader("Content-Length"); if (ctheader == null) { return getDocument(); } else { return getDocument().substring(0, Integer.valueOf(ctheader)); } }
int status = response.getStatus(); if(status>=HttpServletResponse.SC_OK+100) throw new IOException("Code: "+response.getStatus()+" Message: "+response.getErrorMessage()+" Content: "+response.getDocument()); return response.getDocument();
response = new MockHttpServletResponse(request); List<Cookie> lastResponseCookies = lastResponse.getCookies(); if (lastResponse.isRedirect()) for (Cookie cookie : responseCookies.allAsList()) response.addCookie(cookie);
/** * Asserts that the Ajax location header is present. */ public void assertAjaxLocation() { if (null != getLastResponse().getHeader("Location")) { fail("Location header should *not* be present when using Ajax"); } String ajaxLocation = getLastResponse().getHeader("Ajax-Location"); if (null == ajaxLocation) { throw new AssertionFailedError("Ajax-Location header should be present when using Ajax"); } int statusCode = getLastResponse().getStatus(); if (statusCode != 200) { throw new AssertionFailedError("Expected HTTP status code to be 200 (OK)"); } }
@Test public void testRetrieveContent() { tester.executeUrl(OContentShareResource.urlFor(doc, "name", null, false).toString()); MockHttpServletResponse response = tester.getLastResponse(); assertEquals(response.getStatus(), HttpServletResponse.SC_OK); }
MockHttpServletResponse servletResponse = new MockHttpServletResponse(servletRequest);
/** * @return binary response */ public String getBinaryResponse() { String ctheader = getHeader("Content-Length"); if (ctheader == null) { return getDocument(); } else { return getDocument().substring(0, Integer.valueOf(ctheader)); } }
response = new MockHttpServletResponse(request); List<Cookie> lastResponseCookies = lastResponse.getCookies(); if (lastResponse.isRedirect()) for (Cookie cookie : responseCookies.allAsList()) response.addCookie(cookie);
/** * Asserts that the Ajax location header is present. */ public void assertAjaxLocation() { if (null != getLastResponse().getHeader("Location")) { fail("Location header should *not* be present when using Ajax"); } String ajaxLocation = getLastResponse().getHeader("Ajax-Location"); if (null == ajaxLocation) { fail("Ajax-Location header should be present when using Ajax"); } int statusCode = getLastResponse().getStatus(); if (statusCode != 200) { fail("Expected HTTP status code to be 200 (OK)"); } }
@Test public void testRetrieveNotExistsContent() { tester.executeUrl(OContentShareResource.urlFor(doc, "notFound", "text/plain", false).toString()); MockHttpServletResponse response = tester.getLastResponse(); assertEquals(response.getStatus(), HttpServletResponse.SC_NOT_FOUND); }
//I assumed that you want to use the current user's session for rendering. If this isn't the case, you'll have to use a mock session MockHttpServletRequest mockReq = new MockHttpServletRequest( WebApplication.get(), ((WebRequest)getRequest()).getHttpServletRequest().getSession(), WebApplication.get().getServletContext() ); MockHttpServletResponse mockRes = new MockHttpServletResponse( mockReq ); WebResponse res = new WebResponse(mockRes); ServletWebRequest req = new ServletWebRequest( mockReq ); RequestCycle cycle = new WebRequestCycle( WebApplication.get(), req, res ); PageParameters pp = new PageParameters(); //add page parameters here //Your email page should really be a bookmarkable page, but if it isn't, you can replace the request target with something that better suits your case cycle.request( new BookmarkablePageRequestTarget( EmailPage.class, pp )); System.out.println( mockRes.getDocument() );
@Test public void testRetrieveEmptyContent() { tester.executeUrl(OContentShareResource.urlFor(doc, "phone", "text/plain", false).toString()); MockHttpServletResponse response = tester.getLastResponse(); assertEquals(response.getStatus(), HttpServletResponse.SC_NOT_FOUND); } }
/** * Delegate to initialize method. */ @Override public void reset() { initialize(); }
/** * The last response as a string when a page is tested via {@code startPage()} methods. * <p> * In case the processed component was not a {@link Page} then the automatically created page * markup gets removed. If you need the whole returned markup in this case use * {@link #getLastResponse()}{@link MockHttpServletResponse#getDocument() .getDocument()} * </p> * * @return last response as String. */ public String getLastResponseAsString() { String response = lastResponse.getDocument(); // null, if a Page was rendered last if (componentInPage == null) { return response; } // remove the markup for the auto-generated page. leave just component's markup int end = response.lastIndexOf("</body>"); if (end > -1) { int start = response.indexOf("<body>") + "<body>".length(); response = response.substring(start, end); } return response; }
/** * Create the response object. * * @param servletRequest */ public MockHttpServletResponse(MockHttpServletRequest servletRequest) { this.servletRequest = servletRequest; initialize(); }
/** * The last response as a string when a page is tested via {@code startPage()} methods. * <p> * In case the processed component was not a {@link Page} then the automatically created page * markup gets removed. If you need the whole returned markup in this case use * {@link #getLastResponse()}{@link MockHttpServletResponse#getDocument() .getDocument()} * </p> * * @return last response as String. */ public String getLastResponseAsString() { String response = lastResponse.getDocument(); // null, if a Page was rendered last if (componentInPage == null) { return response; } // remove the markup for the auto-generated page. leave just component's markup int end = response.lastIndexOf("</body>"); if (end > -1) { int start = response.indexOf("<body>") + "<body>".length(); response = response.substring(start, end); } return response; }
/** * Create the response object. * * @param servletRequest */ public MockHttpServletResponse(MockHttpServletRequest servletRequest) { initialize(); }
public final void checkToastrSuccessMessage(String expectedMessage) { List<String> foundMessages = new ArrayList<>(); IOUtils.lineIterator(new StringReader(getLastResponse().getDocument())) .forEachRemaining( line -> { if (line.startsWith("toastr.success")) { String message = extractMessage(line); foundMessages.add(message); } } ); if (!foundMessages.contains(expectedMessage)) { throw new AssertionError(String.format("Não foi possível encontrar a mensagem '%s', mensagens encontradas: '%s'", expectedMessage, foundMessages)); } }