|
| 1 | +package server |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "encoding/json" |
| 6 | + "net/http" |
| 7 | + "net/http/httptest" |
| 8 | + "os" |
| 9 | + "testing" |
| 10 | +) |
| 11 | + |
| 12 | +func TestGetBodyLimit(t *testing.T) { |
| 13 | + // Table-driven tests for environment variable parsing |
| 14 | + tests := []struct { |
| 15 | + name string |
| 16 | + envValue string |
| 17 | + expected int64 |
| 18 | + }{ |
| 19 | + { |
| 20 | + name: "Default value when unset", |
| 21 | + envValue: "", |
| 22 | + expected: 150 << 20, // 150 MB |
| 23 | + }, |
| 24 | + { |
| 25 | + name: "Valid custom value", |
| 26 | + envValue: "500", |
| 27 | + expected: 500 << 20, // 500 MB |
| 28 | + }, |
| 29 | + { |
| 30 | + name: "Invalid string falls back to default", |
| 31 | + envValue: "not_a_number", |
| 32 | + expected: 150 << 20, // 150 MB |
| 33 | + }, |
| 34 | + } |
| 35 | + |
| 36 | + for _, tt := range tests { |
| 37 | + t.Run(tt.name, func(t *testing.T) { |
| 38 | + // Setup environment |
| 39 | + if tt.envValue != "" { |
| 40 | + os.Setenv("MAX_BODY_SIZE_MB", tt.envValue) |
| 41 | + } else { |
| 42 | + os.Unsetenv("MAX_BODY_SIZE_MB") |
| 43 | + } |
| 44 | + |
| 45 | + // Clean up after test |
| 46 | + t.Cleanup(func() { |
| 47 | + os.Unsetenv("MAX_BODY_SIZE_MB") |
| 48 | + }) |
| 49 | + |
| 50 | + got := getBodyLimit() |
| 51 | + if got != tt.expected { |
| 52 | + t.Errorf("getBodyLimit() = %d bytes, want %d bytes", got, tt.expected) |
| 53 | + } |
| 54 | + }) |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +func TestReadBody_LimitExceeded(t *testing.T) { |
| 59 | + // Set the limit artificially low (1 MB) |
| 60 | + os.Setenv("MAX_BODY_SIZE_MB", "1") |
| 61 | + t.Cleanup(func() { os.Unsetenv("MAX_BODY_SIZE_MB") }) |
| 62 | + |
| 63 | + // Create a 2MB dummy payload |
| 64 | + bigPayload := bytes.Repeat([]byte("a"), 2<<20) |
| 65 | + |
| 66 | + req := httptest.NewRequest(http.MethodPost, "/validate", bytes.NewReader(bigPayload)) |
| 67 | + w := httptest.NewRecorder() |
| 68 | + |
| 69 | + _, err := readBody(w, req) |
| 70 | + if err == nil { |
| 71 | + t.Fatal("Expected an error when reading body larger than limit, got nil") |
| 72 | + } |
| 73 | + |
| 74 | + // http.MaxBytesReader returns an error containing "request body too large" |
| 75 | + if err.Error() != "http: request body too large" { |
| 76 | + t.Errorf("Expected 'request body too large' error, got: %v", err) |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +func TestValidateHandler_PayloadTooLarge(t *testing.T) { |
| 81 | + // Set the limit artificially low (1 MB) |
| 82 | + os.Setenv("MAX_BODY_SIZE_MB", "1") |
| 83 | + t.Cleanup(func() { os.Unsetenv("MAX_BODY_SIZE_MB") }) |
| 84 | + |
| 85 | + // We can pass nil for the validator because the body limit check |
| 86 | + // happens before the handler attempts to use the validator! |
| 87 | + handler := NewHandler(nil) |
| 88 | + |
| 89 | + // Create a 2MB dummy payload |
| 90 | + bigPayload := bytes.Repeat([]byte(`{"type":"Feature"}`), 100000) |
| 91 | + |
| 92 | + req := httptest.NewRequest(http.MethodPost, "/validate", bytes.NewReader(bigPayload)) |
| 93 | + w := httptest.NewRecorder() |
| 94 | + |
| 95 | + handler.Validate(w, req) |
| 96 | + |
| 97 | + res := w.Result() |
| 98 | + defer res.Body.Close() |
| 99 | + |
| 100 | + // Ensure the server rejected it as a Bad Request |
| 101 | + if res.StatusCode != http.StatusBadRequest { |
| 102 | + t.Errorf("Expected status %d, got %d", http.StatusBadRequest, res.StatusCode) |
| 103 | + } |
| 104 | + |
| 105 | + // Verify the JSON error message |
| 106 | + var errResponse map[string]string |
| 107 | + if err := json.NewDecoder(res.Body).Decode(&errResponse); err != nil { |
| 108 | + t.Fatalf("Failed to decode response JSON: %v", err) |
| 109 | + } |
| 110 | + |
| 111 | + expectedErrFragment := "could not read request body" |
| 112 | + if errStr, ok := errResponse["error"]; !ok || len(errStr) < len(expectedErrFragment) || errStr[:len(expectedErrFragment)] != expectedErrFragment { |
| 113 | + t.Errorf("Expected error starting with '%s', got '%s'", expectedErrFragment, errStr) |
| 114 | + } |
| 115 | +} |
| 116 | + |
| 117 | +func TestHealthHandler(t *testing.T) { |
| 118 | + handler := NewHandler(nil) |
| 119 | + |
| 120 | + req := httptest.NewRequest(http.MethodGet, "/health", nil) |
| 121 | + w := httptest.NewRecorder() |
| 122 | + |
| 123 | + handler.Health(w, req) |
| 124 | + |
| 125 | + res := w.Result() |
| 126 | + defer res.Body.Close() |
| 127 | + |
| 128 | + if res.StatusCode != http.StatusOK { |
| 129 | + t.Errorf("Expected status 200, got %d", res.StatusCode) |
| 130 | + } |
| 131 | + |
| 132 | + var response map[string]string |
| 133 | + if err := json.NewDecoder(res.Body).Decode(&response); err != nil { |
| 134 | + t.Fatalf("Failed to decode response: %v", err) |
| 135 | + } |
| 136 | + |
| 137 | + if status, ok := response["status"]; !ok || status != "ok" { |
| 138 | + t.Errorf("Expected status='ok', got %v", response) |
| 139 | + } |
| 140 | +} |
0 commit comments