org.eclipse.jetty.servlet.ServletTester Java Examples

The following examples show how to use org.eclipse.jetty.servlet.ServletTester. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: EventsApiTest.java    From java-slack-sdk with MIT License 6 votes vote down vote up
@Test
public void urlVerification() throws Exception {
    ServletTester tester = TestUtils.getServletTester(webApp);
    HttpTester.Request request = TestUtils.prepareRequest();

    UrlVerificationPayload payload = new UrlVerificationPayload();
    payload.setChallenge("cha-xxxxxx");
    payload.setToken("token-xxxx");
    request.setContent(GsonFactory.createSnakeCase().toJson(payload));

    HttpTester.Response response = HttpTester.parseResponse(tester.getResponses(request.generate()));

    assertThat(response.getContent(), is(equalTo("cha-xxxxxx")));
    assertThat(response.getStatus(), is(equalTo(200)));
    assertThat(response.get("Content-Type"), is(startsWith("text/plain")));
}
 
Example #2
Source File: UrlRewriteServletContextListenerTest.java    From knox with Apache License 2.0 6 votes vote down vote up
@Before
public void setUp() throws Exception {
  String descriptorUrl = getTestResource( "rewrite.xml" ).toExternalForm();

  server = new ServletTester();
  server.setContextPath( "/" );
  server.getContext().addEventListener( new UrlRewriteServletContextListener() );
  server.getContext().setInitParameter(
      UrlRewriteServletContextListener.DESCRIPTOR_LOCATION_INIT_PARAM_NAME, descriptorUrl );

  FilterHolder filter = server.addFilter( UrlRewriteServletFilter.class, "/*", EnumSet.of( DispatcherType.REQUEST ) );
  filter.setFilter( new UrlRewriteServletFilter() );

  interactions = new ArrayDeque<>();

  ServletHolder servlet = server.addServlet( MockServlet.class, "/" );
  servlet.setServlet( new MockServlet( "mock-servlet", interactions ) );
  servlet.setInitParameter(
      UrlRewriteServletContextListener.DESCRIPTOR_LOCATION_INIT_PARAM_NAME,
      getTestResource( "rewrite.xml" ).toExternalForm() );

  server.start();
}
 
Example #3
Source File: EventsApiTest.java    From java-slack-sdk with MIT License 6 votes vote down vote up
@Test
public void app_uninstalled() throws Exception {
    ServletTester tester = TestUtils.getServletTester(webApp);
    HttpTester.Request request = TestUtils.prepareRequest();

    request.setContent("{\n" +
            "    \"token\": \"XXYYZZ\",\n" +
            "    \"team_id\": \"TXXXXXXXX\",\n" +
            "    \"api_app_id\": \"AXXXXXXXXX\",\n" +
            "    \"event\": {\n" +
            "        \"type\": \"app_uninstalled\"\n" +
            "    },\n" +
            "    \"type\": \"event_callback\",\n" +
            "    \"event_id\": \"EvXXXXXXXX\",\n" +
            "    \"event_time\": 1234567890\n" +
            "}");

    HttpTester.Response response = HttpTester.parseResponse(tester.getResponses(request.generate()));

    // wait for the async execution
    Thread.sleep(200L);

    assertThat(response.getStatus(), is(equalTo(200)));
    assertThat(appUninstallsCalls.get(), is(1));
}
 
Example #4
Source File: EventsApiTest.java    From java-slack-sdk with MIT License 6 votes vote down vote up
@Test
public void goodbye() throws Exception {
    ServletTester tester = TestUtils.getServletTester(webApp);
    HttpTester.Request request = TestUtils.prepareRequest();

    request.setContent("{\n" +
            "    \"token\": \"XXYYZZ\",\n" +
            "    \"team_id\": \"TXXXXXXXX\",\n" +
            "    \"api_app_id\": \"AXXXXXXXXX\",\n" +
            "    \"event\": {\n" +
            "        \"type\": \"goodbye\"\n" +
            "    },\n" +
            "    \"type\": \"event_callback\",\n" +
            "    \"event_id\": \"EvXXXXXXXX\",\n" +
            "    \"event_time\": 1234567890\n" +
            "}");

    HttpTester.Response response = HttpTester.parseResponse(tester.getResponses(request.generate()));

    // wait for the async execution
    Thread.sleep(200L);

    assertThat(response.getStatus(), is(equalTo(200)));
    assertThat(goodbyeCalls.get(), is(1));
}
 
