Skip to content

Commit

Permalink
fix URL bug
Browse files Browse the repository at this point in the history
  • Loading branch information
jadbin committed May 5, 2017
1 parent 37c454c commit 1cee1dd
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
9 changes: 5 additions & 4 deletions tests/test_downloadermws.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import pytest
from aiohttp import web
from yarl import URL

from xpaw.config import Config
from xpaw.http import HttpRequest, HttpResponse
Expand Down Expand Up @@ -137,10 +138,10 @@ def _retry(request, reason):
mw = RetryMiddleware.from_config(Config())
monkeypatch.setattr(mw, "retry", _retry)
req = HttpRequest("http://www.example.com")
resp = HttpResponse("http://www.example.com", 400)
resp = HttpResponse(URL("http://www.example.com"), 400)
loop.run_until_complete(mw.handle_response(req, resp))
with pytest.raises(ErrorFlag):
resp = HttpResponse("http://www.example.com", 503)
resp = HttpResponse(URL("http://www.example.com"), 503)
loop.run_until_complete(mw.handle_response(req, resp))

def test_handle_error(self, loop, monkeypatch):
Expand Down Expand Up @@ -184,8 +185,8 @@ def test_match_status(self):
class TestResponseMatchMiddleware:
def test_handle_response(self, loop):
req = HttpRequest("http://www.baidu.com")
resp = HttpResponse("http://www.baidu.com", 200, body="<title>百度一下,你就知道</title>".encode("utf-8"))
wrong_resp = HttpResponse("http://www.qq.com", 200, body="<title>腾讯QQ</title>".encode("utf-8"))
resp = HttpResponse(URL("http://www.baidu.com"), 200, body="<title>百度一下,你就知道</title>".encode("utf-8"))
wrong_resp = HttpResponse(URL("http://www.qq.com"), 200, body="<title>腾讯QQ</title>".encode("utf-8"))
mw = ResponseMatchMiddleware.from_config(Config({"response_match": {"url_pattern": "www\\.baidu\\.com",
"body_pattern": "百度",
"encoding": "utf-8"}}))
Expand Down
3 changes: 2 additions & 1 deletion xpaw/downloadermws/retry.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,9 @@ def from_config(cls, config):
c.get("encoding"))

async def handle_response(self, request, response):
req_url = str(request.url)
if response.body:
if self._url_pattern.search(request.url):
if self._url_pattern.search(req_url):
if self._encoding:
text = response.body.decode(self._encoding, errors="replace")
else:
Expand Down

0 comments on commit 1cee1dd

Please sign in to comment.