use of com.google.appengine.api.urlfetch.URLFetchServicePb.URLFetchRequest 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);
}
use of com.google.appengine.api.urlfetch.URLFetchServicePb.URLFetchRequest 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());
}
use of com.google.appengine.api.urlfetch.URLFetchServicePb.URLFetchRequest in project appengine-java-standard by GoogleCloudPlatform.
the class URLFetchServiceImplTest method testSync_Timeout.
@Test
public void testSync_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.makeSyncCall(same(ApiProxy.getCurrentEnvironment()), eq(URLFetchServiceImpl.PACKAGE), eq("Fetch"), eq(requestProto.toByteArray()))).thenThrow(new ApiProxy.ApplicationException(ErrorCode.DEADLINE_EXCEEDED.getNumber(), errorDetails));
SocketTimeoutException ex = assertThrows(SocketTimeoutException.class, () -> new URLFetchServiceImpl().fetch(request));
assertThat(ex).hasMessageThat().isEqualTo("Timeout while fetching URL: http://slow-domain.com/foo");
}
use of com.google.appengine.api.urlfetch.URLFetchServicePb.URLFetchRequest 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");
}
use of com.google.appengine.api.urlfetch.URLFetchServicePb.URLFetchRequest in project appengine-java-standard by GoogleCloudPlatform.
the class URLFetchServiceImplTest method testSync_ResponseHeadersUncombined.
@Test
public void testSync_ResponseHeadersUncombined() 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.getHeadersUncombined()).hasSize(2);
assertThat(response.getHeadersUncombined().get(0).getName()).isEqualTo("Test-Header");
assertThat(response.getHeadersUncombined().get(0).getValue()).isEqualTo("Response1");
assertThat(response.getHeadersUncombined().get(1).getName()).isEqualTo("Test-Header");
assertThat(response.getHeadersUncombined().get(1).getValue()).isEqualTo("Response2");
}
Aggregations