Example #5
Source File: SiestaTestSupport.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
@Before
public void startJetty() throws Exception {
  servletTester = new ServletTester();
  servletTester.getContext().addEventListener(new GuiceServletContextListener()
  {
    final Injector injector = Guice.createInjector(new TestModule());

    @Override
    protected Injector getInjector() {
      return injector;
    }
  });

  url = servletTester.createConnector(true) + TestModule.MOUNT_POINT;
  servletTester.addFilter(GuiceFilter.class, "/*", EnumSet.of(DispatcherType.REQUEST));
  servletTester.addServlet(DummyServlet.class, "/*");
  servletTester.start();

  client = ClientBuilder.newClient();
}
 
Example #6
Source File: EventsApiHandlerTest.java    From java-slack-sdk with MIT License 6 votes vote down vote up
@Ignore
@Test
public void goodbye() throws Exception {
    ServletTester tester = getServletTester();
    HttpTester.Request request = prepareRequest();

    request.setContent("{\n" +
            "    \"token\": \"XXYYZZ\",\n" +
            "    \"team_id\": \"TXXXXXXXX\",\n" +
            "    \"api_app_id\": \"AXXXXXXXXX\",\n" +
            "    \"event\": {\n" +
            "        \"type\": \"goodbye\"\n" +
            "    },\n" +
            "    \"type\": \"event_callback\",\n" +
            "    \"event_id\": \"EvXXXXXXXX\",\n" +
            "    \"event_time\": 1234567890\n" +
            "}");

    HttpTester.Response response = HttpTester.parseResponse(tester.getResponses(request.generate()));

    // wait for the async execution
    Thread.sleep(200L);

    assertThat(response.getStatus(), is(equalTo(200)));
    assertThat(GOODBYE_CALL_COUNTER.get(), is(1));
}
 
Example #7
Source File: EventsApiHandlerTest.java    From java-slack-sdk with MIT License 6 votes vote down vote up
@Ignore
@Test
public void app_uninstalled() throws Exception {
    ServletTester tester = getServletTester();
    HttpTester.Request request = prepareRequest();

    request.setContent("{\n" +
            "    \"token\": \"XXYYZZ\",\n" +
            "    \"team_id\": \"TXXXXXXXX\",\n" +
            "    \"api_app_id\": \"AXXXXXXXXX\",\n" +
            "    \"event\": {\n" +
            "        \"type\": \"app_uninstalled\"\n" +
            "    },\n" +
            "    \"type\": \"event_callback\",\n" +
            "    \"event_id\": \"EvXXXXXXXX\",\n" +
            "    \"event_time\": 1234567890\n" +
            "}");

    HttpTester.Response response = HttpTester.parseResponse(tester.getResponses(request.generate()));

    // wait for the async execution
    Thread.sleep(200L);

    assertThat(response.getStatus(), is(equalTo(200)));
    assertThat(APP_UNINSTALLED_CALL_COUNTER.get(), is(1));
}
 
Example #8
Source File: EventsApiHandlerTest.java    From java-slack-sdk with MIT License 6 votes vote down vote up
@Ignore
@Test
public void urlVerification() throws Exception {
    ServletTester tester = getServletTester();
    HttpTester.Request request = prepareRequest();

    UrlVerificationPayload payload = new UrlVerificationPayload();
    payload.setChallenge("cha-xxxxxx");
    payload.setToken("token-xxxx");
    request.setContent(GsonFactory.createSnakeCase().toJson(payload));

    HttpTester.Response response = HttpTester.parseResponse(tester.getResponses(request.generate()));

    assertThat(response.getContent(), is(equalTo("cha-xxxxxx")));
    assertThat(response.getStatus(), is(equalTo(200)));
    assertThat(response.get("Content-Type"), is(equalTo("text/plain")));
}
 
