def test_send_log_record(caplog, m): caplog.set_level(logging.INFO) m.get("https://ovalmoney.com/user", status_code=200) s = Session() s._logger.info = MagicMock() s.get("https://ovalmoney.com/user") assert s._logger.info.called
def test_send_log_record_exception(caplog, m): caplog.set_level(logging.INFO) m.get("https://ovalmoney.com/user", exc=requests.exceptions.ConnectTimeout) s = Session() s._logger.exception = MagicMock() with pytest.raises(requests.exceptions.ConnectTimeout): s.get("https://ovalmoney.com/user") assert s._logger.exception.called
def test_custom_header(m): """ Check whether a request having the header set, continues to work correctly """ test_path = "https://ovalmoney.com/user" m.get(test_path, status_code=200) s = Session() response = s.get(test_path, headers={"sweeties": "chocolate"}) assert response.status_code == 200
def test_form_data_request_body_log(caplog, m): caplog.set_level(logging.INFO) m.post("https://ovalmoney.com/form_data") s = Session() s.post("https://ovalmoney.com/form_data", data={ "key": "value", "key2": "value2" }) log_rec = [rec for rec in caplog.records][0] assert log_rec.msg["request"]["body"] == "key=value&key2=value2"
def test_text_reponse_body_log(caplog, m): caplog.set_level(logging.INFO) m.get( "https://ovalmoney.com/text_data", headers={"Content-Type": "text/plain"}, text="Response", ) s = Session() s.get("https://ovalmoney.com/text_data") log_rec = [rec for rec in caplog.records][0] assert log_rec.msg["response"]["body"] == "Response"
def test_raw_data_request_body_log(caplog, m): caplog.set_level(logging.INFO) m.post("https://ovalmoney.com/raw_data") s = Session() s.post( "https://ovalmoney.com/raw_data", headers={"Content-Type": "video"}, data=b"\xFF\x8F", ) log_rec = [rec for rec in caplog.records][0] assert log_rec.msg["request"]["body"] == BODY_NOT_LOGGABLE.format( log_rec.msg["request"]["header"]["Content-Type"])
def test_session_fail_http_log(caplog, m): caplog.set_level(logging.INFO) m.get("https://ovalmoney.com/user", exc=requests.exceptions.ConnectTimeout) s = Session() with pytest.raises(requests.exceptions.ConnectTimeout): s.get("https://ovalmoney.com/user") log_rec = [rec for rec in caplog.records][0] assert isinstance(log_rec.exc_info[1], requests.exceptions.ConnectTimeout) log = log_rec.msg assert log["request"]["method"] == "GET" assert log["request"]["url"] == "https://ovalmoney.com/user" assert log["request"]["path"] == "/user" assert log["request"]["host"] == "ovalmoney.com"
def test_route_plus_custom_header(caplog, m): """ Check whether a request having both the `route` and the header set, continues to work correctly """ caplog.set_level(logging.INFO) test_path = "https://ovalmoney.com/user" m.get(test_path, status_code=200) s = Session() route = "test/route" response = s.get(test_path, route=route, headers={"sweeties": "chocolate"}) log = [rec.msg for rec in caplog.records][0] assert log["request"]["route"] == route assert response.status_code == 200
def test_no_route(m): """ Check whether a request without the `route` attribute works correctly """ test_path = "https://ovalmoney.com/user" m.get(test_path, status_code=200) m.post(test_path, status_code=201) m.put(test_path, status_code=202) s = Session() r_get = s.get(test_path) r_post = s.post(test_path) r_put = s.put(test_path) assert r_get.status_code == 200 assert r_post.status_code == 201 assert r_put.status_code == 202
def test_route_set(caplog, m): """ Check whether a request having the `route` set, continues to work correctly """ caplog.set_level(logging.INFO) test_path = "https://ovalmoney.com/user" m.get(test_path, status_code=200) s = Session() route = "test/route" response = s.get(test_path, route=route) log = [rec.msg for rec in caplog.records][0] assert log["request"]["route"] == route assert response.status_code == 200
def test_session_success(caplog, m): caplog.set_level(logging.INFO) m.get( "https://ovalmoney.com/user?key1=value1&key2=value2&key2=value3", json={"test": True}, headers={"Content-Type": "application/json"}, status_code=200, ) s = Session() response = s.get( "https://ovalmoney.com/user", params={ "key1": "value1", "key2": ["value2", "value3"] }, ) assert response.headers assert response.status_code == 200 assert response.json() == {"test": True}
def test_session_success_log(caplog, m): caplog.set_level(logging.INFO) m.get( "https://ovalmoney.com/user?key1=value1&key2=value2&key2=value3", json={"test": True}, headers={"Content-Type": "application/json"}, status_code=200, ) with freeze_time(datetime.utcnow()): s = Session(log_tag="test") s.get( "https://ovalmoney.com/user", params={ "key1": "value1", "key2": ["value2", "value3"] }, ) now = datetime.utcnow().timestamp() log = [rec.msg for rec in caplog.records][0] assert log["extra_tags"] == ["test", "requests_out"] assert log["request"]["start"] == now assert log["request"]["end"] == now assert log["request"]["time"] == 0.0 assert log["request"]["method"] == "GET" assert (log["request"]["url"] == "https://ovalmoney.com/user?key1=value1&key2=value2&key2=value3") assert log["request"]["path"] == "/user" assert log["request"]["host"] == "ovalmoney.com" assert log["request"]["query"] == { "key1": "value1", "key2": "value2,value3" } assert log["response"]["body"] == '{"test": true}' assert log["response"]["status_code"] == 200
def test_session_fail_http(m): m.get("https://ovalmoney.com/user", exc=requests.exceptions.ConnectTimeout) s = Session() with pytest.raises(requests.exceptions.ConnectTimeout): s.get("https://ovalmoney.com/user")