Пример #1
0
 def test_404(self):
     status_code = 404
     headers, body = sample("pr_not_found", status_code)
     with pytest.raises(BadRequest) as exc_info:
         sansio.decipher_response(status_code, headers, body)
     assert exc_info.value.status_code == http.HTTPStatus(status_code)
     assert str(exc_info.value) == "Not Found"
Пример #2
0
    def test_next_with_search_api(self):
        status_code = 200
        headers, body = sample("search_issues_page_1", status_code)
        data, rate_limit, more = sansio.decipher_response(status_code, headers, body)
        assert more == (
            "https://api.github.com/search/issues"
            "?q=repo%3Abrettcannon%2Fgidgethub+state%3Aclosed"
            "+rate+&per_page=3&page=2"
        )
        assert rate_limit.remaining == 9
        assert {"items", "incomplete_results", "total_count"} == data.keys()
        expected_first_url = (
            "https://api.github.com/repos/brettcannon/gidgethub/issues/25"
        )
        assert data["items"][0]["url"] == expected_first_url

        headers, body = sample("search_issues_page_last", status_code)
        data, rate_limit, more = sansio.decipher_response(status_code, headers, body)
        assert more is None
        assert rate_limit.remaining == 9
        assert {"items", "incomplete_results", "total_count"} == data.keys()
        expected_first_url = (
            "https://api.github.com/repos/brettcannon/gidgethub/issues/10"
        )
        assert data["items"][0]["url"] == expected_first_url
Пример #3
0
    def test_next(self):
        status_code = 200
        headers, body = sample("pr_page_1", status_code)
        data, rate_limit, more = sansio.decipher_response(
            status_code, headers, body)
        assert more == "https://api.github.com/repositories/4164482/pulls?page=2"
        assert rate_limit.remaining == 53
        assert data[0][
            "url"] == "https://api.github.com/repos/django/django/pulls/8053"

        headers, body = sample("pr_page_2", status_code)
        data, rate_limit, more = sansio.decipher_response(
            status_code, headers, body)
        assert more == "https://api.github.com/repositories/4164482/pulls?page=3"
        assert rate_limit.remaining == 50
        assert data[0][
            "url"] == "https://api.github.com/repos/django/django/pulls/7805"

        headers, body = sample("pr_page_last", status_code)
        data, rate_limit, more = sansio.decipher_response(
            status_code, headers, body)
        assert more is None
        assert rate_limit.remaining == 48
        assert data[0][
            "url"] == "https://api.github.com/repos/django/django/pulls/6395"
Пример #4
0
 def test_4XX_message(self):
     status_code = 400
     message = json.dumps({"message": "it went bad"}).encode("UTF-8")
     headers = {"content-type": "application/json; charset=utf-8"}
     with pytest.raises(BadRequest) as exc_info:
         sansio.decipher_response(status_code, headers, message)
     assert exc_info.value.status_code == http.HTTPStatus(status_code)
     assert str(exc_info.value) == "it went bad"
Пример #5
0
 def test_422(self):
     status_code = 422
     errors = [{"resource": "Issue", "field": "title", "code": "missing_field"}]
     body = json.dumps({"message": "it went bad", "errors": errors})
     body = body.encode("utf-8")
     headers = {"content-type": "application/json; charset=utf-8"}
     with pytest.raises(InvalidField) as exc_info:
         sansio.decipher_response(status_code, headers, body)
     assert exc_info.value.status_code == http.HTTPStatus(status_code)
     assert str(exc_info.value) == "it went bad for 'title'"
Пример #6
0
 async def test__request_with_body(self):
     """Make sure that that abstract method is implemented properly."""
     request_headers = sansio.create_headers("gidgethub")
     gh = gh_tornado.GitHubAPI("gidgethub")
     # This leads to a 404.
     tornado_call = await gh._request(
         "POST", "https://api.github.com/rate_limit", request_headers, b"bogus"
     )
     with pytest.raises(BadRequest):
         sansio.decipher_response(*tornado_call)
