Skip to content

Commit

Permalink
Fix #138: pass "Accept: application/json" header to force JSON responses
Browse files Browse the repository at this point in the history
  • Loading branch information
frankie567 committed Jul 16, 2020
1 parent 6aac4ba commit a4c273d
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 2 deletions.
3 changes: 2 additions & 1 deletion httpx_oauth/clients/github.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ def __init__(self, client_id: str, client_secret: str, name: str = "github"):
async def get_id_email(self, token: str) -> Tuple[str, str]:
async with httpx.AsyncClient() as client:
response = await client.get(
PROFILE_ENDPOINT, headers={"Authorization": f"token {token}"},
PROFILE_ENDPOINT,
headers={**self.request_headers, "Authorization": f"token {token}"},
)

if response.status_code >= 400:
Expand Down
11 changes: 10 additions & 1 deletion httpx_oauth/oauth2.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ class BaseOAuth2(Generic[T]):
refresh_token_endpoint: Optional[str]
revoke_token_endpoint: Optional[str]
base_scopes: Optional[List[str]]
request_headers: Dict[str, str]

def __init__(
self,
Expand All @@ -81,6 +82,10 @@ def __init__(
self.name = name
self.base_scopes = base_scopes

self.request_headers = {
"Accept": "application/json",
}

async def get_authorization_url(
self,
redirect_uri: str,
Expand Down Expand Up @@ -116,6 +121,7 @@ async def get_access_token(self, code: str, redirect_uri: str):
"client_id": self.client_id,
"client_secret": self.client_secret,
},
headers=self.request_headers,
)

data = cast(Dict[str, Any], response.json())
Expand All @@ -138,6 +144,7 @@ async def refresh_token(self, refresh_token: str):
"client_id": self.client_id,
"client_secret": self.client_secret,
},
headers=self.request_headers,
)

data = cast(Dict[str, Any], response.json())
Expand All @@ -157,7 +164,9 @@ async def revoke_token(self, token: str, token_type_hint: str = None):
if token_type_hint is not None:
data["token_type_hint"] = token_type_hint

response = await client.post(self.revoke_token_endpoint, data=data)
response = await client.post(
self.revoke_token_endpoint, data=data, headers=self.request_headers
)

if response.status_code == 400:
raise RevokeTokenError(response.json())
Expand Down
1 change: 1 addition & 0 deletions tests/test_clients_github.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ async def test_success(self, get_respx_call_args):
url, headers, content = await get_respx_call_args(request)

assert headers["Authorization"] == "token TOKEN"
assert headers["Accept"] == "application/json"
assert user_id == "42"
assert user_email == "arthur@camelot.bt"

Expand Down
3 changes: 3 additions & 0 deletions tests/test_oauth2.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ async def test_get_access_token(self, load_mock, get_respx_call_args):

url, headers, content = await get_respx_call_args(request)
assert headers["Content-Type"] == "application/x-www-form-urlencoded"
assert headers["Accept"] == "application/json"
assert "grant_type=authorization_code" in content
assert "code=CODE" in content
assert "redirect_uri=https%3A%2F%2Fwww.tintagel.bt%2Foauth-callback" in content
Expand Down Expand Up @@ -153,6 +154,7 @@ async def test_refresh_token(self, load_mock, get_respx_call_args):

url, headers, content = await get_respx_call_args(request)
assert headers["Content-Type"] == "application/x-www-form-urlencoded"
assert headers["Accept"] == "application/json"
assert "grant_type=refresh_token" in content
assert "refresh_token=REFRESH_TOKEN" in content
assert f"client_id={CLIENT_ID}" in content
Expand Down Expand Up @@ -193,6 +195,7 @@ async def test_revoke_token(self, load_mock, get_respx_call_args):

url, headers, content = await get_respx_call_args(request)
assert headers["Content-Type"] == "application/x-www-form-urlencoded"
assert headers["Accept"] == "application/json"
assert "token=TOKEN" in content
assert "token_type_hint=TOKEN_TYPE_HINT" in content

Expand Down

0 comments on commit a4c273d

Please sign in to comment.