@Test public void testProxyAuthGet() throws Exception { JettyProxyProvider proxy = new JettyProxyProvider("u", "p"); proxy.addBehaviour("/*", new Debug(), new Content()); proxy.start(); URL url = new URL("http://speutel.invalid/foo"); SocketAddress sa = new InetSocketAddress("localhost", proxy.getPort()); Proxy p = new Proxy(Type.HTTP, sa); Authenticator.setDefault( new Authenticator() { @Override protected PasswordAuthentication getPasswordAuthentication() { if (getRequestingHost().equals("localhost")) { String password = "******"; return new PasswordAuthentication("u", password.toCharArray()); } return super.getPasswordAuthentication(); } }); HttpURLConnection conn = (HttpURLConnection) url.openConnection(p); conn.setDoInput(true); conn.connect(); assertEquals(200, conn.getResponseCode()); assertEquals("foo", read(conn.getContent()).trim()); conn.disconnect(); }
@Test public void testProxyGet() throws Exception { JettyProxyProvider proxy = new JettyProxyProvider(); proxy.addBehaviour("/*", new Debug(), new Content()); proxy.start(); URL url = new URL("http://speutel.invalid/foo"); SocketAddress sa = new InetSocketAddress("localhost", proxy.getPort()); Proxy p = new Proxy(Type.HTTP, sa); HttpURLConnection conn = (HttpURLConnection) url.openConnection(p); conn.setDoInput(true); conn.connect(); assertEquals(200, conn.getResponseCode()); assertEquals("foo", read(conn.getContent()).trim()); conn.disconnect(); }
@Test public void testProxyAuthGetFail407() throws Exception { JettyProxyProvider proxy = new JettyProxyProvider("u", "p"); proxy.addBehaviour("/*", new Debug(), new Content()); proxy.start(); URL url = new URL("http://speutel.invalid/foo"); SocketAddress sa = new InetSocketAddress("localhost", proxy.getPort()); Proxy p = new Proxy(Type.HTTP, sa); HttpURLConnection conn = (HttpURLConnection) url.openConnection(p); try { conn.setDoInput(true); conn.connect(); conn.getResponseCode(); } catch (IOException e) { assertTrue("expected status code 407", e.getMessage().contains("407")); } finally { conn.disconnect(); } }
@Test public void testProxyPut() throws Exception { JettyProxyProvider proxy = new JettyProxyProvider(); Consumer consumer = new Consumer(); proxy.addBehaviour("/*", new Debug(), consumer); proxy.start(); URL url = new URL("http://speutel.invalid/foo"); SocketAddress sa = new InetSocketAddress("localhost", proxy.getPort()); Proxy p = new Proxy(Type.HTTP, sa); HttpURLConnection conn = (HttpURLConnection) url.openConnection(p); conn.setDoOutput(true); conn.setRequestMethod("PUT"); conn.connect(); byte[] bytes = "TestPut".getBytes("US-ASCII"); conn.getOutputStream().write(bytes); conn.getOutputStream().close(); assertEquals(200, conn.getResponseCode()); assertEquals(bytes.length, consumer.getTotal()); conn.disconnect(); }