mirror of https://github.com/scrapy/scrapy.git
https proxy tunneling - add a test (not perfect, but covers all impl) and fix for py3
This commit is contained in:
parent
98c060d0b2
commit
0f527849f2
|
|
@ -78,7 +78,7 @@ class TunnelingTCP4ClientEndpoint(TCP4ClientEndpoint):
|
|||
for it.
|
||||
"""
|
||||
|
||||
_responseMatcher = re.compile('HTTP/1\.. 200')
|
||||
_responseMatcher = re.compile(b'HTTP/1\.. 200')
|
||||
|
||||
def __init__(self, reactor, host, port, proxyConf, contextFactory,
|
||||
timeout=30, bindAddress=None):
|
||||
|
|
@ -92,11 +92,15 @@ class TunnelingTCP4ClientEndpoint(TCP4ClientEndpoint):
|
|||
|
||||
def requestTunnel(self, protocol):
|
||||
"""Asks the proxy to open a tunnel."""
|
||||
tunnelReq = 'CONNECT %s:%s HTTP/1.1\r\n' % (self._tunneledHost,
|
||||
self._tunneledPort)
|
||||
tunnelReq = (
|
||||
b'CONNECT ' +
|
||||
to_bytes(self._tunneledHost, encoding='ascii') + b':' +
|
||||
to_bytes(str(self._tunneledPort)) +
|
||||
b' HTTP/1.1\r\n')
|
||||
if self._proxyAuthHeader:
|
||||
tunnelReq += 'Proxy-Authorization: %s\r\n' % self._proxyAuthHeader
|
||||
tunnelReq += '\r\n'
|
||||
tunnelReq += \
|
||||
b'Proxy-Authorization: ' + self._proxyAuthHeader + b'\r\n'
|
||||
tunnelReq += b'\r\n'
|
||||
protocol.transport.write(tunnelReq)
|
||||
self._protocolDataReceived = protocol.dataReceived
|
||||
protocol.dataReceived = self.processProxyResponse
|
||||
|
|
|
|||
|
|
@ -388,6 +388,16 @@ class HttpProxyTestCase(unittest.TestCase):
|
|||
request = Request('https://example.com', meta={'proxy': http_proxy})
|
||||
return self.download_request(request, Spider('foo')).addCallback(_test)
|
||||
|
||||
@defer.inlineCallbacks
|
||||
def test_download_with_proxy_https_timeout(self):
|
||||
http_proxy = self.getURL('')
|
||||
domain = 'https://no-such-domain.nosuch'
|
||||
request = Request(
|
||||
domain, meta={'proxy': http_proxy, 'download_timeout': 0.2})
|
||||
d = self.download_request(request, Spider('foo'))
|
||||
timeout = yield self.assertFailure(d, error.TimeoutError)
|
||||
self.assertIn(domain, timeout.osError)
|
||||
|
||||
def test_download_without_proxy(self):
|
||||
def _test(response):
|
||||
self.assertEquals(response.status, 200)
|
||||
|
|
|
|||
Loading…
Reference in New Issue