|
| 1 | +package test_locally.api.socket_mode; |
| 2 | + |
| 3 | +import com.slack.api.Slack; |
| 4 | +import com.slack.api.SlackConfig; |
| 5 | +import com.slack.api.socket_mode.SocketModeClient; |
| 6 | +import lombok.extern.slf4j.Slf4j; |
| 7 | +import okhttp3.Credentials; |
| 8 | +import org.eclipse.jetty.proxy.ConnectHandler; |
| 9 | +import org.eclipse.jetty.proxy.ProxyServlet; |
| 10 | +import org.eclipse.jetty.server.Request; |
| 11 | +import org.eclipse.jetty.server.Server; |
| 12 | +import org.eclipse.jetty.server.ServerConnector; |
| 13 | +import org.eclipse.jetty.servlet.ServletContextHandler; |
| 14 | +import org.eclipse.jetty.servlet.ServletHolder; |
| 15 | +import org.junit.After; |
| 16 | +import org.junit.Before; |
| 17 | +import org.junit.Test; |
| 18 | +import test_with_remote_apis.AuthProxyHeadersTest; |
| 19 | +import util.PortProvider; |
| 20 | +import util.socket_mode.MockWebApiServer; |
| 21 | + |
| 22 | +import javax.servlet.ServletException; |
| 23 | +import javax.servlet.http.HttpServletRequest; |
| 24 | +import javax.servlet.http.HttpServletResponse; |
| 25 | +import java.io.IOException; |
| 26 | +import java.util.Collections; |
| 27 | +import java.util.HashMap; |
| 28 | +import java.util.Locale; |
| 29 | +import java.util.Map; |
| 30 | +import java.util.concurrent.atomic.AtomicInteger; |
| 31 | + |
| 32 | +@Slf4j |
| 33 | +public class SocketModeClient_Proxies_Test { |
| 34 | + |
| 35 | + static final String VALID_APP_TOKEN = "xapp-valid-123123123123123123123123123123123123"; |
| 36 | + |
| 37 | + MockWebApiServer webApiServer = new MockWebApiServer(); |
| 38 | + SlackConfig config = new SlackConfig(); |
| 39 | + |
| 40 | + static Server proxyServer = new Server(); |
| 41 | + static ServerConnector proxyServerConnector = new ServerConnector(proxyServer); |
| 42 | + static Integer proxyServerPort; |
| 43 | + |
| 44 | + AtomicInteger proxyCallCount = new AtomicInteger(0); |
| 45 | + |
| 46 | + @Before |
| 47 | + public void setUp() throws Exception { |
| 48 | + webApiServer.start(); |
| 49 | + config.setMethodsEndpointUrlPrefix(webApiServer.getMethodsEndpointPrefix()); |
| 50 | + |
| 51 | + // https://github.com/eclipse/jetty.project/blob/jetty-9.2.30.v20200428/examples/embedded/src/main/java/org/eclipse/jetty/embedded/ProxyServer.java |
| 52 | + proxyServerPort = PortProvider.getPort(AuthProxyHeadersTest.class.getName()); |
| 53 | + proxyServerConnector.setPort(proxyServerPort); |
| 54 | + proxyServer.addConnector(proxyServerConnector); |
| 55 | + ConnectHandler proxy = new ConnectHandler() { |
| 56 | + @Override |
| 57 | + public void handle(String target, Request br, HttpServletRequest request, HttpServletResponse res) |
| 58 | + throws ServletException, IOException { |
| 59 | + log.info("Proxy server handles a new connection (target: {})", target); |
| 60 | + super.handle(target, br, request, res); |
| 61 | + if (res.getStatus() != 407) { |
| 62 | + proxyCallCount.incrementAndGet(); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + @Override |
| 67 | + protected boolean handleAuthentication(HttpServletRequest request, HttpServletResponse response, String address) { |
| 68 | + for (String name : Collections.list(request.getHeaderNames())) { |
| 69 | + log.info("{}: {}", name, request.getHeader(name)); |
| 70 | + if (name.toLowerCase(Locale.ENGLISH).equals("proxy-authorization")) { |
| 71 | + return request.getHeader(name).equals("Basic bXktdXNlcm5hbWU6bXktcGFzc3dvcmQ="); |
| 72 | + } |
| 73 | + } |
| 74 | + return false; |
| 75 | + } |
| 76 | + }; |
| 77 | + proxyServer.setHandler(proxy); |
| 78 | + ServletContextHandler context = new ServletContextHandler(proxy, "/", ServletContextHandler.SESSIONS); |
| 79 | + ServletHolder proxyServlet = new ServletHolder(ProxyServlet.class); |
| 80 | + context.addServlet(proxyServlet, "/*"); |
| 81 | + proxyServer.start(); |
| 82 | + |
| 83 | + config.setProxyUrl("http://127.0.0.1:" + proxyServerPort); |
| 84 | + |
| 85 | + Map<String, String> proxyHeaders = new HashMap<>(); |
| 86 | + String username = "my-username"; |
| 87 | + String password = "my-password"; |
| 88 | + proxyHeaders.put("Proxy-Authorization", Credentials.basic(username, password)); |
| 89 | + config.setProxyHeaders(proxyHeaders); |
| 90 | + } |
| 91 | + |
| 92 | + @After |
| 93 | + public void tearDown() throws Exception { |
| 94 | + webApiServer.stop(); |
| 95 | + |
| 96 | + proxyServer.removeConnector(proxyServerConnector); |
| 97 | + proxyServer.stop(); |
| 98 | + } |
| 99 | + |
| 100 | + @Test(expected = UnsupportedOperationException.class) |
| 101 | + public void proxyAuth() throws Exception { |
| 102 | + SlackConfig config = new SlackConfig(); |
| 103 | + config.setMethodsEndpointUrlPrefix(webApiServer.getMethodsEndpointPrefix()); |
| 104 | + config.setProxyUrl("http://my-username:my-password@localhost:" + proxyServerPort + "/"); |
| 105 | + Slack slack = Slack.getInstance(config); |
| 106 | + slack.socketMode(VALID_APP_TOKEN, SocketModeClient.Backend.JavaWebSocket); |
| 107 | + } |
| 108 | +} |
0 commit comments