Skip to content

Commit 41288e7

Browse files
committed
Default to json payload
1 parent 033f95e commit 41288e7

File tree

1 file changed

+19
-8
lines changed
  • coderd/coderdtest/oidctest

1 file changed

+19
-8
lines changed

coderd/coderdtest/oidctest/idp.go

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -864,19 +864,30 @@ func (f *FakeIDP) httpHandler(t testing.TB) http.Handler {
864864
// Store the claims for the next refresh
865865
f.refreshIDTokenClaims.Store(refreshToken, claims)
866866

867-
if mediaType, _, _ := mime.ParseMediaType(r.Header.Get("Accept")); mediaType == "application/json" {
867+
mediaType, _, _ := mime.ParseMediaType(r.Header.Get("Accept"))
868+
if mediaType == "application/x-www-form-urlencoded" {
869+
// This val encode might not work for some data structures.
870+
// It's good enough for now...
871+
rw.Header().Set("Content-Type", "application/x-www-form-urlencoded")
872+
vals := url.Values{}
873+
for k, v := range token {
874+
vals.Set(k, fmt.Sprintf("%v", v))
875+
}
876+
_, _ = rw.Write([]byte(vals.Encode()))
877+
return
878+
}
879+
// Default to json since the oauth2 package doesn't use Accept headers.
880+
if mediaType == "application/json" || mediaType == "" {
868881
rw.Header().Set("Content-Type", "application/json")
869882
_ = json.NewEncoder(rw).Encode(token)
870883
return
871884
}
872885

873-
// Default to form encode. Just to make sure our code sets the right headers.
874-
rw.Header().Set("Content-Type", "application/x-www-form-urlencoded")
875-
vals := url.Values{}
876-
for k, v := range token {
877-
vals.Set(k, fmt.Sprintf("%v", v))
878-
}
879-
_, _ = rw.Write([]byte(vals.Encode()))
886+
// If we get something we don't support, throw an error.
887+
httpapi.Write(r.Context(), rw, http.StatusBadRequest, codersdk.Response{
888+
Message: "'Accept' header contains unsupported media type",
889+
Detail: fmt.Sprintf("Found %q", mediaType),
890+
})
880891
}))
881892

882893
validateMW := func(rw http.ResponseWriter, r *http.Request) (email string, ok bool) {

0 commit comments

Comments
 (0)