Question regarding using net::EmbeddedTestServer in https

34 views
Skip to first unread message

jun...@chromium.org

unread,
Nov 16, 2017, 4:59:14 PM11/16/17
to Chromium-dev
 Hi, I tried to use net::EmbeddedTestServer in https, and have the following content browser test code:

  net::EmbeddedTestServer https_embedded_test_server(net::EmbeddedTestServer::TYPE_HTTPS);
  https_embedded_test_server.ServeFilesFromSourceDirectory("content/test/data");
  ASSERT_TRUE(https_embedded_test_server.Start());
  GURL main_frame_url = https_embedded_test_server.GetURL("/device_sensors/cross_origin_iframe.html");
  LOG(INFO) << "|main_frame_url| is: " << main_frame_url;
  NavigateToURL(shell(), main_frame_url);

  The cross_origin_iframe.html file looks like:

  <html>
  <head>
    <title>cross-origin iframe</title>
    <script>
    function output() {
      console.log("output() called.");
    }
    </script>
  </head>
  <body onload="output()">
    <iframe id="cross_origin_iframe"></iframe>
  </body>
  </html>

This one works fine and the js code prints the "output() called". And the |main_frame_url| is:

Now I changed the code a bit by adding "a.com" in the url:
   
  net::EmbeddedTestServer https_embedded_test_server(net::EmbeddedTestServer::TYPE_HTTPS);
  https_embedded_test_server.ServeFilesFromSourceDirectory("content/test/data");
  ASSERT_TRUE(https_embedded_test_server.Start());
  GURL main_frame_url = https_embedded_test_server.GetURL("a.com", "/device_sensors/cross_origin_iframe.html");
  LOG(INFO) << "|main_frame_url| is: " << main_frame_url;
  NavigateToURL(shell(), main_frame_url);

And call:
host_resolver()->AddRule("*", "127.0.0.1");
in:
void SetUpOnMainThread() override
 
The js code seems not be called since the "output() called" is not printed, the |main_frame_url| is:


However, using the default embedded_test_server() works:
  ASSERT_TRUE(embedded_test_server()->Start());
  GURL main_frame_url = embedded_test_server()->GetURL("a.com", "/device_sensors/cross_origin_iframe.html");
  LOG(INFO) << "|main_frame_url| is: " << main_frame_url;
  NavigateToURL(shell(), main_frame_url);
I also need to add call:
host_resolver()->AddRule("*", "127.0.0.1");
in:
void SetUpOnMainThread() override;

The |main_frame_url| is:

Any suggestion how to make the second case work? Thanks!

Giovanni Ortuño

unread,
Nov 16, 2017, 5:35:10 PM11/16/17
to jun...@chromium.org, Chromium-dev
I can't see anything wrong with your code. Do you have a patch with the second variation, i.e. https server and URL with hostname, that we can look at?

Also check the logs for errors. If the request is not being handled by the file request handler then you should see a 404. You can also try adding log statements to the EmbeddedTestServer code to see where your request is getting lost; I would start here.

Gio

--
--
Chromium Developers mailing list: chromi...@chromium.org
View archives, change email options, or unsubscribe:
http://groups.google.com/a/chromium.org/group/chromium-dev
---
You received this message because you are subscribed to the Google Groups "Chromium-dev" group.
To view this discussion on the web visit https://groups.google.com/a/chromium.org/d/msgid/chromium-dev/d3911f63-e0a3-4c20-9e24-fe8ebc7e4e56%40chromium.org.

jun...@chromium.org

unread,
Nov 16, 2017, 6:36:27 PM11/16/17
to Chromium-dev, jun...@chromium.org
I uploaded a sample CL at:

I added some log to the embedded_test_server.cc, and found that when there is no output from js (the second case), the EmbeddedTestServer::HandleRequest() is not called at all.

jun...@chromium.org

unread,
Nov 16, 2017, 7:38:23 PM11/16/17
to Chromium-dev, jun...@chromium.org
My browser test code here is very similar to:
except it is in //chrome, but my browser test code is in //content. And I couldn't find similar code in //content that uses EmbeddedTestServer in https with a hostname in the URL.

One more difference between the test code in //chrome and //content is that in //chrome, it uses ui_test_utils::NavigateToURL() to navigate:
But in //content, it uses content::NavigateToURL() to navigate:

Not sure if this is the reason.

jun...@chromium.org

unread,
Nov 16, 2017, 8:04:39 PM11/16/17
to Chromium-dev, jun...@chromium.org

Ryan Sleevi

unread,
Nov 17, 2017, 8:33:34 AM11/17/17
to jun...@chromium.org, Chromium-dev
You shouldn't have to ignore certificate errors for your test. That's a pattern we should be actively discouraging, even for tests - in part, because we're trying to get rid of that flag.

For browser_tests, the general answer is you should be using a MockCertVerifier if you want to override how certificate verification behaves. You can see https://cs.chromium.org/chromium/src/chrome/browser/ssl/cert_verifier_browser_test.h?rcl=dcf0d8a5e21519cf563a210c3b5c79d6f109e8fe&l=5 for how this is setup.

Reply all
Reply to author
Forward
0 new messages