def test_redirect_to_same_host(self): finished = self.fetcher.fetch_title( 'http://foo.test/', hostname_tag=True) request, result = self.protocol.requests.pop() redirect_headers = Headers() redirect_headers.addRawHeader('Location', 'http://foo.test/bar') response = Response._construct( ('HTTP', 1, 1), 301, 'Moved Permanently', redirect_headers, AbortableStringTransport(), request) result.callback(response) request, result = self.protocol.requests.pop() response = Response._construct(('HTTP', 1, 1), 200, 'OK', Headers(), AbortableStringTransport(), request) result.callback(response) finished.addCallback(self.assertEqual, u'[foo.test] Unknown document') return finished
def test_redirect_to_same_host(self): finished = self.fetcher.fetch_title('http://foo.test/', hostname_tag=True) request, result = self.protocol.requests.pop() redirect_headers = Headers() redirect_headers.addRawHeader('Location', 'http://foo.test/bar') response = Response._construct(('HTTP', 1, 1), 301, 'Moved Permanently', redirect_headers, AbortableStringTransport(), request) result.callback(response) request, result = self.protocol.requests.pop() response = Response._construct(('HTTP', 1, 1), 200, 'OK', Headers(), AbortableStringTransport(), request) result.callback(response) finished.addCallback(self.assertEqual, u'[foo.test] Unknown document') return finished
def test_no_redirect(self): finished = self.fetcher.fetch_title('http://foo.test/', hostname_tag=True) request, result = self.protocol.requests.pop() response = Response._construct(('HTTP', 1, 1), 200, 'OK', Headers(), AbortableStringTransport(), request) result.callback(response) finished.addCallback(self.assertEqual, u'[foo.test] Unknown document') return finished
def test_no_redirect(self): finished = self.fetcher.fetch_title( 'http://foo.test/', hostname_tag=True) request, result = self.protocol.requests.pop() response = Response._construct(('HTTP', 1, 1), 200, 'OK', Headers(), AbortableStringTransport(), request) result.callback(response) finished.addCallback(self.assertEqual, u'[foo.test] Unknown document') return finished
def test_soft_redirect(self): self.fetcher.extractors = { 'text/html': SoftRedirectExtractor(u'http://bar.test/')} finished = self.fetcher.fetch_title( 'http://foo.test/', hostname_tag=True) request, result = self.protocol.requests.pop() redirect_headers = Headers() redirect_headers.addRawHeader('Content-Type', 'text/html') response = Response._construct( ('HTTP', 1, 1), 200, 'OK', redirect_headers, AbortableStringTransport(), request) result.callback(response) request, result = self.protocol.requests.pop() response = Response._construct(('HTTP', 1, 1), 200, 'OK', Headers(), AbortableStringTransport(), request) result.callback(response) finished.addCallback(self.assertEqual, u'[foo.test → bar.test] Unknown document') return finished
def test_tag_iri_to_uri(self): finished = self.fetcher.fetch_title(u'http://ドメイン名例.test/', hostname_tag=True) request, result = self.protocol.requests.pop() response = Response._construct(('HTTP', 1, 1), 200, 'OK', Headers(), AbortableStringTransport(), request) result.callback(response) finished.addCallback( self.assertEqual, u'[ドメイン名例.test → xn--eckwd4c7cu47r2wf.test] ' u'Unknown document') return finished
def test_soft_redirect(self): self.fetcher.extractors = { 'text/html': SoftRedirectExtractor(u'http://bar.test/') } finished = self.fetcher.fetch_title('http://foo.test/', hostname_tag=True) request, result = self.protocol.requests.pop() redirect_headers = Headers() redirect_headers.addRawHeader('Content-Type', 'text/html') response = Response._construct(('HTTP', 1, 1), 200, 'OK', redirect_headers, AbortableStringTransport(), request) result.callback(response) request, result = self.protocol.requests.pop() response = Response._construct(('HTTP', 1, 1), 200, 'OK', Headers(), AbortableStringTransport(), request) result.callback(response) finished.addCallback(self.assertEqual, u'[foo.test → bar.test] Unknown document') return finished
def test_tag_iri_to_uri(self): finished = self.fetcher.fetch_title( u'http://ドメイン名例.test/', hostname_tag=True) request, result = self.protocol.requests.pop() response = Response._construct(('HTTP', 1, 1), 200, 'OK', Headers(), AbortableStringTransport(), request) result.callback(response) finished.addCallback(self.assertEqual, u'[ドメイン名例.test → xn--eckwd4c7cu47r2wf.test] ' u'Unknown document') return finished
def test_infinite_hard_redirection(self): finished = self.fetcher.fetch_title( u'http://foo.test/', hostname_tag=True, friendly_errors=True) while self.protocol.requests: request, result = self.protocol.requests.pop() redirect_headers = Headers() redirect_headers.addRawHeader('Location', 'http://foo.test/') response = Response._construct( ('HTTP', 1, 1), 301, 'Moved Permanently', redirect_headers, AbortableStringTransport(), request) result.callback(response) finished.addCallback(self.assertEqual, u'[foo.test] Encountered too many redirects.') return finished
def test_infinite_hard_redirection(self): finished = self.fetcher.fetch_title(u'http://foo.test/', hostname_tag=True, friendly_errors=True) while self.protocol.requests: request, result = self.protocol.requests.pop() redirect_headers = Headers() redirect_headers.addRawHeader('Location', 'http://foo.test/') response = Response._construct( ('HTTP', 1, 1), 301, 'Moved Permanently', redirect_headers, AbortableStringTransport(), request) result.callback(response) finished.addCallback(self.assertEqual, u'[foo.test] Encountered too many redirects.') return finished
def test_content_length_unknown(self): agent = CassetteAgent(self.agent, '') finished = agent.request('GET', 'http://foo.test/') request, result = self.protocol.requests.pop() response = Response._construct(('HTTP', 1, 1), 200, 'OK', Headers(), AbortableStringTransport(), request) response._bodyDataFinished() result.callback(response) finished.addCallback(readBody) def assert_length_absent(deferred_result): interaction = agent.cassette.as_dict()['http_interactions'][0] self.assertFalse( interaction['response']['headers'].get('Content-Length')) return deferred_result finished.addCallback(assert_length_absent) return finished
def test_infinite_soft_redirection(self): self.fetcher.extractors = { 'text/html': SoftRedirectExtractor(u'http://foo.test/')} finished = self.fetcher.fetch_title( u'http://foo.test/', hostname_tag=True, friendly_errors=True) while self.protocol.requests: request, result = self.protocol.requests.pop() redirect_headers = Headers() redirect_headers.addRawHeader('Content-Type', 'text/html') response = Response._construct( ('HTTP', 1, 1), 200, 'OK', redirect_headers, AbortableStringTransport(), request) result.callback(response) finished.addCallback(self.assertEqual, u'[foo.test] Encountered too many redirects.') return finished
def test_infinite_soft_redirection(self): self.fetcher.extractors = { 'text/html': SoftRedirectExtractor(u'http://foo.test/') } finished = self.fetcher.fetch_title(u'http://foo.test/', hostname_tag=True, friendly_errors=True) while self.protocol.requests: request, result = self.protocol.requests.pop() redirect_headers = Headers() redirect_headers.addRawHeader('Content-Type', 'text/html') response = Response._construct(('HTTP', 1, 1), 200, 'OK', redirect_headers, AbortableStringTransport(), request) result.callback(response) finished.addCallback(self.assertEqual, u'[foo.test] Encountered too many redirects.') return finished
def test_header_isolation(self): agent = CassetteAgent(self.agent, '') finished = agent.request('GET', 'http://foo.test/') request, result = self.protocol.requests.pop() headers = Headers() headers.addRawHeader('Content-Encoding', 'gzip') response = Response._construct(('HTTP', 1, 1), 200, 'OK', headers, AbortableStringTransport(), request) response._bodyDataFinished() result.callback(response) @inlineCallbacks def assert_intact_headers(agent_response): yield readBody(agent_response) agent_response.headers.removeHeader('Content-Encoding') interaction = agent.cassette.as_dict()['http_interactions'][0] self.assertEqual( ['gzip'], interaction['response']['headers']['Content-Encoding']) returnValue(agent_response) finished.addCallback(assert_intact_headers) return finished