|
| 1 | +package middlewares |
| 2 | + |
| 3 | +import ( |
| 4 | + "net/http" |
| 5 | + "net/http/httptest" |
| 6 | + "strconv" |
| 7 | + "testing" |
| 8 | + "time" |
| 9 | + |
| 10 | + "github.com/mojixcoder/kid" |
| 11 | + "github.com/stretchr/testify/assert" |
| 12 | +) |
| 13 | + |
| 14 | +func TestSetHeader(t *testing.T) { |
| 15 | + testCases := []struct { |
| 16 | + key string |
| 17 | + val string |
| 18 | + emptyVal string |
| 19 | + expectedRes string |
| 20 | + }{ |
| 21 | + {key: "empty_max_age", val: strconv.Itoa(int(time.Duration(0).Seconds())), emptyVal: "0", expectedRes: ""}, |
| 22 | + {key: "max_age", val: strconv.Itoa(int((time.Hour).Seconds())), emptyVal: "0", expectedRes: "3600"}, |
| 23 | + {key: "empty_headers", val: "", emptyVal: "", expectedRes: ""}, |
| 24 | + {key: "headers", val: "headers", emptyVal: "", expectedRes: "headers"}, |
| 25 | + {key: "empty_creds", val: "false", emptyVal: "false", expectedRes: ""}, |
| 26 | + {key: "creds", val: "true", emptyVal: "false", expectedRes: "true"}, |
| 27 | + } |
| 28 | + |
| 29 | + header := make(http.Header) |
| 30 | + |
| 31 | + for _, testCase := range testCases { |
| 32 | + t.Run(testCase.key, func(t *testing.T) { |
| 33 | + setHeader(header, testCase.key, testCase.val, testCase.emptyVal) |
| 34 | + assert.Equal(t, testCase.expectedRes, header.Get(testCase.key)) |
| 35 | + }) |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +func TestSetCorsDefaults(t *testing.T) { |
| 40 | + cors := &CorsConfig{} |
| 41 | + |
| 42 | + setCorsDefaults(cors) |
| 43 | + assert.Equal(t, DefaultCorsConfig.AllowedOrigins, cors.AllowedOrigins) |
| 44 | + assert.Equal(t, DefaultCorsConfig.AllowedMethods, cors.AllowedMethods) |
| 45 | + |
| 46 | + cors = &CorsConfig{AllowedMethods: []string{http.MethodConnect}} |
| 47 | + |
| 48 | + setCorsDefaults(cors) |
| 49 | + assert.Equal(t, []string{http.MethodConnect}, cors.AllowedMethods) |
| 50 | +} |
| 51 | + |
| 52 | +func TestIsPreflight(t *testing.T) { |
| 53 | + req := httptest.NewRequest(http.MethodOptions, "/test", nil) |
| 54 | + assert.False(t, isPreflight(req)) |
| 55 | + |
| 56 | + req.Header.Set("Access-Control-Request-Method", http.MethodGet) |
| 57 | + assert.True(t, isPreflight(req)) |
| 58 | +} |
| 59 | + |
| 60 | +func TestCorsConfig_isAllowedOrigin(t *testing.T) { |
| 61 | + cfg := CorsConfig{AllowedOrigins: []string{"http://localhost:2376"}} |
| 62 | + |
| 63 | + assert.True(t, cfg.isAllowedOrigin(nil, "http://localhost:2376")) |
| 64 | + assert.False(t, cfg.isAllowedOrigin(nil, "http://localhost:2377")) |
| 65 | + assert.False(t, cfg.allowAllOrigins) |
| 66 | + |
| 67 | + cfg.AllowedOrigins = []string{"*"} |
| 68 | + |
| 69 | + assert.True(t, cfg.isAllowedOrigin(nil, "http://localhost:2376")) |
| 70 | + assert.True(t, cfg.allowAllOrigins) |
| 71 | + assert.True(t, cfg.isAllowedOrigin(nil, "http://localhost:2377")) |
| 72 | + |
| 73 | + cfg.AllowOriginFunc = func(c *kid.Context, origin string) bool { |
| 74 | + return false |
| 75 | + } |
| 76 | + |
| 77 | + assert.False(t, cfg.isAllowedOrigin(nil, "http://localhost:2376")) |
| 78 | +} |
| 79 | + |
| 80 | +func TestNewCors(t *testing.T) { |
| 81 | + k := kid.New() |
| 82 | + k.Use(NewCors()) |
| 83 | + |
| 84 | + res := httptest.NewRecorder() |
| 85 | + req := httptest.NewRequest(http.MethodOptions, "/test", nil) |
| 86 | + req.Header.Add("Access-Control-Request-Method", http.MethodPost) |
| 87 | + req.Header.Add("Origin", "http://localhost:2376") |
| 88 | + |
| 89 | + k.ServeHTTP(res, req) |
| 90 | + |
| 91 | + assert.Equal(t, http.StatusNoContent, res.Code) |
| 92 | + assert.Equal(t, "Origin", res.Header().Get("Vary")) |
| 93 | + assert.Equal(t, "*", res.Header().Get("Access-Control-Allow-Origin")) |
| 94 | + assert.Equal(t, "GET, POST, PUT, PATCH, DELETE, OPTIONS", res.Header().Get("Access-Control-Allow-Methods")) |
| 95 | + |
| 96 | + res = httptest.NewRecorder() |
| 97 | + req = httptest.NewRequest(http.MethodPost, "/test", nil) |
| 98 | + req.Header.Add("Origin", "http://localhost:2376") |
| 99 | + |
| 100 | + k.ServeHTTP(res, req) |
| 101 | + |
| 102 | + assert.Equal(t, http.StatusNotFound, res.Code) |
| 103 | + assert.Equal(t, "Origin", res.Header().Get("Vary")) |
| 104 | + assert.Equal(t, "*", res.Header().Get("Access-Control-Allow-Origin")) |
| 105 | + assert.Empty(t, res.Header().Get("Access-Control-Allow-Methods")) |
| 106 | +} |
| 107 | + |
| 108 | +func TestNewCorsWithConfig(t *testing.T) { |
| 109 | + cfg := CorsConfig{ |
| 110 | + AllowedOrigins: []string{"http://localhost:2376"}, |
| 111 | + AllowedMethods: []string{http.MethodGet, http.MethodPost}, |
| 112 | + AllowedHeaders: []string{"Content-Type", "Accept"}, |
| 113 | + ExposedHeaders: []string{"User-Agent"}, |
| 114 | + AllowCredentials: true, |
| 115 | + AllowPrivateNetwork: true, |
| 116 | + MaxAge: 24 * time.Hour, |
| 117 | + } |
| 118 | + |
| 119 | + k := kid.New() |
| 120 | + k.Use(NewCorsWithConfig(cfg)) |
| 121 | + |
| 122 | + res := httptest.NewRecorder() |
| 123 | + req := httptest.NewRequest(http.MethodOptions, "/test", nil) |
| 124 | + req.Header.Add("Access-Control-Request-Method", http.MethodPost) |
| 125 | + req.Header.Add("Origin", "http://localhost:2376") |
| 126 | + req.Header.Add("Access-Control-Request-Private-Network", "true") |
| 127 | + |
| 128 | + k.ServeHTTP(res, req) |
| 129 | + |
| 130 | + assert.Equal(t, http.StatusNoContent, res.Code) |
| 131 | + assert.Equal(t, "Origin", res.Header().Get("Vary")) |
| 132 | + assert.Equal(t, "http://localhost:2376", res.Header().Get("Access-Control-Allow-Origin")) |
| 133 | + assert.Equal(t, "GET, POST", res.Header().Get("Access-Control-Allow-Methods")) |
| 134 | + assert.Equal(t, "Content-Type, Accept", res.Header().Get("Access-Control-Allow-Headers")) |
| 135 | + assert.Equal(t, "User-Agent", res.Header().Get("Access-Control-Expose-Headers")) |
| 136 | + assert.Equal(t, "true", res.Header().Get("Access-Control-Allow-Credentials")) |
| 137 | + assert.Equal(t, "true", res.Header().Get("Access-Control-Allow-Private-Network")) |
| 138 | + assert.Equal(t, "86400", res.Header().Get("Access-Control-Max-Age")) |
| 139 | + |
| 140 | + res = httptest.NewRecorder() |
| 141 | + req = httptest.NewRequest(http.MethodOptions, "/test", nil) |
| 142 | + req.Header.Add("Access-Control-Request-Method", http.MethodPost) |
| 143 | + |
| 144 | + k.ServeHTTP(res, req) |
| 145 | + assert.Equal(t, http.StatusNotFound, res.Code) |
| 146 | + assert.Empty(t, res.Header().Get("Access-Control-Allow-Origin")) |
| 147 | + |
| 148 | + res = httptest.NewRecorder() |
| 149 | + req = httptest.NewRequest(http.MethodOptions, "/test", nil) |
| 150 | + req.Header.Add("Access-Control-Request-Method", http.MethodPost) |
| 151 | + req.Header.Add("Origin", "http://localhost:4000") |
| 152 | + |
| 153 | + k.ServeHTTP(res, req) |
| 154 | + |
| 155 | + assert.Equal(t, http.StatusNotFound, res.Code) |
| 156 | + assert.Empty(t, res.Header().Get("Access-Control-Allow-Origin")) |
| 157 | +} |
0 commit comments