Search in sources :

Example 66 with URL

use of java.net.URL in project bazel by bazelbuild.

the class HttpConnectorTest method badHost_throwsIOException.

@Test
public void badHost_throwsIOException() throws Exception {
    thrown.expect(IOException.class);
    thrown.expectMessage("Unknown host: bad.example");
    connector.connect(new URL("http://bad.example"), ImmutableMap.<String, String>of());
}
Also used : URL(java.net.URL) Test(org.junit.Test)

Example 67 with URL

use of java.net.URL in project bazel by bazelbuild.

the class HttpConnectorTest method serverError_retriesConnect.

@Test
public void serverError_retriesConnect() throws Exception {
    try (ServerSocket server = new ServerSocket(0, 1, InetAddress.getByName("127.0.0.1"))) {
        @SuppressWarnings("unused") Future<?> possiblyIgnoredError = executor.submit(new Callable<Object>() {

            @Override
            public Object call() throws Exception {
                try (Socket socket = server.accept()) {
                    readHttpRequest(socket.getInputStream());
                    sendLines(socket, "HTTP/1.1 500 Incredible Catastrophe", "Date: Fri, 31 Dec 1999 23:59:59 GMT", "Connection: close", "Content-Type: text/plain", "Content-Length: 8", "", "nononono");
                }
                try (Socket socket = server.accept()) {
                    readHttpRequest(socket.getInputStream());
                    sendLines(socket, "HTTP/1.1 200 OK", "Date: Fri, 31 Dec 1999 23:59:59 GMT", "Connection: close", "Content-Type: text/plain", "Content-Length: 5", "", "hello");
                }
                return null;
            }
        });
        try (Reader payload = new InputStreamReader(connector.connect(new URL(String.format("http://127.0.0.1:%d", server.getLocalPort())), ImmutableMap.<String, String>of()).getInputStream(), ISO_8859_1)) {
            assertThat(CharStreams.toString(payload)).isEqualTo("hello");
            assertThat(clock.currentTimeMillis()).isEqualTo(100L);
        }
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) ServerSocket(java.net.ServerSocket) ExpectedException(org.junit.rules.ExpectedException) IOException(java.io.IOException) Socket(java.net.Socket) ServerSocket(java.net.ServerSocket) URL(java.net.URL) Test(org.junit.Test)

Example 68 with URL

use of java.net.URL in project bazel by bazelbuild.

the class HttpConnectorTest method always500_givesUpEventually.

@Test
public void always500_givesUpEventually() throws Exception {
    final AtomicInteger tries = new AtomicInteger();
    try (ServerSocket server = new ServerSocket(0, 1, InetAddress.getByName("127.0.0.1"))) {
        @SuppressWarnings("unused") Future<?> possiblyIgnoredError = executor.submit(new Callable<Object>() {

            @Override
            public Object call() throws Exception {
                while (true) {
                    try (Socket socket = server.accept()) {
                        readHttpRequest(socket.getInputStream());
                        sendLines(socket, "HTTP/1.1 500 Oh My", "Date: Fri, 31 Dec 1999 23:59:59 GMT", "Connection: close", "Content-Type: text/plain", "Content-Length: 0", "", "");
                        tries.incrementAndGet();
                    }
                }
            }
        });
        thrown.expect(IOException.class);
        thrown.expectMessage("500 Oh My");
        try {
            connector.connect(new URL(String.format("http://127.0.0.1:%d", server.getLocalPort())), ImmutableMap.<String, String>of());
        } finally {
            assertThat(tries.get()).isGreaterThan(2);
        }
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ServerSocket(java.net.ServerSocket) ExpectedException(org.junit.rules.ExpectedException) IOException(java.io.IOException) Socket(java.net.Socket) ServerSocket(java.net.ServerSocket) URL(java.net.URL) Test(org.junit.Test)

Example 69 with URL

use of java.net.URL in project bazel by bazelbuild.

the class HttpConnectorTest method serverSays403_clientRetriesAnyway.

@Test
public void serverSays403_clientRetriesAnyway() throws Exception {
    final AtomicInteger tries = new AtomicInteger();
    try (ServerSocket server = new ServerSocket(0, 1, InetAddress.getByName("127.0.0.1"))) {
        @SuppressWarnings("unused") Future<?> possiblyIgnoredError = executor.submit(new Callable<Object>() {

            @Override
            public Object call() throws Exception {
                while (true) {
                    try (Socket socket = server.accept()) {
                        readHttpRequest(socket.getInputStream());
                        sendLines(socket, "HTTP/1.1 403 Forbidden", "Date: Fri, 31 Dec 1999 23:59:59 GMT", "Connection: close", "Content-Type: text/plain", "Content-Length: 0", "", "");
                        tries.incrementAndGet();
                    }
                }
            }
        });
        thrown.expect(IOException.class);
        thrown.expectMessage("403 Forbidden");
        try {
            connector.connect(new URL(String.format("http://127.0.0.1:%d", server.getLocalPort())), ImmutableMap.<String, String>of());
        } finally {
            assertThat(tries.get()).isGreaterThan(2);
        }
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ServerSocket(java.net.ServerSocket) ExpectedException(org.junit.rules.ExpectedException) IOException(java.io.IOException) Socket(java.net.Socket) ServerSocket(java.net.ServerSocket) URL(java.net.URL) Test(org.junit.Test)

Example 70 with URL

use of java.net.URL in project bazel by bazelbuild.

the class HttpConnectorTest method redirectToDifferentServer_works.

@Test
public void redirectToDifferentServer_works() throws Exception {
    try (ServerSocket server1 = new ServerSocket(0, 1, InetAddress.getByName("127.0.0.1"));
        ServerSocket server2 = new ServerSocket(0, 1, InetAddress.getByName("127.0.0.1"))) {
        @SuppressWarnings("unused") Future<?> possiblyIgnoredError = executor.submit(new Callable<Object>() {

            @Override
            public Object call() throws Exception {
                try (Socket socket = server1.accept()) {
                    readHttpRequest(socket.getInputStream());
                    sendLines(socket, "HTTP/1.1 301 Redirect", "Date: Fri, 31 Dec 1999 23:59:59 GMT", "Connection: close", String.format("Location: http://127.0.0.1:%d/doodle.tar.gz", server2.getLocalPort()), "Content-Length: 0", "", "");
                }
                return null;
            }
        });
        @SuppressWarnings("unused") Future<?> possiblyIgnoredError1 = executor.submit(new Callable<Object>() {

            @Override
            public Object call() throws Exception {
                try (Socket socket = server2.accept()) {
                    readHttpRequest(socket.getInputStream());
                    sendLines(socket, "HTTP/1.1 200 OK", "Date: Fri, 31 Dec 1999 23:59:59 GMT", "Connection: close", "Content-Type: text/plain", "Content-Length: 5", "", "hello");
                }
                return null;
            }
        });
        URLConnection connection = connector.connect(new URL(String.format("http://127.0.0.1:%d", server1.getLocalPort())), ImmutableMap.<String, String>of());
        assertThat(connection.getURL()).isEqualTo(new URL(String.format("http://127.0.0.1:%d/doodle.tar.gz", server2.getLocalPort())));
        try (InputStream input = connection.getInputStream()) {
            assertThat(ByteStreams.toByteArray(input)).isEqualTo("hello".getBytes(US_ASCII));
        }
    }
}
Also used : InputStream(java.io.InputStream) ServerSocket(java.net.ServerSocket) ExpectedException(org.junit.rules.ExpectedException) IOException(java.io.IOException) Socket(java.net.Socket) ServerSocket(java.net.ServerSocket) URLConnection(java.net.URLConnection) URL(java.net.URL) Test(org.junit.Test)

Aggregations

URL (java.net.URL)10796 IOException (java.io.IOException)2404 Test (org.junit.Test)2220 File (java.io.File)1991 MalformedURLException (java.net.MalformedURLException)1425 InputStream (java.io.InputStream)1263 HttpURLConnection (java.net.HttpURLConnection)1166 ArrayList (java.util.ArrayList)841 Bus (org.apache.cxf.Bus)748 SpringBusFactory (org.apache.cxf.bus.spring.SpringBusFactory)722 URLConnection (java.net.URLConnection)653 QName (javax.xml.namespace.QName)633 InputStreamReader (java.io.InputStreamReader)605 Service (javax.xml.ws.Service)549 HashMap (java.util.HashMap)533 URLClassLoader (java.net.URLClassLoader)526 BufferedReader (java.io.BufferedReader)518 DoubleItPortType (org.example.contract.doubleit.DoubleItPortType)361 URISyntaxException (java.net.URISyntaxException)348 URI (java.net.URI)331