Search in sources :

Example 11 with URLFetchRequest

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

the class URLFetchServiceImplTest method testSync_SimplePOST.

@Test
public void testSync_SimplePOST() throws Exception {
    String requestContent = "this=is&post=content";
    String responseContent = "<p>This is the desired response.</p>";
    URL url = new URL("http://google.com/foo");
    HTTPRequest request = new HTTPRequest(url, HTTPMethod.POST);
    request.setPayload(requestContent.getBytes(UTF_8));
    URLFetchRequest requestProto = URLFetchRequest.newBuilder().setUrl(url.toString()).setMethod(RequestMethod.POST).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 12 with URLFetchRequest

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

the class LocalURLFetchCertValidationTest method testCertVerifiedWithAsking.

@Test
public void testCertVerifiedWithAsking() throws Exception {
    LocalRpcService.Status status = new LocalRpcService.Status();
    URLFetchRequest request = URLFetchRequest.newBuilder().setMethod(RequestMethod.GET).setUrl(urlWithInvalidCert).setMustValidateServerCertificate(true).setFollowRedirects(false).build();
    ApiProxy.ApplicationException ae = assertThrows(ApiProxy.ApplicationException.class, () -> fetchService.fetch(status, request));
    assertThat(ae.getApplicationError()).isEqualTo(ErrorCode.SSL_CERTIFICATE_ERROR.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 13 with URLFetchRequest

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

the class LocalURLFetchCertValidationTest method testCertNotVerifiedWithoutAsking.

@Test
public void testCertNotVerifiedWithoutAsking() throws Exception {
    LocalRpcService.Status status = new LocalRpcService.Status();
    URLFetchRequest request = URLFetchRequest.newBuilder().setMethod(RequestMethod.GET).setUrl(urlWithInvalidCert).setMustValidateServerCertificate(false).setFollowRedirects(false).build();
    URLFetchResponse response = fetchService.fetch(status, request);
    assertThat(status.getErrorCode()).isEqualTo(ErrorCode.OK.getNumber());
    assertThat(response.getStatusCode()).isEqualTo(302);
}
Also used : URLFetchResponse(com.google.appengine.api.urlfetch.URLFetchServicePb.URLFetchResponse) LocalRpcService(com.google.appengine.tools.development.LocalRpcService) URLFetchRequest(com.google.appengine.api.urlfetch.URLFetchServicePb.URLFetchRequest) Test(org.junit.Test)

Example 14 with URLFetchRequest

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

the class UrlFetchJobTest method testNewFetchRequest.

@Test
public void testNewFetchRequest() {
    QueueXml.Entry entry = new QueueXml.Entry();
    entry.setTarget("target");
    TaskQueueAddRequest.Builder req = TaskQueueAddRequest.newBuilder().setMethod(RequestMethod.GET).setUrl(ByteString.copyFromUtf8("/this/is/the/url")).setTaskName(ByteString.copyFromUtf8("name")).setQueueName(ByteString.copyFromUtf8("queuename")).setBody(ByteString.copyFromUtf8("this is the body")).setEtaUsec(10).addHeader(TaskQueueAddRequest.Header.newBuilder().setKey(ByteString.copyFromUtf8("key1")).setValue(ByteString.copyFromUtf8("value1")).build()).addHeader(TaskQueueAddRequest.Header.newBuilder().setKey(ByteString.copyFromUtf8("key2")).setValue(ByteString.copyFromUtf8("value2")).build());
    UrlFetchJob job = new UrlFetchJob();
    URLFetchRequest fetchReq = job.newFetchRequest("yar", req, "http://localhost:3344", 1, entry, 300);
    assertThat(fetchReq.getMethod()).isEqualTo(URLFetchServicePb.URLFetchRequest.RequestMethod.GET);
    assertThat(fetchReq.getUrl()).isEqualTo("http://localhost:3344/this/is/the/url");
    assertThat(fetchReq.getPayload()).isEqualTo(ByteString.copyFromUtf8("this is the body"));
    assertThat(fetchReq.getHeaderCount()).isEqualTo(10);
    assertThat(fetchReq.getHeader(0)).isEqualTo(Header.newBuilder().setKey("key1").setValue("value1").build());
    assertThat(fetchReq.getHeader(1)).isEqualTo(Header.newBuilder().setKey("key2").setValue("value2").build());
    assertThat(fetchReq.getHeader(2)).isEqualTo(Header.newBuilder().setKey(UrlFetchJob.X_GOOGLE_DEV_APPSERVER_SKIPADMINCHECK).setValue("true").build());
    assertThat(fetchReq.getHeader(3)).isEqualTo(Header.newBuilder().setKey(UrlFetchJob.X_APPENGINE_QUEUE_NAME).setValue(req.getQueueName().toStringUtf8()).build());
    assertThat(fetchReq.getHeader(4)).isEqualTo(Header.newBuilder().setKey(UrlFetchJob.X_APPENGINE_TASK_NAME).setValue("yar").build());
    assertThat(fetchReq.getHeader(5)).isEqualTo(Header.newBuilder().setKey(UrlFetchJob.X_APPENGINE_TASK_RETRY_COUNT).setValue("1").build());
    final String eta = new DecimalFormat("0.000000").format(req.getEtaUsec() / 1.0E6);
    assertThat(fetchReq.getHeader(6)).isEqualTo(Header.newBuilder().setKey(UrlFetchJob.X_APPENGINE_TASK_ETA).setValue(eta).build());
    assertThat(fetchReq.getHeader(7)).isEqualTo(Header.newBuilder().setKey(UrlFetchJob.X_APPENGINE_SERVER_NAME).setValue("target").build());
    assertThat(fetchReq.getHeader(8)).isEqualTo(Header.newBuilder().setKey(UrlFetchJob.X_APPENGINE_TASK_EXECUTION_COUNT).setValue("1").build());
    assertThat(fetchReq.getHeader(9)).isEqualTo(Header.newBuilder().setKey(UrlFetchJob.X_APPENGINE_TASK_PREVIOUS_RESPONSE).setValue("300").build());
}
Also used : TaskQueueAddRequest(com.google.appengine.api.taskqueue.TaskQueuePb.TaskQueueAddRequest) QueueXml(com.google.apphosting.utils.config.QueueXml) DecimalFormat(java.text.DecimalFormat) ByteString(com.google.protobuf.ByteString) URLFetchRequest(com.google.appengine.api.urlfetch.URLFetchServicePb.URLFetchRequest) Test(org.junit.Test)

Example 15 with URLFetchRequest

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

the class URLFetchServiceImplTest method testSync_SimpleDELETE.

@Test
public void testSync_SimpleDELETE() throws Exception {
    String responseContent = "<p>This is the desired response.</p>";
    URL url = new URL("http://google.com/foo");
    HTTPRequest request = new HTTPRequest(url, HTTPMethod.DELETE);
    URLFetchRequest requestProto = URLFetchRequest.newBuilder().setUrl(url.toString()).setMethod(RequestMethod.DELETE).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