Skip to content

Commit eab76f0

Browse files
committed
added cors creds
1 parent cfc1c61 commit eab76f0

File tree

4 files changed

+147
-86
lines changed

4 files changed

+147
-86
lines changed

coverage.out

Lines changed: 64 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -88,64 +88,67 @@ github.com/GolangToolKits/grrt/reqRoute.go:273.14,274.41 1 1
8888
github.com/GolangToolKits/grrt/reqRoute.go:274.41,276.4 1 1
8989
github.com/GolangToolKits/grrt/reqRoute.go:278.8,280.3 1 1
9090
github.com/GolangToolKits/grrt/reqRoute.go:284.55,288.2 3 1
91-
github.com/GolangToolKits/grrt/reqRouter.go:35.71,42.53 3 1
92-
github.com/GolangToolKits/grrt/reqRouter.go:42.53,45.3 2 1
93-
github.com/GolangToolKits/grrt/reqRouter.go:45.8,48.16 3 1
94-
github.com/GolangToolKits/grrt/reqRouter.go:56.3,56.34 1 1
95-
github.com/GolangToolKits/grrt/reqRouter.go:48.16,51.23 3 1
96-
github.com/GolangToolKits/grrt/reqRouter.go:51.23,54.5 1 1
97-
github.com/GolangToolKits/grrt/reqRouter.go:56.34,58.4 1 1
98-
github.com/GolangToolKits/grrt/reqRouter.go:58.9,58.43 1 1
99-
github.com/GolangToolKits/grrt/reqRouter.go:58.43,60.4 1 1
100-
github.com/GolangToolKits/grrt/reqRouter.go:60.9,63.4 2 1
101-
github.com/GolangToolKits/grrt/reqRouter.go:87.38,91.2 3 1
102-
github.com/GolangToolKits/grrt/reqRouter.go:94.69,97.16 3 1
103-
github.com/GolangToolKits/grrt/reqRouter.go:111.2,111.11 1 1
104-
github.com/GolangToolKits/grrt/reqRouter.go:97.16,99.3 1 1
105-
github.com/GolangToolKits/grrt/reqRouter.go:99.8,101.28 2 1
106-
github.com/GolangToolKits/grrt/reqRouter.go:107.3,107.12 1 1
107-
github.com/GolangToolKits/grrt/reqRouter.go:101.28,102.55 1 1
108-
github.com/GolangToolKits/grrt/reqRouter.go:102.55,105.5 2 1
109-
github.com/GolangToolKits/grrt/reqRouter.go:107.12,109.4 1 1
110-
github.com/GolangToolKits/grrt/reqRouter.go:115.95,118.16 3 1
111-
github.com/GolangToolKits/grrt/reqRouter.go:132.2,132.11 1 1
112-
github.com/GolangToolKits/grrt/reqRouter.go:118.16,120.3 1 1
113-
github.com/GolangToolKits/grrt/reqRouter.go:120.8,122.28 2 1
114-
github.com/GolangToolKits/grrt/reqRouter.go:128.3,128.12 1 1
115-
github.com/GolangToolKits/grrt/reqRouter.go:122.28,123.55 1 1
116-
github.com/GolangToolKits/grrt/reqRouter.go:123.55,126.5 2 1
117-
github.com/GolangToolKits/grrt/reqRouter.go:128.12,130.4 1 1
118-
github.com/GolangToolKits/grrt/reqRouter.go:136.49,139.16 3 1
119-
github.com/GolangToolKits/grrt/reqRouter.go:142.2,142.11 1 1
120-
github.com/GolangToolKits/grrt/reqRouter.go:139.16,141.3 1 1
121-
github.com/GolangToolKits/grrt/reqRouter.go:146.34,148.2 1 1
122-
github.com/GolangToolKits/grrt/reqRouter.go:151.55,154.28 3 1
123-
github.com/GolangToolKits/grrt/reqRouter.go:154.28,156.20 2 1
124-
github.com/GolangToolKits/grrt/reqRouter.go:159.3,159.55 1 1
125-
github.com/GolangToolKits/grrt/reqRouter.go:156.20,157.12 1 1
126-
github.com/GolangToolKits/grrt/reqRouter.go:164.55,168.2 3 1
127-
github.com/GolangToolKits/grrt/reqRouter.go:171.56,174.28 3 1
128-
github.com/GolangToolKits/grrt/reqRouter.go:174.28,176.20 2 1
129-
github.com/GolangToolKits/grrt/reqRouter.go:179.3,179.55 1 1
130-
github.com/GolangToolKits/grrt/reqRouter.go:176.20,177.12 1 1
131-
github.com/GolangToolKits/grrt/reqRouter.go:183.55,189.2 5 1
132-
github.com/GolangToolKits/grrt/reqRouter.go:191.49,195.2 3 1
133-
github.com/GolangToolKits/grrt/reqRouter.go:197.70,205.23 7 1
134-
github.com/GolangToolKits/grrt/reqRouter.go:226.2,226.19 1 1
135-
github.com/GolangToolKits/grrt/reqRouter.go:205.23,206.13 1 1
136-
github.com/GolangToolKits/grrt/reqRouter.go:211.3,213.17 3 1
137-
github.com/GolangToolKits/grrt/reqRouter.go:206.13,207.12 1 1
138-
github.com/GolangToolKits/grrt/reqRouter.go:208.9,208.19 1 1
139-
github.com/GolangToolKits/grrt/reqRouter.go:208.19,209.9 1 1
140-
github.com/GolangToolKits/grrt/reqRouter.go:213.17,214.28 1 1
141-
github.com/GolangToolKits/grrt/reqRouter.go:214.28,215.38 1 1
142-
github.com/GolangToolKits/grrt/reqRouter.go:215.38,219.11 4 1
143-
github.com/GolangToolKits/grrt/reqRouter.go:222.9,224.4 1 1
144-
github.com/GolangToolKits/grrt/reqRouter.go:229.96,231.36 2 1
145-
github.com/GolangToolKits/grrt/reqRouter.go:236.2,237.27 2 1
146-
github.com/GolangToolKits/grrt/reqRouter.go:231.36,232.32 1 1
147-
github.com/GolangToolKits/grrt/reqRouter.go:232.32,234.4 1 1
148-
github.com/GolangToolKits/grrt/router.go:25.25,28.2 2 1
149-
github.com/GolangToolKits/grrt/router.go:31.46,33.49 2 1
150-
github.com/GolangToolKits/grrt/router.go:36.2,36.12 1 1
151-
github.com/GolangToolKits/grrt/router.go:33.49,35.3 1 1
91+
github.com/GolangToolKits/grrt/reqRouter.go:37.71,44.53 3 1
92+
github.com/GolangToolKits/grrt/reqRouter.go:44.53,47.3 2 1
93+
github.com/GolangToolKits/grrt/reqRouter.go:47.8,50.16 3 1
94+
github.com/GolangToolKits/grrt/reqRouter.go:58.3,58.34 1 1
95+
github.com/GolangToolKits/grrt/reqRouter.go:50.16,53.23 3 1
96+
github.com/GolangToolKits/grrt/reqRouter.go:53.23,56.5 1 1
97+
github.com/GolangToolKits/grrt/reqRouter.go:58.34,60.4 1 1
98+
github.com/GolangToolKits/grrt/reqRouter.go:60.9,60.43 1 1
99+
github.com/GolangToolKits/grrt/reqRouter.go:60.43,62.4 1 1
100+
github.com/GolangToolKits/grrt/reqRouter.go:62.9,65.4 2 1
101+
github.com/GolangToolKits/grrt/reqRouter.go:89.38,93.2 3 1
102+
github.com/GolangToolKits/grrt/reqRouter.go:96.69,99.16 3 1
103+
github.com/GolangToolKits/grrt/reqRouter.go:113.2,113.11 1 1
104+
github.com/GolangToolKits/grrt/reqRouter.go:99.16,101.3 1 1
105+
github.com/GolangToolKits/grrt/reqRouter.go:101.8,103.28 2 1
106+
github.com/GolangToolKits/grrt/reqRouter.go:109.3,109.12 1 1
107+
github.com/GolangToolKits/grrt/reqRouter.go:103.28,104.55 1 1
108+
github.com/GolangToolKits/grrt/reqRouter.go:104.55,107.5 2 1
109+
github.com/GolangToolKits/grrt/reqRouter.go:109.12,111.4 1 1
110+
github.com/GolangToolKits/grrt/reqRouter.go:117.95,120.16 3 1
111+
github.com/GolangToolKits/grrt/reqRouter.go:134.2,134.11 1 1
112+
github.com/GolangToolKits/grrt/reqRouter.go:120.16,122.3 1 1
113+
github.com/GolangToolKits/grrt/reqRouter.go:122.8,124.28 2 1
114+
github.com/GolangToolKits/grrt/reqRouter.go:130.3,130.12 1 1
115+
github.com/GolangToolKits/grrt/reqRouter.go:124.28,125.55 1 1
116+
github.com/GolangToolKits/grrt/reqRouter.go:125.55,128.5 2 1
117+
github.com/GolangToolKits/grrt/reqRouter.go:130.12,132.4 1 1
118+
github.com/GolangToolKits/grrt/reqRouter.go:138.49,141.16 3 1
119+
github.com/GolangToolKits/grrt/reqRouter.go:144.2,144.11 1 1
120+
github.com/GolangToolKits/grrt/reqRouter.go:141.16,143.3 1 1
121+
github.com/GolangToolKits/grrt/reqRouter.go:148.34,150.2 1 1
122+
github.com/GolangToolKits/grrt/reqRouter.go:153.44,155.2 1 1
123+
github.com/GolangToolKits/grrt/reqRouter.go:158.55,161.28 3 1
124+
github.com/GolangToolKits/grrt/reqRouter.go:161.28,163.20 2 1
125+
github.com/GolangToolKits/grrt/reqRouter.go:166.3,166.55 1 1
126+
github.com/GolangToolKits/grrt/reqRouter.go:163.20,164.12 1 1
127+
github.com/GolangToolKits/grrt/reqRouter.go:171.55,175.2 3 1
128+
github.com/GolangToolKits/grrt/reqRouter.go:178.56,181.28 3 1
129+
github.com/GolangToolKits/grrt/reqRouter.go:181.28,183.20 2 1
130+
github.com/GolangToolKits/grrt/reqRouter.go:186.3,186.55 1 1
131+
github.com/GolangToolKits/grrt/reqRouter.go:183.20,184.12 1 1
132+
github.com/GolangToolKits/grrt/reqRouter.go:190.55,195.28 5 1
133+
github.com/GolangToolKits/grrt/reqRouter.go:198.2,198.30 1 1
134+
github.com/GolangToolKits/grrt/reqRouter.go:195.28,197.3 1 1
135+
github.com/GolangToolKits/grrt/reqRouter.go:201.49,205.2 3 1
136+
github.com/GolangToolKits/grrt/reqRouter.go:207.70,215.23 7 1
137+
github.com/GolangToolKits/grrt/reqRouter.go:236.2,236.19 1 1
138+
github.com/GolangToolKits/grrt/reqRouter.go:215.23,216.13 1 1
139+
github.com/GolangToolKits/grrt/reqRouter.go:221.3,223.17 3 1
140+
github.com/GolangToolKits/grrt/reqRouter.go:216.13,217.12 1 1
141+
github.com/GolangToolKits/grrt/reqRouter.go:218.9,218.19 1 1
142+
github.com/GolangToolKits/grrt/reqRouter.go:218.19,219.9 1 1
143+
github.com/GolangToolKits/grrt/reqRouter.go:223.17,224.28 1 1
144+
github.com/GolangToolKits/grrt/reqRouter.go:224.28,225.38 1 1
145+
github.com/GolangToolKits/grrt/reqRouter.go:225.38,229.11 4 1
146+
github.com/GolangToolKits/grrt/reqRouter.go:232.9,234.4 1 1
147+
github.com/GolangToolKits/grrt/reqRouter.go:239.96,241.36 2 1
148+
github.com/GolangToolKits/grrt/reqRouter.go:246.2,247.27 2 1
149+
github.com/GolangToolKits/grrt/reqRouter.go:241.36,242.32 1 1
150+
github.com/GolangToolKits/grrt/reqRouter.go:242.32,244.4 1 1
151+
github.com/GolangToolKits/grrt/router.go:26.25,29.2 2 1
152+
github.com/GolangToolKits/grrt/router.go:32.46,34.49 2 1
153+
github.com/GolangToolKits/grrt/router.go:37.2,37.12 1 1
154+
github.com/GolangToolKits/grrt/router.go:34.49,36.3 1 1

