Java Code Examples for com.google.api.client.testing.http.HttpTesting#SIMPLE_URL

The following examples show how to use com.google.api.client.testing.http.HttpTesting#SIMPLE_URL . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: UploadIdResponseInterceptorTest.java    From beam with Apache License 2.0 6 votes vote down vote up
/**
 * Builds an HttpResponse with the given string response.
 *
 * @param header header value to provide or null if none.
 * @param uploadId upload id to provide in the url upload id param or null if none.
 * @param uploadType upload type to provide in url upload type param or null if none.
 * @return HttpResponse with the given parameters
 * @throws IOException
 */
private HttpResponse buildHttpResponse(String header, String uploadId, String uploadType)
    throws IOException {
  MockHttpTransport.Builder builder = new MockHttpTransport.Builder();
  MockLowLevelHttpResponse resp = new MockLowLevelHttpResponse();
  builder.setLowLevelHttpResponse(resp);
  resp.setStatusCode(200);
  GenericUrl url = new GenericUrl(HttpTesting.SIMPLE_URL);
  if (header != null) {
    resp.addHeader("X-GUploader-UploadID", header);
  }
  if (uploadId != null) {
    url.put("upload_id", uploadId);
  }
  if (uploadType != null) {
    url.put("uploadType", uploadType);
  }
  return builder.build().createRequestFactory().buildGetRequest(url).execute();
}
 
Example 2
Source File: NetHttpTransportTest.java    From google-http-java-client with Apache License 2.0 6 votes vote down vote up
public void testExecute_mock() throws Exception {
  for (String method : METHODS) {
    boolean isPutOrPost = method.equals("PUT") || method.equals("POST");
    MockHttpURLConnection connection = new MockHttpURLConnection(new URL(HttpTesting.SIMPLE_URL));
    connection.setRequestMethod(method);
    NetHttpRequest request = new NetHttpRequest(connection);
    setContent(request, null, "");
    request.execute();
    assertEquals(isPutOrPost, connection.doOutputCalled());
    setContent(request, null, " ");
    if (isPutOrPost) {
      request.execute();
    } else {
      try {
        request.execute();
        fail("expected " + IllegalArgumentException.class);
      } catch (IllegalArgumentException e) {
        // expected
      }
    }
    assertEquals(isPutOrPost, connection.doOutputCalled());
  }
}
 
Example 3
Source File: NetHttpRequestTest.java    From google-http-java-client with Apache License 2.0 6 votes vote down vote up
@Test
public void testInterruptedWriteWithResponse() throws Exception {
  MockHttpURLConnection connection =
      new MockHttpURLConnection(new URL(HttpTesting.SIMPLE_URL)) {
        @Override
        public OutputStream getOutputStream() throws IOException {
          return new OutputStream() {
            @Override
            public void write(int b) throws IOException {
              throw new IOException("Error writing request body to server");
            }
          };
        }
      };
  connection.setResponseCode(401);
  connection.setRequestMethod("POST");
  NetHttpRequest request = new NetHttpRequest(connection);
  InputStream is = NetHttpRequestTest.class.getClassLoader().getResourceAsStream("file.txt");
  HttpContent content = new InputStreamContent("text/plain", is);
  request.setStreamingContent(content);

  LowLevelHttpResponse response = request.execute();
  assertEquals(401, response.getStatusCode());
}
 
Example 4
Source File: ExportMessageReceiverTest.java    From healthcare-dicom-dicomweb-adapter with Apache License 2.0 5 votes vote down vote up
ExportMessageReceiver testCStoreExportMessageReceiver(
    int serverPort, FakeWebServer fakeWebServer, StubAckReplyConsumer replyConsumer)
    throws Exception {
  ByteString pubsubMessageBytes = ByteString.copyFromUtf8(testPubsubPath);
  PubsubMessage pubsubMessage = PubsubMessage.newBuilder().setData(pubsubMessageBytes).build();
  DicomWebClient dicomWebClient =
      new DicomWebClient(fakeWebServer.createRequestFactory(), HttpTesting.SIMPLE_URL, "/studies");
  DicomSender dicomSender =
      new CStoreSender(clientAE, serverAET, serverHost, serverPort, dicomWebClient);
  ExportMessageReceiver receiver = new ExportMessageReceiver(dicomSender);
  receiver.receiveMessage(pubsubMessage, replyConsumer);
  return receiver;
}
 
