Skip to content

Commit

Permalink
add tests for Cors.areHeadersAllowed() (#128)
Browse files Browse the repository at this point in the history
  • Loading branch information
komuw authored Jun 19, 2022
1 parent a4a5ce8 commit da52b07
Showing 1 changed file with 58 additions and 0 deletions.
58 changes: 58 additions & 0 deletions cors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -647,3 +647,61 @@ func TestOptionsSuccessStatusCodeOverride(t *testing.T) {
assertResponse(t, res, http.StatusOK)
})
}

func TestCorsAreHeadersAllowed(t *testing.T) {
cases := []struct {
name string
allowedHeaders []string
requestedHeaders []string
want bool
}{
{
name: "nil allowedHeaders",
allowedHeaders: nil,
requestedHeaders: parseHeaderList("X-PINGOTHER, Content-Type"),
want: false,
},
{
name: "star allowedHeaders",
allowedHeaders: []string{"*"},
requestedHeaders: parseHeaderList("X-PINGOTHER, Content-Type"),
want: true,
},
{
name: "empty reqHeader",
allowedHeaders: nil,
requestedHeaders: parseHeaderList(""),
want: true,
},
{
name: "match allowedHeaders",
allowedHeaders: []string{"Content-Type", "X-PINGOTHER", "X-APP-KEY"},
requestedHeaders: parseHeaderList("X-PINGOTHER, Content-Type"),
want: true,
},
{
name: "not matched allowedHeaders",
allowedHeaders: []string{"X-PINGOTHER"},
requestedHeaders: parseHeaderList("X-API-KEY, Content-Type"),
want: false,
},
{
name: "allowedHeaders should be a superset of requestedHeaders",
allowedHeaders: []string{"X-PINGOTHER"},
requestedHeaders: parseHeaderList("X-PINGOTHER, Content-Type"),
want: false,
},
}

for _, tt := range cases {
tt := tt

t.Run(tt.name, func(t *testing.T) {
c := New(Options{AllowedHeaders: tt.allowedHeaders})
have := c.areHeadersAllowed(tt.requestedHeaders)
if have != tt.want {
t.Errorf("Cors.areHeadersAllowed() have: %t want: %t", have, tt.want)
}
})
}
}

0 comments on commit da52b07

Please sign in to comment.