Java Code Examples for com.google.api.client.testing.http.MockLowLevelHttpRequest#setResponse()

The following examples show how to use com.google.api.client.testing.http.MockLowLevelHttpRequest#setResponse() . 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: GithubArchiveTest.java    From copybara with Apache License 2.0 5 votes vote down vote up
@Before
public void setup() throws IOException {
  httpTransport =
      new MockHttpTransport() {
        @Override
        public LowLevelHttpRequest buildRequest(String method, String url) {
          String requestString = method + " " + url;
          MockLowLevelHttpRequest request = new MockLowLevelHttpRequest();
          MockLowLevelHttpResponse response = new MockLowLevelHttpResponse();
          request.setResponse(response);
          response.setStatusCode(200);
          response.setContent(responseContent);
          if (!url.equals(expectedRequest)) {
            response.setStatusCode(404);
            response.setContent(
                String.format("UNEXPECTED REQUEST (Returning 404) REQUEST: %s, expected: %s",
                    requestString, expectedRequest));
          }
          return request;
        }
      };
  RemoteFileOptions options = new RemoteFileOptions();
  options.transport = () -> new GclientHttpStreamFactory(httpTransport, Duration.ofSeconds(20));
  Console console = new TestingConsole();
  OptionsBuilder optionsBuilder = new OptionsBuilder().setConsole(console);
  optionsBuilder.remoteFile = options;
  skylark = new SkylarkTestExecutor(optionsBuilder);
}
 
Example 2
Source File: UpdateRegistrarRdapBaseUrlsActionTest.java    From nomulus with Apache License 2.0 5 votes vote down vote up
@Override
public LowLevelHttpRequest buildRequest(String method, String url) {
  assertThat(method).isEqualTo("GET");
  MockLowLevelHttpRequest httpRequest = new MockLowLevelHttpRequest(url);
  httpRequest.setResponse(simulatedResponses.get(requestsSent.size()));
  requestsSent.add(httpRequest);
  return httpRequest;
}
 
Example 3
Source File: GerritApiTest.java    From copybara with Apache License 2.0 4 votes vote down vote up
@Before
public void setUp() throws Exception {
  OptionsBuilder options =
      new OptionsBuilder()
          .setWorkdirToRealTempDir()
          .setEnvironment(GitTestUtil.getGitEnv().getEnvironment())
          .setOutputRootToTmpDir();

  credentialsFile = Files.createTempFile("credentials", "test");
  Files.write(credentialsFile, "https://user:[email protected]".getBytes(UTF_8));
  GitRepository repo = newBareRepo(Files.createTempDirectory("test_repo"),
      getGitEnv(), /*verbose=*/true, DEFAULT_TIMEOUT, /*noVerify=*/false)
      .init()
      .withCredentialHelper("store --file=" + credentialsFile);

  httpTransport =
      new MockHttpTransport() {
        @Override
        public LowLevelHttpRequest buildRequest(String method, String url) {
          String requestString = method + " " + url;
          MockLowLevelHttpRequest request = new MockLowLevelHttpRequest();
          MockLowLevelHttpResponse response = new MockLowLevelHttpResponse();
          request.setResponse(response);
          for (Entry<Predicate<String>, byte[]> entry : requestToResponse.entrySet()) {
            if (entry.getKey().test(requestString)) {
              byte[] content = entry.getValue();
              assertWithMessage("'" + method + " " + url + "'").that(content).isNotNull();
              if (content.length == 0) {
                // No content
                response.setStatusCode(204);
                return request;
              }
              response.setContent(content);
              return request;
            }
          }
          response.setStatusCode(404);
          response.setContent(("NO BASE_URL MATCHED! (Returning 404) REQUEST: " + requestString));
          return request;
        }
      };

  GerritOptions gerritOptions =
      new GerritOptions(options.general, options.git) {
        @Override
        protected HttpTransport getHttpTransport() {
          return httpTransport;
        }

        @Override
        protected GitRepository getCredentialsRepo() {
          return repo;
        }
      };
  gerritApi = gerritOptions.newGerritApi(getHost() + "/foo/bar/baz");
}
 
Example 4
Source File: HttpHandlerTest.java    From googleads-java-lib with Apache License 2.0 4 votes vote down vote up
/** Tests that the timeout set on the message context is passed to the underlying request. */
@Test
public void testInvokeSetsTimeout() {
  MessageContext messageContext = new MessageContext(axisEngine);
  messageContext.setRequestMessage(requestMessage);
  messageContext.setProperty(MessageContext.TRANS_URL, "https://www.example.com");

  // Do not care about XML parsing for this test, so set the response's status code to 302
  // to trigger an AxisFault.
  MockLowLevelHttpResponse lowLevelHttpResponse = new MockLowLevelHttpResponse();
  lowLevelHttpResponse.setContent("Intentional failure");
  lowLevelHttpResponse.setStatusCode(302);

  /*
   * Set timeout on the message context, then create a custom mock transport that will capture
   * invocations of LowLevelHttpRequest.setTimeout(int, int) and record the arguments passed.
   */
  int timeout = 1234567;
  messageContext.setTimeout(timeout);
  final int[] actualTimeouts = new int[] {Integer.MIN_VALUE, Integer.MIN_VALUE};
  MockLowLevelHttpRequest lowLevelHttpRequest =
      new MockLowLevelHttpRequest() {
        @Override
        public void setTimeout(int connectTimeout, int readTimeout) throws IOException {
          actualTimeouts[0] = connectTimeout;
          actualTimeouts[1] = readTimeout;
          super.setTimeout(connectTimeout, readTimeout);
        }
      };
  lowLevelHttpRequest.setResponse(lowLevelHttpResponse);
  MockHttpTransport mockTransport =
      new MockHttpTransport.Builder().setLowLevelHttpRequest(lowLevelHttpRequest).build();
  httpHandler = new HttpHandler(mockTransport, streamListener);

  try {
    httpHandler.invoke(messageContext);
    fail("Expected an AxisFault");
  } catch (AxisFault e) {
    assertThat(e.getFaultString(), Matchers.containsString("302"));
  }
  assertArrayEquals(
      "Timeouts not set to expected values", new int[] {timeout, timeout}, actualTimeouts);
}