Example 5
Source File: MethodOverrideTest.java    From google-api-java-client with Apache License 2.0 5 votes vote down vote up
public void testInterceptMaxLength() throws IOException {
  HttpTransport transport = new MockHttpTransport();
  GenericUrl url = new GenericUrl(HttpTesting.SIMPLE_URL);
  url.set("a", "foo");
  HttpRequest request =
      transport.createRequestFactory().buildGetRequest(HttpTesting.SIMPLE_GENERIC_URL);
  new MethodOverride().intercept(request);
  assertEquals(HttpMethods.GET, request.getRequestMethod());
  assertNull(request.getHeaders().get(MethodOverride.HEADER));
  assertNull(request.getContent());
  char[] arr = new char[MethodOverride.MAX_URL_LENGTH];
  Arrays.fill(arr, 'x');
  url.set("a", new String(arr));
  request.setUrl(url);
  new MethodOverride().intercept(request);
  assertEquals(HttpMethods.POST, request.getRequestMethod());
  assertEquals(HttpMethods.GET, request.getHeaders().get(MethodOverride.HEADER));
  assertEquals(HttpTesting.SIMPLE_GENERIC_URL, request.getUrl());
  char[] arr2 = new char[arr.length + 2];
  Arrays.fill(arr2, 'x');
  arr2[0] = 'a';
  arr2[1] = '=';
  UrlEncodedContent content = (UrlEncodedContent) request.getContent();
  ByteArrayOutputStream out = new ByteArrayOutputStream();
  content.writeTo(out);
  assertEquals(new String(arr2), out.toString());
}
 
Example 6
Source File: MockHttpUrlConnectionTest.java    From google-http-java-client with Apache License 2.0 5 votes vote down vote up
public void testSetInputStreamAndInputStreamImmutable() throws IOException {
  MockHttpURLConnection connection = new MockHttpURLConnection(new URL(HttpTesting.SIMPLE_URL));
  connection.setInputStream(new ByteArrayInputStream(StringUtils.getBytesUtf8(RESPONSE_BODY)));
  connection.setInputStream(new ByteArrayInputStream(StringUtils.getBytesUtf8("override")));
  byte[] buf = new byte[10];
  InputStream in = connection.getInputStream();
  int n = 0, bytes = 0;
  while ((n = in.read(buf)) != -1) {
    bytes += n;
  }
  assertEquals(RESPONSE_BODY, new String(buf, 0, bytes));
}
 
Example 7
Source File: MockHttpUrlConnectionTest.java    From google-http-java-client with Apache License 2.0 5 votes vote down vote up
public void testSetGetMultipleHeaders() throws IOException {
  MockHttpURLConnection connection = new MockHttpURLConnection(new URL(HttpTesting.SIMPLE_URL));
  List<String> values = Arrays.asList("value1", "value2", "value3");
  for (String value : values) {
    connection.addHeader(HEADER_NAME, value);
  }
  Map<String, List<String>> headers = connection.getHeaderFields();
  assertEquals(3, headers.get(HEADER_NAME).size());
  for (int i = 0; i < 3; i++) {
    assertEquals(values.get(i), headers.get(HEADER_NAME).get(i));
  }
}
 
Example 8
Source File: NetHttpRequestTest.java    From google-http-java-client with Apache License 2.0 5 votes vote down vote up
@Test
public void testChunkedLengthNotSet() throws Exception {
  MockHttpURLConnection connection = new MockHttpURLConnection(new URL(HttpTesting.SIMPLE_URL));
  connection.setRequestMethod("POST");
  NetHttpRequest request = new NetHttpRequest(connection);
  HttpContent content =
      new ByteArrayContent("text/plain", "sample".getBytes(StandardCharsets.UTF_8));
  request.setStreamingContent(content);
  request.setContentLength(content.getLength());
  request.execute();

  assertEquals(connection.getChunkLength(), -1);
  assertEquals("6", request.getRequestProperty("Content-Length"));
}
 