Example #9
Source File: EventsApiTest.java    From java-slack-sdk with MIT License 6 votes vote down vote up
@Test
public void invalid() throws Exception {
    ServletTester tester = ServletTestUtils.getServletTester(webApp);
    HttpTester.Request request = ServletTestUtils.prepareRequest("invalid", "{\n" +
            "    \"token\": \"XXYYZZ\",\n" +
            "    \"team_id\": \"TXXXXXXXX\",\n" +
            "    \"api_app_id\": \"AXXXXXXXXX\",\n" +
            "    \"event\": {\n" +
            "        \"type\": \"goodbye\"\n" +
            "    },\n" +
            "    \"type\": \"event_callback\",\n" +
            "    \"event_id\": \"EvXXXXXXXX\",\n" +
            "    \"event_time\": 1234567890\n" +
            "}");

    HttpTester.Response response = HttpTester.parseResponse(tester.getResponses(request.generate()));

    // wait for the async execution
    Thread.sleep(200L);

    assertThat(response.getStatus(), is(equalTo(401)));
    assertThat(goodbyeCalls.get(), is(0));
}
 
Example #10
Source File: EventsApiTest.java    From java-slack-sdk with MIT License 6 votes vote down vote up
@Test
public void goodbye() throws Exception {
    ServletTester tester = ServletTestUtils.getServletTester(webApp);
    HttpTester.Request request = ServletTestUtils.prepareRequest(signingSecret, "{\n" +
            "    \"token\": \"XXYYZZ\",\n" +
            "    \"team_id\": \"TXXXXXXXX\",\n" +
            "    \"api_app_id\": \"AXXXXXXXXX\",\n" +
            "    \"event\": {\n" +
            "        \"type\": \"goodbye\"\n" +
            "    },\n" +
            "    \"type\": \"event_callback\",\n" +
            "    \"event_id\": \"EvXXXXXXXX\",\n" +
            "    \"event_time\": 1234567890\n" +
            "}");

    HttpTester.Response response = HttpTester.parseResponse(tester.getResponses(request.generate()));

    // wait for the async execution
    Thread.sleep(200L);

    assertThat(response.getStatus(), is(equalTo(200)));
    assertThat(goodbyeCalls.get(), is(1));
}
 
Example #11
Source File: EventsApiTest.java    From java-slack-sdk with MIT License 6 votes vote down vote up
@Test
public void app_uninstalled() throws Exception {
    ServletTester tester = ServletTestUtils.getServletTester(webApp);
    HttpTester.Request request = ServletTestUtils.prepareRequest(signingSecret, "{\n" +
            "    \"token\": \"XXYYZZ\",\n" +
            "    \"team_id\": \"TXXXXXXXX\",\n" +
            "    \"api_app_id\": \"AXXXXXXXXX\",\n" +
            "    \"event\": {\n" +
            "        \"type\": \"app_uninstalled\"\n" +
            "    },\n" +
            "    \"type\": \"event_callback\",\n" +
            "    \"event_id\": \"EvXXXXXXXX\",\n" +
            "    \"event_time\": 1234567890\n" +
            "}");

    HttpTester.Response response = HttpTester.parseResponse(tester.getResponses(request.generate()));

    // wait for the async execution
    Thread.sleep(200L);

    assertThat(response.getStatus(), is(equalTo(200)));
    assertThat(appUninstallsCalls.get(), is(1));
}
 
Example #12
Source File: SlashCommandTest.java    From java-slack-sdk with MIT License 5 votes vote down vote up
@Test
public void echo() throws Exception {
    ServletTester tester = TestUtils.getServletTester(webApp);
    HttpTester.Request request = TestUtils.prepareRequest();

    // https://api.slack.com/slash-commands
    request.setContent("token=gIkuvaNzQIHg97ATvDxqgjtO" +
            "&team_id=T0001" +
            "&team_domain=example" +
            "&enterprise_id=E0001" +
            "&enterprise_name=Globular%20Construct%20Inc" +
            "&channel_id=C2147483705" +
            "&channel_name=test" +
            "&user_id=U2147483697" +
            "&user_name=Steve" +
            "&command=/echo" +
            "&text=Can I ask you a favor?" +
            "&response_url=https://hooks.slack.com/commands/1234/5678" +
            "&trigger_id=13345224609.738474920.8088930838d88f008e0");

    HttpTester.Response response = HttpTester.parseResponse(tester.getResponses(request.generate()));

    assertThat(response.getContent(), is(equalTo("")));
    assertThat(response.getStatus(), is(equalTo(200)));
    assertThat(response.get("Content-Type"), is(startsWith("plain/text")));
    assertThat(echoCalls.get(), is(1));

    verify(echoSenderMock, times(1)).send(Mockito.any(SlashCommandResponse.class));
}
 
