mirror of
https://github.com/kevin1024/vcrpy.git
synced 2025-12-09 01:03:24 +00:00
There is a weird quirk in HTTP. You can send the same header twice. For this reason, headers are represented by a dict, with lists as the values. However, it appears that HTTPlib is completely incapable of sending the same header twice. This puts me in a weird position: I want to be able to accurately represent HTTP headers in cassettes, but I don't want the extra step of always having to do [0] in the general case, i.e. request.headers['key'][0] In addition, some servers sometimes send the same header more than once, and httplib *can* deal with this situation. Futhermore, I wanted to keep the request and response cassette format as similar as possible. For this reason, in cassettes I keep a dict with lists as keys, but once deserialized into VCR, I keep them as plain, naked dicts.
39 lines
1.0 KiB
Python
39 lines
1.0 KiB
Python
import pytest
|
|
|
|
from vcr.request import Request
|
|
|
|
|
|
def test_str():
|
|
req = Request('GET', 'http://www.google.com/', '', {})
|
|
str(req) == '<Request (GET) http://www.google.com/>'
|
|
|
|
|
|
def test_headers():
|
|
headers = {'X-Header1': ['h1'], 'X-Header2': 'h2'}
|
|
req = Request('GET', 'http://go.com/', '', headers)
|
|
assert req.headers == {'X-Header1': 'h1', 'X-Header2': 'h2'}
|
|
|
|
req.add_header('X-Header1', 'h11')
|
|
assert req.headers == {'X-Header1': 'h11', 'X-Header2': 'h2'}
|
|
|
|
|
|
@pytest.mark.parametrize("uri, expected_port", [
|
|
('http://go.com/', 80),
|
|
('http://go.com:80/', 80),
|
|
('http://go.com:3000/', 3000),
|
|
('https://go.com/', 433),
|
|
('https://go.com:433/', 433),
|
|
('https://go.com:3000/', 3000),
|
|
])
|
|
def test_port(uri, expected_port):
|
|
req = Request('GET', uri, '', {})
|
|
assert req.port == expected_port
|
|
|
|
|
|
def test_uri():
|
|
req = Request('GET', 'http://go.com/', '', {})
|
|
assert req.uri == 'http://go.com/'
|
|
|
|
req = Request('GET', 'http://go.com:80/', '', {})
|
|
assert req.uri == 'http://go.com:80/'
|