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

The following examples show how to use com.squareup.okhttp.mockwebserver.MockWebServer#getPort() . 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: ChromeServiceImplTest.java    From chrome-devtools-java-client with Apache License 2.0 6 votes vote down vote up
@Test(expected = ChromeServiceException.class)
public void testActivateTabOnNotFoundResponse()
    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().setResponseCode(HttpURLConnection.HTTP_NOT_FOUND));
  server.start();

  try {
    ChromeServiceImpl service = new ChromeServiceImpl(server.getHostName(), server.getPort());
    service.activateTab(tab);
  } finally {
    server.shutdown();
  }
}
 
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: 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 5
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 6
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 7
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 8
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();
}