public void setUp(String username, Map<String, String> initParams) throws Exception {
    ServiceRegistry mockServiceRegistry = EasyMock.createNiceMock(ServiceRegistry.class);
    EasyMock.expect(mockServiceRegistry.lookupServiceURL("test-cluster", "NAMENODE"))
        .andReturn("test-nn-scheme://test-nn-host:411")
        .anyTimes();
    EasyMock.expect(mockServiceRegistry.lookupServiceURL("test-cluster", "JOBTRACKER"))
        .andReturn("test-jt-scheme://test-jt-host:511")
        .anyTimes();

    GatewayServices mockGatewayServices = EasyMock.createNiceMock(GatewayServices.class);
    EasyMock.expect(mockGatewayServices.getService(GatewayServices.SERVICE_REGISTRY_SERVICE))
        .andReturn(mockServiceRegistry)
        .anyTimes();

    EasyMock.replay(mockServiceRegistry, mockGatewayServices);

    String descriptorUrl = getTestResource("rewrite.xml").toExternalForm();

    Log.setLog(new NoOpLogger());

    server = new ServletTester();
    server.setContextPath("/");
    server.getContext().addEventListener(new UrlRewriteServletContextListener());
    server
        .getContext()
        .setInitParameter(
            UrlRewriteServletContextListener.DESCRIPTOR_LOCATION_INIT_PARAM_NAME, descriptorUrl);
    server.getContext().setAttribute(GatewayServices.GATEWAY_CLUSTER_ATTRIBUTE, "test-cluster");
    server
        .getContext()
        .setAttribute(GatewayServices.GATEWAY_SERVICES_ATTRIBUTE, mockGatewayServices);

    FilterHolder setupFilter =
        server.addFilter(SetupFilter.class, "/*", EnumSet.of(DispatcherType.REQUEST));
    setupFilter.setFilter(new SetupFilter(username));
    FilterHolder rewriteFilter =
        server.addFilter(UrlRewriteServletFilter.class, "/*", EnumSet.of(DispatcherType.REQUEST));
    if (initParams != null) {
      for (Map.Entry<String, String> entry : initParams.entrySet()) {
        rewriteFilter.setInitParameter(entry.getKey(), entry.getValue());
      }
    }
    rewriteFilter.setFilter(new UrlRewriteServletFilter());

    interactions = new ArrayQueue<MockInteraction>();

    ServletHolder servlet = server.addServlet(MockServlet.class, "/");
    servlet.setServlet(new MockServlet("mock-servlet", interactions));

    server.start();

    interaction = new MockInteraction();
    request = new HttpTester();
    response = new HttpTester();
  }
 @Before
 public void setUp() throws Exception {
   tester = new ServletTester();
   tester.setContextPath("/");
   tester.addServlet(GroupServlet.class, "/groups/*");
   tester.start();
 }
 public void start() throws Exception {
   Assert.assertThat(
       "No servlet defined yet.  Did you use #setContentServlet()?",
       servletTester,
       notNullValue());
   servletTester.dump();
   servletTester.start();
 }
 /**
  * Set the servlet that provides content for the GzipFilter in being tested.
  *
  * @param servletClass the servlet that will provide content.
  * @return the FilterHolder for configuring the GzipFilter's initParameters with
  */
 public FilterHolder setContentServlet(Class<? extends Servlet> servletClass) throws IOException {
   servletTester = new ServletTester();
   servletTester.setContextPath("/context");
   servletTester.setResourceBase(testdir.getDir().getCanonicalPath());
   ServletHolder servletHolder = servletTester.addServlet(servletClass, "/");
   servletHolder.setInitParameter("baseDir", testdir.getDir().getAbsolutePath());
   FilterHolder holder = servletTester.addFilter(gzipFilterClass, "/*", 0);
   return holder;
 }
  @BeforeClass
  public static void init() throws Exception {
    // Set up server
    server.setContextPath(CONTEXT);

    // Add our servlet under test
    server.addServlet(TokenEndpoint.class, "/revoke");
    server.addServlet(TokenEndpoint.class, "/token");

    // Let's do dis
    server.start();
  }
  @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);
    }
  }
 public void stop() {
   // NOTE: Do not cleanup the testdir.  Failures can't be diagnosed if you do that.
   // IO.delete(testdir.getDir()):
   try {
     servletTester.stop();
   } catch (Exception e) {
     // Don't toss this out into Junit as this would be the last exception
     // that junit will report as being the cause of the test failure.
     // when in reality, the earlier setup issue is the real cause.
     e.printStackTrace(System.err);
   }
 }
Exemple #10
0
  @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());
  }
Exemple #11
0
  @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());
  }
Exemple #12
0
  @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"));
  }
Exemple #13
0
  @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"));
  }
Exemple #14
0
  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;
  }
 protected void processRequest() throws Exception {
   response.parse(tester.getResponses(request.generate()));
 }
 @After
 public void tearDownTester() throws Exception {
   tester.stop();
 }
 @Before
 public void setUpTester() throws Exception {
   setUp(tester);
   tester.start();
 }
Exemple #18
0
 @AfterClass
 public static void shutdown() throws Exception {
   server.stop();
 }
 @Override
 protected void setUp(ServletTester tester) {
   tester.addServlet(HealthCheckServlet.class, "/healthchecks");
   tester.setAttribute("com.codahale.metrics.servlets.HealthCheckServlet.registry", registry);
   tester.setAttribute("com.codahale.metrics.servlets.HealthCheckServlet.executor", threadPool);
 }