def testGetRawResponseHeaders(self):
   r = Request()
   r.protocol = 'http/1.1'
   r.status = 200
   r.status_text = 'Hello world'
   r.response_headers = {'Foo': 'Bar', 'Baz': 'Foo'}
   self.assertEquals('HTTP/1.1 200 Hello world\x00Baz: Foo\x00Foo: Bar\x00',
                     r.GetRawResponseHeaders())
 def testGetHTTPResponseHeader(self):
   r = Request()
   r.response_headers = {}
   self.assertEquals(None, r.GetHTTPResponseHeader('Foo'))
   r.response_headers = {'Foo': 'Bar', 'Baz': 'Foo'}
   self.assertEquals('Bar', r.GetHTTPResponseHeader('Foo'))
   r.response_headers = {'foo': 'Bar', 'Baz': 'Foo'}
   self.assertEquals('Bar', r.GetHTTPResponseHeader('Foo'))
 def testContentType(self):
   r = Request()
   r.response_headers = {}
   self.assertEquals(None, r.GetContentType())
   r.response_headers = {'Content-Type': 'application/javascript'}
   self.assertEquals('application/javascript', r.GetContentType())
   r.response_headers = {'Content-Type': 'application/javascript;bla'}
   self.assertEquals('application/javascript', r.GetContentType())
 def testMaxAge(self):
   rq = Request()
   self.assertEqual(-1, rq.MaxAge())
   rq.response_headers = {}
   self.assertEqual(-1, rq.MaxAge())
   rq.response_headers[
       'Cache-Control'] = 'private,s-maxage=0,max-age=0,must-revalidate'
   self.assertEqual(0, rq.MaxAge())
   rq.response_headers[
       'Cache-Control'] = 'private,s-maxage=0,no-store,max-age=100'
   self.assertEqual(-1, rq.MaxAge())
   rq.response_headers[
       'Cache-Control'] = 'private,s-maxage=0'
   self.assertEqual(-1, rq.MaxAge())
Exemplo n.º 5
0
 def testGetHTTPResponseHeader(self):
     r = Request()
     r.response_headers = {}
     self.assertEquals(None, r.GetHTTPResponseHeader('Foo'))
     r.response_headers = {'Foo': 'Bar', 'Baz': 'Foo'}
     self.assertEquals('Bar', r.GetHTTPResponseHeader('Foo'))
     r.response_headers = {'foo': 'Bar', 'Baz': 'Foo'}
     self.assertEquals('Bar', r.GetHTTPResponseHeader('Foo'))
Exemplo n.º 6
0
class TimeBetweenTestCase(unittest.TestCase):
    _REQUEST = Request.FromJsonDict({
        'url': 'http://bla.com',
        'request_id': '1234.1',
        'frame_id': '123.1',
        'initiator': {
            'type': 'other'
        },
        'timestamp': 2,
        'timing': {}
    })

    def setUp(self):
        super(TimeBetweenTestCase, self).setUp()
        self.first = copy.deepcopy(self._REQUEST)
        self.first.timing = Timing.FromDevToolsDict({
            'requestTime': 123456,
            'receiveHeadersEnd': 100,
            'loadingFinished': 500
        })
        self.second = copy.deepcopy(self._REQUEST)
        self.second.timing = Timing.FromDevToolsDict({
            'requestTime': 123456 + 1,
            'receiveHeadersEnd': 200,
            'loadingFinished': 600
        })

    def testTimeBetweenParser(self):
        self.assertEquals(900, TimeBetween(self.first, self.second, 'parser'))

    def testTimeBetweenScript(self):
        self.assertEquals(500, TimeBetween(self.first, self.second, 'script'))
