Search in sources :

Example 16 with URLFetchResponse

use of com.google.appengine.api.urlfetch.URLFetchServicePb.URLFetchResponse 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 17 with URLFetchResponse

use of com.google.appengine.api.urlfetch.URLFetchServicePb.URLFetchResponse 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)

Example 18 with URLFetchResponse

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

the class URLFetchServiceImplTest method testAsync_URLOnly.

@Test
public void testAsync_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.makeAsyncCall(same(ApiProxy.getCurrentEnvironment()), eq(URLFetchServiceImpl.PACKAGE), eq("Fetch"), eq(requestProto.toByteArray()), any())).thenReturn(Futures.immediateFuture(responseProto.toByteArray()));
    Future<HTTPResponse> responseFuture = new URLFetchServiceImpl().fetchAsync(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 19 with URLFetchResponse

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

the class URLFetchServiceImplTest method testSync_TruncateResponse_AllowTruncate.

@Test
public void testSync_TruncateResponse_AllowTruncate() throws Exception {
    URL url = new URL("http://non-existent-domain.com/foo");
    HTTPRequest request = new HTTPRequest(url, HTTPMethod.GET, allowTruncate());
    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());
    HTTPResponse response = new URLFetchServiceImpl().fetch(request);
    assertThat(response.getResponseCode()).isEqualTo(200);
    verify(delegate).makeSyncCall(any(), any(), any(), any());
}
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 20 with URLFetchResponse

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

the class URLFetchServiceImplTest method testSync_ResponseHeadersCombined.

@Test
public void testSync_ResponseHeadersCombined() throws Exception {
    URL url = new URL("http://google.com/foo");
    HTTPRequest request = new HTTPRequest(url);
    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)).addHeader(URLFetchResponse.Header.newBuilder().setKey("Test-Header").setValue("Response1")).addHeader(URLFetchResponse.Header.newBuilder().setKey("Test-Header").setValue("Response2")).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);
    assertThat(response.getHeaders()).hasSize(1);
    assertThat(response.getHeaders().get(0).getName()).isEqualTo("Test-Header");
    assertThat(response.getHeaders().get(0).getValue()).isEqualTo("Response1, Response2");
}
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

URLFetchResponse (com.google.appengine.api.urlfetch.URLFetchServicePb.URLFetchResponse)21 URLFetchRequest (com.google.appengine.api.urlfetch.URLFetchServicePb.URLFetchRequest)19 URL (java.net.URL)18 Test (org.junit.Test)16 ByteString (com.google.protobuf.ByteString)11 SocketTimeoutException (java.net.SocketTimeoutException)3 ApiProxy (com.google.apphosting.api.ApiProxy)2 IOException (java.io.IOException)2 FutureWrapper (com.google.appengine.api.utils.FutureWrapper)1 LatencyPercentiles (com.google.appengine.tools.development.LatencyPercentiles)1 LocalRpcService (com.google.appengine.tools.development.LocalRpcService)1 ApplicationException (com.google.apphosting.api.ApiProxy.ApplicationException)1 SSLException (javax.net.ssl.SSLException)1 HttpEntity (org.apache.http.HttpEntity)1 HttpResponse (org.apache.http.HttpResponse)1 HttpRequestBase (org.apache.http.client.methods.HttpRequestBase)1 BasicHttpParams (org.apache.http.params.BasicHttpParams)1 HttpParams (org.apache.http.params.HttpParams)1