Java Code Examples for com.squareup.okhttp.mockwebserver.MockWebServer#takeRequest()

The following examples show how to use com.squareup.okhttp.mockwebserver.MockWebServer#takeRequest() . 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: ChromeServiceImplTest.java    From chrome-devtools-java-client with Apache License 2.0 6 votes vote down vote up
@Test
public void testActivateTab() throws IOException, ChromeServiceException, InterruptedException {
  MockWebServer server = new MockWebServer();
  ObjectMapper objectMapper = new ObjectMapper();

  ChromeTab tab =
      objectMapper.readerFor(ChromeTab.class).readValue(getFixture("chrome/tab.json"));

  server.enqueue(new MockResponse());
  server.start();

  ChromeServiceImpl service = new ChromeServiceImpl(server.getHostName(), server.getPort());

  service.activateTab(tab);

  RecordedRequest request = server.takeRequest();
  assertEquals(1, server.getRequestCount());
  assertEquals(
      "GET /json/activate/(2C5C79DD1137419CC8839D61D91CEB2A) HTTP/1.1", request.getRequestLine());

  server.shutdown();
}
 
Example 2
Source File: BaseBitbucketMockTest.java    From bitbucket-rest with Apache License 2.0 6 votes vote down vote up
protected RecordedRequest assertSent(final MockWebServer server, 
        final String method, 
        final String expectedPath, 
        final Map<String, ?> queryParams) throws InterruptedException {


    final RecordedRequest request = server.takeRequest();
    assertThat(request.getMethod()).isEqualTo(method);
    assertThat(request.getHeader(HttpHeaders.ACCEPT)).isEqualTo(APPLICATION_JSON);

    final String path = request.getPath();
    final String rawPath = path.contains("?") ? path.substring(0, path.indexOf('?')) : path;
    assertThat(rawPath).isEqualTo(expectedPath);

    final Map<String, String> normalizedParams = Maps.transformValues(queryParams, Functions.toStringFunction());
    assertThat(normalizedParams).isEqualTo(extractParams(path));

    return request;
}
 
Example 3
Source File: ChromeServiceImplTest.java    From chrome-devtools-java-client with Apache License 2.0 6 votes vote down vote up
@Test
public void testCloseTab() throws IOException, ChromeServiceException, InterruptedException {
  MockWebServer server = new MockWebServer();

  ObjectMapper objectMapper = new ObjectMapper();
  ChromeTab tab =
      objectMapper.readerFor(ChromeTab.class).readValue(getFixture("chrome/tab.json"));

  server.enqueue(new MockResponse());
  server.start();

  ChromeServiceImpl service = new ChromeServiceImpl(server.getHostName(), server.getPort());

  service.closeTab(tab);

  RecordedRequest request = server.takeRequest();
  assertEquals(1, server.getRequestCount());
  assertEquals(
      "GET /json/close/(2C5C79DD1137419CC8839D61D91CEB2A) HTTP/1.1", request.getRequestLine());

  server.shutdown();
}
 
Example 4
Source File: BaseJenkinsMockTest.java    From jenkins-rest with Apache License 2.0 6 votes vote down vote up
protected RecordedRequest assertSent(final MockWebServer server, 
        final String method, 
        final String expectedPath, 
        final Map<String, ?> queryParams) throws InterruptedException {

    RecordedRequest request = server.takeRequest();
    assertThat(request.getMethod()).isEqualTo(method);
    assertThat(request.getHeader(HttpHeaders.ACCEPT)).isEqualTo(APPLICATION_JSON);

    final String path = request.getPath();
    final String rawPath = path.contains("?") ? path.substring(0, path.indexOf('?')) : path;
    assertThat(rawPath).isEqualTo(expectedPath);

    final Map<String, String> normalizedParams = Maps.transformValues(queryParams, Functions.toStringFunction());
    assertThat(normalizedParams).isEqualTo(extractParams(path));

    return request;
}
 
Example 5
Source File: BaseJenkinsMockTest.java    From jenkins-rest with Apache License 2.0 5 votes vote down vote up
protected RecordedRequest assertSentWithFormData(final MockWebServer server, 
        final String method, 
        final String path, 
        final String body) throws InterruptedException {

    RecordedRequest request = server.takeRequest();
    assertThat(request.getMethod()).isEqualTo(method);
    assertThat(request.getPath()).isEqualTo(path);
    assertThat(request.getUtf8Body()).isEqualTo(body);
    assertThat(request.getHeader(HttpHeaders.ACCEPT)).isEqualTo(APPLICATION_JSON);
    assertThat(request.getHeader(HttpHeaders.CONTENT_TYPE)).isEqualTo(MediaType.APPLICATION_FORM_URLENCODED);
    return request;
}
 