Exemplo n.º 7
0
 def testAsyncScriptDependency(self):
     JS_REQUEST_WITH_ASYNC_STACK = Request.FromJsonDict({
         'url':
         'http://bla.com/cat.js',
         'request_id':
         '1234.14',
         'initiator': {
             'type': 'script',
             'stack': {
                 'callFrames': [],
                 'parent': {
                     'callFrames': [{
                         'url': 'http://bla.com/nyancat.js'
                     }]
                 }
             }
         },
         'timestamp':
         10,
         'timing':
         TimingFromDict({})
     })
     loading_trace = test_utils.LoadingTraceFromEvents(
         [TestRequests.JS_REQUEST, JS_REQUEST_WITH_ASYNC_STACK])
     request_dependencies_lens = RequestDependencyLens(loading_trace)
     deps = request_dependencies_lens.GetRequestDependencies()
     self.assertEquals(1, len(deps))
     self._AssertDependencyIs(deps[0], TestRequests.JS_REQUEST.request_id,
                              JS_REQUEST_WITH_ASYNC_STACK.request_id,
                              'script')
 def testGetRawResponseHeaders(self):
   r = Request()
   r.protocol = 'http/1.1'
   r.status = 200
   r.status_text = 'Hello world'
   r.response_headers = {'Foo': 'Bar', 'Baz': 'Foo'}
   self.assertEquals('HTTP/1.1 200 Hello world\x00Baz: Foo\x00Foo: Bar\x00',
                     r.GetRawResponseHeaders())
class _MatcherTestCase(unittest.TestCase):
    _RULES_WITH_WHITELIST = [
        '/thisisanad.', '@@myadvertisingdomain.com/*',
        '@@||www.mydomain.com/ads/$elemhide'
    ]
    _SCRIPT_RULE = 'domainwithscripts.com/*$script'
    _THIRD_PARTY_RULE = 'domainwithscripts.com/*$third-party'
    _SCRIPT_REQUEST = Request.FromJsonDict({
        'url': 'http://domainwithscripts.com/bla.js',
        'resource_type': 'Script',
        'request_id': '1234.1',
        'frame_id': '123.1',
        'initiator': {
            'type': 'other'
        },
        'timestamp': 2,
        'timing': {}
    })

    def testRemovesWhitelistRules(self):
        matcher = _RulesMatcher(self._RULES_WITH_WHITELIST, False)
        self.assertEquals(3, len(matcher._rules))
        matcher = _RulesMatcher(self._RULES_WITH_WHITELIST, True)
        self.assertEquals(1, len(matcher._rules))

    def testScriptRule(self):
        matcher = _RulesMatcher([self._SCRIPT_RULE], False)
        request = copy.deepcopy(self._SCRIPT_REQUEST)
        request.resource_type = 'Stylesheet'
        self.assertFalse(
            matcher.Matches(request,
                            ContentClassificationLensTestCase._DOCUMENT_URL))
        self.assertTrue(
            matcher.Matches(self._SCRIPT_REQUEST,
                            ContentClassificationLensTestCase._DOCUMENT_URL))

    def testGetTldPlusOne(self):
        self.assertEquals(
            'easy.com',
            _RulesMatcher._GetTldPlusOne('http://www.easy.com/hello/you'))
        self.assertEquals(
            'not-so-easy.co.uk',
            _RulesMatcher._GetTldPlusOne(
                'http://www.not-so-easy.co.uk/hello/you'))
        self.assertEquals('hard.co.uk',
                          _RulesMatcher._GetTldPlusOne('http://hard.co.uk/'))

    def testThirdPartyRule(self):
        matcher = _RulesMatcher([self._THIRD_PARTY_RULE], False)
        request = copy.deepcopy(self._SCRIPT_REQUEST)
        document_url = 'http://www.domainwithscripts.com/good-morning'
        self.assertFalse(matcher.Matches(request, document_url))
        document_url = 'http://anotherdomain.com/good-morning'
        self.assertTrue(matcher.Matches(request, document_url))
