@Test public void testServiceRegistryFunctionsOnJsonRequestBody() throws Exception { Map<String, String> initParams = new HashMap<String, String>(); initParams.put("request.body", "oozie-conf"); setUp("test-user", initParams); String input = TestUtils.getResourceString( ServiceRegistryFunctionsTest.class, "test-input-body.json", "UTF-8"); String expect = TestUtils.getResourceString( ServiceRegistryFunctionsTest.class, "test-expect-body.json", "UTF-8"); // Setup the server side request/response interaction. interaction .expect() .method("PUT") .requestUrl("http://test-host:42/test-path") .contentType("application/json") .characterEncoding("UTF-8") .content(expect, Charset.forName("UTF-8")); interaction.respond().status(200); interactions.add(interaction); request.setMethod("PUT"); request.setURI("/test-path"); request.setVersion("HTTP/1.1"); request.setHeader("Host", "test-host:42"); request.setContentType("application/json; charset=UTF-8"); request.setContent(input); response.parse(server.getResponses(request.generate())); // Test the results. assertThat(response.getStatus(), Is.is(200)); }
/** * Makes sure that the response contains an unfiltered file contents. * * <p>This is used to test exclusions and passthroughs in the GzipFilter. * * <p>An example is to test that it is possible to configure GzipFilter to not recompress content * that shouldn't be compressed by the GzipFilter. * * @param requestedFilename the filename used to on the GET request,. * @param testResourceSha1Sum the sha1sum file that contains the SHA1SUM checksum that will be * used to verify that the response contents are what is intended. * @param expectedContentType */ public void assertIsResponseNotGzipFiltered( String requestedFilename, String testResourceSha1Sum, String expectedContentType) throws Exception { System.err.printf("[GzipTester] requesting /context/%s%n", requestedFilename); HttpTester request = new HttpTester(); HttpTester response = new HttpTester(); request.setMethod("GET"); request.setVersion("HTTP/1.0"); request.setHeader("Host", "tester"); request.setHeader("Accept-Encoding", compressionType); if (this.userAgent != null) request.setHeader("User-Agent", this.userAgent); request.setURI("/context/" + requestedFilename); // Issue the request ByteArrayBuffer reqsBuff = new ByteArrayBuffer(request.generate().getBytes()); // Collect the response(s) ByteArrayBuffer respBuff = servletTester.getResponses(reqsBuff); response.parse(respBuff.asArray()); dumpHeaders(requestedFilename + " / Response Headers", response); // Assert the response headers String prefix = requestedFilename + " / Response"; Assert.assertThat(prefix + ".method", response.getMethod(), nullValue()); Assert.assertThat(prefix + ".status", response.getStatus(), is(HttpServletResponse.SC_OK)); Assert.assertThat( prefix + ".header[Content-Length]", response.getHeader("Content-Length"), notNullValue()); Assert.assertThat( prefix + ".header[Content-Encoding] (should not be recompressed by GzipFilter)", response.getHeader("Content-Encoding"), nullValue()); Assert.assertThat( prefix + ".header[Content-Type] (should have a Content-Type associated with it)", response.getHeader("Content-Type"), notNullValue()); Assert.assertThat( prefix + ".header[Content-Type]", response.getHeader("Content-Type"), is(expectedContentType)); ByteArrayInputStream bais = null; DigestOutputStream digester = null; try { MessageDigest digest = MessageDigest.getInstance("SHA1"); bais = new ByteArrayInputStream(response.getContentBytes()); digester = new DigestOutputStream(new NoOpOutputStream(), digest); IO.copy(bais, digester); String actualSha1Sum = Hex.asHex(digest.digest()); String expectedSha1Sum = loadExpectedSha1Sum(testResourceSha1Sum); Assert.assertEquals( requestedFilename + " / SHA1Sum of content", expectedSha1Sum, actualSha1Sum); } finally { IO.close(digester); IO.close(bais); } }
public void assertIsResponseGzipCompressed(String requestedFilename, String serverFilename) throws Exception { System.err.printf("[GzipTester] requesting /context/%s%n", requestedFilename); HttpTester request = new HttpTester(); HttpTester response = new HttpTester(); request.setMethod("GET"); request.setVersion("HTTP/1.0"); request.setHeader("Host", "tester"); request.setHeader("Accept-Encoding", compressionType); if (this.userAgent != null) request.setHeader("User-Agent", this.userAgent); request.setURI("/context/" + requestedFilename); // Issue the request ByteArrayBuffer reqsBuff = new ByteArrayBuffer(request.generate().getBytes()); // Collect the response(s) ByteArrayBuffer respBuff = servletTester.getResponses(reqsBuff); response.parse(respBuff.asArray()); // Assert the response headers Assert.assertThat("Response.method", response.getMethod(), nullValue()); // // Assert.assertThat("Response.status",response.getStatus(),is(HttpServletResponse.SC_OK)); Assert.assertThat( "Response.header[Content-Length]", response.getHeader("Content-Length"), notNullValue()); Assert.assertThat( "Response.header[Content-Encoding]", response.getHeader("Content-Encoding"), containsString(compressionType)); // Assert that the decompressed contents are what we expect. File serverFile = testdir.getFile(serverFilename); String expected = IO.readToString(serverFile); String actual = null; ByteArrayInputStream bais = null; InputStream in = null; ByteArrayOutputStream out = null; try { bais = new ByteArrayInputStream(response.getContentBytes()); if (compressionType.equals(GzipFilter.GZIP)) { in = new GZIPInputStream(bais); } else if (compressionType.equals(GzipFilter.DEFLATE)) { in = new InflaterInputStream(bais, new Inflater(true)); } out = new ByteArrayOutputStream(); IO.copy(in, out); actual = out.toString(encoding); assertThat("Uncompressed contents", actual, equalTo(expected)); } finally { IO.close(out); IO.close(in); IO.close(bais); } }
@Test public void testCreateToken401() throws Exception { HttpTester req = new HttpTester(); req.setMethod("POST"); req.setHeader("Content-Type", "application/x-www-form-urlencoded"); req.setContent(DIRECT_AUTH); req.setURI(CONTEXT + TokenEndpoint.TOKEN_GRANT_ENDPOINT); req.setVersion("HTTP/1.0"); HttpTester resp = new HttpTester(); resp.parse(server.getResponses(req.generate())); assertEquals(401, resp.getStatus()); }
@Test public void testDeleteToken() throws Exception { when(ServiceLocator.getInstance().getTokenStore().delete("token_to_be_deleted")) .thenReturn(true); HttpTester req = new HttpTester(); req.setMethod("POST"); req.setHeader("Content-Type", "application/x-www-form-urlencoded"); req.setContent("token_to_be_deleted"); req.setURI(CONTEXT + TokenEndpoint.TOKEN_REVOKE_ENDPOINT); req.setVersion("HTTP/1.0"); HttpTester resp = new HttpTester(); resp.parse(server.getResponses(req.generate())); assertEquals(204, resp.getStatus()); }
@Test public void testCreateTokenWithPassword() throws Exception { when(ServiceLocator.getInstance() .getCredentialAuth() .authenticate(any(PasswordCredentials.class))) .thenReturn(claim); HttpTester req = new HttpTester(); req.setMethod("POST"); req.setHeader("Content-Type", "application/x-www-form-urlencoded"); req.setContent(DIRECT_AUTH); req.setURI(CONTEXT + TokenEndpoint.TOKEN_GRANT_ENDPOINT); req.setVersion("HTTP/1.0"); HttpTester resp = new HttpTester(); resp.parse(server.getResponses(req.generate())); assertEquals(201, resp.getStatus()); assertTrue(resp.getContent().contains("expires_in\":10")); assertTrue(resp.getContent().contains("Bearer")); }
@Test public void testCreateTokenWithRefreshToken() throws Exception { when(ServiceLocator.getInstance().getTokenStore().get(anyString())) .thenReturn(new AuthenticationBuilder(claim).build()); when(ServiceLocator.getInstance().getIdmService().listRoles(anyString(), anyString())) .thenReturn(Arrays.asList("admin", "user")); HttpTester req = new HttpTester(); req.setMethod("POST"); req.setHeader("Content-Type", "application/x-www-form-urlencoded"); req.setContent(REFRESH_TOKEN); req.setURI(CONTEXT + TokenEndpoint.TOKEN_GRANT_ENDPOINT); req.setVersion("HTTP/1.0"); HttpTester resp = new HttpTester(); resp.parse(server.getResponses(req.generate())); assertEquals(201, resp.getStatus()); assertTrue(resp.getContent().contains("expires_in\":10")); assertTrue(resp.getContent().contains("Bearer")); }
private HttpTester executeRequest(String uri) throws IOException, Exception { System.err.printf("[GzipTester] requesting %s%n", uri); HttpTester request = new HttpTester(); HttpTester response = new HttpTester(); request.setMethod("GET"); request.setVersion("HTTP/1.0"); request.setHeader("Host", "tester"); request.setHeader("Accept-Encoding", compressionType); if (this.userAgent != null) request.setHeader("User-Agent", this.userAgent); request.setURI(uri); // Issue the request ByteArrayBuffer reqsBuff = new ByteArrayBuffer(request.generate().getBytes()); // Collect the response(s) ByteArrayBuffer respBuff = servletTester.getResponses(reqsBuff); response.parse(respBuff.asArray()); return response; }