Search in sources :

Example 1 with URLFetchRequest

use of com.google.appengine.api.urlfetch.URLFetchServicePb.URLFetchRequest in project appengine-java-standard by GoogleCloudPlatform.

the class URLFetchServiceImplTest method testAsync_Timeout.

@Test
public void testAsync_Timeout() throws Exception {
    URL url = new URL("http://slow-domain.com/foo");
    HTTPRequest request = new HTTPRequest(url);
    URLFetchRequest requestProto = URLFetchRequest.newBuilder().setUrl(url.toString()).setMethod(RequestMethod.GET).setFollowRedirects(true).build();
    String errorDetails = "details";
    when(delegate.makeAsyncCall(same(ApiProxy.getCurrentEnvironment()), eq(URLFetchServiceImpl.PACKAGE), eq("Fetch"), eq(requestProto.toByteArray()), any())).thenReturn(Futures.immediateFailedFuture(new ApiProxy.ApplicationException(ErrorCode.DEADLINE_EXCEEDED.getNumber(), errorDetails)));
    Future<HTTPResponse> response = new URLFetchServiceImpl().fetchAsync(request);
    ExecutionException ex = assertThrows(ExecutionException.class, response::get);
    assertThat(ex).hasCauseThat().isInstanceOf(SocketTimeoutException.class);
    assertThat(ex).hasCauseThat().hasMessageThat().isEqualTo("Timeout while fetching URL: http://slow-domain.com/foo");
}
Also used : ByteString(com.google.protobuf.ByteString) URLFetchRequest(com.google.appengine.api.urlfetch.URLFetchServicePb.URLFetchRequest) ExecutionException(java.util.concurrent.ExecutionException) URL(java.net.URL) Test(org.junit.Test)

Example 2 with URLFetchRequest

use of com.google.appengine.api.urlfetch.URLFetchServicePb.URLFetchRequest in project appengine-java-standard by GoogleCloudPlatform.

the class URLFetchServiceImplTest method deadlineMatches.

private void deadlineMatches(Double expectedDeadline, FetchOptions options) throws Exception {
    URL url = new URL("http://www.google.com");
    HTTPRequest request;
    if (options == null) {
        request = new HTTPRequest(url, HTTPMethod.GET);
    } else {
        request = new HTTPRequest(url, HTTPMethod.GET, options);
    }
    URLFetchRequest requestProto = URLFetchRequest.newBuilder().setUrl(url.toString()).setMethod(RequestMethod.GET).setFollowRedirects(true).build();
    String responseContent = "<p>This is the desired response.</p>";
    URLFetchResponse responseProto = URLFetchResponse.newBuilder().setStatusCode(200).setContent(ByteString.copyFromUtf8(responseContent)).build();
    when(delegate.makeSyncCall(hasCorrectDeadline(ApiProxy.getCurrentEnvironment(), expectedDeadline), eq(URLFetchServiceImpl.PACKAGE), eq("Fetch"), eq(requestProto.toByteArray()))).thenReturn(responseProto.toByteArray());
    URLFetchServiceImpl.DeadlineParser.INSTANCE.refresh();
    HTTPResponse response = new URLFetchServiceImpl().fetch(request);
    assertThat(new String(response.getContent(), UTF_8)).isEqualTo(responseContent);
}
Also used : URLFetchResponse(com.google.appengine.api.urlfetch.URLFetchServicePb.URLFetchResponse) ByteString(com.google.protobuf.ByteString) URLFetchRequest(com.google.appengine.api.urlfetch.URLFetchServicePb.URLFetchRequest) URL(java.net.URL)

Example 3 with URLFetchRequest

use of com.google.appengine.api.urlfetch.URLFetchServicePb.URLFetchRequest in project appengine-java-standard by GoogleCloudPlatform.

the class URLFetchServiceImplTest method testSync_TruncateResponse_DisallowTruncate.

@Test
public void testSync_TruncateResponse_DisallowTruncate() throws Exception {
    URL url = new URL("http://non-existent-domain.com/foo");
    HTTPRequest request = new HTTPRequest(url, HTTPMethod.GET, disallowTruncate());
    URLFetchRequest requestProto = URLFetchRequest.newBuilder().setUrl(url.toString()).setMethod(RequestMethod.GET).setFollowRedirects(true).build();
    URLFetchResponse responseProto = URLFetchResponse.newBuilder().setStatusCode(200).setContentWasTruncated(true).build();
    when(delegate.makeSyncCall(same(ApiProxy.getCurrentEnvironment()), eq(URLFetchServiceImpl.PACKAGE), eq("Fetch"), eq(requestProto.toByteArray()))).thenReturn(responseProto.toByteArray());
    ResponseTooLargeException ex = assertThrows(ResponseTooLargeException.class, () -> new URLFetchServiceImpl().fetch(request));
    assertThat(ex).hasMessageThat().contains(url.toString());
}
Also used : URLFetchResponse(com.google.appengine.api.urlfetch.URLFetchServicePb.URLFetchResponse) URLFetchRequest(com.google.appengine.api.urlfetch.URLFetchServicePb.URLFetchRequest) URL(java.net.URL) Test(org.junit.Test)