Exemplo n.º 10
0
 def testMaxAge(self):
     rq = Request()
     self.assertEqual(-1, rq.MaxAge())
     rq.response_headers = {}
     self.assertEqual(-1, rq.MaxAge())
     rq.response_headers[
         'Cache-Control'] = 'private,s-maxage=0,max-age=0,must-revalidate'
     self.assertEqual(0, rq.MaxAge())
     rq.response_headers[
         'Cache-Control'] = 'private,s-maxage=0,no-store,max-age=100'
     self.assertEqual(-1, rq.MaxAge())
     rq.response_headers['Cache-Control'] = 'private,s-maxage=0'
     self.assertEqual(-1, rq.MaxAge())
     # Case-insensitive match.
     rq.response_headers['cache-control'] = 'max-age=600'
     self.assertEqual(600, rq.MaxAge())
Exemplo n.º 11
0
class TestRequests(object):
    FIRST_REDIRECT_REQUEST = Request.FromJsonDict({
        'url': 'http://bla.com',
        'request_id': '1234.redirect.1',
        'initiator': {
            'type': 'other'
        },
        'timestamp': 0.5,
        'timing': TimingFromDict({})
    })
    SECOND_REDIRECT_REQUEST = Request.FromJsonDict({
        'url': 'http://bla.com/redirect1',
        'request_id': '1234.redirect.2',
        'initiator': {
            'type': 'redirect',
            'initiating_request': '1234.redirect.1'
        },
        'timestamp': 1,
        'timing': TimingFromDict({})
    })
    REDIRECTED_REQUEST = Request.FromJsonDict({
        'url': 'http://bla.com/index.html',
        'request_id': '1234.1',
        'frame_id': '123.1',
        'initiator': {
            'type': 'redirect',
            'initiating_request': '1234.redirect.2'
        },
        'timestamp': 2,
        'timing': TimingFromDict({})
    })
    REQUEST = Request.FromJsonDict({
        'url': 'http://bla.com/index.html',
        'request_id': '1234.1',
        'frame_id': '123.1',
        'initiator': {
            'type': 'other'
        },
        'timestamp': 2,
        'timing': TimingFromDict({})
    })
    JS_REQUEST = Request.FromJsonDict({
        'url': 'http://bla.com/nyancat.js',
        'request_id': '1234.12',
        'frame_id': '123.123',
        'initiator': {
            'type': 'parser',
            'url': 'http://bla.com/index.html'
        },
        'timestamp': 3,
        'timing': TimingFromDict({})
    })
    JS_REQUEST_OTHER_FRAME = Request.FromJsonDict({
        'url': 'http://bla.com/nyancat.js',
        'request_id': '1234.42',
        'frame_id': '123.13',
        'initiator': {
            'type': 'parser',
            'url': 'http://bla.com/index.html'
        },
        'timestamp': 4,
        'timing': TimingFromDict({})
    })
    JS_REQUEST_UNRELATED_FRAME = Request.FromJsonDict({
        'url':
        'http://bla.com/nyancat.js',
        'request_id':
        '1234.56',
        'frame_id':
        '123.99',
        'initiator': {
            'type': 'parser',
            'url': 'http://bla.com/index.html'
        },
        'timestamp':
        5,
        'timing':
        TimingFromDict({})
    })
    JS_REQUEST_2 = Request.FromJsonDict({
        'url': 'http://bla.com/cat.js',
        'request_id': '1234.13',
        'frame_id': '123.123',
        'initiator': {
            'type': 'script',
            'stack': {
                'callFrames': [{
                    'url': 'unknown'
                }, {
                    'url': 'http://bla.com/nyancat.js'
                }]
            }
        },
        'timestamp': 10,
        'timing': TimingFromDict({})
    })
    PAGE_EVENTS = [{
        'method': 'Page.frameAttached',
        'frame_id': '123.13',
        'parent_frame_id': '123.1'
    }, {
        'method': 'Page.frameAttached',
        'frame_id': '123.123',
        'parent_frame_id': '123.1'
    }]

    @classmethod
    def CreateLoadingTrace(cls, trace_events=None):
        return test_utils.LoadingTraceFromEvents([
            cls.FIRST_REDIRECT_REQUEST, cls.SECOND_REDIRECT_REQUEST,
            cls.REDIRECTED_REQUEST, cls.REQUEST, cls.JS_REQUEST,
            cls.JS_REQUEST_2, cls.JS_REQUEST_OTHER_FRAME,
            cls.JS_REQUEST_UNRELATED_FRAME
        ], cls.PAGE_EVENTS, trace_events)
