2016-10-06 00:38:08 +03:00
|
|
|
import pytest # NOQA
|
|
|
|
import os
|
|
|
|
import utilities
|
|
|
|
import expected
|
|
|
|
from openwpmtest import OpenWPMTest
|
|
|
|
from ..automation import TaskManager
|
2016-10-06 02:35:22 +03:00
|
|
|
from ..automation.platform_utils import parse_http_stack_trace_str
|
|
|
|
|
2016-10-06 00:38:08 +03:00
|
|
|
|
|
|
|
class TestHTTPInstrument(OpenWPMTest):
|
|
|
|
NUM_BROWSERS = 1
|
|
|
|
|
|
|
|
def get_config(self, data_dir):
|
|
|
|
manager_params, browser_params = TaskManager.load_default_params(self.NUM_BROWSERS)
|
|
|
|
manager_params['data_directory'] = data_dir
|
|
|
|
manager_params['log_directory'] = data_dir
|
|
|
|
browser_params[0]['headless'] = True
|
2016-11-29 23:43:47 +03:00
|
|
|
browser_params[0]['http_instrument'] = True
|
2016-10-06 00:38:08 +03:00
|
|
|
manager_params['db'] = os.path.join(manager_params['data_directory'],
|
|
|
|
manager_params['database_name'])
|
|
|
|
return manager_params, browser_params
|
|
|
|
|
|
|
|
def test_page_visit(self, tmpdir):
|
|
|
|
test_url = utilities.BASE_TEST_URL + '/http_test_page.html'
|
|
|
|
db = self.visit(test_url, str(tmpdir))
|
|
|
|
|
|
|
|
# HTTP Requests
|
|
|
|
rows = utilities.query_db(db, (
|
|
|
|
"SELECT url, top_level_url, is_XHR, is_frame_load, is_full_page, "
|
|
|
|
"is_third_party_channel, is_third_party_window, triggering_origin "
|
|
|
|
"loading_origin, loading_href, content_policy_type FROM http_requests_ext"))
|
|
|
|
observed_records = set()
|
|
|
|
for row in rows:
|
|
|
|
observed_records.add(row)
|
|
|
|
assert expected.http_requests == observed_records
|
|
|
|
|
|
|
|
# HTTP Responses
|
|
|
|
rows = utilities.query_db(db,
|
|
|
|
"SELECT url, referrer, location FROM http_responses_ext")
|
|
|
|
observed_records = set()
|
|
|
|
for row in rows:
|
|
|
|
observed_records.add(row)
|
|
|
|
assert expected.http_responses == observed_records
|
|
|
|
|
|
|
|
#TODO: test that cache hits are recorded. Will need a custom command to
|
|
|
|
#refresh page.
|
|
|
|
|
|
|
|
#TODO: test that javascript content is saved correctly
|
2016-10-06 02:35:22 +03:00
|
|
|
|
|
|
|
def test_http_stacktrace(self, tmpdir):
|
2016-10-08 11:54:06 +03:00
|
|
|
test_url = utilities.BASE_TEST_URL + '/http_stacktrace.html'
|
2016-10-06 21:04:29 +03:00
|
|
|
db = self.visit(test_url, str(tmpdir), sleep_after=3)
|
2016-10-06 02:35:22 +03:00
|
|
|
rows = utilities.query_db(db, (
|
|
|
|
"SELECT url, req_call_stack FROM http_requests_ext"))
|
|
|
|
for row in rows:
|
|
|
|
url, stacktrace = row
|
2016-10-08 11:54:06 +03:00
|
|
|
if url.endswith("inject_pixel.js"):
|
|
|
|
assert stacktrace == expected.stack_trace_inject_js
|
|
|
|
if url.endswith("test_image.png"):
|
|
|
|
assert stacktrace == expected.stack_trace_inject_image
|
|
|
|
if url.endswith("Blank.gif"):
|
|
|
|
assert stacktrace == expected.stack_trace_inject_pixel
|
|
|
|
|
|
|
|
def test_parse_http_stack_trace_str(self, tmpdir):
|
|
|
|
stacktrace = expected.stack_trace_inject_image
|
|
|
|
stack_frames = parse_http_stack_trace_str(stacktrace)
|
|
|
|
assert stack_frames == expected.call_stack_inject_image
|
2016-10-06 21:04:29 +03:00
|
|
|
|
|
|
|
def test_http_stacktrace_nonjs_loads(self, tmpdir):
|
2016-10-08 11:54:06 +03:00
|
|
|
# stacktrace should be empty for requests NOT triggered by scripts
|
2016-10-06 21:04:29 +03:00
|
|
|
test_url = utilities.BASE_TEST_URL + '/http_test_page.html'
|
|
|
|
db = self.visit(test_url, str(tmpdir), sleep_after=3)
|
|
|
|
rows = utilities.query_db(db, (
|
|
|
|
"SELECT url, req_call_stack FROM http_requests_ext"))
|
|
|
|
for row in rows:
|
|
|
|
_, stacktrace = row
|
2016-10-08 11:54:06 +03:00
|
|
|
assert stacktrace == ""
|