|
5 | 5 | import io
|
6 | 6 | import json
|
7 | 7 | import pathlib
|
| 8 | +import socket |
8 | 9 | import ssl
|
9 | 10 | from unittest import mock
|
10 | 11 |
|
|
13 | 14 |
|
14 | 15 | import aiohttp
|
15 | 16 | from aiohttp import ServerFingerprintMismatch, hdrs, web
|
| 17 | +from aiohttp.abc import AbstractResolver |
16 | 18 | from aiohttp.helpers import create_future
|
17 | 19 | from aiohttp.multipart import MultipartWriter
|
18 | 20 |
|
@@ -2237,3 +2239,52 @@ def test_creds_in_auth_and_url(loop):
|
2237 | 2239 | auth=aiohttp.BasicAuth('user2', 'pass2'))
|
2238 | 2240 | finally:
|
2239 | 2241 | yield from session.close()
|
| 2242 | + |
| 2243 | + |
| 2244 | +@asyncio.coroutine |
| 2245 | +def test_drop_auth_on_redirect_to_other_host(test_server, loop): |
| 2246 | + @asyncio.coroutine |
| 2247 | + def srv1(request): |
| 2248 | + assert request.host == 'host1.com' |
| 2249 | + assert request.headers['Authorization'] == 'Basic dXNlcjpwYXNz' |
| 2250 | + raise web.HTTPFound('http://host2.com/path2') |
| 2251 | + |
| 2252 | + @asyncio.coroutine |
| 2253 | + def srv2(request): |
| 2254 | + assert request.host == 'host2.com' |
| 2255 | + assert 'Authorization' not in request.headers |
| 2256 | + return web.Response() |
| 2257 | + |
| 2258 | + app = web.Application() |
| 2259 | + app.router.add_route('GET', '/path1', srv1) |
| 2260 | + app.router.add_route('GET', '/path2', srv2) |
| 2261 | + |
| 2262 | + server = yield from test_server(app) |
| 2263 | + |
| 2264 | + class FakeResolver(AbstractResolver): |
| 2265 | + |
| 2266 | + @asyncio.coroutine |
| 2267 | + def resolve(self, host, port=0, family=socket.AF_INET): |
| 2268 | + return [{'hostname': host, |
| 2269 | + 'host': server.host, |
| 2270 | + 'port': server.port, |
| 2271 | + 'family': socket.AF_INET, |
| 2272 | + 'proto': 0, |
| 2273 | + 'flags': socket.AI_NUMERICHOST}] |
| 2274 | + |
| 2275 | + @asyncio.coroutine |
| 2276 | + def close(self): |
| 2277 | + pass |
| 2278 | + |
| 2279 | + connector = aiohttp.TCPConnector(loop=loop, resolver=FakeResolver()) |
| 2280 | + client = aiohttp.ClientSession(connector=connector) |
| 2281 | + try: |
| 2282 | + resp = yield from client.get('http://host1.com/path1', |
| 2283 | + auth=aiohttp.BasicAuth('user', 'pass')) |
| 2284 | + assert resp.status == 200 |
| 2285 | + resp = yield from client.get('http://host1.com/path1', |
| 2286 | + headers={'Authorization': |
| 2287 | + 'Basic dXNlcjpwYXNz'}) |
| 2288 | + assert resp.status == 200 |
| 2289 | + finally: |
| 2290 | + yield from client.close() |
0 commit comments