class ContentClassificationLensTestCase(unittest.TestCase):
    _DOCUMENT_URL = 'http://bla.com'
    _MAIN_FRAME_ID = '123.1'
    _REQUEST = Request.FromJsonDict({
        'url': _DOCUMENT_URL,
        'document_url': _DOCUMENT_URL,
        'request_id': '1234.1',
        'frame_id': _MAIN_FRAME_ID,
        'initiator': {
            'type': 'other'
        },
        'timestamp': 2,
        'status': 200,
        'timing': {},
        'resource_type': 'Document'
    })
    _PAGE_EVENTS = [{
        'method': 'Page.frameStartedLoading',
        'frame_id': _MAIN_FRAME_ID
    }, {
        'method': 'Page.frameAttached',
        'frame_id': '123.13',
        'parent_frame_id': _MAIN_FRAME_ID
    }]
    _RULES = ['bla.com']

    def testGetDocumentUrl(self):
        trace = test_utils.LoadingTraceFromEvents([self._REQUEST],
                                                  self._PAGE_EVENTS)
        lens = ContentClassificationLens(trace, [], [])
        self.assertEquals(self._DOCUMENT_URL, lens._GetDocumentUrl())
        # Don't be fooled by redirects.
        request = copy.deepcopy(self._REQUEST)
        request.status = 302
        request.document_url = 'http://www.bla.com'
        trace = test_utils.LoadingTraceFromEvents([request, self._REQUEST],
                                                  self._PAGE_EVENTS)
        lens = ContentClassificationLens(trace, [], [])
        self.assertEquals(self._DOCUMENT_URL, lens._GetDocumentUrl())

    def testGetDocumentUrlSeveralChanges(self):
        request = copy.deepcopy(self._REQUEST)
        request.status = 200
        request.document_url = 'http://www.blabla.com'
        request2 = copy.deepcopy(request)
        request2.document_url = 'http://www.blablabla.com'
        trace = test_utils.LoadingTraceFromEvents(
            [self._REQUEST, request, request2], self._PAGE_EVENTS)
        lens = ContentClassificationLens(trace, [], [])
        self.assertEquals(request2.document_url, lens._GetDocumentUrl())

    def testNoRules(self):
        trace = test_utils.LoadingTraceFromEvents([self._REQUEST],
                                                  self._PAGE_EVENTS)
        lens = ContentClassificationLens(trace, [], [])
        self.assertFalse(lens.IsAdRequest(self._REQUEST))
        self.assertFalse(lens.IsTrackingRequest(self._REQUEST))

    def testAdRequest(self):
        trace = test_utils.LoadingTraceFromEvents([self._REQUEST],
                                                  self._PAGE_EVENTS)
        lens = ContentClassificationLens(trace, self._RULES, [])
        self.assertTrue(lens.IsAdRequest(self._REQUEST))
        self.assertFalse(lens.IsTrackingRequest(self._REQUEST))

    def testTrackingRequest(self):
        trace = test_utils.LoadingTraceFromEvents([self._REQUEST],
                                                  self._PAGE_EVENTS)
        lens = ContentClassificationLens(trace, [], self._RULES)
        self.assertFalse(lens.IsAdRequest(self._REQUEST))
        self.assertTrue(lens.IsTrackingRequest(self._REQUEST))

    def testMainFrameIsNotAnAdFrame(self):
        trace = test_utils.LoadingTraceFromEvents([self._REQUEST],
                                                  self._PAGE_EVENTS)
        lens = ContentClassificationLens(trace, self._RULES, [])
        self.assertFalse(lens.IsAdOrTrackingFrame(self._MAIN_FRAME_ID))

    def testAdFrame(self):
        request = copy.deepcopy(self._REQUEST)
        request.request_id = '1234.2'
        request.frame_id = '123.123'
        trace = test_utils.LoadingTraceFromEvents([self._REQUEST, request],
                                                  self._PAGE_EVENTS)
        lens = ContentClassificationLens(trace, self._RULES, [])
        self.assertTrue(lens.IsAdOrTrackingFrame(request.frame_id))

    def testAdAndTrackingRequests(self):
        ad_request = copy.deepcopy(self._REQUEST)
        ad_request.request_id = '1234.2'
        ad_request.frame_id = '123.123'
        non_ad_request_non_ad_frame = copy.deepcopy(self._REQUEST)
        non_ad_request_non_ad_frame.request_id = '1234.3'
        non_ad_request_non_ad_frame.url = 'http://www.example.com'
        non_ad_request_non_ad_frame.frame_id = '123.456'
        non_ad_request_ad_frame = copy.deepcopy(self._REQUEST)
        non_ad_request_ad_frame.request_id = '1234.4'
        non_ad_request_ad_frame.url = 'http://www.example.com'
        non_ad_request_ad_frame.frame_id = ad_request.frame_id

        trace = test_utils.LoadingTraceFromEvents([
            self._REQUEST, ad_request, non_ad_request_non_ad_frame,
            non_ad_request_ad_frame
        ], self._PAGE_EVENTS)
        lens = ContentClassificationLens(trace, self._RULES, [])
        self.assertSetEqual(
            set([self._REQUEST, ad_request, non_ad_request_ad_frame]),
            set(lens.AdAndTrackingRequests()))