Пример #7
0
 def test_422_html_response(self):
     # https://github.com/brettcannon/gidgethub/issues/81
     status_code = 422
     body = "<html><body>Mistakes were made ...</body></html>"
     encoded_body = body.encode("utf-8")
     headers = {"content-type": "text/html; charset=utf-8"}
     with pytest.raises(BadRequestUnknownError) as exc_info:
         sansio.decipher_response(status_code, headers, encoded_body)
     assert exc_info.value.status_code == http.HTTPStatus(status_code)
     assert exc_info.value.response == body
Пример #8
0
 def test_403_forbidden(self):
     status_code = 403
     headers = {
         "content-type": "application/json; charset=utf-8",
         "x-ratelimit-limit": "2",
         "x-ratelimit-remaining": "1",
         "x-ratelimit-reset": "1",
     }
     with pytest.raises(BadRequest) as exc_info:
         sansio.decipher_response(status_code, headers, b"")
     assert exc_info.value.status_code == http.HTTPStatus(status_code)
Пример #9
0
 def test_403_rate_limit_exceeded(self):
     status_code = 403
     headers = {
         "content-type": "application/json; charset=utf-8",
         "x-ratelimit-limit": "2",
         "x-ratelimit-remaining": "0",
         "x-ratelimit-reset": "1",
     }
     body = json.dumps({"message": "oops"}).encode("UTF-8")
     with pytest.raises(RateLimitExceeded) as exc_info:
         sansio.decipher_response(status_code, headers, body)
     assert exc_info.value.status_code == http.HTTPStatus(status_code)
Пример #10
0
 def test_422_no_errors_object(self):
     status_code = 422
     body = json.dumps({
         "message":
         "Reference does not exist",
         "documentation_url":
         "https://developer.github.com/v3/git/refs/#delete-a-reference",
     })
     body = body.encode("utf-8")
     headers = {"content-type": "application/json; charset=utf-8"}
     with pytest.raises(InvalidField) as exc_info:
         sansio.decipher_response(status_code, headers, body)
     assert exc_info.value.status_code == http.HTTPStatus(status_code)
     assert str(exc_info.value) == "Reference does not exist"
Пример #11
0
 def test_422_custom_code(self):
     status_code = 422
     errors = [{
         "resource": "PullRequest",
         "code": "custom",
         "message": "A pull request already exists for foo:1.",
     }]
     body = json.dumps({"message": "it went bad", "errors": errors})
     body = body.encode("utf-8")
     headers = {"content-type": "application/json; charset=utf-8"}
     with pytest.raises(ValidationError) as exc_info:
         sansio.decipher_response(status_code, headers, body)
     assert exc_info.value.status_code == http.HTTPStatus(status_code)
     assert (str(exc_info.value) ==
             "it went bad: 'A pull request already exists for foo:1.'")
Пример #12
0
 def test_200(self):
     status_code = 200
     headers, body = sample("pr_single", status_code)
     data, rate_limit, more = sansio.decipher_response(status_code, headers, body)
     assert more is None
     assert rate_limit.remaining == 53
     assert data["url"] == "https://api.github.com/repos/python/cpython/pulls/1"
Пример #13
0
 def test_text_body(self):
     """Test requesting non-JSON data like a diff."""
     status_code = 200
     headers, body = sample("pr_diff", status_code)
     data, rate_limit, more = sansio.decipher_response(status_code, headers, body)
     assert more is None
     assert rate_limit.remaining == 43
     assert data.startswith("diff --git")
Пример #14
0
 def test_204(self):
     """Test both a 204 response and an empty response body."""
     status_code = 204
     headers, body = sample("pr_merged", status_code)
     data, rate_limit, more = sansio.decipher_response(status_code, headers, body)
     assert more is None
     assert rate_limit.remaining == 41
     assert data is None
Пример #15
0
 async def test__request(self):
     """Make sure that that abstract method is implemented properly."""
     request_headers = sansio.create_headers("gidgethub")
     gh = gh_tornado.GitHubAPI("gidgethub")
     tornado_call = await gh._request(
         "GET", "https://api.github.com/rate_limit", request_headers
     )
     data, rate_limit, _ = sansio.decipher_response(*tornado_call)
     assert "rate" in data