Example #13
Source File: UrlRewriteServletFilterTest.java    From knox with Apache License 2.0 5 votes vote down vote up
private void testSetUp(Map<String,String> initParams ) throws Exception {
  String descriptorUrl = getTestResource( "rewrite.xml" ).toExternalForm();

  server = new ServletTester();
  server.setContextPath( "/" );
  server.getContext().addEventListener( new UrlRewriteServletContextListener() );
  server.getContext().setInitParameter(
      UrlRewriteServletContextListener.DESCRIPTOR_LOCATION_INIT_PARAM_NAME, descriptorUrl );

  FilterHolder setupFilter = server.addFilter( SetupFilter.class, "/*", EnumSet.of( DispatcherType.REQUEST ) );
  setupFilter.setFilter( new SetupFilter() );
  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 ArrayDeque<>();

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

  server.start();

  interaction = new MockInteraction();
  request = HttpTester.newRequest();
  response = null;
}
 
Example #14
Source File: TestUtils.java    From knox with Apache License 2.0 5 votes vote down vote up
public static HttpTester.Response execute( ServletTester server, HttpTester.Request request ) throws Exception {
  LOG.debug( "execute: request=" + request );
  ByteBuffer requestBuffer = request.generate();
  LOG.trace( "execute: requestBuffer=[" + new String(requestBuffer.array(),0,requestBuffer.limit(), StandardCharsets.UTF_8) + "]" );
  ByteBuffer responseBuffer = server.getResponses( requestBuffer, 30, TimeUnit.SECONDS );
  HttpTester.Response response = HttpTester.parseResponse( responseBuffer );
  LOG.trace( "execute: responseBuffer=[" + new String(responseBuffer.array(),0,responseBuffer.limit(), StandardCharsets.UTF_8) + "]" );
  LOG.debug( "execute: reponse=" + response );
  return response;
}
 
Example #15
Source File: UsernameFunctionProcessorTest.java    From knox with Apache License 2.0 5 votes vote down vote up
private void testSetup(String username, Map<String,String> initParams ) throws Exception {
  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 );

  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 ArrayDeque<>();

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

  server.start();

  interaction = new MockInteraction();
  request = HttpTester.newRequest();
  response = null;
}
 
Example #16
Source File: EventsApiHandlerTest.java    From java-slack-sdk with MIT License 5 votes vote down vote up
@Ignore
@Test
public void message() throws Exception {
    ServletTester tester = getServletTester();
    HttpTester.Request request = prepareRequest();

    request.setContent("{\n" +
            "    \"token\": \"XXYYZZ\",\n" +
            "    \"team_id\": \"TXXXXXXXX\",\n" +
            "    \"api_app_id\": \"AXXXXXXXXX\",\n" +
            "    \"event\": {\n" +
            "        \"type\": \"message\",\n" +
            "        \"channel\": \"C2147483705\",\n" +
            "        \"user\": \"U2147483697\",\n" +
            "        \"text\": \"Hello world\",\n" +
            "        \"ts\": \"1355517523.000005\"\n" +
            "}   ,\n" +
            "    \"type\": \"event_callback\",\n" +
            "    \"event_id\": \"EvXXXXXXXX\",\n" +
            "    \"event_time\": 1234567890\n" +
            "}");

    HttpTester.Response response1 = HttpTester.parseResponse(tester.getResponses(request.generate()));
    HttpTester.Response response2 = HttpTester.parseResponse(tester.getResponses(request.generate()));
    HttpTester.Response response3 = HttpTester.parseResponse(tester.getResponses(request.generate()));

    // wait for the async execution
    Thread.sleep(200L);

    assertThat(response1.getStatus(), is(equalTo(200)));
    assertThat(response2.getStatus(), is(equalTo(200)));
    assertThat(response3.getStatus(), is(equalTo(200)));
    assertThat(MESSAGE_CALL_COUNTER.get(), is(3));
}
 
