diff --git a/httpretty/__init__.py b/httpretty/__init__.py index 4fcf314..61dd537 100644 --- a/httpretty/__init__.py +++ b/httpretty/__init__.py @@ -225,7 +225,9 @@ class fakesock(object): method, path, version = re.split('\s+', verb.strip(), 3) - request = HTTPretty.historify_request(data) + headers, body = data.split('\r\n\r\n') + + request = HTTPretty.historify_request(headers, body) info = URIInfo(hostname=self._host, port=self._port, path=path, last_request=request) @@ -375,6 +377,14 @@ class Entry(object): return r'' % ( self.method, self.uri, self.status) + def normalize_headers(self, headers): + new = {} + for k in headers: + new_k = '-'.join([s.title() for s in k.split('-')]) + new[new_k] = headers[k] + + return new + def fill_filekind(self, fk): now = datetime.utcnow() @@ -391,8 +401,9 @@ class Entry(object): if self.adding_headers: headers.update(self.adding_headers) - status = headers.get('Status', self.status) + headers = self.normalize_headers(headers) + status = headers.get('Status', self.status) string_list = [ 'HTTP/1.1 %d %s' % (status, STATUSES[status]), ] @@ -403,7 +414,9 @@ class Entry(object): if not self.forcing_headers: content_type = headers.pop('Content-Type', 'text/plain; charset=utf-8') - content_length = headers.pop('Content-Length', len(self.body)) + + content_length = headers.pop('Content-Length', self.body_length) + string_list.append( 'Content-Type: %s' % content_type) string_list.append( @@ -418,6 +431,7 @@ class Entry(object): fk.write("\n".join(string_list)) fk.write('\n\r\n') + fk.write(self.body) fk.seek(0) @@ -559,7 +573,7 @@ class HTTPretty(object): headers['body'] = body headers['adding_headers'] = adding_headers headers['forcing_headers'] = forcing_headers - headers['status'] = status + headers['status'] = int(status) return Entry(method=None, uri=None, **headers) @classmethod diff --git a/requirements.pip b/requirements.pip new file mode 100644 index 0000000..e09ad84 --- /dev/null +++ b/requirements.pip @@ -0,0 +1,10 @@ +bolacha>=0.6.0 +coverage>=3.5.3 +distribute>=0.6.30 +httplib2>=0.7.6 +multiprocessing>=2.6.2.1 +nose>=1.2.1 +requests>=0.14.2 +sure>=1.0.6 +tornado>=2.4 +wsgiref>=0.1.2 diff --git a/requirements.txt b/requirements.txt deleted file mode 100644 index b21ec44..0000000 --- a/requirements.txt +++ /dev/null @@ -1,7 +0,0 @@ -nose -sure -bolacha -httplib2 -tornado -multiprocessing -coverage diff --git a/tests/functional/test_httplib2.py b/tests/functional/test_httplib2.py index 8c3dc6c..0e18093 100644 --- a/tests/functional/test_httplib2.py +++ b/tests/functional/test_httplib2.py @@ -55,13 +55,13 @@ def test_httpretty_should_mock_headers_httplib2(now): headers, _ = httplib2.Http().request('http://github.com', 'GET') assert that(headers['status']).equals('201') - assert that(headers).equals({ - 'content-type': 'text/plain', + assert that(dict(headers)).equals({ + 'content-type': 'text/plain; charset=utf-8', 'connection': 'close', 'content-length': '35', 'status': '201', 'server': 'Python/HTTPretty', - 'date': now.strftime('%a, %d %b %Y %H:%M:%SGMT'), + 'date': now.strftime('%a, %d %b %Y %H:%M:%S GMT'), }) @@ -70,7 +70,7 @@ def test_httpretty_should_mock_headers_httplib2(now): def test_httpretty_should_allow_adding_and_overwritting_httplib2(now): u"HTTPretty should allow adding and overwritting headers with httplib2" - HTTPretty.register_uri(HTTPretty.GET, "http://github.com/", + HTTPretty.register_uri(HTTPretty.GET, "http://github.com/foo", body="this is supposed to be the response", adding_headers={ 'Server': 'Apache', @@ -78,11 +78,11 @@ def test_httpretty_should_allow_adding_and_overwritting_httplib2(now): 'Content-Type': 'application/json', }) - headers, _ = httplib2.Http().request('http://github.com', 'GET') + headers, _ = httplib2.Http().request('http://github.com/foo', 'GET') - assert that(headers).equals({ + assert that(dict(headers)).equals({ 'content-type': 'application/json', - 'content-location': 'http://github.com/', + 'content-location': 'http://github.com/foo', 'connection': 'close', 'content-length': '27', 'status': '200', @@ -96,16 +96,16 @@ def test_httpretty_should_allow_adding_and_overwritting_httplib2(now): def test_httpretty_should_allow_forcing_headers_httplib2(now): u"HTTPretty should allow forcing headers with httplib2" - HTTPretty.register_uri(HTTPretty.GET, "http://github.com/", + HTTPretty.register_uri(HTTPretty.GET, "http://github.com/foo", body="this is supposed to be the response", forcing_headers={ 'Content-Type': 'application/xml', }) - headers, _ = httplib2.Http().request('http://github.com', 'GET') + headers, _ = httplib2.Http().request('http://github.com/foo', 'GET') - assert that(headers).equals({ - 'content-location': 'http://github.com/', # httplib2 FORCES + assert that(dict(headers)).equals({ + 'content-location': 'http://github.com/foo', # httplib2 FORCES # content-location # even if the # server does not @@ -121,17 +121,17 @@ def test_httpretty_should_allow_adding_and_overwritting_by_kwargs_u2(now): u"HTTPretty should allow adding and overwritting headers by keyword args " \ "with httplib2" - HTTPretty.register_uri(HTTPretty.GET, "http://github.com/", + HTTPretty.register_uri(HTTPretty.GET, "http://github.com/foo", body="this is supposed to be the response", server='Apache', content_length='27', content_type='application/json') - headers, _ = httplib2.Http().request('http://github.com', 'GET') + headers, _ = httplib2.Http().request('http://github.com/foo', 'GET') - assert that(headers).equals({ + assert that(dict(headers)).equals({ 'content-type': 'application/json', - 'content-location': 'http://github.com/', # httplib2 FORCES + 'content-location': 'http://github.com/foo', # httplib2 FORCES # content-location # even if the # server does not @@ -225,4 +225,3 @@ def test_can_inspect_last_request_with_ssl(now): 'text/json', ) assert that(body).equals('{"repositories": ["HTTPretty", "lettuce"]}') - diff --git a/tests/functional/test_urllib2.py b/tests/functional/test_urllib2.py index aadf120..b1d049b 100644 --- a/tests/functional/test_urllib2.py +++ b/tests/functional/test_urllib2.py @@ -54,15 +54,16 @@ def test_httpretty_should_mock_headers_urllib2(now): status=201) request = urllib2.urlopen('http://github.com') + headers = dict(request.headers) request.close() assert that(request.code).equals(201) assert that(headers).equals({ - 'content-type': 'text/plain', + 'content-type': 'text/plain; charset=utf-8', 'connection': 'close', 'content-length': '35', - 'status': '201 Created', + 'status': '201', 'server': 'Python/HTTPretty', 'date': now.strftime('%a, %d %b %Y %H:%M:%S GMT'), }) @@ -90,7 +91,7 @@ def test_httpretty_should_allow_adding_and_overwritting_urllib2(now): 'content-type': 'application/json', 'connection': 'close', 'content-length': '27', - 'status': '200 OK', + 'status': '200', 'server': 'Apache', 'date': now.strftime('%a, %d %b %Y %H:%M:%S GMT'), }) @@ -123,9 +124,9 @@ def test_httpretty_should_allow_adding_and_overwritting_by_kwargs_u2(now): "keyword args with urllib2" HTTPretty.register_uri(HTTPretty.GET, "http://github.com/", - body="this is supposed to be the response", + body="this is supposed to be the response, indeed", server='Apache', - content_length='23456789', + content_length='111111', content_type='application/json') request = urllib2.urlopen('http://github.com') @@ -136,8 +137,8 @@ def test_httpretty_should_allow_adding_and_overwritting_by_kwargs_u2(now): assert that(headers).equals({ 'content-type': 'application/json', 'connection': 'close', - 'content-length': '23456789', - 'status': '200 OK', + 'content-length': '111111', + 'status': '200', 'server': 'Apache', 'date': now.strftime('%a, %d %b %Y %H:%M:%S GMT'), })