Search in sources :

Example 1 with Debug

use of org.sonatype.goodies.httpfixture.server.jetty.behaviour.Debug in project goodies by sonatype.

the class JettyProxyProviderTest method testProxyPut.

@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();
}
Also used : Proxy(java.net.Proxy) HttpURLConnection(java.net.HttpURLConnection) Consumer(org.sonatype.goodies.httpfixture.server.jetty.behaviour.Consumer) InetSocketAddress(java.net.InetSocketAddress) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress) Debug(org.sonatype.goodies.httpfixture.server.jetty.behaviour.Debug) URL(java.net.URL) Test(org.junit.Test)

Example 2 with Debug

use of org.sonatype.goodies.httpfixture.server.jetty.behaviour.Debug in project goodies by sonatype.

the class JettyProxyProviderTest method testAuthAfterProxyAuthGetFail401.

@Test
public void testAuthAfterProxyAuthGetFail401() throws Exception {
    JettyProxyProvider proxy = new JettyProxyProvider("u", "p");
    proxy.addBehaviour("/*", new Debug(), new Content());
    proxy.addAuthentication("/*", "BASIC");
    proxy.addUser("user", "password");
    proxy.start();
    Authenticator.setDefault(new Authenticator() {

        @Override
        protected PasswordAuthentication getPasswordAuthentication() {
            if (getRequestingHost().equals("localhost")) {
                String password = "p";
                return new PasswordAuthentication("u", password.toCharArray());
            }
            return super.getPasswordAuthentication();
        }
    });
    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();
        assertEquals(401, conn.getResponseCode());
    } finally {
        conn.disconnect();
    }
}
Also used : Proxy(java.net.Proxy) HttpURLConnection(java.net.HttpURLConnection) Content(org.sonatype.goodies.httpfixture.server.jetty.behaviour.Content) InetSocketAddress(java.net.InetSocketAddress) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress) Debug(org.sonatype.goodies.httpfixture.server.jetty.behaviour.Debug) Authenticator(java.net.Authenticator) URL(java.net.URL) PasswordAuthentication(java.net.PasswordAuthentication) Test(org.junit.Test)

Example 3 with Debug

use of org.sonatype.goodies.httpfixture.server.jetty.behaviour.Debug in project goodies by sonatype.

the class JettyProxyProviderTest method testProxyAuthGet.

@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 = "p";
                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();
}
Also used : Proxy(java.net.Proxy) HttpURLConnection(java.net.HttpURLConnection) Content(org.sonatype.goodies.httpfixture.server.jetty.behaviour.Content) InetSocketAddress(java.net.InetSocketAddress) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress) Debug(org.sonatype.goodies.httpfixture.server.jetty.behaviour.Debug) URL(java.net.URL) Authenticator(java.net.Authenticator) PasswordAuthentication(java.net.PasswordAuthentication) Test(org.junit.Test)

Example 4 with Debug

use of org.sonatype.goodies.httpfixture.server.jetty.behaviour.Debug in project goodies by sonatype.

the class JettyProxyProviderTest method testAuthAfterProxyAuthGet.

@Test
public void testAuthAfterProxyAuthGet() throws Exception {
    JettyProxyProvider proxy = new JettyProxyProvider("u", "p");
    proxy.addBehaviour("/*", new Debug(), new Content());
    proxy.addAuthentication("/*", "BASIC");
    proxy.addUser("user", "password");
    proxy.start();
    Authenticator.setDefault(new Authenticator() {

        @Override
        protected PasswordAuthentication getPasswordAuthentication() {
            if (getRequestingHost().equals("localhost")) {
                String password = "p";
                return new PasswordAuthentication("u", password.toCharArray());
            } else {
                String password = "password";
                return new PasswordAuthentication("user", password.toCharArray());
            }
        }
    });
    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();
        assertEquals(200, conn.getResponseCode());
    } finally {
        conn.disconnect();
    }
}
Also used : Proxy(java.net.Proxy) HttpURLConnection(java.net.HttpURLConnection) Content(org.sonatype.goodies.httpfixture.server.jetty.behaviour.Content) InetSocketAddress(java.net.InetSocketAddress) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress) Debug(org.sonatype.goodies.httpfixture.server.jetty.behaviour.Debug) Authenticator(java.net.Authenticator) URL(java.net.URL) PasswordAuthentication(java.net.PasswordAuthentication) Test(org.junit.Test)

Example 5 with Debug

use of org.sonatype.goodies.httpfixture.server.jetty.behaviour.Debug in project goodies by sonatype.

the class JettyProxyProviderTest method testProxyAuthGetFail407.

@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();
    }
}
Also used : Proxy(java.net.Proxy) HttpURLConnection(java.net.HttpURLConnection) Content(org.sonatype.goodies.httpfixture.server.jetty.behaviour.Content) InetSocketAddress(java.net.InetSocketAddress) IOException(java.io.IOException) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress) Debug(org.sonatype.goodies.httpfixture.server.jetty.behaviour.Debug) URL(java.net.URL) Test(org.junit.Test)

Aggregations

HttpURLConnection (java.net.HttpURLConnection)6 InetSocketAddress (java.net.InetSocketAddress)6 Proxy (java.net.Proxy)6 SocketAddress (java.net.SocketAddress)6 URL (java.net.URL)6 Test (org.junit.Test)6 Debug (org.sonatype.goodies.httpfixture.server.jetty.behaviour.Debug)6 Content (org.sonatype.goodies.httpfixture.server.jetty.behaviour.Content)5 Authenticator (java.net.Authenticator)3 PasswordAuthentication (java.net.PasswordAuthentication)3 IOException (java.io.IOException)1 Consumer (org.sonatype.goodies.httpfixture.server.jetty.behaviour.Consumer)1