Example 9
Source File: NetHttpRequestTest.java    From google-http-java-client with Apache License 2.0 5 votes vote down vote up
@Test
public void testChunkedLengthSet() throws Exception {
  MockHttpURLConnection connection = new MockHttpURLConnection(new URL(HttpTesting.SIMPLE_URL));
  connection.setRequestMethod("POST");
  NetHttpRequest request = new NetHttpRequest(connection);
  InputStream is = NetHttpRequestTest.class.getClassLoader().getResourceAsStream("file.txt");
  HttpContent content = new InputStreamContent("text/plain", is);
  request.setStreamingContent(content);
  request.setContentEncoding("gzip");
  request.execute();

  assertEquals(4096, connection.getChunkLength());
  assertNull(request.getRequestProperty("Content-Length"));
}
 
Example 10
Source File: NetHttpRequestTest.java    From google-http-java-client with Apache License 2.0 5 votes vote down vote up
@Test
public void testErrorOnClose() throws Exception {
  MockHttpURLConnection connection =
      new MockHttpURLConnection(new URL(HttpTesting.SIMPLE_URL)) {
        @Override
        public OutputStream getOutputStream() throws IOException {
          return new OutputStream() {
            @Override
            public void write(int b) throws IOException {
              return;
            }

            @Override
            public void close() throws IOException {
              throw new IOException("Error during close");
            }
          };
        }
      };
  connection.setRequestMethod("POST");
  NetHttpRequest request = new NetHttpRequest(connection);
  InputStream is = NetHttpRequestTest.class.getClassLoader().getResourceAsStream("file.txt");
  HttpContent content = new InputStreamContent("text/plain", is);
  request.setStreamingContent(content);

  try {
    request.execute();
    fail("Expected to throw an IOException");
  } catch (IOException e) {
    assertEquals("Error during close", e.getMessage());
  }
}
 
Example 11
Source File: NetHttpRequestTest.java    From google-http-java-client with Apache License 2.0 5 votes vote down vote up
@Test
public void testInterruptedWriteErrorOnResponse() throws Exception {
  MockHttpURLConnection connection =
      new MockHttpURLConnection(new URL(HttpTesting.SIMPLE_URL)) {
        @Override
        public OutputStream getOutputStream() throws IOException {
          return new OutputStream() {
            @Override
            public void write(int b) throws IOException {
              throw new IOException("Error writing request body to server");
            }
          };
        }

        @Override
        public int getResponseCode() throws IOException {
          throw new IOException("Error parsing response code");
        }
      };
  connection.setRequestMethod("POST");
  NetHttpRequest request = new NetHttpRequest(connection);
  InputStream is = NetHttpRequestTest.class.getClassLoader().getResourceAsStream("file.txt");
  HttpContent content = new InputStreamContent("text/plain", is);
  request.setStreamingContent(content);

  try {
    request.execute();
    fail("Expected to throw an IOException");
  } catch (IOException e) {
    assertEquals("Error writing request body to server", e.getMessage());
  }
}
 
Example 12
Source File: NetHttpRequestTest.java    From google-http-java-client with Apache License 2.0 5 votes vote down vote up
@Test
public void testInterruptedWriteWithoutResponse() throws Exception {
  MockHttpURLConnection connection =
      new MockHttpURLConnection(new URL(HttpTesting.SIMPLE_URL)) {
        @Override
        public OutputStream getOutputStream() throws IOException {
          return new OutputStream() {
            @Override
            public void write(int b) throws IOException {
              throw new IOException("Error writing request body to server");
            }
          };
        }
      };
  connection.setRequestMethod("POST");
  NetHttpRequest request = new NetHttpRequest(connection);
  InputStream is = NetHttpRequestTest.class.getClassLoader().getResourceAsStream("file.txt");
  HttpContent content = new InputStreamContent("text/plain", is);
  request.setStreamingContent(content);

  try {
    request.execute();
    fail("Expected to throw an IOException");
  } catch (IOException e) {
    assertEquals("Error writing request body to server", e.getMessage());
  }
}
 