Example #17
Source File: EventsApiTest.java    From java-slack-sdk with MIT License 5 votes vote down vote up
@Test
public void message() throws Exception {
    ServletTester tester = ServletTestUtils.getServletTester(webApp);
    HttpTester.Request request = ServletTestUtils.prepareRequest(signingSecret, "{\n" +
            "    \"token\": \"XXYYZZ\",\n" +
            "    \"team_id\": \"TXXXXXXXX\",\n" +
            "    \"api_app_id\": \"AXXXXXXXXX\",\n" +
            "    \"event\": {\n" +
            "        \"type\": \"message\",\n" +
            "        \"channel\": \"C2147483705\",\n" +
            "        \"user\": \"U2147483697\",\n" +
            "        \"text\": \"Hello world\",\n" +
            "        \"ts\": \"1355517523.000005\"\n" +
            "}   ,\n" +
            "    \"type\": \"event_callback\",\n" +
            "    \"event_id\": \"EvXXXXXXXX\",\n" +
            "    \"event_time\": 1234567890\n" +
            "}");

    HttpTester.Response response1 = HttpTester.parseResponse(tester.getResponses(request.generate()));
    HttpTester.Response response2 = HttpTester.parseResponse(tester.getResponses(request.generate()));
    HttpTester.Response response3 = HttpTester.parseResponse(tester.getResponses(request.generate()));

    // wait for the async execution
    Thread.sleep(200L);

    assertThat(response1.getStatus(), is(equalTo(200)));
    assertThat(response2.getStatus(), is(equalTo(200)));
    assertThat(response3.getStatus(), is(equalTo(200)));
    assertThat(messageCalls.get(), is(3));
}
 
Example #18
Source File: EventsApiTest.java    From java-slack-sdk with MIT License 5 votes vote down vote up
@Test
public void urlVerification() throws Exception {
    ServletTester tester = ServletTestUtils.getServletTester(webApp);

    UrlVerificationPayload payload = new UrlVerificationPayload();
    payload.setChallenge("cha-xxxxxx");
    payload.setToken("token-xxxx");

    HttpTester.Request request = ServletTestUtils.prepareRequest(signingSecret, GsonFactory.createSnakeCase().toJson(payload));
    HttpTester.Response response = HttpTester.parseResponse(tester.getResponses(request.generate()));

    assertThat(response.getContent(), is(equalTo("cha-xxxxxx")));
    assertThat(response.getStatus(), is(equalTo(200)));
    assertThat(response.get("Content-Type"), is(startsWith("text/plain")));
}
 
Example #19
Source File: WebEndpointTest.java    From java-slack-sdk with MIT License 5 votes vote down vote up
@Test
public void foo_towards_bar() throws Exception {
    ServletTester tester = TestUtils.getServletTester(barApp);
    HttpTester.Request request = buildRequest("POST", "/foo");
    HttpTester.Response response = HttpTester.parseResponse(tester.getResponses(request.generate()));
    assertThat(response.getContent(), is(equalTo("{\"error\":\"Not found\"}")));
    assertThat(response.getStatus(), is(equalTo(404)));
}
 
Example #20
Source File: WebEndpointTest.java    From java-slack-sdk with MIT License 5 votes vote down vote up
@Test
public void bar_POST() throws Exception {
    ServletTester tester = TestUtils.getServletTester(barApp);
    HttpTester.Request request = buildRequest("POST", "/bar");
    HttpTester.Response response = HttpTester.parseResponse(tester.getResponses(request.generate()));
    assertThat(response.getContent(), is(equalTo("Thanks!")));
    assertThat(response.getStatus(), is(equalTo(200)));
}
 
Example #21
Source File: WebEndpointTest.java    From java-slack-sdk with MIT License 5 votes vote down vote up
@Test
public void bar_GET() throws Exception {
    ServletTester tester = TestUtils.getServletTester(barApp);
    HttpTester.Request request = buildRequest("GET", "/bar");
    HttpTester.Response response = HttpTester.parseResponse(tester.getResponses(request.generate()));
    assertThat(response.getContent(), is(equalTo("{\"error\":\"Not found\"}")));
    assertThat(response.getStatus(), is(equalTo(404)));
}
 
Example #22
Source File: WebEndpointTest.java    From java-slack-sdk with MIT License 5 votes vote down vote up
@Test
public void foo_POST() throws Exception {
    ServletTester tester = TestUtils.getServletTester(fooApp);
    HttpTester.Request request = buildRequest("POST", "/foo");
    HttpTester.Response response = HttpTester.parseResponse(tester.getResponses(request.generate()));
    assertThat(response.getContent(), is(equalTo("{\"error\":\"Not found\"}")));
    assertThat(response.getStatus(), is(equalTo(404)));
}
 
Example #23
Source File: WebEndpointTest.java    From java-slack-sdk with MIT License 5 votes vote down vote up
@Test
public void foo_GET() throws Exception {
    ServletTester tester = TestUtils.getServletTester(fooApp);
    HttpTester.Request request = buildRequest("GET", "/foo");
    HttpTester.Response response = HttpTester.parseResponse(tester.getResponses(request.generate()));
    assertThat(response.getContent(), is(equalTo("Thanks!")));
    assertThat(response.getStatus(), is(equalTo(200)));
}
 
