Skip to content

Commit fdee240

Browse files
committed
Fix CORS module structure and tests
1 parent 0e64db6 commit fdee240

12 files changed

Lines changed: 213 additions & 0 deletions

File tree

cors/v1/README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# CORS v1
2+
3+
This module provides basic CORS header functionality for HTTP responses.
4+
5+
## Usage
6+
7+
```go
8+
import "github.com/DakshithaS/go-multi-major-modules/cors/v1"
9+
10+
cors.AddCORSHeaders(w)
11+
```

cors/v1/cors_test.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package cors_test
2+
3+
import (
4+
"net/http/httptest"
5+
"testing"
6+
7+
cors "github.com/DakshithaS/go-multi-major-modules/cors/v1/src"
8+
)
9+
10+
func TestAddCORSHeaders(t *testing.T) {
11+
w := httptest.NewRecorder()
12+
cors.AddCORSHeaders(w)
13+
14+
if w.Header().Get("Access-Control-Allow-Origin") != "*" {
15+
t.Errorf("Expected Access-Control-Allow-Origin to be '*', got %s", w.Header().Get("Access-Control-Allow-Origin"))
16+
}
17+
if w.Header().Get("Access-Control-Allow-Methods") != "GET, POST, PUT, DELETE, OPTIONS" {
18+
t.Errorf("Expected Access-Control-Allow-Methods to be 'GET, POST, PUT, DELETE, OPTIONS', got %s", w.Header().Get("Access-Control-Allow-Methods"))
19+
}
20+
if w.Header().Get("Access-Control-Allow-Headers") != "Content-Type, Authorization" {
21+
t.Errorf("Expected Access-Control-Allow-Headers to be 'Content-Type, Authorization', got %s", w.Header().Get("Access-Control-Allow-Headers"))
22+
}
23+
}

cors/v1/go.mod

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module github.com/DakshithaS/go-multi-major-modules/cors/v1
2+
3+
go 1.21

cors/v1/src/cors.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package cors
2+
3+
import "net/http"
4+
5+
// AddCORSHeaders adds basic CORS headers to the response
6+
func AddCORSHeaders(w http.ResponseWriter) {
7+
w.Header().Set("Access-Control-Allow-Origin", "*")
8+
w.Header().Set("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS")
9+
w.Header().Set("Access-Control-Allow-Headers", "Content-Type, Authorization")
10+
}

cors/v2/README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# CORS v2
2+
3+
This module provides configurable CORS header functionality for HTTP responses.
4+
5+
## Usage
6+
7+
```go
8+
import "github.com/DakshithaS/go-multi-major-modules/cors/v2"
9+
10+
options := cors.CORSOptions{
11+
AllowedOrigins: []string{"http://example.com"},
12+
AllowedMethods: []string{"GET", "POST"},
13+
AllowedHeaders: []string{"Content-Type"},
14+
}
15+
16+
cors.AddCORSHeadersWithOptions(w, options)
17+
```

cors/v2/cors_test.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package cors_test
2+
3+
import (
4+
"net/http/httptest"
5+
"testing"
6+
7+
cors "github.com/DakshithaS/go-multi-major-modules/cors/v2/src"
8+
)
9+
10+
func TestAddCORSHeadersWithOptions(t *testing.T) {
11+
w := httptest.NewRecorder()
12+
options := cors.CORSOptions{
13+
AllowedOrigins: []string{"http://example.com"},
14+
AllowedMethods: []string{"GET", "POST"},
15+
AllowedHeaders: []string{"Content-Type"},
16+
AllowCredentials: true,
17+
}
18+
cors.AddCORSHeadersWithOptions(w, options)
19+
20+
if w.Header().Get("Access-Control-Allow-Origin") != "http://example.com" {
21+
t.Errorf("Expected Access-Control-Allow-Origin to be 'http://example.com', got %s", w.Header().Get("Access-Control-Allow-Origin"))
22+
}
23+
if w.Header().Get("Access-Control-Allow-Methods") != "GET, POST" {
24+
t.Errorf("Expected Access-Control-Allow-Methods to be 'GET, POST', got %s", w.Header().Get("Access-Control-Allow-Methods"))
25+
}
26+
if w.Header().Get("Access-Control-Allow-Headers") != "Content-Type" {
27+
t.Errorf("Expected Access-Control-Allow-Headers to be 'Content-Type', got %s", w.Header().Get("Access-Control-Allow-Headers"))
28+
}
29+
if w.Header().Get("Access-Control-Allow-Credentials") != "true" {
30+
t.Errorf("Expected Access-Control-Allow-Credentials to be 'true', got %s", w.Header().Get("Access-Control-Allow-Credentials"))
31+
}
32+
}

