From da52b0701de54d82f71776d634d4183b69c3a915 Mon Sep 17 00:00:00 2001 From: Komu Wairagu Date: Sun, 19 Jun 2022 22:58:39 +0300 Subject: [PATCH] add tests for Cors.areHeadersAllowed() (#128) --- cors_test.go | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) diff --git a/cors_test.go b/cors_test.go index 7947334..4d7b5f7 100644 --- a/cors_test.go +++ b/cors_test.go @@ -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) + } + }) + } +}