def test_make_request(monkeypatch, serving_app): monkeypatch.setattr("frost.github.BASE_URL", serving_app.url) monkeypatch.setattr("frost.github.API_URL", serving_app.url + "/api") @serving_app.route("/") def home(): return flask.jsonify({"msg": "The base domain."}) @serving_app.route("/api/") def api(): data = {"accept": flask.request.headers.get("Accept"), "something": flask.request.headers.get("X-Something")} return flask.jsonify(data) data = _github_request("GET", "/", base=serving_app.url) assert data == {"msg": "The base domain."} data = _github_request("GET", "/") assert data == {"accept": "application/json", "something": None} headers = {"X-Something": "abc"} data = _github_request("GET", "/", headers=headers) assert data == {"accept": "application/json", "something": "abc"} headers = {"Accept": "text/html", "X-Something": "abc"} data = _github_request("GET", "/", headers=headers) assert data == {"accept": "text/html", "something": "abc"}
def test_invalid_json(monkeypatch, serving_app): monkeypatch.setattr("frost.github.API_URL", serving_app.url) @serving_app.route("/text") def home(): return "Home\n" @serving_app.route("/invalid") def fake_content_type(): headers = {"Content-Type": "application/json"} return "Home\n", headers for url in ("/text", "/invalid"): with pytest.raises(Exception) as exc: _github_request("GET", url) assert "GitHub returned bad JSON" in str(exc)
def test_bad_status(monkeypatch, serving_app): monkeypatch.setattr("frost.github.API_URL", serving_app.url) @serving_app.route("/") def home(): return flask.jsonify({"message": "I'm a teapot."}), 418 @serving_app.route("/badtype") def badtype(): headers = {"Content-Type": "application/json"} return '""', 418, headers @serving_app.route("/nomsg") def nomsg(): return flask.jsonify({}), 418 with pytest.raises(Exception) as exc: _github_request("GET", "/") assert "418: I'm a teapot." in str(exc) with pytest.raises(Exception) as exc: _github_request("GET", "/badtype") assert "418: <no error message>" in str(exc) with pytest.raises(Exception) as exc: _github_request("GET", "/nomsg") assert "418: <no error message>" in str(exc)
def test_no_connection(monkeypatch): with pytest.raises(Exception) as exc: monkeypatch.setattr("frost.github.API_URL", "http://0.0.0.0:1234") _github_request("GET", "/") assert "Failed to communicate with GitHub" in str(exc)