Exemplo n.º 13
0
class RequestDependencyLensTestCase(unittest.TestCase):
    _REDIRECT_REQUEST = Request.FromJsonDict({
        'url': 'http://bla.com',
        'request_id': '1234.redirect.1',
        'initiator': {
            'type': 'other'
        },
        'timestamp': 1,
        'timing': TimingFromDict({})
    })
    _REDIRECTED_REQUEST = Request.FromJsonDict({
        'url': 'http://bla.com',
        'request_id': '1234.1',
        'frame_id': '123.1',
        'initiator': {
            'type': 'redirect',
            'initiating_request': '1234.redirect.1'
        },
        'timestamp': 2,
        'timing': TimingFromDict({})
    })
    _REQUEST = Request.FromJsonDict({
        'url': 'http://bla.com',
        'request_id': '1234.1',
        'frame_id': '123.1',
        'initiator': {
            'type': 'other'
        },
        'timestamp': 2,
        'timing': TimingFromDict({})
    })
    _JS_REQUEST = Request.FromJsonDict({
        'url': 'http://bla.com/nyancat.js',
        'request_id': '1234.12',
        'frame_id': '123.1',
        'initiator': {
            'type': 'parser',
            'url': 'http://bla.com'
        },
        'timestamp': 3,
        'timing': TimingFromDict({})
    })
    _JS_REQUEST_OTHER_FRAME = Request.FromJsonDict({
        'url': 'http://bla.com/nyancat.js',
        'request_id': '1234.42',
        'frame_id': '123.13',
        'initiator': {
            'type': 'parser',
            'url': 'http://bla.com'
        },
        'timestamp': 4,
        'timing': TimingFromDict({})
    })
    _JS_REQUEST_UNRELATED_FRAME = Request.FromJsonDict({
        'url':
        'http://bla.com/nyancat.js',
        'request_id':
        '1234.42',
        'frame_id':
        '123.99',
        'initiator': {
            'type': 'parser',
            'url': 'http://bla.com'
        },
        'timestamp':
        5,
        'timing':
        TimingFromDict({})
    })
    _JS_REQUEST_2 = Request.FromJsonDict({
        'url': 'http://bla.com/cat.js',
        'request_id': '1234.13',
        'frame_id': '123.1',
        'initiator': {
            'type': 'script',
            'stack': {
                'callFrames': [{
                    'url': 'unknown'
                }, {
                    'url': 'http://bla.com/nyancat.js'
                }]
            }
        },
        'timestamp': 10,
        'timing': TimingFromDict({})
    })
    _PAGE_EVENTS = [{
        'method': 'Page.frameAttached',
        'frame_id': '123.13',
        'parent_frame_id': '123.1'
    }]

    def testRedirectDependency(self):
        loading_trace = test_utils.LoadingTraceFromEvents(
            [self._REDIRECT_REQUEST, self._REDIRECTED_REQUEST])
        request_dependencies_lens = RequestDependencyLens(loading_trace)
        deps = request_dependencies_lens.GetRequestDependencies()
        self.assertEquals(1, len(deps))
        (first, second, reason) = deps[0]
        self.assertEquals('redirect', reason)
        self.assertEquals(self._REDIRECT_REQUEST.request_id, first.request_id)
        self.assertEquals(self._REQUEST.request_id, second.request_id)

    def testScriptDependency(self):
        loading_trace = test_utils.LoadingTraceFromEvents(
            [self._JS_REQUEST, self._JS_REQUEST_2])
        request_dependencies_lens = RequestDependencyLens(loading_trace)
        deps = request_dependencies_lens.GetRequestDependencies()
        self.assertEquals(1, len(deps))
        self._AssertDependencyIs(deps[0], self._JS_REQUEST.request_id,
                                 self._JS_REQUEST_2.request_id, 'script')

    def testAsyncScriptDependency(self):
        JS_REQUEST_WITH_ASYNC_STACK = Request.FromJsonDict({
            'url':
            'http://bla.com/cat.js',
            'request_id':
            '1234.14',
            'initiator': {
                'type': 'script',
                'stack': {
                    'callFrames': [],
                    'parent': {
                        'callFrames': [{
                            'url': 'http://bla.com/nyancat.js'
                        }]
                    }
                }
            },
            'timestamp':
            10,
            'timing':
            TimingFromDict({})
        })
        loading_trace = test_utils.LoadingTraceFromEvents(
            [self._JS_REQUEST, JS_REQUEST_WITH_ASYNC_STACK])
        request_dependencies_lens = RequestDependencyLens(loading_trace)
        deps = request_dependencies_lens.GetRequestDependencies()
        self.assertEquals(1, len(deps))
        self._AssertDependencyIs(deps[0], self._JS_REQUEST.request_id,
                                 JS_REQUEST_WITH_ASYNC_STACK.request_id,
                                 'script')

    def testParserDependency(self):
        loading_trace = test_utils.LoadingTraceFromEvents(
            [self._REQUEST, self._JS_REQUEST])
        request_dependencies_lens = RequestDependencyLens(loading_trace)
        deps = request_dependencies_lens.GetRequestDependencies()
        self.assertEquals(1, len(deps))
        self._AssertDependencyIs(deps[0], self._REQUEST.request_id,
                                 self._JS_REQUEST.request_id, 'parser')

    def testSeveralDependencies(self):
        loading_trace = test_utils.LoadingTraceFromEvents([
            self._REDIRECT_REQUEST, self._REDIRECTED_REQUEST, self._JS_REQUEST,
            self._JS_REQUEST_2
        ])
        request_dependencies_lens = RequestDependencyLens(loading_trace)
        deps = request_dependencies_lens.GetRequestDependencies()
        self.assertEquals(3, len(deps))
        self._AssertDependencyIs(deps[0], self._REDIRECT_REQUEST.request_id,
                                 self._REQUEST.request_id, 'redirect')
        self._AssertDependencyIs(deps[1], self._REQUEST.request_id,
                                 self._JS_REQUEST.request_id, 'parser')
        self._AssertDependencyIs(deps[2], self._JS_REQUEST.request_id,
                                 self._JS_REQUEST_2.request_id, 'script')

    def testDependencyDifferentFrame(self):
        """Checks that a more recent request from another frame is ignored."""
        loading_trace = test_utils.LoadingTraceFromEvents([
            self._JS_REQUEST, self._JS_REQUEST_OTHER_FRAME, self._JS_REQUEST_2
        ])
        request_dependencies_lens = RequestDependencyLens(loading_trace)
        deps = request_dependencies_lens.GetRequestDependencies()
        self.assertEquals(1, len(deps))
        self._AssertDependencyIs(deps[0], self._JS_REQUEST.request_id,
                                 self._JS_REQUEST_2.request_id, 'script')

    def testDependencySameParentFrame(self):
        """Checks that a more recent request from an unrelated frame is ignored
    if there is one from a related frame."""
        loading_trace = test_utils.LoadingTraceFromEvents([
            self._JS_REQUEST_OTHER_FRAME, self._JS_REQUEST_UNRELATED_FRAME,
            self._JS_REQUEST_2
        ], self._PAGE_EVENTS)
        request_dependencies_lens = RequestDependencyLens(loading_trace)
        deps = request_dependencies_lens.GetRequestDependencies()
        self.assertEquals(1, len(deps))
        self._AssertDependencyIs(deps[0],
                                 self._JS_REQUEST_OTHER_FRAME.request_id,
                                 self._JS_REQUEST_2.request_id, 'script')

    def _AssertDependencyIs(self, dep, first_request_id, second_request_id,
                            reason):
        (first, second, dependency_reason) = dep
        self.assertEquals(reason, dependency_reason)
        self.assertEquals(first_request_id, first.request_id)
        self.assertEquals(second_request_id, second.request_id)
 def testContentType(self):
   r = Request()
   r.response_headers = {}
   self.assertEquals(None, r.GetContentType())
   r.response_headers = {'Content-Type': 'application/javascript'}
   self.assertEquals('application/javascript', r.GetContentType())
   # Case-insensitive match.
   r.response_headers = {'content-type': 'application/javascript'}
   self.assertEquals('application/javascript', r.GetContentType())
   # Parameters are filtered out.
   r.response_headers = {'Content-Type': 'application/javascript;bla'}
   self.assertEquals('application/javascript', r.GetContentType())
   # MIME type takes precedence over 'Content-Type' header.
   r.mime_type = 'image/webp'
   self.assertEquals('image/webp', r.GetContentType())
   r.mime_type = None
   # Test for 'ping' type.
   r.status = 204
   self.assertEquals('ping', r.GetContentType())
   r.status = None
   r.response_headers = {'Content-Type': 'application/javascript',
                         'content-length': '0'}
   self.assertEquals('ping', r.GetContentType())
   # Test for 'redirect' type.
   r.response_headers = {'Content-Type': 'application/javascript',
                         'location': 'http://foo',
                         'content-length': '0'}
   self.assertEquals('redirect', r.GetContentType())
