def test_request_timeout(full_session): session, build_opener, opener, response = full_session session.request('GET', 'http://foo.bar', timeout=1) args, kwargs = opener.open.call_args assert kwargs['timeout'] == 1
def test_request_response(full_session, monkeypatch): session, build_opener, opener, response = full_session session.request('GET', 'http://foo.bar') args, kwargs = opener.open.call_args response.call_args[1]['request'] = args[0]
def test_request_fallbacks(full_session, monkeypatch): session, build_opener, opener, response = full_session session.params['foo'] = 'bar' session.headers['baz'] = 'qux' calls = [ call(Sentinel, None), # auth call(Sentinel, None), # cert call(Sentinel, session.cookies), # cookies call(Sentinel, None), # unix_socket call(Sentinel, True), # verify ] _fallback = MagicMock(return_value=None) monkeypatch.setattr(session, '_fallback', _fallback) session.request('GET', 'http://foo.bar') _fallback.assert_has_calls(calls) assert _fallback.call_count == 5 args, kwargs = opener.open.call_args request = args[0] assert request.full_url == 'http://foo.bar?foo=bar' assert dict(request.headers) == {'Baz': 'qux'}
def test_request_data_json(full_session): session, build_opener, opener, response = full_session with pytest.raises(TypeError): session.request('GET', 'http://foo.bar', data='foo', json='bar') session.request('GET', 'http://foo.bar')
def test_request_digest(full_session): session, build_opener, opener, response = full_session session.request('GET', 'http://foo.bar', auth=HTTPDigestAuth('foo', 'bar')) args, kwargs = build_opener.call_args h = [h for h in args if isinstance(h, HTTPDigestAuthHandler)] assert len(h) == 1
def test_request_auth(full_session): session, build_opener, opener, response = full_session session.request('GET', 'http://foo.bar', auth=('foo', 'bar')) args, kwargs = opener.open.call_args request = args[0] assert request.headers['Authorization'] == 'Basic Zm9vOmJhcg=='
def test_request_unix_socket(full_session): session, build_opener, opener, response = full_session session.request('GET', 'http://foo.bar', unix_socket='/foo/bar') args, kwargs = build_opener.call_args h = [h for h in args if isinstance(h, UnixHTTPHandler)] assert len(h) == 1 assert h[0]._unix_socket == '/foo/bar'
def test_request_json(full_session): session, build_opener, opener, response = full_session session.request('POST', 'http://foo.bar', json={'foo': 'bar'}) args, kwargs = opener.open.call_args request = args[0] assert request.method == 'POST' assert request.data == b'{"foo": "bar"}' assert request.headers['Content-type'] == 'application/json'
def test_request_json_headers_encode(full_session, mocker): session, build_opener, opener, response = full_session dumps = mocker.patch('json.dumps') session.request( 'POST', 'http://foo.bar', json={'foo': 'bar'}, headers={'content-type': 'application/json; charset="latin1"'}) dumps().encode.assert_called_once_with('latin1')
def test_request_files(full_session): session, build_opener, opener, response = full_session session.request('GET', 'http://foo.bar', files={'file1': { 'content': 'bar' }}) args, kwargs = opener.open.call_args request = args[0] headers = normalize_headers(request.headers) assert headers.get_boundary() is not None assert headers.get_content_type() == 'multipart/form-data'
def test_request_overrides(full_session, monkeypatch): session, build_opener, opener, response = full_session session.params['foo'] = 'bar' session.headers['baz'] = 'qux' cookies = http.cookiejar.CookieJar() calls = [ call(('foo', 'bar'), None), # auth call(('cert', 'key'), None), # cert call(cookies, session.cookies), # cookies call(True, None), # unix_socket call(False, True), # verify ] _fallback = MagicMock(return_value=None) monkeypatch.setattr(session, '_fallback', _fallback) session.request('GET', 'http://foo.bar', auth=('foo', 'bar'), cert=('cert', 'key'), cookies=cookies, unix_socket=True, verify=False, headers={ 'baz': 'foo', 'here': 'i am' }, params={ 'foo': 'baz', 'another': 'one' }) _fallback.assert_has_calls(calls) assert _fallback.call_count == 5 args, kwargs = opener.open.call_args request = args[0] assert request.full_url == 'http://foo.bar?foo=baz&another=one' assert dict(request.headers) == {'Baz': 'foo', 'Here': 'i am'}