Search in sources :

Example 11 with HttpResponseException

use of com.google.api.client.http.HttpResponseException in project providence by morimekta.

the class HttpClientHandlerNetworkTest method testBrokenPipe_NetHttpTransport.

@Test
public void testBrokenPipe_NetHttpTransport() throws Failure {
    HttpRequestFactory factory = new NetHttpTransport().createRequestFactory();
    TestService.Iface client = new TestService.Client(new HttpClientHandler(this::endpoint, factory, provider));
    try {
        // The request must be larger than the socket read buffer, to force it to fail the write to socket.
        client.test(new Request(Strings.times("request ", 1024 * 1024)));
        fail("No exception");
    } catch (HttpResponseException e) {
        fail("When did this become a HttpResponseException?");
    } catch (IOException ex) {
        // TODO: This should be a HttpResponseException
        assertThat(ex.getMessage(), is("Error writing request body to server"));
    }
}
Also used : HttpRequestFactory(com.google.api.client.http.HttpRequestFactory) TestService(net.morimekta.test.providence.client.TestService) NetHttpTransport(com.google.api.client.http.javanet.NetHttpTransport) Request(net.morimekta.test.providence.client.Request) HttpResponseException(com.google.api.client.http.HttpResponseException) IOException(java.io.IOException) Test(org.junit.Test)

Example 12 with HttpResponseException

use of com.google.api.client.http.HttpResponseException in project providence by morimekta.

the class HttpClientHandlerTest method testSimpleRequest_404_notFound.

@Test
public void testSimpleRequest_404_notFound() throws IOException, Failure, TException {
    TestService.Iface client = new TestService.Client(new HttpClientHandler(this::notfound, factory(), provider, instrumentation));
    try {
        client.test(new Request("request"));
        fail("No exception");
    } catch (HttpResponseException ex) {
        assertThat(ex.getStatusCode(), is(404));
        assertThat(ex.getStatusMessage(), is("Not Found"));
    }
    verify(instrumentation).onTransportException(any(HttpResponseException.class), anyDouble(), any(PServiceCall.class), isNull());
    verifyNoMoreInteractions(impl, instrumentation);
}
Also used : TestService(net.morimekta.test.providence.client.TestService) PServiceCall(net.morimekta.providence.PServiceCall) HttpServletRequest(javax.servlet.http.HttpServletRequest) Request(net.morimekta.test.providence.client.Request) HttpResponseException(com.google.api.client.http.HttpResponseException) Test(org.junit.Test)

Example 13 with HttpResponseException

use of com.google.api.client.http.HttpResponseException in project providence by morimekta.

the class HttpClientHandlerTest method testSimpleRequest_405_notSupported.

@Test
public void testSimpleRequest_405_notSupported() throws IOException, Failure, TException {
    GenericUrl url = endpoint();
    url.setRawPath("/does_not_exists");
    TestService.Iface client = new TestService.Client(new HttpClientHandler(() -> url, factory(), provider, instrumentation));
    try {
        client.test(new Request("request"));
        fail("No exception");
    } catch (HttpResponseException ex) {
        assertThat(ex.getStatusCode(), is(405));
        assertThat(ex.getStatusMessage(), is("HTTP method POST is not supported by this URL"));
    }
    verify(instrumentation).onTransportException(any(HttpResponseException.class), anyDouble(), any(PServiceCall.class), isNull());
    verifyNoMoreInteractions(impl, instrumentation);
}
Also used : TestService(net.morimekta.test.providence.client.TestService) PServiceCall(net.morimekta.providence.PServiceCall) HttpServletRequest(javax.servlet.http.HttpServletRequest) Request(net.morimekta.test.providence.client.Request) HttpResponseException(com.google.api.client.http.HttpResponseException) GenericUrl(com.google.api.client.http.GenericUrl) Test(org.junit.Test)

Example 14 with HttpResponseException

use of com.google.api.client.http.HttpResponseException in project providence by morimekta.

the class RPC method run.