Exemplo n.º 15
0
class TestRequests(object):
    FIRST_REDIRECT_REQUEST = Request.FromJsonDict({
        'url': 'http://bla.com',
        'request_id': '1234.redirect.1',
        'initiator': {
            'type': 'other'
        },
        'timestamp': 0.5,
        'timing': {}
    })
    SECOND_REDIRECT_REQUEST = Request.FromJsonDict({
        'url': 'http://bla.com/redirect1',
        'request_id': '1234.redirect.2',
        'initiator': {
            'type': 'redirect',
            'initiating_request': '1234.redirect.1'
        },
        'timestamp': 1,
        'timing': {}
    })
    REDIRECTED_REQUEST = Request.FromJsonDict({
        'url': 'http://bla.com/index.html',
        'request_id': '1234.1',
        'frame_id': '123.1',
        'initiator': {
            'type': 'redirect',
            'initiating_request': '1234.redirect.2'
        },
        'timestamp': 2,
        'timing': {}
    })
    REQUEST = Request.FromJsonDict({
        'url': 'http://bla.com/index.html',
        'request_id': '1234.1',
        'frame_id': '123.1',
        'initiator': {
            'type': 'other'
        },
        'timestamp': 2,
        'timing': {}
    })
    JS_REQUEST = Request.FromJsonDict({
        'url': 'http://bla.com/nyancat.js',
        'request_id': '1234.12',
        'frame_id': '123.123',
        'initiator': {
            'type': 'parser',
            'url': 'http://bla.com/index.html'
        },
        'timestamp': 3,
        'timing': {}
    })
    JS_REQUEST_OTHER_FRAME = Request.FromJsonDict({
        'url': 'http://bla.com/nyancat.js',
        'request_id': '1234.42',
        'frame_id': '123.13',
        'initiator': {
            'type': 'parser',
            'url': 'http://bla.com/index.html'
        },
        'timestamp': 4,
        'timing': {}
    })
    JS_REQUEST_UNRELATED_FRAME = Request.FromJsonDict({
        'url': 'http://bla.com/nyancat.js',
        'request_id': '1234.56',
        'frame_id': '123.99',
        'initiator': {
            'type': 'parser',
            'url': 'http://bla.com/index.html'
        },
        'timestamp': 5,
        'timing': {}
    })
    JS_REQUEST_2 = Request.FromJsonDict({
        'url': 'http://bla.com/cat.js',
        'request_id': '1234.13',
        'frame_id': '123.123',
        'initiator': {
            'type': 'script',
            'stack': {
                'callFrames': [{
                    'url': 'unknown'
                }, {
                    'url': 'http://bla.com/nyancat.js'
                }]
            }
        },
        'timestamp': 10,
        'timing': {}
    })
    PAGE_EVENTS = [{
        'method': 'Page.frameAttached',
        'frame_id': '123.13',
        'parent_frame_id': '123.1'
    }, {
        'method': 'Page.frameAttached',
        'frame_id': '123.123',
        'parent_frame_id': '123.1'
    }]

    @classmethod
    def CreateLoadingTrace(cls, trace_events=None):
        # This creates a set of requests with the following dependency structure.
        #
        # 1234.redirect.1 -> 1234.redirect.2
        # 1234.redirect.2 -> 1234.1
        # 1234.1 -> 1234.12
        # 1234.1 -> 1234.42
        # 1234.1 -> 1234.56
        # 1234.12 -> 1234.13

        trace = test_utils.LoadingTraceFromEvents([
            cls.FIRST_REDIRECT_REQUEST, cls.SECOND_REDIRECT_REQUEST,
            cls.REDIRECTED_REQUEST, cls.REQUEST, cls.JS_REQUEST,
            cls.JS_REQUEST_2, cls.JS_REQUEST_OTHER_FRAME,
            cls.JS_REQUEST_UNRELATED_FRAME
        ], cls.PAGE_EVENTS, trace_events)
        # Serialize and deserialize so that clients can change events without
        # affecting future tests.
        return LoadingTrace.FromJsonDict(trace.ToJsonDict())
