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);
}
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());
}
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);
}
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());
}
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);
}
Aggregations