/** * Print the response. */ protected void printResponse(MockHttpServletResponse response) throws Exception { String body = (response.getCharacterEncoding() != null ? response.getContentAsString() : MISSING_CHARACTER_ENCODING); this.printer.printValue("Status", response.getStatus()); this.printer.printValue("Error message", response.getErrorMessage()); this.printer.printValue("Headers", getResponseHeaders(response)); this.printer.printValue("Content type", response.getContentType()); this.printer.printValue("Body", body); this.printer.printValue("Forwarded URL", response.getForwardedUrl()); this.printer.printValue("Redirected URL", response.getRedirectedUrl()); printCookies(response.getCookies()); }
@Test public void commenceWhenNoBearerTokenErrorThenStatus401AndAuthHeader() throws Exception { MockHttpServletRequest request = new MockHttpServletRequest(); MockHttpServletResponse response = new MockHttpServletResponse(); this.authenticationEntryPoint.commence(request, response, new BadCredentialsException("test")); assertThat(response.getStatus()).isEqualTo(401); assertThat(response.getHeader("WWW-Authenticate")).isEqualTo("Bearer"); }
/** * Extracts the true binary stream out of the response. The usual way (going thru {@link * MockHttpServletResponse#getOutputStreamContent()}) mangles bytes if the content is not made * of chars. * * @param response */ protected ByteArrayInputStream getBinaryInputStream(MockHttpServletResponse response) { return new ByteArrayInputStream(response.getContentAsByteArray()); }
private WebResponseData webResponseData() throws IOException { List<NameValuePair> responseHeaders = responseHeaders(); int statusCode = (this.response.getRedirectedUrl() != null ? HttpStatus.MOVED_PERMANENTLY.value() : this.response.getStatus()); String statusMessage = statusMessage(statusCode); return new WebResponseData(this.response.getContentAsByteArray(), statusCode, statusMessage, responseHeaders); }
private StubMvcResult getRedirectedUrlStubMvcResult(String redirectUrl) throws Exception { MockHttpServletResponse response = new MockHttpServletResponse(); response.sendRedirect(redirectUrl); StubMvcResult mvcResult = new StubMvcResult(null, null, null, null, null, null, response); return mvcResult; }
/** * Executes a request using the PUT method. * * @param path the portion of the request after the context, for example: "api/datastores.xml" * @param body the content to send as the body of the request * @param contentType the mime-type to set for the request being sent */ protected InputStream put(String path, byte[] body, String contentType) throws Exception { MockHttpServletResponse response = putAsServletResponse(path, body, contentType); return new ByteArrayInputStream(response.getContentAsString().getBytes()); }
@Test public void testInterpolationMixedUnsupportedXML() throws Exception { final File xml = new File( "./src/test/resources/requestGetCoverageInterpolationMixedUnsupported.xml"); final String request = FileUtils.readFileToString(xml); MockHttpServletResponse response = postAsServletResponse("wcs", request); assertEquals("application/xml", response.getContentType()); Document dom = dom(new ByteArrayInputStream(response.getContentAsString().getBytes())); print(dom); }
private void assertOriginPixelColor(MockHttpServletResponse response, int[] expected) throws DataSourceException, IOException { assertEquals("image/tiff", response.getContentType()); byte[] bytes = response.getContentAsByteArray(); GeoTiffReader reader = new GeoTiffReader(new ByteArrayInputStream(bytes)); GridCoverage2D coverage = reader.read(null); Raster raster = coverage.getRenderedImage().getData(); int[] pixel = new int[3]; raster.getPixel(0, 0, pixel); assertThat(pixel, equalTo(expected)); } }
@Test public void testBasic() throws Exception { final File xml = new File("./src/test/resources/rangesubset/requestGetCoverageRangeSubsetting.xml"); final String request = FileUtils.readFileToString(xml); MockHttpServletResponse response = postAsServletResponse("wcs", request); assertEquals("image/tiff", response.getContentType()); byte[] tiffContents = getBinary(response); File file = File.createTempFile("bm_gtiff", "bm_gtiff.tiff", new File("./target")); FileUtils.writeByteArrayToFile(file, tiffContents); final GeoTiffReader reader = new GeoTiffReader(file); assertTrue( CRS.equalsIgnoreMetadata( reader.getCoordinateReferenceSystem(), CRS.decode("EPSG:4326", true))); assertEquals(360, reader.getOriginalGridRange().getSpan(0)); assertEquals(360, reader.getOriginalGridRange().getSpan(1)); final GridCoverage2D coverage = reader.read(null); assertEquals(1, coverage.getSampleDimensions().length); GridCoverage2D sourceCoverage = (GridCoverage2D) this.getCatalog() .getCoverageByName("BlueMarble") .getGridCoverageReader(null, null) .read(null); assertEnvelopeEquals(sourceCoverage, coverage); reader.dispose(); scheduleForCleaning(coverage); scheduleForCleaning(sourceCoverage); }
@Test public void testScaleAxesByFactorXML() throws Exception { final File xml = new File("./src/test/resources/requestGetCoverageScaleAxesByFactor.xml"); final String request = FileUtils.readFileToString(xml); MockHttpServletResponse response = postAsServletResponse("wcs", request); assertEquals("image/tiff", response.getContentType()); byte[] tiffContents = getBinary(response); File file = File.createTempFile("bm_gtiff", "bm_gtiff.tiff", new File("./target")); FileUtils.writeByteArrayToFile(file, tiffContents); // check the tiff structure is the one requested final GeoTiffReader reader = new GeoTiffReader(file); assertTrue( CRS.equalsIgnoreMetadata( reader.getCoordinateReferenceSystem(), CRS.decode("EPSG:4326", true))); assertEquals(1260, reader.getOriginalGridRange().getSpan(0)); assertEquals(1260, reader.getOriginalGridRange().getSpan(1)); final GridCoverage2D coverage = reader.read(null); assertNotNull(coverage); assertEnvelopeEquals(sourceCoverage, coverage); reader.dispose(); scheduleForCleaning(coverage); }
@Test public void testCoverageTrimmingBorders() throws Exception { final File xml = new File("./src/test/resources/trimming/requestGetCoverageTrimmingBorders.xml"); final String request = FileUtils.readFileToString(xml); MockHttpServletResponse response = postAsServletResponse("wcs", request); // make sure we are not getting a service exception assertEquals("image/tiff", response.getContentType()); }
public static void main(String[] args) throws Exception { final HttpRequestMonitorFilterTest test = new HttpRequestMonitorFilterTest(); test.before(); final MockHttpServletResponse servletResponse = new MockHttpServletResponse(); test.httpRequestMonitorFilter.doFilter(test.requestWithAccept("text/html"), servletResponse, test.writeInResponseWhenCallingDoFilter(test.testHtml)); final String response = servletResponse.getContentAsString(); FileUtils.write(new File("build/stagemonitor-widget.html"), response, StandardCharsets.UTF_8); }
@Test public void invalid_principal_throws() { Authentication a = mock(Authentication.class); when(a.getPrincipal()).thenReturn(new Object()); try { successHandler.setSavedAccountOptionCookie(new MockHttpServletRequest(), new MockHttpServletResponse(), a); }catch (IllegalArgumentException x) { assertEquals("Unrecognized authentication principle.", x.getMessage()); } }
UaaAuthentication authentication = new UaaAuthentication(principal, null, Collections.EMPTY_LIST, null, true, System.currentTimeMillis()); MockHttpServletRequest request = new MockHttpServletRequest(); request.setSecure(secure); MockHttpServletResponse response = new MockHttpServletResponse(); Cookie accountOptionCookie = response.getCookie("Saved-Account-user-id"); assertThat(accountOptionCookie, notNullValue()); String cookieValue = accountOptionCookie.getValue(); expectedCookieValue.setOrigin(user.getOrigin()); assertEquals(URLEncoder.encode(JsonUtils.writeValueAsString(expectedCookieValue)), cookieValue); assertEquals(true, accountOptionCookie.isHttpOnly()); assertEquals(365*24*60*60, accountOptionCookie.getMaxAge()); assertEquals("/login", accountOptionCookie.getPath()); Assert.assertEquals(secure, accountOptionCookie.getSecure()); assertEquals("user-id", uaaPrincipal.getValue().getId()); Cookie currentUserCookie = response.getCookie("Current-User"); assertThat(currentUserCookie, notNullValue()); assertThat(currentUserCookie.getValue(), containsString("user-id"));
protected void setUp() throws Exception { super.setUp(); HelloWorld helloWorld = new HelloWorld(); Service service = new Service( "hello", helloWorld, new Version("1.0.0"), Collections.singletonList("hello")); request = new MockHttpServletRequest() { public int getServerPort() { return 8080; } }; request.setScheme("http"); request.setServerName("localhost"); request.setContextPath("geoserver"); response = new MockHttpServletResponse(); handler = new DefaultServiceExceptionHandler(); requestInfo = new Request(); requestInfo.setHttpRequest(request); requestInfo.setHttpResponse(response); requestInfo.setService("hello"); requestInfo.setVersion("1.0.0"); }
@Test public void getNamedDispatcherForDefaultServlet() throws Exception { final String name = "default"; RequestDispatcher namedDispatcher = sc.getNamedDispatcher(name); assertNotNull(namedDispatcher); MockHttpServletResponse response = new MockHttpServletResponse(); namedDispatcher.forward(new MockHttpServletRequest(sc), response); assertEquals(name, response.getForwardedUrl()); }
@Test // SPR-12677 public void contentTypeHeaderWithMoreComplexCharsetSyntax() { String contentType = "test/plain;charset=\"utf-8\";foo=\"charset=bar\";foocharset=bar;foo=bar"; response.setHeader("Content-Type", contentType); assertEquals(contentType, response.getContentType()); assertEquals(contentType, response.getHeader("Content-Type")); assertEquals("UTF-8", response.getCharacterEncoding()); response = new MockHttpServletResponse(); response.addHeader("Content-Type", contentType); assertEquals(contentType, response.getContentType()); assertEquals(contentType, response.getHeader("Content-Type")); assertEquals("UTF-8", response.getCharacterEncoding()); }
@Before public void setUp() { given(wac.getServletContext()).willReturn(mockServletContext); given(testContext.getApplicationContext()).willReturn(wac); MockHttpServletRequest request = new MockHttpServletRequest(mockServletContext); MockHttpServletResponse response = new MockHttpServletResponse(); ServletWebRequest servletWebRequest = new ServletWebRequest(request, response); request.setAttribute(SET_UP_OUTSIDE_OF_STEL, "true"); RequestContextHolder.setRequestAttributes(servletWebRequest); assertSetUpOutsideOfStelAttributeExists(); }
/** * Create new MockServletConfig. * @param servletContext the ServletContext that the JSP page runs in * @param request the current HttpServletRequest * @param response the current HttpServletResponse * @param servletConfig the ServletConfig (hardly ever accessed from within a tag) */ public MockPageContext(@Nullable ServletContext servletContext, @Nullable HttpServletRequest request, @Nullable HttpServletResponse response, @Nullable ServletConfig servletConfig) { this.servletContext = (servletContext != null ? servletContext : new MockServletContext()); this.request = (request != null ? request : new MockHttpServletRequest(servletContext)); this.response = (response != null ? response : new MockHttpServletResponse()); this.servletConfig = (servletConfig != null ? servletConfig : new MockServletConfig(servletContext)); }
@Test void refreshTokenGrantType_doesNotReturnIdToken_toNonOpenIdClients() throws Exception { client = setUpClients("nonopenidclient", "", "scim.me", "password,refresh_token", true); user = setUpUser("joe-user", "", OriginKeys.UAA, "uaa"); String refreshToken = getJwtRefreshToken(client.getClientId(), SECRET, user.getUserName(), SECRET, "localhost"); MockHttpServletResponse refreshResponse = useRefreshToken(refreshToken, client.getClientId(), SECRET, "localhost"); assertEquals(HttpStatus.SC_OK, refreshResponse.getStatus()); CompositeToken compositeToken = JsonUtils.readValue(refreshResponse.getContentAsString(), CompositeToken.class); assertNull(compositeToken.getIdTokenValue()); }