Example 4 with URLFetchRequest

use of com.google.appengine.api.urlfetch.URLFetchServicePb.URLFetchRequest in project appengine-java-standard by GoogleCloudPlatform.

the class URLFetchServiceImplTest method testSync_Timeout2.

@Test
public void testSync_Timeout2() throws Exception {
    URL url = new URL("http://slow-domain.com/foo");
    HTTPRequest request = new HTTPRequest(url);
    URLFetchRequest requestProto = URLFetchRequest.newBuilder().setUrl(url.toString()).setMethod(RequestMethod.GET).setFollowRedirects(true).build();
    when(delegate.makeSyncCall(same(ApiProxy.getCurrentEnvironment()), eq(URLFetchServiceImpl.PACKAGE), eq("Fetch"), eq(requestProto.toByteArray()))).thenThrow(new ApiProxy.ApiDeadlineExceededException("urlfetch", "fetch"));
    SocketTimeoutException ex = assertThrows(SocketTimeoutException.class, () -> new URLFetchServiceImpl().fetch(request));
    assertThat(ex).hasMessageThat().contains(url.toString());
}
Also used : SocketTimeoutException(java.net.SocketTimeoutException) ApiProxy(com.google.apphosting.api.ApiProxy) URLFetchRequest(com.google.appengine.api.urlfetch.URLFetchServicePb.URLFetchRequest) URL(java.net.URL) Test(org.junit.Test)

Example 5 with URLFetchRequest

use of com.google.appengine.api.urlfetch.URLFetchServicePb.URLFetchRequest in project appengine-java-standard by GoogleCloudPlatform.

the class URLFetchServiceImplTest method checkTlsValidation.

private void checkTlsValidation(FetchOptions fetchOptions, Consumer<URLFetchRequest> check) throws Exception {
    URL url = new URL("https://validate.me/");
    HTTPRequest request = new HTTPRequest(url, HTTPMethod.GET, fetchOptions);
    URLFetchResponse urlFetchResponse = URLFetchResponse.newBuilder().setStatusCode(200).build();
    when(delegate.makeSyncCall(any(), any(), any(), any())).thenReturn(urlFetchResponse.toByteArray());
    new URLFetchServiceImpl().fetch(request);
    ArgumentCaptor<byte[]> requestProtoCaptor = ArgumentCaptor.forClass(byte[].class);
    verify(delegate).makeSyncCall(any(), eq(URLFetchServiceImpl.PACKAGE), eq("Fetch"), requestProtoCaptor.capture());
    URLFetchRequest urlFetchRequest = URLFetchRequest.parseFrom(requestProtoCaptor.getValue(), ExtensionRegistry.getGeneratedRegistry());
    check.accept(urlFetchRequest);
}
Also used : URLFetchResponse(com.google.appengine.api.urlfetch.URLFetchServicePb.URLFetchResponse) URLFetchRequest(com.google.appengine.api.urlfetch.URLFetchServicePb.URLFetchRequest) URL(java.net.URL)

Aggregations

URLFetchRequest (com.google.appengine.api.urlfetch.URLFetchServicePb.URLFetchRequest)35 Test (org.junit.Test)31 URL (java.net.URL)29 ByteString (com.google.protobuf.ByteString)22 URLFetchResponse (com.google.appengine.api.urlfetch.URLFetchServicePb.URLFetchResponse)19 ApiProxy (com.google.apphosting.api.ApiProxy)12 IOException (java.io.IOException)5 LocalRpcService (com.google.appengine.tools.development.LocalRpcService)3 SocketTimeoutException (java.net.SocketTimeoutException)3 ExecutionException (java.util.concurrent.ExecutionException)2 TaskQueueAddRequest (com.google.appengine.api.taskqueue.TaskQueuePb.TaskQueueAddRequest)1 LatencyPercentiles (com.google.appengine.tools.development.LatencyPercentiles)1 ApplicationException (com.google.apphosting.api.ApiProxy.ApplicationException)1 QueueXml (com.google.apphosting.utils.config.QueueXml)1 MalformedURLException (java.net.MalformedURLException)1 UnknownHostException (java.net.UnknownHostException)1 DecimalFormat (java.text.DecimalFormat)1 SSLException (javax.net.ssl.SSLException)1 HttpEntity (org.apache.http.HttpEntity)1 HttpResponse (org.apache.http.HttpResponse)1