|
| 1 | +package integration_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/stretchr/testify/require" |
| 9 | + "github.com/stretchr/testify/suite" |
| 10 | +) |
| 11 | + |
| 12 | +type ErrorScenariosIntegrationTestSuite struct { |
| 13 | + IntegrationTestSuite |
| 14 | +} |
| 15 | + |
| 16 | +func (suite *ErrorScenariosIntegrationTestSuite) TestErrorScenarios() { |
| 17 | + largePayload := suite.generateLargePayload(1024 * 100) // 100KB payload |
| 18 | + largePayloadJSON, err := json.Marshal(largePayload) |
| 19 | + |
| 20 | + require.NoError(suite.T(), err, "Failed to marshal large payload") |
| 21 | + |
| 22 | + tests := []testInput{ |
| 23 | + { |
| 24 | + name: "invalid-endpoint", |
| 25 | + endpoint: "/integration/non-existent", |
| 26 | + headers: map[string]string{ |
| 27 | + "X-Token": "integration-test", |
| 28 | + }, |
| 29 | + payload: map[string]any{ |
| 30 | + "test": "data", |
| 31 | + }, |
| 32 | + expectedResponse: expectedResponse{ |
| 33 | + statusCode: 404, |
| 34 | + }, |
| 35 | + }, |
| 36 | + { |
| 37 | + // Must not return 400 error to don't lose data |
| 38 | + name: "invalid-json-payload", |
| 39 | + endpoint: "/integration/invalid-json-payload", |
| 40 | + headers: map[string]string{ |
| 41 | + "X-Token": "integration-test", |
| 42 | + "Content-Type": "application/json", |
| 43 | + }, |
| 44 | + payload: "invalid json{", |
| 45 | + expectedResponse: expectedResponse{ |
| 46 | + statusCode: 204, |
| 47 | + }, |
| 48 | + }, |
| 49 | + { |
| 50 | + name: "missing-required-header", |
| 51 | + endpoint: "/integration/basic-usage", |
| 52 | + headers: map[string]string{}, // No X-Token header |
| 53 | + payload: map[string]any{ |
| 54 | + "test": "should fail", |
| 55 | + }, |
| 56 | + expectedResponse: expectedResponse{ |
| 57 | + statusCode: 401, |
| 58 | + }, |
| 59 | + }, |
| 60 | + { |
| 61 | + name: "large-payload-handling", |
| 62 | + endpoint: "/integration/large-payload", |
| 63 | + headers: map[string]string{ |
| 64 | + "X-Token": "integration-test", |
| 65 | + }, |
| 66 | + payload: largePayload, |
| 67 | + expectedResponse: expectedResponse{ |
| 68 | + statusCode: 204, |
| 69 | + }, |
| 70 | + expectedStorage: expectedStorage{ |
| 71 | + storageType: StorageTypeRedis, |
| 72 | + key: "integration:large-payload-events", |
| 73 | + data: string(largePayloadJSON), |
| 74 | + }, |
| 75 | + }, |
| 76 | + { |
| 77 | + name: "webhook-spec-not-found", |
| 78 | + endpoint: "/integration/missing-webhook", |
| 79 | + headers: map[string]string{ |
| 80 | + "X-Token": "integration-test", |
| 81 | + }, |
| 82 | + payload: map[string]any{ |
| 83 | + "test": "should return 404", |
| 84 | + }, |
| 85 | + expectedResponse: expectedResponse{ |
| 86 | + statusCode: 404, |
| 87 | + }, |
| 88 | + }, |
| 89 | + } |
| 90 | + |
| 91 | + for _, test := range tests { |
| 92 | + suite.Run(test.name, func() { |
| 93 | + suite.runErrorTest(test) |
| 94 | + }) |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +func (suite *ErrorScenariosIntegrationTestSuite) runErrorTest(test testInput) { |
| 99 | + if test.name == "concurrent-requests-same-webhook" { |
| 100 | + suite.runConcurrencyTest(test) |
| 101 | + } else { |
| 102 | + suite.runTest(test) |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +func (suite *ErrorScenariosIntegrationTestSuite) runConcurrencyTest(test testInput) { |
| 107 | + // Send multiple concurrent requests |
| 108 | + concurrency := 10 |
| 109 | + results := make(chan int, concurrency) |
| 110 | + |
| 111 | + for i := 0; i < concurrency; i++ { |
| 112 | + go func(id int) { |
| 113 | + testCopy := test |
| 114 | + testCopy.headers["X-Request-ID"] = fmt.Sprintf("concurrent-%d", id) |
| 115 | + testCopy.payload = map[string]any{ |
| 116 | + "request_id": id, |
| 117 | + "data": "concurrent test", |
| 118 | + } |
| 119 | + |
| 120 | + suite.runTest(testCopy) |
| 121 | + results <- 1 |
| 122 | + }(i) |
| 123 | + } |
| 124 | + |
| 125 | + // Wait for all requests to complete |
| 126 | + for i := 0; i < concurrency; i++ { |
| 127 | + <-results |
| 128 | + } |
| 129 | +} |
| 130 | + |
| 131 | +func (suite *ErrorScenariosIntegrationTestSuite) generateLargePayload(size int) map[string]any { |
| 132 | + largeString := make([]byte, size) |
| 133 | + for i := range largeString { |
| 134 | + largeString[i] = 'A' + byte(i%26) |
| 135 | + } |
| 136 | + |
| 137 | + return map[string]any{ |
| 138 | + "large_data": string(largeString), |
| 139 | + "metadata": map[string]any{ |
| 140 | + "size": size, |
| 141 | + "timestamp": "2023-06-28T18:30:00Z", |
| 142 | + }, |
| 143 | + } |
| 144 | +} |
| 145 | + |
| 146 | +func TestErrorScenariosIntegrationTestSuite(t *testing.T) { |
| 147 | + suite.Run(t, new(ErrorScenariosIntegrationTestSuite)) |
| 148 | +} |
0 commit comments