|
| 1 | +package server |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "io" |
| 6 | + "net/http" |
| 7 | + "net/http/httptest" |
| 8 | + "testing" |
| 9 | + |
| 10 | + "github.com/stretchr/testify/assert" |
| 11 | +) |
| 12 | + |
| 13 | +// mockHandler is a simple mock HTTP handler for testing |
| 14 | +type mockHandler struct { |
| 15 | + statusCode int |
| 16 | + response []byte |
| 17 | +} |
| 18 | + |
| 19 | +func (m *mockHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { |
| 20 | + w.WriteHeader(m.statusCode) |
| 21 | + w.Write(m.response) |
| 22 | +} |
| 23 | + |
| 24 | +func TestWebhookBridgeHandler_HTTPMethods(t *testing.T) { |
| 25 | + tests := []struct { |
| 26 | + name string |
| 27 | + method string |
| 28 | + path string |
| 29 | + body []byte |
| 30 | + expectedStatus int |
| 31 | + expectedBody string |
| 32 | + }{ |
| 33 | + { |
| 34 | + name: "invalid method GET", |
| 35 | + method: "GET", |
| 36 | + path: "/billing/webhooks/callback/stripe", |
| 37 | + body: nil, |
| 38 | + expectedStatus: http.StatusMethodNotAllowed, |
| 39 | + expectedBody: "method not allowed", |
| 40 | + }, |
| 41 | + { |
| 42 | + name: "invalid method PUT", |
| 43 | + method: "PUT", |
| 44 | + path: "/billing/webhooks/callback/stripe", |
| 45 | + body: []byte(`{"test":"data"}`), |
| 46 | + expectedStatus: http.StatusMethodNotAllowed, |
| 47 | + expectedBody: "method not allowed", |
| 48 | + }, |
| 49 | + { |
| 50 | + name: "invalid method DELETE", |
| 51 | + method: "DELETE", |
| 52 | + path: "/billing/webhooks/callback/stripe", |
| 53 | + body: nil, |
| 54 | + expectedStatus: http.StatusMethodNotAllowed, |
| 55 | + expectedBody: "method not allowed", |
| 56 | + }, |
| 57 | + } |
| 58 | + |
| 59 | + for _, tt := range tests { |
| 60 | + t.Run(tt.name, func(t *testing.T) { |
| 61 | + mockFrontierHandler := &mockHandler{ |
| 62 | + statusCode: http.StatusOK, |
| 63 | + response: []byte(`{}`), |
| 64 | + } |
| 65 | + |
| 66 | + var body io.Reader |
| 67 | + if tt.body != nil { |
| 68 | + body = bytes.NewReader(tt.body) |
| 69 | + } |
| 70 | + req := httptest.NewRequest(tt.method, tt.path, body) |
| 71 | + rr := httptest.NewRecorder() |
| 72 | + |
| 73 | + bridgeHandler := WebhookBridgeHandler(mockFrontierHandler) |
| 74 | + bridgeHandler.ServeHTTP(rr, req) |
| 75 | + |
| 76 | + assert.Equal(t, tt.expectedStatus, rr.Code) |
| 77 | + assert.Contains(t, rr.Body.String(), tt.expectedBody) |
| 78 | + }) |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +func TestWebhookBridgeHandler_PathParsing(t *testing.T) { |
| 83 | + tests := []struct { |
| 84 | + name string |
| 85 | + path string |
| 86 | + shouldBeValid bool |
| 87 | + }{ |
| 88 | + { |
| 89 | + name: "missing provider", |
| 90 | + path: "/billing/webhooks/callback", |
| 91 | + shouldBeValid: false, |
| 92 | + }, |
| 93 | + { |
| 94 | + name: "missing provider with trailing slash", |
| 95 | + path: "/billing/webhooks/callback/", |
| 96 | + shouldBeValid: false, |
| 97 | + }, |
| 98 | + { |
| 99 | + name: "wrong path - missing webhooks", |
| 100 | + path: "/billing/wrong/callback/stripe", |
| 101 | + shouldBeValid: false, |
| 102 | + }, |
| 103 | + { |
| 104 | + name: "wrong path - missing callback", |
| 105 | + path: "/billing/webhooks/wrong/stripe", |
| 106 | + shouldBeValid: false, |
| 107 | + }, |
| 108 | + { |
| 109 | + name: "wrong path - missing billing prefix", |
| 110 | + path: "/v1beta1/billing/webhooks/callback/stripe", |
| 111 | + shouldBeValid: false, |
| 112 | + }, |
| 113 | + { |
| 114 | + name: "completely wrong path", |
| 115 | + path: "/v2/api/webhooks", |
| 116 | + shouldBeValid: false, |
| 117 | + }, |
| 118 | + } |
| 119 | + |
| 120 | + for _, tt := range tests { |
| 121 | + t.Run(tt.name, func(t *testing.T) { |
| 122 | + mockFrontierHandler := &mockHandler{ |
| 123 | + statusCode: http.StatusOK, |
| 124 | + response: []byte(`{}`), |
| 125 | + } |
| 126 | + req := httptest.NewRequest("POST", tt.path, bytes.NewReader([]byte(`{}`))) |
| 127 | + rr := httptest.NewRecorder() |
| 128 | + |
| 129 | + bridgeHandler := WebhookBridgeHandler(mockFrontierHandler) |
| 130 | + bridgeHandler.ServeHTTP(rr, req) |
| 131 | + |
| 132 | + if tt.shouldBeValid { |
| 133 | + // Should not get 404 for path issues (might get other errors from handler logic) |
| 134 | + assert.NotEqual(t, http.StatusNotFound, rr.Code, "should not return 404 for valid path") |
| 135 | + } else { |
| 136 | + // Should get 404 for invalid paths |
| 137 | + assert.Equal(t, http.StatusNotFound, rr.Code, "should return 404 for invalid path") |
| 138 | + } |
| 139 | + }) |
| 140 | + } |
| 141 | +} |
| 142 | + |
| 143 | +func TestWebhookBridgeHandler_SuccessfulRequest(t *testing.T) { |
| 144 | + // Mock handler that verifies the request was transformed correctly |
| 145 | + mockFrontierHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 146 | + // Verify the request path was changed to ConnectRPC procedure |
| 147 | + assert.Contains(t, r.URL.Path, "BillingWebhookCallback") |
| 148 | + |
| 149 | + // Verify content type |
| 150 | + assert.Equal(t, "application/json", r.Header.Get("Content-Type")) |
| 151 | + |
| 152 | + // Read and verify body was encoded as ConnectRPC request |
| 153 | + body, err := io.ReadAll(r.Body) |
| 154 | + assert.NoError(t, err) |
| 155 | + assert.Contains(t, string(body), "provider") |
| 156 | + assert.Contains(t, string(body), "body") |
| 157 | + |
| 158 | + // Return success |
| 159 | + w.Header().Set("Content-Type", "application/json") |
| 160 | + w.WriteHeader(http.StatusOK) |
| 161 | + w.Write([]byte(`{}`)) |
| 162 | + }) |
| 163 | + |
| 164 | + req := httptest.NewRequest("POST", "/billing/webhooks/callback/stripe", bytes.NewReader([]byte(`{"event":"test"}`))) |
| 165 | + req.Header.Set("Stripe-Signature", "test-signature") |
| 166 | + rr := httptest.NewRecorder() |
| 167 | + |
| 168 | + bridgeHandler := WebhookBridgeHandler(mockFrontierHandler) |
| 169 | + bridgeHandler.ServeHTTP(rr, req) |
| 170 | + |
| 171 | + assert.Equal(t, http.StatusOK, rr.Code) |
| 172 | +} |
0 commit comments