(t *testing.T)
| 371 | } |
| 372 | |
| 373 | func TestServer_CORSOrigins(t *testing.T) { |
| 374 | cases := []struct { |
| 375 | name string |
| 376 | allowedOrigins []string |
| 377 | originHeader string |
| 378 | expectedStatusCode int |
| 379 | expectedCORSOrigin string |
| 380 | expectCORSOriginHeader bool |
| 381 | validationErrorMsg string |
| 382 | }{ |
| 383 | { |
| 384 | name: "wildcard origins - any origin allowed", |
| 385 | allowedOrigins: []string{"*"}, |
| 386 | originHeader: "https://example.com", |
| 387 | expectedStatusCode: http.StatusOK, |
| 388 | expectedCORSOrigin: "*", |
| 389 | expectCORSOriginHeader: true, |
| 390 | }, |
| 391 | { |
| 392 | name: "wildcard origins - malicious origin allowed", |
| 393 | allowedOrigins: []string{"*"}, |
| 394 | originHeader: "http://malicious.com", |
| 395 | expectedStatusCode: http.StatusOK, |
| 396 | expectedCORSOrigin: "*", |
| 397 | expectCORSOriginHeader: true, |
| 398 | }, |
| 399 | { |
| 400 | name: "specific origins - valid origin allowed https", |
| 401 | allowedOrigins: []string{"https://localhost:3000", "http://app.example.com"}, |
| 402 | originHeader: "https://localhost:3000", |
| 403 | expectedStatusCode: http.StatusOK, |
| 404 | expectedCORSOrigin: "https://localhost:3000", |
| 405 | expectCORSOriginHeader: true, |
| 406 | }, |
| 407 | { |
| 408 | name: "specific origins - valid origin allowed http", |
| 409 | allowedOrigins: []string{"https://localhost:3000", "http://app.example.com"}, |
| 410 | originHeader: "http://app.example.com", |
| 411 | expectedStatusCode: http.StatusOK, |
| 412 | expectedCORSOrigin: "http://app.example.com", |
| 413 | expectCORSOriginHeader: true, |
| 414 | }, |
| 415 | { |
| 416 | name: "specific origins - invalid origin rejected", |
| 417 | allowedOrigins: []string{"https://localhost:3000", "http://app.example.com"}, |
| 418 | originHeader: "https://malicious.com", |
| 419 | expectedStatusCode: http.StatusOK, // Server allows request - CORS is enforced by browser |
| 420 | expectCORSOriginHeader: false, |
| 421 | }, |
| 422 | { |
| 423 | name: "no origin header - request not coming from a browser", |
| 424 | allowedOrigins: []string{"https://example.com"}, |
| 425 | originHeader: "", |
| 426 | expectedStatusCode: http.StatusOK, |
| 427 | }, |
| 428 | { |
| 429 | name: "allowed origins must not be empty", |
| 430 | allowedOrigins: []string{}, |
nothing calls this directly
no test coverage detected