def test_http_errors(monkeypatch): resp = MagicMock() resp.status_code = 404 resp.raise_for_status.side_effect = requests.HTTPError('Not Found') get = MagicMock() get.return_value = resp monkeypatch.setattr('requests.get', get) http = HttpWrapper('http://example.org') # the code method will not raise an exception.. assert 404 == http.code() for meth in ('time', 'json', 'cookies', 'headers'): with pytest.raises(HttpError) as ex: # ..but other methods will! getattr(http, meth)() assert 'Not Found' == ex.value.message get.side_effect = requests.Timeout('timed out') http = HttpWrapper('http://example.org') with pytest.raises(HttpError) as ex: http.time() assert 'timeout' == ex.value.message get.side_effect = requests.ConnectionError('connfail') http = HttpWrapper('http://example.org') with pytest.raises(HttpError) as ex: http.code() assert 'connection failed' == ex.value.message get.side_effect = Exception('foofail') http = HttpWrapper('http://example.org') with pytest.raises(HttpError) as ex: http.code() assert 'foofail' == ex.value.message
def test_http_errors(monkeypatch): resp = MagicMock() resp.status_code = 404 resp.raise_for_status.side_effect = requests.HTTPError("Not Found") get = MagicMock() get.return_value = resp monkeypatch.setattr("requests.get", get) http = HttpWrapper("http://example.org") # the code method will not raise an exception.. assert 404 == http.code() for meth in ("time", "json", "cookies", "headers"): with pytest.raises(HttpError) as ex: # ..but other methods will! getattr(http, meth)() assert "Not Found" == ex.value.message get.side_effect = requests.Timeout("timed out") http = HttpWrapper("http://example.org") with pytest.raises(HttpError) as ex: http.time() assert "timeout" == ex.value.message get.side_effect = requests.ConnectionError("connfail") http = HttpWrapper("http://example.org") with pytest.raises(HttpError) as ex: http.code() assert "connection failed" == ex.value.message get.side_effect = Exception("foofail") http = HttpWrapper("http://example.org") with pytest.raises(HttpError) as ex: http.code() assert "foofail" == ex.value.message
def test_http_redirects(monkeypatch, fx_redirects): kwargs, code = fx_redirects exp_allow_redirects = False if 'allow_redirects' not in kwargs else kwargs[ 'allow_redirects'] resp = MagicMock() resp.status_code = code resp.text = '' redirect_url = 'http://example.org/some-file' resp.headers = {'Location': redirect_url} method = MagicMock() method.return_value = resp patch = 'requests.{}'.format(kwargs['method'].lower()) monkeypatch.setattr(patch, method) http = HttpWrapper('http://example.org', **kwargs) assert code == http.code() assert '' == http.text() assert redirect_url == http.headers()['Location'] method.assert_called_once_with('http://example.org', auth=None, headers={'User-Agent': get_user_agent()}, params=None, timeout=10, verify=True, allow_redirects=exp_allow_redirects)
def test_http(monkeypatch): resp = MagicMock() resp.status_code = 200 resp.text = '"foo"' resp.content = resp.text resp.json.return_value = 'foo' get = MagicMock() get.return_value = resp monkeypatch.setattr('requests.get', get) http = HttpWrapper('http://example.org') assert 200 == http.code() assert '"foo"' == http.text() assert 'foo' == http.json() assert 5 == http.content_size() get.assert_called_once_with('http://example.org', auth=None, headers={'User-Agent': get_user_agent()}, params=None, timeout=10, verify=True, allow_redirects=True) resp.json.side_effect = Exception('JSON fail') with pytest.raises(HttpError) as ex: http.json() assert 'JSON fail' == ex.value.message
def test_oauth2(monkeypatch): resp = MagicMock() resp.status_code = 218 resp.text = 'OK' get = MagicMock() get.return_value = resp monkeypatch.setattr('requests.get', get) monkeypatch.setattr('tokens.get', lambda x: 'mytok' if x is 'uid' else 'myothertok') http = HttpWrapper('http://example.org', oauth2=True, timeout=2) assert 218 == http.code() assert 'OK' == http.text() get.assert_called_with('http://example.org', auth=None, headers={'Authorization': 'Bearer mytok', 'User-Agent': get_user_agent()}, params=None, timeout=2, verify=True, allow_redirects=True) http = HttpWrapper('http://example.org', oauth2=True, oauth2_token_name='foo', timeout=2) assert 218 == http.code() assert 'OK' == http.text() get.assert_called_with('http://example.org', auth=None, headers={'Authorization': 'Bearer myothertok', 'User-Agent': get_user_agent()}, params=None, timeout=2, verify=True, allow_redirects=True)
def test_http(monkeypatch): resp = MagicMock() resp.status_code = 200 resp.text = '"foo"' resp.content = resp.text resp.json.return_value = "foo" get = MagicMock() get.return_value = resp monkeypatch.setattr("requests.get", get) http = HttpWrapper("http://example.org") assert 200 == http.code() assert '"foo"' == http.text() assert "foo" == http.json() assert 5 == http.content_size() resp.json.side_effect = Exception("JSON fail") with pytest.raises(HttpError) as ex: http.json() assert "JSON fail" == ex.value.message
def test_oauth2(monkeypatch): resp = MagicMock() resp.status_code = 218 resp.text = "OK" get = MagicMock() get.return_value = resp monkeypatch.setattr("requests.get", get) monkeypatch.setattr("tokens.get", lambda x: "mytok") http = HttpWrapper("http://example.org", oauth2=True, timeout=2) assert 218 == http.code() assert "OK" == http.text() get.assert_called_once_with( "http://example.org", auth=None, headers={"Authorization": "Bearer mytok", "User-Agent": "zmon-worker/0.1"}, params=None, timeout=2, verify=True, )
def test_http_redirects(monkeypatch, fx_redirects): kwargs, code = fx_redirects exp_allow_redirects = False if 'allow_redirects' not in kwargs else kwargs['allow_redirects'] resp = MagicMock() resp.status_code = code resp.text = '' redirect_url = 'http://example.org/some-file' resp.headers = {'Location': redirect_url} method = MagicMock() method.return_value = resp patch = 'requests.{}'.format(kwargs['method'].lower()) monkeypatch.setattr(patch, method) http = HttpWrapper('http://example.org', **kwargs) assert code == http.code() assert '' == http.text() assert redirect_url == http.headers()['Location'] method.assert_called_once_with('http://example.org', auth=None, headers={'User-Agent': get_user_agent()}, params=None, timeout=10, verify=True, allow_redirects=exp_allow_redirects)