cors/v2/go.mod

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module github.com/DakshithaS/go-multi-major-modules/cors/v2
2+
3+
go 1.21

cors/v2/src/cors.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package cors
2+
3+
import "net/http"
4+
5+
// CORSOptions holds configuration for CORS headers
6+
type CORSOptions struct {
7+
AllowedOrigins []string
8+
AllowedMethods []string
9+
AllowedHeaders []string
10+
AllowCredentials bool
11+
}
12+
13+
// AddCORSHeadersWithOptions adds CORS headers based on provided options
14+
func AddCORSHeadersWithOptions(w http.ResponseWriter, options CORSOptions) {
15+
if len(options.AllowedOrigins) > 0 {
16+
w.Header().Set("Access-Control-Allow-Origin", options.AllowedOrigins[0]) // Simplified: just first one
17+
} else {
18+
w.Header().Set("Access-Control-Allow-Origin", "*")
19+
}
20+
21+
if len(options.AllowedMethods) > 0 {
22+
methods := ""
23+
for i, method := range options.AllowedMethods {
24+
if i > 0 {
25+
methods += ", "
26+
}
27+
methods += method
28+
}
29+
w.Header().Set("Access-Control-Allow-Methods", methods)
30+
} else {
31+
w.Header().Set("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS")
32+
}
33+
34+
if len(options.AllowedHeaders) > 0 {
35+
headers := ""
36+
for i, header := range options.AllowedHeaders {
37+
if i > 0 {
38+
headers += ", "
39+
}
40+
headers += header
41+
}
42+
w.Header().Set("Access-Control-Allow-Headers", headers)
43+
} else {
44+
w.Header().Set("Access-Control-Allow-Headers", "Content-Type, Authorization")
45+
}
46+
47+
if options.AllowCredentials {
48+
w.Header().Set("Access-Control-Allow-Credentials", "true")
49+
}
50+
}

cors/v3/README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# CORS v3
2+
3+
This module provides an interface-based CORS handler for HTTP responses.
4+
5+
## Usage
6+
7+
```go
8+
import "github.com/DakshithaS/go-multi-major-modules/cors/v3"
9+
10+
handler := cors.NewDefaultCORSHandler()
11+
handler.HandleCORS(w, r)
12+
```

cors/v3/cors_test.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package cors_test
2+
3+
import (
4+
"net/http/httptest"
5+
"testing"
6+
7+
cors "github.com/DakshithaS/go-multi-major-modules/cors/v3/src"
8+
)
9+
10+
func TestDefaultCORSHandler_HandleCORS(t *testing.T) {
11+
handler := cors.NewDefaultCORSHandler()
12+
w := httptest.NewRecorder()
13+
r := httptest.NewRequest("GET", "/", nil)
14+
15+
handler.HandleCORS(w, r)
16+
17+
if w.Header().Get("Access-Control-Allow-Origin") != "*" {
18+
t.Errorf("Expected Access-Control-Allow-Origin to be '*', got %s", w.Header().Get("Access-Control-Allow-Origin"))
19+
}
20+
if w.Header().Get("Access-Control-Allow-Methods") != "GET, POST, PUT, DELETE, OPTIONS" {
21+
t.Errorf("Expected Access-Control-Allow-Methods to be 'GET, POST, PUT, DELETE, OPTIONS', got %s", w.Header().Get("Access-Control-Allow-Methods"))
22+
}
23+
if w.Header().Get("Access-Control-Allow-Headers") != "Content-Type, Authorization" {
24+
t.Errorf("Expected Access-Control-Allow-Headers to be 'Content-Type, Authorization', got %s", w.Header().Get("Access-Control-Allow-Headers"))
25+
}
26+
}

0 commit comments

Comments
 (0)