1
21
22 package fosite
23
24 import (
25 "fmt"
26 "net/http"
27 "net/url"
28 "strings"
29 "testing"
30
31 "github.com/stretchr/testify/assert"
32 "github.com/stretchr/testify/require"
33 )
34
35 func TestValidateResponseTypes(t *testing.T) {
36 f := &Fosite{}
37 for k, tc := range []struct {
38 rt string
39 art []string
40 expectErr bool
41 }{
42 {
43 rt: "code",
44 art: []string{"token"},
45 expectErr: true,
46 },
47 {
48 rt: "token",
49 art: []string{"token"},
50 },
51 {
52 rt: "",
53 art: []string{"token"},
54 expectErr: true,
55 },
56 {
57 rt: " ",
58 art: []string{"token"},
59 expectErr: true,
60 },
61 {
62 rt: "disable",
63 art: []string{"token"},
64 expectErr: true,
65 },
66 {
67 rt: "code token",
68 art: []string{"token", "code"},
69 expectErr: true,
70 },
71 {
72 rt: "code token",
73 art: []string{"token", "token code"},
74 },
75 {
76 rt: "code token",
77 art: []string{"token", "code token"},
78 },
79 {
80 rt: "code token",
81 art: []string{"token", "code token id_token"},
82 expectErr: true,
83 },
84 } {
85 t.Run(fmt.Sprintf("case=%d", k), func(t *testing.T) {
86 r := &http.Request{Form: url.Values{"response_type": {tc.rt}}}
87 if tc.rt == "disable" {
88 r = &http.Request{Form: url.Values{}}
89 }
90 ar := NewAuthorizeRequest()
91 ar.Request.Client = &DefaultClient{ResponseTypes: tc.art}
92
93 err := f.validateResponseTypes(r, ar)
94 if tc.expectErr {
95 require.Error(t, err)
96 } else {
97 require.NoError(t, err)
98 assert.EqualValues(t, RemoveEmpty(strings.Split(tc.rt, " ")), ar.GetResponseTypes())
99 }
100 })
101 }
102 }
103
View as plain text