bugfix in request_httprepr() function

This commit is contained in:
Pablo Hoffman 2010-07-15 12:04:55 -03:00
parent ec850b9fd1
commit b8aa74ee9e
2 changed files with 10 additions and 5 deletions

View File

@ -63,10 +63,13 @@ class UtilsRequestTest(unittest.TestCase):
def test_request_httprepr(self):
r1 = Request("http://www.example.com")
self.assertEqual(request_httprepr(r1), 'GET http://www.example.com HTTP/1.1\r\nHost: www.example.com\r\n\r\n')
self.assertEqual(request_httprepr(r1), 'GET / HTTP/1.1\r\nHost: www.example.com\r\n\r\n')
r1 = Request("http://www.example.com/some/page.html?arg=1")
self.assertEqual(request_httprepr(r1), 'GET /some/page.html?arg=1 HTTP/1.1\r\nHost: www.example.com\r\n\r\n')
r1 = Request("http://www.example.com", method='POST', headers={"Content-type": "text/html"}, body="Some body")
self.assertEqual(request_httprepr(r1), 'POST http://www.example.com HTTP/1.1\r\nHost: www.example.com\r\nContent-Type: text/html\r\n\r\nSome body')
self.assertEqual(request_httprepr(r1), 'POST / HTTP/1.1\r\nHost: www.example.com\r\nContent-Type: text/html\r\n\r\nSome body')
if __name__ == "__main__":
unittest.main()

View File

@ -6,6 +6,7 @@ scrapy.http.Request objects
import hashlib
import weakref
from base64 import urlsafe_b64encode
from urlparse import urlunparse
from scrapy.utils.url import canonicalize_url
from scrapy.utils.httpobj import urlparse_cached
@ -76,9 +77,10 @@ def request_httprepr(request):
bytes that will be send when performing the request (that's controlled
by Twisted).
"""
hostname = urlparse_cached(request).hostname
s = "%s %s HTTP/1.1\r\n" % (request.method, request.url)
s += "Host: %s\r\n" % hostname
parsed = urlparse_cached(request)
path = urlunparse(('', '', parsed.path or '/', parsed.params, parsed.query, ''))
s = "%s %s HTTP/1.1\r\n" % (request.method, path)
s += "Host: %s\r\n" % parsed.hostname
if request.headers:
s += request.headers.to_string() + "\r\n"
s += "\r\n"