Search in sources :

Example 26 with URLFetchRequest

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

the class URLFetchServiceImplTest method testSync_URLOnly.

@Test
public void testSync_URLOnly() throws Exception {
    URL url = new URL("http://google.com/foo");
    String responseContent = "<p>This is the desired response.</p>";
    URLFetchRequest requestProto = URLFetchRequest.newBuilder().setUrl(url.toString()).setMethod(RequestMethod.GET).setFollowRedirects(true).build();
    URLFetchResponse responseProto = URLFetchResponse.newBuilder().setStatusCode(200).setContent(ByteString.copyFromUtf8(responseContent)).build();
    when(delegate.makeSyncCall(same(ApiProxy.getCurrentEnvironment()), eq(URLFetchServiceImpl.PACKAGE), eq("Fetch"), eq(requestProto.toByteArray()))).thenReturn(responseProto.toByteArray());
    HTTPResponse response = new URLFetchServiceImpl().fetch(url);
    assertThat(response.getResponseCode()).isEqualTo(200);
    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) Test(org.junit.Test)

Example 27 with URLFetchRequest

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

the class URLFetchServiceImplTest method testAsync_SimpleGet.

@Test
public void testAsync_SimpleGet() throws Exception {
    URL url = new URL("http://google.com/foo");
    String responseContent = "<p>This is the desired response.</p>";
    URLFetchRequest requestProto = URLFetchRequest.newBuilder().setUrl(url.toString()).setMethod(RequestMethod.GET).setFollowRedirects(true).build();
    URLFetchResponse responseProto = URLFetchResponse.newBuilder().setStatusCode(200).setContent(ByteString.copyFromUtf8(responseContent)).build();
    when(delegate.makeAsyncCall(same(ApiProxy.getCurrentEnvironment()), eq(URLFetchServiceImpl.PACKAGE), eq("Fetch"), eq(requestProto.toByteArray()), any())).thenReturn(Futures.immediateFuture(responseProto.toByteArray()));
    Future<HTTPResponse> responseFuture = new URLFetchServiceImpl().fetchAsync(new HTTPRequest(url));
    HTTPResponse response = responseFuture.get();
    assertThat(response.getResponseCode()).isEqualTo(200);
    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) Test(org.junit.Test)

Example 28 with URLFetchRequest

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

the class URLFetchServiceImplTest method testSync_InvalidURLException.

@Test
public void testSync_InvalidURLException() throws Exception {
    URL url = new URL("http://badrequest.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.makeSyncCall(same(ApiProxy.getCurrentEnvironment()), eq(URLFetchServiceImpl.PACKAGE), eq("Fetch"), eq(requestProto.toByteArray()))).thenThrow(new ApiProxy.ApplicationException(ErrorCode.INVALID_URL.getNumber(), errorDetails));
    MalformedURLException ex = assertThrows(MalformedURLException.class, () -> new URLFetchServiceImpl().fetch(request));
    assertThat(ex).hasMessageThat().contains(url.toString());
}
Also used : MalformedURLException(java.net.MalformedURLException) ApiProxy(com.google.apphosting.api.ApiProxy) ByteString(com.google.protobuf.ByteString) URLFetchRequest(com.google.appengine.api.urlfetch.URLFetchServicePb.URLFetchRequest) URL(java.net.URL) Test(org.junit.Test)

Example 29 with URLFetchRequest

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

the class LocalURLFetchServiceIntegrationTest method testBadURL.

@Test
public void testBadURL() throws Exception {
    startServer();
    LocalRpcService.Status status = new LocalRpcService.Status();
    URLFetchRequest request = URLFetchRequest.newBuilder().setMethod(RequestMethod.GET).setUrl("a bad url").build();
    LocalURLFetchService localFetchService = new LocalURLFetchService();
    localFetchService.init(null, ImmutableMap.of());
    localFetchService.setTimeoutInMs(5000);
    ApiProxy.ApplicationException exception = assertThrows(ApiProxy.ApplicationException.class, () -> localFetchService.fetch(status, request));
    assertThat(exception.getApplicationError()).isEqualTo(ErrorCode.INVALID_URL.getNumber());
}
Also used : ApiProxy(com.google.apphosting.api.ApiProxy) LocalRpcService(com.google.appengine.tools.development.LocalRpcService) URLFetchRequest(com.google.appengine.api.urlfetch.URLFetchServicePb.URLFetchRequest) Test(org.junit.Test)

Example 30 with URLFetchRequest

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

the class URLFetchServiceImplTest method testSync_SimplePUT.

@Test
public void testSync_SimplePUT() throws Exception {
    String requestContent = "this=is&put=content";
    String responseContent = "<p>This is the desired response.</p>";
    URL url = new URL("http://google.com/foo");
    HTTPRequest request = new HTTPRequest(url, HTTPMethod.PUT);
    request.setPayload(requestContent.getBytes(UTF_8));
    URLFetchRequest requestProto = URLFetchRequest.newBuilder().setUrl(url.toString()).setMethod(RequestMethod.PUT).setPayload(ByteString.copyFromUtf8(requestContent)).setFollowRedirects(true).build();
    URLFetchResponse responseProto = URLFetchResponse.newBuilder().setStatusCode(200).setContent(ByteString.copyFromUtf8(responseContent)).build();
    when(delegate.makeSyncCall(same(ApiProxy.getCurrentEnvironment()), eq(URLFetchServiceImpl.PACKAGE), eq("Fetch"), eq(requestProto.toByteArray()))).thenReturn(responseProto.toByteArray());
    HTTPResponse response = new URLFetchServiceImpl().fetch(request);
    assertThat(response.getResponseCode()).isEqualTo(200);
    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) Test(org.junit.Test)

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