|
| 1 | +package sdk |
| 2 | + |
| 3 | +import ( |
| 4 | + "net/http" |
| 5 | + "net/http/httptest" |
| 6 | + "testing" |
| 7 | +) |
| 8 | + |
| 9 | +func TestApiCorsHeadersWithConfig_Enabled(t *testing.T) { |
| 10 | + handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 11 | + w.WriteHeader(http.StatusOK) |
| 12 | + w.Write([]byte("test response")) |
| 13 | + }) |
| 14 | + |
| 15 | + corsHandler := ApiCorsHeadersWithConfig(true, "*")(handler) |
| 16 | + |
| 17 | + // Test GET request |
| 18 | + req := httptest.NewRequest("GET", "/dev/projects", nil) |
| 19 | + w := httptest.NewRecorder() |
| 20 | + corsHandler.ServeHTTP(w, req) |
| 21 | + |
| 22 | + if w.Header().Get("Access-Control-Allow-Origin") != "*" { |
| 23 | + t.Errorf("Expected Access-Control-Allow-Origin to be '*', got '%s'", w.Header().Get("Access-Control-Allow-Origin")) |
| 24 | + } |
| 25 | + |
| 26 | + if w.Header().Get("Access-Control-Allow-Methods") != "GET,POST,PUT,PATCH,DELETE,OPTIONS" { |
| 27 | + t.Errorf("Expected Access-Control-Allow-Methods to include all methods, got '%s'", w.Header().Get("Access-Control-Allow-Methods")) |
| 28 | + } |
| 29 | + |
| 30 | + if w.Code != http.StatusOK { |
| 31 | + t.Errorf("Expected status code 200, got %d", w.Code) |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +func TestApiCorsHeadersWithConfig_OptionsRequest(t *testing.T) { |
| 36 | + handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 37 | + t.Error("Handler should not be called for OPTIONS request") |
| 38 | + }) |
| 39 | + |
| 40 | + corsHandler := ApiCorsHeadersWithConfig(true, "https://example.com")(handler) |
| 41 | + |
| 42 | + // Test OPTIONS preflight request |
| 43 | + req := httptest.NewRequest("OPTIONS", "/dev/projects", nil) |
| 44 | + w := httptest.NewRecorder() |
| 45 | + corsHandler.ServeHTTP(w, req) |
| 46 | + |
| 47 | + if w.Header().Get("Access-Control-Allow-Origin") != "https://example.com" { |
| 48 | + t.Errorf("Expected Access-Control-Allow-Origin to be 'https://example.com', got '%s'", w.Header().Get("Access-Control-Allow-Origin")) |
| 49 | + } |
| 50 | + |
| 51 | + if w.Code != http.StatusOK { |
| 52 | + t.Errorf("Expected status code 200 for OPTIONS request, got %d", w.Code) |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +func TestApiCorsHeadersWithConfig_Disabled(t *testing.T) { |
| 57 | + handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 58 | + w.WriteHeader(http.StatusOK) |
| 59 | + w.Write([]byte("test response")) |
| 60 | + }) |
| 61 | + |
| 62 | + corsHandler := ApiCorsHeadersWithConfig(false, "*")(handler) |
| 63 | + |
| 64 | + // Test GET request with CORS disabled |
| 65 | + req := httptest.NewRequest("GET", "/dev/projects", nil) |
| 66 | + w := httptest.NewRecorder() |
| 67 | + corsHandler.ServeHTTP(w, req) |
| 68 | + |
| 69 | + if w.Header().Get("Access-Control-Allow-Origin") != "" { |
| 70 | + t.Errorf("Expected no CORS headers when disabled, but got Access-Control-Allow-Origin: '%s'", w.Header().Get("Access-Control-Allow-Origin")) |
| 71 | + } |
| 72 | + |
| 73 | + if w.Code != http.StatusOK { |
| 74 | + t.Errorf("Expected status code 200, got %d", w.Code) |
| 75 | + } |
| 76 | +} |
0 commit comments