diff options
Diffstat (limited to 'tests/functional/test_httplib2.py')
| -rw-r--r-- | tests/functional/test_httplib2.py | 309 |
1 files changed, 163 insertions, 146 deletions
diff --git a/tests/functional/test_httplib2.py b/tests/functional/test_httplib2.py index 9aa6a5b..8d4f947 100644 --- a/tests/functional/test_httplib2.py +++ b/tests/functional/test_httplib2.py @@ -38,14 +38,15 @@ from httpretty.core import decode_utf8 def test_httpretty_should_mock_a_simple_get_with_httplib2_read(now): "HTTPretty should mock a simple GET with httplib2.context.http" - HTTPretty.register_uri(HTTPretty.GET, "http://yipit.com/", - body="Find the best daily deals") + HTTPretty.register_uri( + HTTPretty.GET, "http://yipit.com/", body="Find the best daily deals" + ) - _, got = httplib2.Http().request('http://yipit.com', 'GET') - expect(got).to.equal(b'Find the best daily deals') + _, got = httplib2.Http().request("http://yipit.com", "GET") + expect(got).to.equal(b"Find the best daily deals") - expect(HTTPretty.last_request.method).to.equal('GET') - expect(HTTPretty.last_request.path).to.equal('/') + expect(HTTPretty.last_request.method).to.equal("GET") + expect(HTTPretty.last_request.path).to.equal("/") @httprettified @@ -53,14 +54,14 @@ def test_httpretty_should_mock_a_simple_get_with_httplib2_read(now): def test_httpretty_provides_easy_access_to_querystrings(now): "HTTPretty should provide an easy access to the querystring" - HTTPretty.register_uri(HTTPretty.GET, "http://yipit.com/", - body="Find the best daily deals") + HTTPretty.register_uri( + HTTPretty.GET, "http://yipit.com/", body="Find the best daily deals" + ) - httplib2.Http().request('http://yipit.com?foo=bar&foo=baz&chuck=norris', 'GET') - expect(HTTPretty.last_request.querystring).to.equal({ - 'foo': ['bar', 'baz'], - 'chuck': ['norris'], - }) + httplib2.Http().request("http://yipit.com?foo=bar&foo=baz&chuck=norris", "GET") + expect(HTTPretty.last_request.querystring).to.equal( + {"foo": ["bar", "baz"], "chuck": ["norris"]} + ) @httprettified @@ -68,20 +69,25 @@ def test_httpretty_provides_easy_access_to_querystrings(now): def test_httpretty_should_mock_headers_httplib2(now): "HTTPretty should mock basic headers with httplib2" - HTTPretty.register_uri(HTTPretty.GET, "http://github.com/", - body="this is supposed to be the response", - status=201) + HTTPretty.register_uri( + HTTPretty.GET, + "http://github.com/", + body="this is supposed to be the response", + status=201, + ) - headers, _ = httplib2.Http().request('http://github.com', 'GET') - expect(headers['status']).to.equal('201') - expect(dict(headers)).to.equal({ - '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:%S GMT'), - }) + headers, _ = httplib2.Http().request("http://github.com", "GET") + expect(headers["status"]).to.equal("201") + expect(dict(headers)).to.equal( + { + "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:%S GMT"), + } + ) @httprettified @@ -89,25 +95,30 @@ def test_httpretty_should_mock_headers_httplib2(now): def test_httpretty_should_allow_adding_and_overwritting_httplib2(now): "HTTPretty should allow adding and overwritting headers with httplib2" - HTTPretty.register_uri(HTTPretty.GET, "http://github.com/foo", - body="this is supposed to be the response", - adding_headers={ - 'Server': 'Apache', - 'Content-Length': '27', - 'Content-Type': 'application/json', - }) - - headers, _ = httplib2.Http().request('http://github.com/foo', 'GET') + HTTPretty.register_uri( + HTTPretty.GET, + "http://github.com/foo", + body="this is supposed to be the response", + adding_headers={ + "Server": "Apache", + "Content-Length": "27", + "Content-Type": "application/json", + }, + ) - expect(dict(headers)).to.equal({ - 'content-type': 'application/json', - 'content-location': 'http://github.com/foo', - 'connection': 'close', - 'content-length': '27', - 'status': '200', - 'server': 'Apache', - 'date': now.strftime('%a, %d %b %Y %H:%M:%S GMT'), - }) + headers, _ = httplib2.Http().request("http://github.com/foo", "GET") + + expect(dict(headers)).to.equal( + { + "content-type": "application/json", + "content-location": "http://github.com/foo", + "connection": "close", + "content-length": "27", + "status": "200", + "server": "Apache", + "date": now.strftime("%a, %d %b %Y %H:%M:%S GMT"), + } + ) @httprettified @@ -115,52 +126,59 @@ def test_httpretty_should_allow_adding_and_overwritting_httplib2(now): def test_httpretty_should_allow_forcing_headers_httplib2(now): "HTTPretty should allow forcing headers with httplib2" - 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/foo', 'GET') + HTTPretty.register_uri( + HTTPretty.GET, + "http://github.com/foo", + body="this is supposed to be the response", + forcing_headers={"Content-Type": "application/xml"}, + ) - expect(dict(headers)).to.equal({ - 'content-location': 'http://github.com/foo', # httplib2 FORCES - # content-location - # even if the - # server does not - # provide it - 'content-type': 'application/xml', - 'status': '200', # httplib2 also ALWAYS put status on headers - }) + headers, _ = httplib2.Http().request("http://github.com/foo", "GET") + + expect(dict(headers)).to.equal( + { + "content-location": "http://github.com/foo", # httplib2 FORCES + # content-location + # even if the + # server does not + # provide it + "content-type": "application/xml", + "status": "200", # httplib2 also ALWAYS put status on headers + } + ) @httprettified @within(two=microseconds) def test_httpretty_should_allow_adding_and_overwritting_by_kwargs_u2(now): - "HTTPretty should allow adding and overwritting headers by keyword args " \ - "with httplib2" - - 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/foo', 'GET') - - expect(dict(headers)).to.equal({ - 'content-type': 'application/json', - 'content-location': 'http://github.com/foo', # httplib2 FORCES - # content-location - # even if the - # server does not - # provide it - 'connection': 'close', - 'content-length': '27', - 'status': '200', - 'server': 'Apache', - 'date': now.strftime('%a, %d %b %Y %H:%M:%S GMT'), - }) + "HTTPretty should allow adding and overwritting headers by keyword args " "with httplib2" + + 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/foo", "GET") + + expect(dict(headers)).to.equal( + { + "content-type": "application/json", + "content-location": "http://github.com/foo", # httplib2 FORCES + # content-location + # even if the + # server does not + # provide it + "connection": "close", + "content-length": "27", + "status": "200", + "server": "Apache", + "date": now.strftime("%a, %d %b %Y %H:%M:%S GMT"), + } + ) @httprettified @@ -169,29 +187,28 @@ def test_rotating_responses_with_httplib2(now): "HTTPretty should support rotating responses with httplib2" HTTPretty.register_uri( - HTTPretty.GET, "https://api.yahoo.com/test", + HTTPretty.GET, + "https://api.yahoo.com/test", responses=[ HTTPretty.Response(body="first response", status=201), - HTTPretty.Response(body='second and last response', status=202), - ]) + HTTPretty.Response(body="second and last response", status=202), + ], + ) - headers1, body1 = httplib2.Http().request( - 'https://api.yahoo.com/test', 'GET') + headers1, body1 = httplib2.Http().request("https://api.yahoo.com/test", "GET") - expect(headers1['status']).to.equal('201') - expect(body1).to.equal(b'first response') + expect(headers1["status"]).to.equal("201") + expect(body1).to.equal(b"first response") - headers2, body2 = httplib2.Http().request( - 'https://api.yahoo.com/test', 'GET') + headers2, body2 = httplib2.Http().request("https://api.yahoo.com/test", "GET") - expect(headers2['status']).to.equal('202') - expect(body2).to.equal(b'second and last response') + expect(headers2["status"]).to.equal("202") + expect(body2).to.equal(b"second and last response") - headers3, body3 = httplib2.Http().request( - 'https://api.yahoo.com/test', 'GET') + headers3, body3 = httplib2.Http().request("https://api.yahoo.com/test", "GET") - expect(headers3['status']).to.equal('202') - expect(body3).to.equal(b'second and last response') + expect(headers3["status"]).to.equal("202") + expect(body3).to.equal(b"second and last response") @httprettified @@ -199,24 +216,22 @@ def test_rotating_responses_with_httplib2(now): def test_can_inspect_last_request(now): "HTTPretty.last_request is a mimetools.Message request from last match" - HTTPretty.register_uri(HTTPretty.POST, "http://api.github.com/", - body='{"repositories": ["HTTPretty", "lettuce"]}') + HTTPretty.register_uri( + HTTPretty.POST, + "http://api.github.com/", + body='{"repositories": ["HTTPretty", "lettuce"]}', + ) headers, body = httplib2.Http().request( - 'http://api.github.com', 'POST', + "http://api.github.com", + "POST", body='{"username": "gabrielfalcao"}', - headers={ - 'content-type': 'text/json', - }, + headers={"content-type": "text/json"}, ) - expect(HTTPretty.last_request.method).to.equal('POST') - expect(HTTPretty.last_request.body).to.equal( - b'{"username": "gabrielfalcao"}', - ) - expect(HTTPretty.last_request.headers['content-type']).to.equal( - 'text/json', - ) + expect(HTTPretty.last_request.method).to.equal("POST") + expect(HTTPretty.last_request.body).to.equal(b'{"username": "gabrielfalcao"}') + expect(HTTPretty.last_request.headers["content-type"]).to.equal("text/json") expect(body).to.equal(b'{"repositories": ["HTTPretty", "lettuce"]}') @@ -225,24 +240,22 @@ def test_can_inspect_last_request(now): def test_can_inspect_last_request_with_ssl(now): "HTTPretty.last_request is recorded even when mocking 'https' (SSL)" - HTTPretty.register_uri(HTTPretty.POST, "https://secure.github.com/", - body='{"repositories": ["HTTPretty", "lettuce"]}') + HTTPretty.register_uri( + HTTPretty.POST, + "https://secure.github.com/", + body='{"repositories": ["HTTPretty", "lettuce"]}', + ) headers, body = httplib2.Http().request( - 'https://secure.github.com', 'POST', + "https://secure.github.com", + "POST", body='{"username": "gabrielfalcao"}', - headers={ - 'content-type': 'text/json', - }, + headers={"content-type": "text/json"}, ) - expect(HTTPretty.last_request.method).to.equal('POST') - expect(HTTPretty.last_request.body).to.equal( - b'{"username": "gabrielfalcao"}', - ) - expect(HTTPretty.last_request.headers['content-type']).to.equal( - 'text/json', - ) + expect(HTTPretty.last_request.method).to.equal("POST") + expect(HTTPretty.last_request.body).to.equal(b'{"username": "gabrielfalcao"}') + expect(HTTPretty.last_request.headers["content-type"]).to.equal("text/json") expect(body).to.equal(b'{"repositories": ["HTTPretty", "lettuce"]}') @@ -251,40 +264,42 @@ def test_can_inspect_last_request_with_ssl(now): def test_httpretty_ignores_querystrings_from_registered_uri(now): "Registering URIs with query string cause them to be ignored" - HTTPretty.register_uri(HTTPretty.GET, "http://yipit.com/?id=123", - body="Find the best daily deals") + HTTPretty.register_uri( + HTTPretty.GET, "http://yipit.com/?id=123", body="Find the best daily deals" + ) - _, got = httplib2.Http().request('http://yipit.com/?id=123', 'GET') + _, got = httplib2.Http().request("http://yipit.com/?id=123", "GET") - expect(got).to.equal(b'Find the best daily deals') - expect(HTTPretty.last_request.method).to.equal('GET') - expect(HTTPretty.last_request.path).to.equal('/?id=123') + expect(got).to.equal(b"Find the best daily deals") + expect(HTTPretty.last_request.method).to.equal("GET") + expect(HTTPretty.last_request.path).to.equal("/?id=123") @httprettified @within(two=microseconds) def test_callback_response(now): - ("HTTPretty should all a callback function to be set as the body with" - " httplib2") + ("HTTPretty should all a callback function to be set as the body with" " httplib2") def request_callback(request, uri, headers): - return [200, headers, "The {} response from {}".format(decode_utf8(request.method), uri)] + return [ + 200, + headers, + "The {} response from {}".format(decode_utf8(request.method), uri), + ] HTTPretty.register_uri( - HTTPretty.GET, "https://api.yahoo.com/test", - body=request_callback) + HTTPretty.GET, "https://api.yahoo.com/test", body=request_callback + ) - headers1, body1 = httplib2.Http().request( - 'https://api.yahoo.com/test', 'GET') + headers1, body1 = httplib2.Http().request("https://api.yahoo.com/test", "GET") expect(body1).to.equal(b"The GET response from https://api.yahoo.com/test") HTTPretty.register_uri( - HTTPretty.POST, "https://api.yahoo.com/test_post", - body=request_callback) + HTTPretty.POST, "https://api.yahoo.com/test_post", body=request_callback + ) - headers2, body2 = httplib2.Http().request( - 'https://api.yahoo.com/test_post', 'POST') + headers2, body2 = httplib2.Http().request("https://api.yahoo.com/test_post", "POST") expect(body2).to.equal(b"The POST response from https://api.yahoo.com/test_post") @@ -299,7 +314,9 @@ def test_httpretty_should_allow_registering_regexes(): body="Found brand", ) - response, body = httplib2.Http().request('https://api.yipit.com/v1/deal;brand=gap', 'GET') - expect(body).to.equal(b'Found brand') - expect(HTTPretty.last_request.method).to.equal('GET') - expect(HTTPretty.last_request.path).to.equal('/v1/deal;brand=gap') + response, body = httplib2.Http().request( + "https://api.yipit.com/v1/deal;brand=gap", "GET" + ) + expect(body).to.equal(b"Found brand") + expect(HTTPretty.last_request.method).to.equal("GET") + expect(HTTPretty.last_request.path).to.equal("/v1/deal;brand=gap") |
