|
| 1 | +package forcesslheroku |
| 2 | + |
| 3 | +import ( |
| 4 | + "net/http" |
| 5 | + "net/http/httptest" |
| 6 | + "testing" |
| 7 | +) |
| 8 | + |
| 9 | +var testCases = []struct { |
| 10 | + goEnv string |
| 11 | + proto string |
| 12 | + expectLoc string |
| 13 | +}{ |
| 14 | + {goEnv: "production", proto: "http", |
| 15 | + expectLoc: "https://example.com/test"}, |
| 16 | + {goEnv: "production", proto: "https"}, |
| 17 | + {goEnv: "test", proto: "http"}, |
| 18 | + {goEnv: "test", proto: "https"}, |
| 19 | +} |
| 20 | + |
| 21 | +func TestForceSsl(t *testing.T) { |
| 22 | + noopHandler := func(w http.ResponseWriter, r *http.Request) {} |
| 23 | + forceSsl := ForceSsl(http.HandlerFunc(noopHandler)) |
| 24 | + |
| 25 | + for _, tt := range testCases { |
| 26 | + getenv = func(key string) string { |
| 27 | + switch key { |
| 28 | + case goEnviron: |
| 29 | + return tt.goEnv |
| 30 | + default: |
| 31 | + return "" |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + t.Run(tt.goEnv+"_"+tt.proto, func(t *testing.T) { |
| 36 | + req := httptest.NewRequest("", "/test", nil) |
| 37 | + req.Header.Add(xForwardedProtoHeader, tt.proto) |
| 38 | + |
| 39 | + res := httptest.NewRecorder() |
| 40 | + forceSsl.ServeHTTP(res, req) |
| 41 | + |
| 42 | + if location := res.Header().Get("Location"); location != tt.expectLoc { |
| 43 | + t.Errorf("expected Location header '%s', got '%s'", |
| 44 | + tt.expectLoc, location) |
| 45 | + } |
| 46 | + }) |
| 47 | + } |
| 48 | +} |
| 49 | + |
0 commit comments