Skip to content

Commit

Permalink
handler/revoke: respecting ErrInvalidRequest code (#380)
Browse files Browse the repository at this point in the history
This commit modifies the case for ErrInvalidRequest in
WriteRevocationResponse to respect the 400 error code
and not fallthrough to ErrInvalidClient.

Author:    DefinitelyNotAGoat <baldrich@protonmail.com>
  • Loading branch information
DefinitelyNotAGoat authored and aeneasr committed Aug 29, 2019
1 parent 54426bb commit cc34bfb
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 1 deletion.
11 changes: 10 additions & 1 deletion revoke_handler.go
Expand Up @@ -101,7 +101,16 @@ func (f *Fosite) WriteRevocationResponse(rw http.ResponseWriter, err error) {

switch errors.Cause(err).Error() {
case ErrInvalidRequest.Error():
fallthrough
rw.Header().Set("Content-Type", "application/json;charset=UTF-8")

js, err := json.Marshal(ErrInvalidRequest)
if err != nil {
http.Error(rw, fmt.Sprintf(`{"error": "%s"}`, err.Error()), http.StatusInternalServerError)
return
}

rw.WriteHeader(ErrInvalidRequest.Code)
rw.Write(js)
case ErrInvalidClient.Error():
rw.Header().Set("Content-Type", "application/json;charset=UTF-8")

Expand Down
46 changes: 46 additions & 0 deletions revoke_handler_test.go
Expand Up @@ -25,6 +25,7 @@ import (
"context"
"fmt"
"net/http"
"net/http/httptest"
"net/url"
"testing"

Expand Down Expand Up @@ -217,3 +218,48 @@ func TestNewRevocationRequest(t *testing.T) {
})
}
}

func TestWriteRevocationResponse(t *testing.T) {
ctrl := gomock.NewController(t)
store := internal.NewMockStorage(ctrl)
hasher := internal.NewMockHasher(ctrl)
defer ctrl.Finish()

fosite := &Fosite{Store: store, Hasher: hasher}

type args struct {
rw *httptest.ResponseRecorder
err error
}
cases := []struct {
input args
expectCode int
}{
{
input: args{
rw: httptest.NewRecorder(),
err: ErrInvalidRequest,
},
expectCode: ErrInvalidRequest.Code,
},
{
input: args{
rw: httptest.NewRecorder(),
err: ErrInvalidClient,
},
expectCode: ErrInvalidClient.Code,
},
{
input: args{
rw: httptest.NewRecorder(),
err: nil,
},
expectCode: http.StatusOK,
},
}

for _, tc := range cases {
fosite.WriteRevocationResponse(tc.input.rw, tc.input.err)
assert.Equal(t, tc.expectCode, tc.input.rw.Code)
}
}

0 comments on commit cc34bfb

Please sign in to comment.