Java Code Examples for com.browserup.bup.BrowserUpProxyServer#start()
The following examples show how to use
com.browserup.bup.BrowserUpProxyServer#start() .
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: ProxyManager.java From browserup-proxy with Apache License 2.0 | 5 votes |
private BrowserUpProxyServer startProxy(BrowserUpProxyServer proxy, int port, InetAddress clientBindAddr, InetAddress serverBindAddr) { if (port != 0) { BrowserUpProxyServer old = proxies.putIfAbsent(port, proxy); if (old != null) { LOG.info("Proxy already exists at port {}", port); throw new ProxyExistsException(port); } } try { proxy.start(port, clientBindAddr, serverBindAddr); if (port == 0) { int realPort = proxy.getPort(); proxies.put(realPort, proxy); } return proxy; } catch (Exception ex) { if (port != 0) { proxies.remove(port); } try { proxy.stop(); } catch (Exception ex2) { ex.addSuppressed(ex2); } throw ex; } }
Example 2
Source File: InterceptorTest.java From browserup-proxy with Apache License 2.0 | 5 votes |
@Test public void testCanModifyRequest() throws IOException { String url = "/modifyrequest"; stubFor( get(urlEqualTo(url)). willReturn(ok(). withBody("success"). withHeader(HttpHeaders.Names.CONTENT_TYPE, "text/plain; charset=utf-8"))); proxy = new BrowserUpProxyServer(); proxy.start(); proxy.addFirstHttpFilterFactory(new HttpFiltersSourceAdapter() { @Override public HttpFilters filterRequest(HttpRequest originalRequest) { return new HttpFiltersAdapter(originalRequest) { @Override public HttpResponse clientToProxyRequest(HttpObject httpObject) { if (httpObject instanceof HttpRequest) { HttpRequest httpRequest = (HttpRequest) httpObject; httpRequest.setUri(httpRequest.uri().replace("/originalrequest", "/modifyrequest")); } return super.clientToProxyRequest(httpObject); } }; } }); try (CloseableHttpClient httpClient = NewProxyServerTestUtil.getNewHttpClient(proxy.getPort())) { CloseableHttpResponse response = httpClient.execute(new HttpGet("http://localhost:" + mockServerPort + "/originalrequest")); String responseBody = NewProxyServerTestUtil.toStringAndClose(response.getEntity().getContent()); assertEquals("Expected server to return a 200", 200, response.getStatusLine().getStatusCode()); assertEquals("Did not receive expected response from mock server", "success", responseBody); verify(1, getRequestedFor(urlEqualTo(url))); } }
Example 3
Source File: InterceptorTest.java From browserup-proxy with Apache License 2.0 | 5 votes |
@Test public void testMitmDisabledHttpsRequestFilterNotAvailable() throws IOException { String url = "/mitmdisabled"; stubFor(get(urlMatching(url)).willReturn(ok().withBody("success"))); proxy = new BrowserUpProxyServer(); proxy.setMitmDisabled(true); proxy.start(); final AtomicBoolean connectRequestFilterFired = new AtomicBoolean(false); final AtomicBoolean getRequestFilterFired = new AtomicBoolean(false); proxy.addRequestFilter(new RequestFilter() { @Override public HttpResponse filterRequest(HttpRequest request, HttpMessageContents contents, HttpMessageInfo messageInfo) { if (request.getMethod().equals(HttpMethod.CONNECT)) { connectRequestFilterFired.set(true); } else if (request.getMethod().equals(HttpMethod.GET)) { getRequestFilterFired.set(true); } return null; } }); try (CloseableHttpClient httpClient = NewProxyServerTestUtil.getNewHttpClient(proxy.getPort())) { CloseableHttpResponse response = httpClient.execute(new HttpGet("https://localhost:" + mockServerHttpsPort + "/mitmdisabled")); assertEquals("Expected server to return a 200", 200, response.getStatusLine().getStatusCode()); assertTrue("Expected request filter to fire on CONNECT", connectRequestFilterFired.get()); assertFalse("Expected request filter to fail to fire on GET because MITM is disabled", getRequestFilterFired.get()); verify(1, getRequestedFor(urlEqualTo(url))); } }
Example 4
Source File: InterceptorTest.java From browserup-proxy with Apache License 2.0 | 4 votes |
/** * Helper method for executing response modification tests. */ private void testModifiedResponse(final String originalText, final String newText) throws IOException { String url = "/modifyresponse"; stubFor( get(urlMatching(url)). willReturn(ok(). withBody(originalText). withHeader(HttpHeaders.Names.CONTENT_TYPE, "text/plain; charset=utf-8"))); proxy = new BrowserUpProxyServer(); proxy.start(); proxy.addFirstHttpFilterFactory(new HttpFiltersSourceAdapter() { @Override public HttpFilters filterRequest(HttpRequest originalRequest) { return new HttpFiltersAdapter(originalRequest) { @Override public HttpObject proxyToClientResponse(HttpObject httpObject) { if (httpObject instanceof FullHttpResponse) { FullHttpResponse httpResponseAndContent = (FullHttpResponse) httpObject; String bodyContent = HttpObjectUtil.extractHttpEntityBody(httpResponseAndContent); if (bodyContent.equals(originalText)) { HttpObjectUtil.replaceTextHttpEntityBody(httpResponseAndContent, newText); } } return super.proxyToClientResponse(httpObject); } }; } @Override public int getMaximumResponseBufferSizeInBytes() { return 10000; } }); try (CloseableHttpClient httpClient = NewProxyServerTestUtil.getNewHttpClient(proxy.getPort())) { CloseableHttpResponse response = httpClient.execute(new HttpGet("http://localhost:" + mockServerPort + "/modifyresponse")); String responseBody = NewProxyServerTestUtil.toStringAndClose(response.getEntity().getContent()); assertEquals("Expected server to return a 200", 200, response.getStatusLine().getStatusCode()); assertEquals("Did not receive expected response from mock server", newText, responseBody); verify(1, getRequestedFor(urlEqualTo(url))); } }
Example 5
Source File: NewProxyServerTest.java From browserup-proxy with Apache License 2.0 | 4 votes |
@Before public void setUpProxyServer() { proxy = new BrowserUpProxyServer(); proxy.start(); }