Example 6
Source File: BaseBitbucketMockTest.java    From bitbucket-rest with Apache License 2.0 5 votes vote down vote up
protected RecordedRequest assertSentAcceptText(final MockWebServer server, 
        final String method, 
        final String path) throws InterruptedException {
    
    final RecordedRequest request = server.takeRequest();
    assertThat(request.getMethod()).isEqualTo(method);
    assertThat(request.getPath()).isEqualTo(path);
    assertThat(request.getHeader(HttpHeaders.ACCEPT)).isEqualTo(MediaType.TEXT_PLAIN);
    return request;
}
 
Example 7
Source File: BaseBitbucketMockTest.java    From bitbucket-rest with Apache License 2.0 5 votes vote down vote up
protected RecordedRequest assertSentWithFormData(final MockWebServer server, 
        final String method, 
        final String path, 
        final String body) throws InterruptedException {
    
    final RecordedRequest request = server.takeRequest();
    assertThat(request.getMethod()).isEqualTo(method);
    assertThat(request.getPath()).isEqualTo(path);
    assertThat(request.getUtf8Body()).isEqualTo(body);
    assertThat(request.getHeader(HttpHeaders.ACCEPT)).isEqualTo(APPLICATION_JSON);
    assertThat(request.getHeader(HttpHeaders.CONTENT_TYPE)).isEqualTo(MediaType.APPLICATION_FORM_URLENCODED);
    return request;
}
 
Example 8
Source File: BaseJenkinsMockTest.java    From jenkins-rest with Apache License 2.0 5 votes vote down vote up
protected RecordedRequest assertSentAccept(MockWebServer server, String method, String path, String acceptType) throws InterruptedException {
    RecordedRequest request = server.takeRequest();
    assertThat(request.getMethod()).isEqualTo(method);
    assertThat(request.getPath()).isEqualTo(path);
    assertThat(request.getHeader(HttpHeaders.ACCEPT)).isEqualTo(acceptType);
    return request;
}
 
Example 9
Source File: BaseJenkinsMockTest.java    From jenkins-rest with Apache License 2.0 5 votes vote down vote up
protected RecordedRequest assertSentAcceptText(MockWebServer server, String method, String path) throws InterruptedException {
    RecordedRequest request = server.takeRequest();
    assertThat(request.getMethod()).isEqualTo(method);
    assertThat(request.getPath()).isEqualTo(path);
    assertThat(request.getHeader(HttpHeaders.ACCEPT)).isEqualTo(MediaType.TEXT_PLAIN);
    return request;
}
 
Example 10
Source File: BaseJenkinsMockTest.java    From jenkins-rest with Apache License 2.0 5 votes vote down vote up
protected RecordedRequest assertSentWithXMLFormDataAccept(MockWebServer server, String method, String path,
    String body, String acceptType) throws InterruptedException {

    RecordedRequest request = server.takeRequest();
    assertThat(request.getMethod()).isEqualTo(method);
    assertThat(request.getPath()).isEqualTo(path);
    assertThat(request.getUtf8Body()).isEqualTo(body);
    assertThat(request.getHeader(HttpHeaders.ACCEPT)).isEqualTo(acceptType);
    assertThat(request.getHeader(HttpHeaders.CONTENT_TYPE)).isEqualTo(MediaType.APPLICATION_XML);
    return request;
}
 
Example 11
Source File: BaseJenkinsMockTest.java    From jenkins-rest with Apache License 2.0 5 votes vote down vote up
protected RecordedRequest assertSentWithFormData(MockWebServer server, String method,
                                                String path, String body,
                                                String acceptType) throws InterruptedException {

    RecordedRequest request = server.takeRequest();
    assertThat(request.getMethod()).isEqualTo(method);
    assertThat(request.getPath()).isEqualTo(path);
    assertThat(request.getUtf8Body()).isEqualTo(body);
    assertThat(request.getHeader(HttpHeaders.ACCEPT)).isEqualTo(acceptType);
    assertThat(request.getHeader(HttpHeaders.CONTENT_TYPE)).isEqualTo(MediaType.APPLICATION_FORM_URLENCODED);
    return request;
}
 
