Skip to content

Commit

Permalink
twitch: return ErrNeedRefresh on 401 response
Browse files Browse the repository at this point in the history
  • Loading branch information
zephyrtronium committed Apr 19, 2024
1 parent b59ef40 commit 5f491b5
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 26 deletions.
6 changes: 5 additions & 1 deletion twitch/twitch.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,11 @@ func reqjson[Resp any](ctx context.Context, client Client, method, url string, b
return fmt.Errorf("couldn't read response: %w", err)
}
resp.Body.Close()
if resp.StatusCode != http.StatusOK {
switch resp.StatusCode {
case http.StatusOK: // do nothing
case http.StatusUnauthorized:
return fmt.Errorf("request failed: %s (%w)", b, ErrNeedRefresh)
default:
return fmt.Errorf("request failed: %s (%s)", b, resp.Status)
}
r := struct {
Expand Down
76 changes: 51 additions & 25 deletions twitch/twitch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,29 +28,55 @@ func (r *reqspy) RoundTrip(req *http.Request) (*http.Response, error) {
}

func TestReqJSON(t *testing.T) {
spy := &reqspy{
respond: &http.Response{
StatusCode: 200,
Body: io.NopCloser(strings.NewReader(`{"data":1}`)),
},
}
cl := Client{
HTTP: &http.Client{
Transport: spy,
},
Token: &oauth2.Token{
AccessToken: "bocchi",
},
}
var u int
err := reqjson(context.Background(), cl, "GET", "https://bocchi.rocks/bocchi", nil, &u)
if err != nil {
t.Errorf("failed to request: %v", err)
}
if u != 1 {
t.Errorf("didn't get the result: want 1, got %d", u)
}
if spy.got.URL.String() != "https://bocchi.rocks/bocchi" {
t.Errorf("request went to the wrong place: want https://bocchi.rocks/bocchi, got %v", spy.got.URL)
}
t.Run("ok", func(t *testing.T) {
spy := &reqspy{
respond: &http.Response{
StatusCode: 200,
Body: io.NopCloser(strings.NewReader(`{"data":1}`)),
},
}
cl := Client{
HTTP: &http.Client{
Transport: spy,
},
Token: &oauth2.Token{
AccessToken: "bocchi",
},
}
var u int
err := reqjson(context.Background(), cl, "GET", "https://bocchi.rocks/bocchi", nil, &u)
if err != nil {
t.Errorf("failed to request: %v", err)
}
if u != 1 {
t.Errorf("didn't get the result: want 1, got %d", u)
}
if got := spy.got.URL.String(); got != "https://bocchi.rocks/bocchi" {
t.Errorf(`request went to the wrong place: want "https://bocchi.rocks/bocchi", got %q`, got)
}
if got := spy.got.Header.Get("Authorization"); got != "Bearer bocchi" {
t.Errorf(`wrong authorization: want "Bearer bocchi", got %q`, got)
}
})
t.Run("expired", func(t *testing.T) {
spy := &reqspy{
respond: &http.Response{
StatusCode: 401,
Body: io.NopCloser(strings.NewReader(`{"data":1}`)),
},
}
cl := Client{
HTTP: &http.Client{
Transport: spy,
},
Token: &oauth2.Token{
AccessToken: "bocchi",
},
}
var u int
err := reqjson(context.Background(), cl, "GET", "https://bocchi.rocks/bocchi", nil, &u)
if !errors.Is(err, ErrNeedRefresh) {
t.Errorf("unauthorized request didn't return ErrNeedRefresh error")
}
})
}

0 comments on commit 5f491b5

Please sign in to comment.