Example #24
Source File: EventsApiTest.java    From java-slack-sdk with MIT License 5 votes vote down vote up
@Test
public void message() throws Exception {
    ServletTester tester = TestUtils.getServletTester(webApp);
    HttpTester.Request request = TestUtils.prepareRequest();

    request.setContent("{\n" +
            "    \"token\": \"XXYYZZ\",\n" +
            "    \"team_id\": \"TXXXXXXXX\",\n" +
            "    \"api_app_id\": \"AXXXXXXXXX\",\n" +
            "    \"event\": {\n" +
            "        \"type\": \"message\",\n" +
            "        \"channel\": \"C2147483705\",\n" +
            "        \"user\": \"U2147483697\",\n" +
            "        \"text\": \"Hello world\",\n" +
            "        \"ts\": \"1355517523.000005\"\n" +
            "}   ,\n" +
            "    \"type\": \"event_callback\",\n" +
            "    \"event_id\": \"EvXXXXXXXX\",\n" +
            "    \"event_time\": 1234567890\n" +
            "}");

    HttpTester.Response response1 = HttpTester.parseResponse(tester.getResponses(request.generate()));
    HttpTester.Response response2 = HttpTester.parseResponse(tester.getResponses(request.generate()));
    HttpTester.Response response3 = HttpTester.parseResponse(tester.getResponses(request.generate()));

    // wait for the async execution
    Thread.sleep(200L);

    assertThat(response1.getStatus(), is(equalTo(200)));
    assertThat(response2.getStatus(), is(equalTo(200)));
    assertThat(response3.getStatus(), is(equalTo(200)));
    assertThat(messageCalls.get(), is(3));
}
 
Example #25
Source File: SlashCommandTest.java    From java-slack-sdk with MIT License 5 votes vote down vote up
@Test
public void weather() throws Exception {
    ServletTester tester = TestUtils.getServletTester(webApp);
    HttpTester.Request request = TestUtils.prepareRequest();

    // https://api.slack.com/slash-commands
    request.setContent("token=gIkuvaNzQIHg97ATvDxqgjtO" +
            "&team_id=T0001" +
            "&team_domain=example" +
            "&enterprise_id=E0001" +
            "&enterprise_name=Globular%20Construct%20Inc" +
            "&channel_id=C2147483705" +
            "&channel_name=test" +
            "&user_id=U2147483697" +
            "&user_name=Steve" +
            "&command=/weather" +
            "&text=94070" +
            "&response_url=https://hooks.slack.com/commands/1234/5678" +
            "&trigger_id=13345224609.738474920.8088930838d88f008e0");

    HttpTester.Response response = HttpTester.parseResponse(tester.getResponses(request.generate()));

    assertThat(response.getContent(), is(equalTo("{\"text\":\"It\\u0027s rainy in the area: 94070\"}")));
    assertThat(response.getStatus(), is(equalTo(200)));
    assertThat(response.get("Content-Type"), is(startsWith("application/json")));
    assertThat(weatherCalls.get(), is(1));
}
 
Example #26
Source File: ServletTestUtils.java    From java-slack-sdk with MIT License 4 votes vote down vote up
public static ServletTester getServletTester(Servlet servlet) throws Exception {
    ServletTester tester = new ServletTester();
    tester.addServlet(new ServletHolder(servlet), "/");
    tester.start();
    return tester;
}
 