Exemplo n.º 16
0
 def _MakeRequest(cls):
     return Request.FromJsonDict(copy.deepcopy(cls._REQUEST))
Exemplo n.º 17
0
 def testContentType(self):
     r = Request()
     r.response_headers = {}
     self.assertEquals(None, r.GetContentType())
     r.response_headers = {'Content-Type': 'application/javascript'}
     self.assertEquals('application/javascript', r.GetContentType())
     # Case-insensitive match.
     r.response_headers = {'content-type': 'application/javascript'}
     self.assertEquals('application/javascript', r.GetContentType())
     # Parameters are filtered out.
     r.response_headers = {'Content-Type': 'application/javascript;bla'}
     self.assertEquals('application/javascript', r.GetContentType())
     # MIME type takes precedence over 'Content-Type' header.
     r.mime_type = 'image/webp'
     self.assertEquals('image/webp', r.GetContentType())
     r.mime_type = None
     # Test for 'ping' type.
     r.status = 204
     self.assertEquals('ping', r.GetContentType())
     r.status = None
     r.response_headers = {
         'Content-Type': 'application/javascript',
         'content-length': '0'
     }
     self.assertEquals('ping', r.GetContentType())
     # Test for 'redirect' type.
     r.response_headers = {
         'Content-Type': 'application/javascript',
         'location': 'http://foo',
         'content-length': '0'
     }
     self.assertEquals('redirect', r.GetContentType())