Skip to content

Commit 361403a

Browse files
authored
Merge pull request #12 from chrisw-dev/check-encoding
check and handle errors around encoding
2 parents eec240c + 2a1a732 commit 361403a

File tree

3 files changed

+17
-11
lines changed

3 files changed

+17
-11
lines changed

internal/handlers/token.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,9 @@ func (h *TokenHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
5555
errorResponse["error_description"] = errorScenario.Description
5656
}
5757

58-
json.NewEncoder(w).Encode(errorResponse)
58+
if err := json.NewEncoder(w).Encode(errorResponse); err != nil {
59+
log.Printf("Error encoding error response: %v", err)
60+
}
5961
return
6062
}
6163

internal/handlers/userinfo.go

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,17 @@ func (h *UserInfoHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
2525
w.WriteHeader(errorScenario.StatusCode)
2626

2727
errorResponse := map[string]string{
28-
"error": errorScenario.ErrorCode,
29-
}
30-
31-
if errorScenario.Description != "" {
32-
errorResponse["error_description"] = errorScenario.Description
33-
}
34-
35-
json.NewEncoder(w).Encode(errorResponse)
36-
return
28+
"error": errorScenario.ErrorCode,
29+
}
30+
31+
if errorScenario.Description != "" {
32+
errorResponse["error_description"] = errorScenario.Description
33+
}
34+
35+
if err := json.NewEncoder(w).Encode(errorResponse); err != nil {
36+
log.Printf("Error encoding error response: %v", err)
37+
}
38+
return
3739
}
3840

3941
authHeader := r.Header.Get("Authorization")

test/integration/oauth_flow_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -428,7 +428,9 @@ func TestUserInfoEndpointErrorScenario(t *testing.T) {
428428
defer tokenResp.Body.Close()
429429

430430
var tokenData map[string]interface{}
431-
json.NewDecoder(tokenResp.Body).Decode(&tokenData)
431+
if err := json.NewDecoder(tokenResp.Body).Decode(&tokenData); err != nil {
432+
t.Fatalf("Failed to decode token response: %v", err)
433+
}
432434
accessToken := tokenData["access_token"].(string)
433435

434436
t.Run("Step 1: Verify userinfo works with valid token", func(t *testing.T) {

0 commit comments

Comments
 (0)