Example #27
Source File: DialogTest.java    From java-slack-sdk with MIT License 4 votes vote down vote up
@Test
public void submission_error() throws Exception {
    ServletTester tester = TestUtils.getServletTester(webApp);
    HttpTester.Request request = TestUtils.prepareRequest();

    String payload = "{\n" +
            "    \"type\": \"dialog_submission\",\n" +
            "    \"submission\": {\n" +
            "        \"name\": \"Sigourney Dreamweaver\",\n" +
            "        \"email\": \"sigdre\",\n" +
            "        \"phone\": \"+1 800-555-1212\",\n" +
            "        \"meal\": \"burrito\",\n" +
            "        \"comment\": \"No sour cream please\",\n" +
            "        \"team_channel\": \"C0LFFBKPB\",\n" +
            "        \"who_should_sing\": \"U0MJRG1AL\"\n" +
            "    },\n" +
            "    \"callback_id\": \"ng-submission-callback-id\",\n" +
            "    \"state\": \"vegetarian\",\n" +
            "    \"team\": {\n" +
            "        \"id\": \"T1ABCD2E12\",\n" +
            "        \"domain\": \"coverbands\"\n" +
            "    },\n" +
            "    \"user\": {\n" +
            "        \"id\": \"W12A3BCDEF\",\n" +
            "        \"name\": \"dreamweaver\"\n" +
            "    },\n" +
            "    \"channel\": {\n" +
            "        \"id\": \"C1AB2C3DE\",\n" +
            "        \"name\": \"coverthon-1999\"\n" +
            "    },\n" +
            "    \"action_ts\": \"936893340.702759\",\n" +
            "    \"token\": \"legacy-fixed-value\",\n" +
            "    \"response_url\": \"https://hooks.slack.com/app/T012AB0A1/123456789/JpmK0yzoZDeRiqfeduTBYXWQ\"\n" +
            "}";
    request.setContent("payload=" + URLEncoder.encode(payload, "UTF-8"));

    HttpTester.Response response = HttpTester.parseResponse(tester.getResponses(request.generate()));

    assertThat(response.getContent(), is(equalTo("{\"errors\":[{\"name\":\"email\",\"error\":\"must be an email!\"}]}")));
    assertThat(response.getStatus(), is(equalTo(200)));
}
 
Example #28
Source File: DialogTest.java    From java-slack-sdk with MIT License 4 votes vote down vote up
@Test
public void submission_ok() throws Exception {
    ServletTester tester = TestUtils.getServletTester(webApp);
    HttpTester.Request request = TestUtils.prepareRequest();

    String payload = "{\n" +
            "    \"type\": \"dialog_submission\",\n" +
            "    \"submission\": {\n" +
            "        \"name\": \"Sigourney Dreamweaver\",\n" +
            "        \"email\": \"[email protected]\",\n" +
            "        \"phone\": \"+1 800-555-1212\",\n" +
            "        \"meal\": \"burrito\",\n" +
            "        \"comment\": \"No sour cream please\",\n" +
            "        \"team_channel\": \"C0LFFBKPB\",\n" +
            "        \"who_should_sing\": \"U0MJRG1AL\"\n" +
            "    },\n" +
            "    \"callback_id\": \"ok-submission-callback-id\",\n" +
            "    \"state\": \"vegetarian\",\n" +
            "    \"team\": {\n" +
            "        \"id\": \"T1ABCD2E12\",\n" +
            "        \"domain\": \"coverbands\"\n" +
            "    },\n" +
            "    \"user\": {\n" +
            "        \"id\": \"W12A3BCDEF\",\n" +
            "        \"name\": \"dreamweaver\"\n" +
            "    },\n" +
            "    \"channel\": {\n" +
            "        \"id\": \"C1AB2C3DE\",\n" +
            "        \"name\": \"coverthon-1999\"\n" +
            "    },\n" +
            "    \"action_ts\": \"936893340.702759\",\n" +
            "    \"token\": \"legacy-fixed-value\",\n" +
            "    \"response_url\": \"https://hooks.slack.com/app/T012AB0A1/123456789/JpmK0yzoZDeRiqfeduTBYXWQ\"\n" +
            "}";
    request.setContent("payload=" + URLEncoder.encode(payload, "UTF-8"));

    HttpTester.Response response = HttpTester.parseResponse(tester.getResponses(request.generate()));

    assertThat(response.getContent(), is(equalTo("")));
    assertThat(response.getStatus(), is(equalTo(200)));

    verify(submissionSenderMock, times(1)).send(Mockito.any(ActionResponse.class));
}
 
Example #29
Source File: EventsApiHandlerTest.java    From java-slack-sdk with MIT License 4 votes vote down vote up
private static ServletTester getServletTester() throws Exception {
    ServletTester tester = new ServletTester();
    tester.addServlet(SampleServlet.class, "/");
    tester.start();
    return tester;
}
 
Example #30
Source File: ServiceRegistryFunctionsTest.java    From knox with Apache License 2.0 4 votes vote down vote up
private void testSetup(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(ServiceType.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 ArrayDeque<>();

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

  server.start();

  interaction = new MockInteraction();
  request = HttpTester.newRequest();
  response = null;
}