Пример #16
0
async def test_headers_and_log(github_api: GitHubAPI) -> None:
    assert github_api.headers is None
    request_headers = sansio.create_headers("algorithms-keeper")
    resp = await github_api._request(
        "GET", "https://api.github.com/rate_limit", request_headers
    )
    data, rate_limit, _ = sansio.decipher_response(*resp)
    assert "rate" in data
    # Response headers should now be set.
    assert github_api.headers is not None
Пример #17
0
async def test__request():
    """Make sure that that abstract method is implemented properly."""
    request_headers = sansio.create_headers("gidgethub")
    async with aiohttp.ClientSession() as session:
        gh = gh_aiohttp.GitHubAPI(session, "gidgethub")
        aio_call = await gh._request("GET",
                                     "https://api.github.com/rate_limit",
                                     request_headers)
    data, rate_limit, _ = sansio.decipher_response(*aio_call)
    assert "rate" in data
Пример #18
0
 def test_no_ratelimit(self):
     """Test no ratelimit in headers."""
     status_code = 201
     headers = {
         "content-type": "application/json; charset=utf-8",
         "link": '<http://example.com>; test="unimportant"',
     }
     data = {
         "id": 208045946,
         "url":
         "https://api.github.com/repos/octocat/Hello-World/labels/bug",
         "name": "bug",
         "color": "f29513",
         "default": True,
     }
     body = json.dumps(data).encode("UTF-8")
     returned_data, rate_limit, more = sansio.decipher_response(
         status_code, headers, body)
     assert more is None
     assert rate_limit is None
     assert returned_data == data
Пример #19
0
 def test_201(self):
     """Test a 201 response along with non-pagination Link header."""
     status_code = 201
     headers = {
         "x-ratelimit-limit": "60",
         "x-ratelimit-remaining": "50",
         "x-ratelimit-reset": "12345678",
         "content-type": "application/json; charset=utf-8",
         "link": '<http://example.com>; test="unimportant"',
     }
     data = {
         "id": 208045946,
         "url":
         "https://api.github.com/repos/octocat/Hello-World/labels/bug",
         "name": "bug",
         "color": "f29513",
         "default": True,
     }
     body = json.dumps(data).encode("UTF-8")
     returned_data, rate_limit, more = sansio.decipher_response(
         status_code, headers, body)
     assert more is None
     assert rate_limit.limit == 60
     assert returned_data == data
Пример #20
0
 def test_2XX_error(self):
     status_code = 205
     with pytest.raises(HTTPException) as exc_info:
         sansio.decipher_response(status_code, {}, b"")
     assert exc_info.value.status_code == http.HTTPStatus(status_code)
Пример #21
0
 def test_3XX(self):
     status_code = 301
     with pytest.raises(RedirectionException) as exc_info:
         sansio.decipher_response(status_code, {}, b"")
     assert exc_info.value.status_code == http.HTTPStatus(status_code)
Пример #22
0
            "x-ratelimit-limit": "60",
            "x-ratelimit-remaining": "50",
            "x-ratelimit-reset": "12345678",
            "content-type": "application/json; charset=utf-8",
            "link": '<http://example.com>; test="unimportant"',
        }
        data = {
            "id": 208_045_946,
            "url":
            "https://api.github.com/repos/octocat/Hello-World/labels/bug",
            "name": "bug",
            "color": "f29513",
            "default": True,
        }
        body = json.dumps(data).encode("UTF-8")
        returned_data, rate_limit, more = sansio.decipher_response(
            status_code, headers, body)
        assert more is None
        assert rate_limit.limit == 60
        assert returned_data == data

    def test_204(self):
        """Test both a 204 response and an empty response body."""
        status_code = 204
        headers, body = sample("pr_merged", status_code)
        data, rate_limit, more = sansio.decipher_response(
            status_code, headers, body)
        assert more is None
        assert rate_limit.remaining == 41
        assert data is None

    def test_next(self):
Пример #23
0
 def test_5XX(self):
     status_code = 502
     with pytest.raises(GitHubBroken) as exc_info:
         sansio.decipher_response(status_code, {}, b"")
     assert exc_info.value.status_code == http.HTTPStatus(status_code)
Пример #24
0
 def test_4XX_no_message(self):
     status_code = 400
     with pytest.raises(BadRequest) as exc_info:
         sansio.decipher_response(status_code, {}, b"")
     assert exc_info.value.status_code == http.HTTPStatus(status_code)