Example 12
Source File: ChromeServiceImplTest.java    From chrome-devtools-java-client with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetTabs() throws IOException, ChromeServiceException, InterruptedException {
  MockWebServer server = new MockWebServer();

  InputStream fixture = getFixture("chrome/tabs.json");
  server.enqueue(new MockResponse().setBody(ChromeServiceImpl.inputStreamToString(fixture)));

  server.start();

  ChromeServiceImpl service = new ChromeServiceImpl(server.getHostName(), server.getPort());

  List<ChromeTab> tabs = service.getTabs();

  RecordedRequest request = server.takeRequest();
  assertEquals(1, server.getRequestCount());
  assertEquals("GET /json/list HTTP/1.1", request.getRequestLine());

  assertFalse(tabs.isEmpty());
  assertEquals(2, tabs.size());

  assertEquals("", tabs.get(0).getDescription());
  assertEquals(
      "/devtools/inspector.html?ws=localhost:9222/devtools/page/(2C5C79DD1137419CC8839D61D91CEB2A)",
      tabs.get(0).getDevtoolsFrontendUrl());
  assertEquals(
      "https://www.google.ba/images/branding/product/ico/googleg_lodp.ico",
      tabs.get(0).getFaviconUrl());
  assertEquals("(2C5C79DD1137419CC8839D61D91CEB2A)", tabs.get(0).getId());
  assertEquals("Google", tabs.get(0).getTitle());
  assertEquals("page", tabs.get(0).getType());
  assertTrue(tabs.get(0).isPageType());
  assertEquals(
      "https://www.google.ba/?gws_rd=cr&dcr=0&ei=93piWq2oKqqJmgWIzbdg", tabs.get(0).getUrl());
  assertEquals(
      "ws://localhost:9222/devtools/page/(2C5C79DD1137419CC8839D61D91CEB2A)",
      tabs.get(0).getWebSocketDebuggerUrl());

  server.shutdown();
}
 
Example 13
Source File: ChromeServiceImplTest.java    From chrome-devtools-java-client with Apache License 2.0 5 votes vote down vote up
@Test
public void testVersion() throws IOException, ChromeServiceException, InterruptedException {
  MockWebServer server = new MockWebServer();

  InputStream fixture = getFixture("chrome/version.json");
  server.enqueue(new MockResponse().setBody(ChromeServiceImpl.inputStreamToString(fixture)));

  server.start();

  ChromeServiceImpl service = new ChromeServiceImpl(server.getHostName(), server.getPort());

  ChromeVersion version = service.getVersion();

  RecordedRequest request = server.takeRequest();
  assertEquals(1, server.getRequestCount());
  assertEquals("GET /json/version HTTP/1.1", request.getRequestLine());

  assertEquals("Chrome/63.0.3239.132", version.getBrowser());
  assertEquals("1.2", version.getProtocolVersion());
  assertEquals(
      "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36",
      version.getUserAgent());
  assertEquals("6.3.292.49", version.getV8Version());
  assertEquals("537.36 (@2e6edcfee630baa3775f37cb11796b1603a64360)", version.getWebKitVersion());
  assertEquals(
      "ws://localhost:9222/devtools/browser/63318df0-09e4-4143-910e-f89525dda26b",
      version.getWebSocketDebuggerUrl());

  server.shutdown();
}
 
Example 14
Source File: ChromeServiceImplTest.java    From chrome-devtools-java-client with Apache License 2.0 5 votes vote down vote up
@Test
public void testCloseTabClearsDevTools()
    throws IOException, ChromeServiceException, InterruptedException, WebSocketServiceException {
  MockWebServer server = new MockWebServer();

  ObjectMapper objectMapper = new ObjectMapper();
  ChromeTab tab =
      objectMapper.readerFor(ChromeTab.class).readValue(getFixture("chrome/tab.json"));

  server.enqueue(new MockResponse());
  server.start();

  expect(webSocketServiceFactory.createWebSocketService(tab.getWebSocketDebuggerUrl()))
      .andReturn(webSocketService);

  webSocketService.addMessageHandler(anyObject());
  webSocketService.close();

  replayAll();

  ChromeServiceImpl service =
      new ChromeServiceImpl(server.getHostName(), server.getPort(), webSocketServiceFactory);

  service.createDevToolsService(tab);

  service.closeTab(tab);

  RecordedRequest request = server.takeRequest();
  assertEquals(1, server.getRequestCount());
  assertEquals(
      "GET /json/close/(2C5C79DD1137419CC8839D61D91CEB2A) HTTP/1.1", request.getRequestLine());

  server.shutdown();

  verifyAll();
}
 
