diff --git a/mocket/mockhttp.py b/mocket/mockhttp.py index abedd698..a25379ec 100644 --- a/mocket/mockhttp.py +++ b/mocket/mockhttp.py @@ -89,11 +89,12 @@ def can_handle(self, data): requestline, _ = decode_utf8(data).split(CRLF, 1) method, path, version = self._parse_requestline(requestline) except ValueError: - Mocket.remove_last_request() - return True + return self == Mocket._last_entry uri = urlsplit(path) kw = dict(keep_blank_values=True) ch = uri.path == self.path and parse_qs(uri.query, **kw) == parse_qs(self.query, **kw) and method == self.method + if ch: + Mocket._last_entry = self return ch @staticmethod diff --git a/test_requirements.txt b/test_requirements.txt index ea86ca8d..721112f4 100644 --- a/test_requirements.txt +++ b/test_requirements.txt @@ -1,4 +1,4 @@ -flake8 +flake8<3.0 pep8 pytest pytest-cov diff --git a/tests/__init__.py b/tests/__init__.py index 0e78c176..340e27b7 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -4,6 +4,8 @@ if PY2: from urllib2 import urlopen, HTTPError + from urllib import urlencode else: from urllib.request import urlopen - from urllib.error import HTTPError \ No newline at end of file + from urllib.parse import urlencode + from urllib.error import HTTPError diff --git a/tests/test_http.py b/tests/test_http.py index 4faa775f..373b4822 100644 --- a/tests/test_http.py +++ b/tests/test_http.py @@ -3,7 +3,7 @@ import time import json import mock -from . import urlopen, HTTPError +from . import urlopen, urlencode, HTTPError import pytest import requests from unittest import TestCase @@ -177,6 +177,18 @@ def test_same_url_different_methods(self): self.assertEquals(methods, methods_from_responses) self.assertEquals(list(range(len(methods))), contents_from_responses) + @mocketize + def test_request_bodies(self): + url = 'http://bit.ly/fakeurl/{0}' + + for e in range(5): + u = url.format(e) + Entry.single_register(Entry.POST, u, body=str(e)) + request_body = urlencode({'key-{0}'.format(e): 'value={0}'.format(e)}) + urlopen(u, request_body.encode('utf-8')) + last_request = Mocket.last_request() + assert last_request.body == request_body + def assertEqualHeaders(self, first, second, msg=None): first = dict((k.lower(), v) for k, v in first.items()) second = dict((k.lower(), v) for k, v in second.items())