Example 13
Source File: NetHttpRequestTest.java    From google-http-java-client with Apache License 2.0 5 votes vote down vote up
private static void postWithTimeout(int timeout) throws Exception {
  MockHttpURLConnection connection = new MockHttpURLConnection(new URL(HttpTesting.SIMPLE_URL));
  connection.setRequestMethod("POST");
  NetHttpRequest request = new NetHttpRequest(connection);
  InputStream is = NetHttpRequestTest.class.getClassLoader().getResourceAsStream("file.txt");
  HttpContent content = new InputStreamContent("text/plain", is);
  request.setStreamingContent(content);
  request.setWriteTimeout(timeout);
  request.execute(new SleepingOutputWriter(5000L));
}
 
Example 14
Source File: UploadIdResponseInterceptorTest.java    From beam with Apache License 2.0 5 votes vote down vote up
/** Check that a response logs with the correct log. */
@Test
public void testResponseLogs() throws IOException {
  new UploadIdResponseInterceptor().interceptResponse(buildHttpResponse("abc", null, "type"));
  GenericUrl url = new GenericUrl(HttpTesting.SIMPLE_URL);
  url.put("uploadType", "type");
  String worker = System.getProperty("worker_id");
  expectedLogs.verifyDebug("Upload ID for url " + url + " on worker " + worker + " is abc");
}
 
Example 15
Source File: ExportMessageReceiverTest.java    From healthcare-dicom-dicomweb-adapter with Apache License 2.0 5 votes vote down vote up
ExportMessageReceiver testStowRsExportMessageReceiver(
    FakeWebServer fakeSourceDicomWebServer,
    FakeWebServer fakeSinkDicomWebServer,
    StubAckReplyConsumer replyConsumer)
    throws Exception {
  ByteString pubsubMessageBytes = ByteString.copyFromUtf8(testPubsubPath);
  PubsubMessage pubsubMessage = PubsubMessage.newBuilder().setData(pubsubMessageBytes).build();
  DicomWebClient sourceDicomWebClient =
      new DicomWebClient(fakeSourceDicomWebServer.createRequestFactory(), HttpTesting.SIMPLE_URL, "/studies");
  DicomWebClient sinkDicomWebClient =
      new DicomWebClient(fakeSinkDicomWebServer.createRequestFactory(), HttpTesting.SIMPLE_URL, "/studies") {
        // This tests only ExportMessageReceiver and StowRsSender.
        // Properly mocking HTTP2 for low-level jetty stowrs implementation would be
        // significantly more difficult (but it's used during integration test)
        public void stowRs(InputStream in) throws IDicomWebClient.DicomWebException {
          try {
            requestFactory.buildGetRequest(new GenericUrl("http://nope")).execute();
          } catch (IOException e) {
            throw new DicomWebException(e);
          }
        }
      };
  DicomSender dicomSender = new StowRsSender(sourceDicomWebClient, sinkDicomWebClient);
  ExportMessageReceiver receiver = new ExportMessageReceiver(dicomSender);
  receiver.receiveMessage(pubsubMessage, replyConsumer);
  return receiver;
}
 
Example 16
Source File: MockHttpUrlConnectionTest.java    From google-http-java-client with Apache License 2.0 4 votes vote down vote up
public void testSetGetHeaders() throws IOException {
  MockHttpURLConnection connection = new MockHttpURLConnection(new URL(HttpTesting.SIMPLE_URL));
  connection.addHeader(HEADER_NAME, "100");
  assertEquals("100", connection.getHeaderField(HEADER_NAME));
}
 
Example 17
Source File: MockHttpUrlConnectionTest.java    From google-http-java-client with Apache License 2.0 4 votes vote down vote up
public void testGetNonExistingHeader() throws IOException {
  MockHttpURLConnection connection = new MockHttpURLConnection(new URL(HttpTesting.SIMPLE_URL));
  assertNull(connection.getHeaderField(HEADER_NAME));
}
 
Example 18
Source File: DicomWebClientTest.java    From healthcare-dicom-dicomweb-adapter with Apache License 2.0 4 votes vote down vote up
@Before
public void setUp() {
  fakeDicomWebServer = new FakeWebServer();
  client = new DicomWebClient(fakeDicomWebServer.createRequestFactory(), HttpTesting.SIMPLE_URL, "/studies");
}
 
Example 19
Source File: MockHttpServer.java    From googleads-java-lib with Apache License 2.0 4 votes vote down vote up
/**
 * Gets the server URL with port.
 */
public String getServerUrl() {
  return HttpTesting.SIMPLE_URL;
}