public void getHello() throws Exception { HttpTester request = makeRequest("GET", "/a/hello"); HttpTester response = getResponse(request); assertEquals(response.getStatus(), 200); assertEquals(response.getContent(), "Hello, world"); }
public void getSimpleParam() throws Exception { HttpTester request = makeRequest("GET", "/a/simple?var=Salomon"); HttpTester response = getResponse(request); assertEquals(response.getStatus(), 200); assertEquals(response.getContent(), "v:Salomon"); }
public void getMatch() throws Exception { HttpTester request = makeRequest("GET", "/b/match-one-two-three"); HttpTester response = getResponse(request); assertEquals(response.getStatus(), 200); assertEquals(response.getContent(), "get:one:two:three"); }
public void getSimpleConsumes() throws Exception { HttpTester request = makeRequest("GET", "/a/simple"); request.addHeader("content-type", "x-sj-test"); HttpTester response = getResponse(request); assertEquals(response.getStatus(), 200); assertEquals(response.getContent(), "Consumes"); }
public void getPrint() throws Exception { HttpTester request = makeRequest("POST", "/a/print"); addPost(request, "txt", "hello the world"); HttpTester response = getResponse(request); assertEquals(response.getStatus(), 200); assertEquals(response.getContent(), "hello the world"); }
public void getSimpleHeader() throws Exception { HttpTester request = makeRequest("GET", "/a/simple"); request.addHeader("x-sj-test", "Salomon"); HttpTester response = getResponse(request); assertEquals(response.getStatus(), 200); assertEquals(response.getContent(), "h:Salomon"); }
public void getSimpleProduces() throws Exception { HttpTester request = makeRequest("GET", "/a/simple"); request.addHeader("accept", "x-sj-test,xml;"); HttpTester response = getResponse(request); assertEquals(response.getStatus(), 200); assertEquals(response.getContent(), "Produces"); }
@Test(invocationCount = 3, dataProvider = "requestMethods") public void allSimple(String method) throws Exception { if (method.equals("HEAD")) return; HttpTester request = makeRequest(method, "/a/simple"); HttpTester response = getResponse(request); assertEquals(response.getStatus(), 200); }
private void dumpHeaders(String prefix, HttpTester http) { System.out.println(prefix); @SuppressWarnings("unchecked") Enumeration<String> names = http.getHeaderNames(); while (names.hasMoreElements()) { String name = names.nextElement(); String value = http.getHeader(name); System.out.printf(" [%s] = %s%n", name, value); } }
@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)); }
@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()); }
private void assertResponseHeaders(int expectedFilesize, int status, HttpTester response) { Assert.assertThat("Response.method", response.getMethod(), nullValue()); Assert.assertThat("Response.status", response.getStatus(), is(status)); if (expectedFilesize != (-1)) { Assert.assertThat( "Response.header[Content-Length]", response.getHeader("Content-Length"), notNullValue()); int serverLength = Integer.parseInt(response.getHeader("Content-Length")); Assert.assertThat("Response.header[Content-Length]", serverLength, is(expectedFilesize)); } Assert.assertThat( "Response.header[Content-Encoding]", response.getHeader("Content-Encoding"), not(containsString(compressionType))); }
@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()); }
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; }
private String readResponse(HttpTester response) throws IOException, UnsupportedEncodingException { String actual = null; InputStream in = null; ByteArrayOutputStream out = null; try { in = new ByteArrayInputStream(response.getContentBytes()); out = new ByteArrayOutputStream(); IO.copy(in, out); actual = out.toString(encoding); } finally { IO.close(out); IO.close(in); } return actual; }
@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")); }
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); } }
public void getExpect404() throws Exception { HttpTester request = makeRequest("GET", "/a/not-existing"); HttpTester response = getResponse(request); assertEquals(response.getStatus(), 404); }
public void getInvalidMethod() throws Exception { HttpTester request = makeRequest("GET", "/a/print"); HttpTester response = getResponse(request); assertEquals(response.getStatus(), 404); }
public void postMissingArgument() throws Exception { HttpTester request = makeRequest("POST", "/a/print"); HttpTester response = getResponse(request); assertEquals(response.getStatus(), 400); }
protected void processRequest() throws Exception { response.parse(tester.getResponses(request.generate())); }
/** * 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); } }