Search in sources :

Example 1 with Content

use of org.sonatype.goodies.httpfixture.server.jetty.behaviour.Content 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 2 with Content

use of org.sonatype.goodies.httpfixture.server.jetty.behaviour.Content 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 3 with Content

use of org.sonatype.goodies.httpfixture.server.jetty.behaviour.Content 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 4 with Content

use of org.sonatype.goodies.httpfixture.server.jetty.behaviour.Content 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)

Example 5 with Content

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

the class JettyProxyProviderTest method testProxyGet.

@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();
}
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) Test(org.junit.Test)

Aggregations

Content (org.sonatype.goodies.httpfixture.server.jetty.behaviour.Content)7 HttpURLConnection (java.net.HttpURLConnection)6 URL (java.net.URL)6 Test (org.junit.Test)6 InetSocketAddress (java.net.InetSocketAddress)5 Proxy (java.net.Proxy)5 SocketAddress (java.net.SocketAddress)5 Debug (org.sonatype.goodies.httpfixture.server.jetty.behaviour.Debug)5 Authenticator (java.net.Authenticator)3 PasswordAuthentication (java.net.PasswordAuthentication)3 Pause (org.sonatype.goodies.httpfixture.server.jetty.behaviour.Pause)2 BufferedReader (java.io.BufferedReader)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 InputStreamReader (java.io.InputStreamReader)1 URLConnection (java.net.URLConnection)1 Redirect (org.sonatype.goodies.httpfixture.server.jetty.behaviour.Redirect)1 Stutter (org.sonatype.goodies.httpfixture.server.jetty.behaviour.Stutter)1 Truncate (org.sonatype.goodies.httpfixture.server.jetty.behaviour.Truncate)1