reqRouter.go

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,22 @@ import (
1515
type contextKey int
1616

1717
const (
18-
varsKey contextKey = iota
19-
corsAllowOriginHeader string = "Access-Control-Allow-Origin"
20-
corsAllowHeadersHeader string = "Access-Control-Allow-Headers"
21-
corsAllowMethodsHeader string = "Access-Control-Allow-Methods"
18+
varsKey contextKey = iota
19+
corsAllowOriginHeader string = "Access-Control-Allow-Origin"
20+
corsAllowHeadersHeader string = "Access-Control-Allow-Headers"
21+
corsAllowMethodsHeader string = "Access-Control-Allow-Methods"
22+
corsAllowCredentialsHeader string = "Access-Control-Allow-Credentials"
2223
)
2324

2425
// ReqRouter RequestRouter
2526
type ReqRouter struct {
26-
namedRoutes map[string]*[]Route
27-
prefixRoutes map[string]Route
28-
corsEnabled bool
29-
allowedHeaders []string
30-
allowedOrigins []string
31-
allowedMethods []string
27+
namedRoutes map[string]*[]Route
28+
prefixRoutes map[string]Route
29+
corsEnabled bool
30+
corsAllowCredentials bool
31+
allowedHeaders []string
32+
allowedOrigins []string
33+
allowedMethods []string
3234
}
3335

3436
// ServeHTTP ServeHTTP dispatches the handler registered in the matched route.
@@ -147,6 +149,11 @@ func (t *ReqRouter) EnableCORS() {
147149
t.corsEnabled = true
148150
}
149151

152+
// CORSAllowCredentials CORSAllowCredentials
153+
func (t *ReqRouter) CORSAllowCredentials() {
154+
t.corsAllowCredentials = true
155+
}
156+
150157
// SetCorsAllowedHeaders SetAllowedHeaders
151158
func (t *ReqRouter) SetCorsAllowedHeaders(hdr string) {
152159
hdr = strings.ReplaceAll(hdr, " ", "")
@@ -185,6 +192,9 @@ func (t *ReqRouter) handleCors(w http.ResponseWriter) {
185192
w.Header().Set(corsAllowOriginHeader, strings.Join(t.allowedOrigins, ", "))
186193
w.Header().Set(corsAllowHeadersHeader, strings.Join(t.allowedHeaders, ", "))
187194
w.Header().Set(corsAllowMethodsHeader, strings.Join(t.allowedMethods, ", "))
195+
if t.corsAllowCredentials {
196+
w.Header().Set(corsAllowCredentialsHeader, "true")
197+
}
188198
w.WriteHeader(http.StatusOK)
189199
}
190200

reqRouter_test.go

Lines changed: 62 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -937,12 +937,13 @@ func TestReqRouter_AllowedMethods(t *testing.T) {
937937
func TestReqRouter_handleCors(t *testing.T) {
938938
tw := httptest.NewRecorder()
939939
type fields struct {
940-
namedRoutes map[string]*[]Route
941-
prefixRoutes map[string]Route
942-
corsEnabled bool
943-
allowedHeaders []string
944-
allowedOrigins []string
945-
allowedMethods []string
940+
namedRoutes map[string]*[]Route
941+
prefixRoutes map[string]Route
942+
corsEnabled bool
943+
corsAllowCredentials bool
944+
allowedHeaders []string
945+
allowedOrigins []string
946+
allowedMethods []string
946947
}
947948
type args struct {
948949
w http.ResponseWriter
@@ -956,9 +957,10 @@ func TestReqRouter_handleCors(t *testing.T) {
956957
{
957958
name: "test 1",
958959
fields: fields{
959-
allowedHeaders: []string{"Content-Type"},
960-
allowedOrigins: []string{"test"},
961-
allowedMethods: []string{"POST", "GET"},
960+
allowedHeaders: []string{"Content-Type"},
961+
allowedOrigins: []string{"test"},
962+
allowedMethods: []string{"POST", "GET"},
963+
corsAllowCredentials: true,
962964
},
963965
args: args{
964966
w: tw,
@@ -968,12 +970,13 @@ func TestReqRouter_handleCors(t *testing.T) {
968970
for _, tt := range tests {
969971
t.Run(tt.name, func(t *testing.T) {
970972
tr := ReqRouter{
971-
namedRoutes: tt.fields.namedRoutes,
972-
prefixRoutes: tt.fields.prefixRoutes,
973-
corsEnabled: tt.fields.corsEnabled,
974-
allowedHeaders: tt.fields.allowedHeaders,
975-
allowedOrigins: tt.fields.allowedOrigins,
976-
allowedMethods: tt.fields.allowedMethods,
973+
namedRoutes: tt.fields.namedRoutes,
974+
prefixRoutes: tt.fields.prefixRoutes,
975+
corsEnabled: tt.fields.corsEnabled,
976+
corsAllowCredentials: tt.fields.corsAllowCredentials,
977+
allowedHeaders: tt.fields.allowedHeaders,
978+
allowedOrigins: tt.fields.allowedOrigins,
979+
allowedMethods: tt.fields.allowedMethods,
977980
}
978981

979982
tr.handleCors(tt.args.w)
@@ -990,6 +993,50 @@ func TestReqRouter_handleCors(t *testing.T) {
990993
if tw.Header().Get("Access-Control-Allow-Methods") != "POST, GET" {
991994
t.Fail()
992995
}
996+
if tw.Header().Get("Access-Control-Allow-Credentials") != "true" {
997+
t.Fail()
998+
}
999+
})
1000+
}
1001+
}
1002+
1003+
func TestReqRouter_CORSAllowCredentials(t *testing.T) {
1004+
type fields struct {
1005+
namedRoutes map[string]*[]Route
1006+
prefixRoutes map[string]Route
1007+
corsEnabled bool
1008+
corsAllowCredentials bool
1009+
allowedHeaders []string
1010+
allowedOrigins []string
1011+
allowedMethods []string
1012+
}
1013+
tests := []struct {
1014+
name string
1015+
fields fields
1016+
}{
1017+
// TODO: Add test cases.
1018+
{
1019+
name: "test 1",
1020+
fields: fields{
1021+
corsAllowCredentials: true,
1022+
},
1023+
},
1024+
}
1025+
for _, tt := range tests {
1026+
t.Run(tt.name, func(t *testing.T) {
1027+
tr := &ReqRouter{
1028+
namedRoutes: tt.fields.namedRoutes,
1029+
prefixRoutes: tt.fields.prefixRoutes,
1030+
corsEnabled: tt.fields.corsEnabled,
1031+
corsAllowCredentials: tt.fields.corsAllowCredentials,
1032+
allowedHeaders: tt.fields.allowedHeaders,
1033+
allowedOrigins: tt.fields.allowedOrigins,
1034+
allowedMethods: tt.fields.allowedMethods,
1035+
}
1036+
tr.CORSAllowCredentials()
1037+
if tr.corsAllowCredentials != true{
1038+
t.Fail()
1039+
}
9931040
})
9941041
}
9951042
}

router.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ type Router interface {
1616

1717
//CORS methods
1818
EnableCORS()
19+
CORSAllowCredentials()
1920
SetCorsAllowedHeaders(headers string)
2021
SetCorsAllowedOrigins(origins string)
2122
SetCorsAllowedMethods(methods string)

0 commit comments

Comments
 (0)