@SuppressWarnings("unchecked")
void run(String... args) {
    try {
        ArgumentParser cli = options.getArgumentParser("pvdrpc", "Providence RPC Tool");
        try {
            cli.parse(args);
            if (options.showHelp()) {
                System.out.println("Providence RPC Tool - " + getVersionString());
                System.out.println("Usage: " + cli.getSingleLineUsage());
                System.out.println();
                System.out.println("Example code to run:");
                System.out.println("$ cat call.json | pvdrpc -I thrift/ -s cal.Calculator http://localhost:8080/service");
                System.out.println("$ pvdrpc -i binary,file:my.data -f json_protocol -I thrift/ -s cal.Calculator http://localhost:8080/service");
                System.out.println();
                cli.printUsage(System.out);
                System.out.println();
                System.out.println("Available formats are:");
                for (Format format : Format.values()) {
                    System.out.println(String.format(" - %-20s : %s", format.name(), format.desc));
                }
                return;
            } else if (options.showVersion()) {
                System.out.println("Providence RPC Tool - " + getVersionString());
                return;
            }
            cli.validate();
            MessageReader in = options.getInput();
            MessageWriter out = options.getOutput();
            PService service = options.getDefinition();
            PServiceCallHandler handler = options.getHandler();
            PServiceCall call = in.read(service);
            in.close();
            PServiceCall resp = handler.handleCall(call, service);
            out.write(resp);
            out.separator();
            out.close();
            if (options.out.base64mime && options.out.file == null) {
                System.out.println();
            }
            return;
        } catch (ConnectException e) {
            System.out.flush();
            System.err.format("Unable to connect to %s: %s%n", options.endpoint, e.getMessage());
            if (options.verbose()) {
                System.err.println();
                e.printStackTrace();
            }
        } catch (HttpResponseException e) {
            System.out.flush();
            System.err.println("Received " + e.getStatusCode() + " " + e.getStatusMessage());
            System.err.println(" - from: " + options.endpoint);
            if (options.verbose()) {
                System.err.println();
                e.printStackTrace();
            }
        } catch (ArgumentException e) {
            System.out.flush();
            System.err.println(e.getMessage());
            System.err.println("Usage: " + cli.getSingleLineUsage());
            System.err.println();
            System.err.println("Run $ pvdrpc --help # for available options.");
        } catch (SerializerException e) {
            System.out.flush();
            System.err.println("Serializer error: " + e.asString());
            if (options.verbose()) {
                System.err.println();
                e.printStackTrace();
            }
        } catch (UncheckedIOException | IOException e) {
            System.out.flush();
            System.err.println("I/O error: " + e.getMessage());
            if (options.verbose()) {
                System.out.flush();
                e.printStackTrace();
            }
        } catch (IllegalArgumentException e) {
            System.out.flush();
            System.err.println("Internal Error: " + e.getMessage());
            if (options.verbose()) {
                System.err.println();
                e.printStackTrace();
            }
        }
    } catch (RuntimeException | IOException e) {
        System.out.flush();
        System.err.println("Unchecked exception: " + e.getMessage());
        if (options.verbose()) {
            System.out.flush();
            e.printStackTrace();
        }
    }
    System.err.flush();
    exit(1);
}
Also used : MessageReader(net.morimekta.providence.mio.MessageReader) HttpResponseException(com.google.api.client.http.HttpResponseException) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) ArgumentParser(net.morimekta.console.args.ArgumentParser) SerializerException(net.morimekta.providence.serializer.SerializerException) Format(net.morimekta.providence.tools.common.formats.Format) PServiceCallHandler(net.morimekta.providence.PServiceCallHandler) PServiceCall(net.morimekta.providence.PServiceCall) MessageWriter(net.morimekta.providence.mio.MessageWriter) ArgumentException(net.morimekta.console.args.ArgumentException) PService(net.morimekta.providence.descriptor.PService) ConnectException(java.net.ConnectException)

Example 15 with HttpResponseException

use of com.google.api.client.http.HttpResponseException in project google-api-java-client by google.

the class MediaHttpDownloaderTest method testDownloadClientError.

public void testDownloadClientError() throws Exception {
    int contentLength = MediaHttpDownloader.MAXIMUM_CHUNK_SIZE * 2;
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    MediaTransport fakeTransport = new MediaTransport(contentLength);
    fakeTransport.testClientError = true;
    MediaHttpDownloader downloader = new MediaHttpDownloader(fakeTransport, null);
    try {
        downloader.download(new GenericUrl(TEST_REQUEST_URL), outputStream);
        fail("Expected " + HttpResponseException.class);
    } catch (HttpResponseException e) {
    // Expected
    }
    // There should be only 1 call made: 1 download request that returned a 404.
    assertEquals(1, fakeTransport.lowLevelExecCalls);
}
Also used : HttpResponseException(com.google.api.client.http.HttpResponseException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) GenericUrl(com.google.api.client.http.GenericUrl)

Aggregations

HttpResponseException (com.google.api.client.http.HttpResponseException)38 GenericUrl (com.google.api.client.http.GenericUrl)15 IOException (java.io.IOException)15 HttpResponse (com.google.api.client.http.HttpResponse)13 Test (org.junit.Test)13 HttpRequest (com.google.api.client.http.HttpRequest)10 HttpRequestFactory (com.google.api.client.http.HttpRequestFactory)6 HttpHeaders (com.google.api.client.http.HttpHeaders)4 LowLevelHttpResponse (com.google.api.client.http.LowLevelHttpResponse)4 ErrorResponseTemplate (com.google.cloud.tools.jib.registry.json.ErrorResponseTemplate)4 GoogleJsonError (com.google.api.client.googleapis.json.GoogleJsonError)3 ErrorEntryTemplate (com.google.cloud.tools.jib.registry.json.ErrorEntryTemplate)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 PServiceCall (net.morimekta.providence.PServiceCall)3 Request (net.morimekta.test.providence.client.Request)3 TestService (net.morimekta.test.providence.client.TestService)3 AccountManager (android.accounts.AccountManager)2 ByteArrayContent (com.google.api.client.http.ByteArrayContent)2 HttpTransport (com.google.api.client.http.HttpTransport)2 NetHttpTransport (com.google.api.client.http.javanet.NetHttpTransport)2