Example 15
Source File: ChromeServiceImplTest.java    From chrome-devtools-java-client with Apache License 2.0 5 votes vote down vote up
@Test
public void testCreateTabWithAboutBlankPage()
    throws IOException, ChromeServiceException, InterruptedException {
  MockWebServer server = new MockWebServer();

  InputStream fixture = getFixture("chrome/tab.json");
  server.enqueue(new MockResponse().setBody(ChromeServiceImpl.inputStreamToString(fixture)));

  server.start();

  ChromeServiceImpl service = new ChromeServiceImpl(server.getHostName(), server.getPort());

  ChromeTab tab = service.createTab();

  RecordedRequest request = server.takeRequest();
  assertEquals(1, server.getRequestCount());
  assertEquals("GET /json/new?about:blank HTTP/1.1", request.getRequestLine());

  assertEquals("", tab.getDescription());
  assertEquals(
      "/devtools/inspector.html?ws=localhost:9222/devtools/page/(2C5C79DD1137419CC8839D61D91CEB2A)",
      tab.getDevtoolsFrontendUrl());
  assertEquals(
      "https://www.google.ba/images/branding/product/ico/googleg_lodp.ico", tab.getFaviconUrl());
  assertEquals("(2C5C79DD1137419CC8839D61D91CEB2A)", tab.getId());
  assertEquals("Google", tab.getTitle());
  assertEquals("page", tab.getType());
  assertTrue(tab.isPageType());
  assertEquals("https://www.google.ba/?gws_rd=cr&dcr=0&ei=93piWq2oKqqJmgWIzbdg", tab.getUrl());
  assertEquals(
      "ws://localhost:9222/devtools/page/(2C5C79DD1137419CC8839D61D91CEB2A)",
      tab.getWebSocketDebuggerUrl());

  server.shutdown();
}
 
Example 16
Source File: ChromeServiceImplTest.java    From chrome-devtools-java-client with Apache License 2.0 5 votes vote down vote up
@Test
public void testCreateTab() throws IOException, ChromeServiceException, InterruptedException {
  MockWebServer server = new MockWebServer();

  InputStream fixture = getFixture("chrome/tab.json");
  server.enqueue(new MockResponse().setBody(ChromeServiceImpl.inputStreamToString(fixture)));

  server.start();

  ChromeServiceImpl service = new ChromeServiceImpl(server.getHostName(), server.getPort());

  ChromeTab tab = service.createTab("some-tab-name");

  RecordedRequest request = server.takeRequest();
  assertEquals(1, server.getRequestCount());
  assertEquals("GET /json/new?some-tab-name HTTP/1.1", request.getRequestLine());

  assertEquals("", tab.getDescription());
  assertEquals(
      "/devtools/inspector.html?ws=localhost:9222/devtools/page/(2C5C79DD1137419CC8839D61D91CEB2A)",
      tab.getDevtoolsFrontendUrl());
  assertEquals(
      "https://www.google.ba/images/branding/product/ico/googleg_lodp.ico", tab.getFaviconUrl());
  assertEquals("(2C5C79DD1137419CC8839D61D91CEB2A)", tab.getId());
  assertEquals("Google", tab.getTitle());
  assertEquals("page", tab.getType());
  assertTrue(tab.isPageType());
  assertEquals("https://www.google.ba/?gws_rd=cr&dcr=0&ei=93piWq2oKqqJmgWIzbdg", tab.getUrl());
  assertEquals(
      "ws://localhost:9222/devtools/page/(2C5C79DD1137419CC8839D61D91CEB2A)",
      tab.getWebSocketDebuggerUrl());

  server.shutdown();
}
 
Example 17
Source File: BaseJenkinsMockTest.java    From jenkins-rest with Apache License 2.0 4 votes vote down vote up
protected RecordedRequest assertSent(MockWebServer server, String method, String path, String json) throws InterruptedException {
    RecordedRequest request = server.takeRequest();
    assertEquals(request.getHeader("Content-Type"), APPLICATION_JSON);
    assertEquals(parser.parse(request.getUtf8Body()), parser.parse(json));
    return request;
}