Skip to content

Commit 3bffb69

Browse files
committed
changed CORS methods
1 parent 1ac20b6 commit 3bffb69

File tree

4 files changed

+145
-106
lines changed

4 files changed

+145
-106
lines changed

coverage.out

Lines changed: 59 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
11
mode: set
2-
github.com/GolangToolKits/grrt/router.go:25.25,28.2 2 1
3-
github.com/GolangToolKits/grrt/router.go:31.46,33.49 2 1
4-
github.com/GolangToolKits/grrt/router.go:36.2,36.12 1 1
5-
github.com/GolangToolKits/grrt/router.go:33.49,35.3 1 1
62
github.com/GolangToolKits/grrt/reqRoute.go:28.32,32.2 3 1
73
github.com/GolangToolKits/grrt/reqRoute.go:35.56,36.14 1 1
84
github.com/GolangToolKits/grrt/reqRoute.go:39.2,39.10 1 1
@@ -93,59 +89,63 @@ github.com/GolangToolKits/grrt/reqRoute.go:274.41,276.4 1 1
9389
github.com/GolangToolKits/grrt/reqRoute.go:278.8,280.3 1 1
9490
github.com/GolangToolKits/grrt/reqRoute.go:284.55,288.2 3 1
9591
github.com/GolangToolKits/grrt/reqRouter.go:34.70,38.53 1 1
96-
github.com/GolangToolKits/grrt/reqRouter.go:42.2,44.15 3 1
97-
github.com/GolangToolKits/grrt/reqRouter.go:52.2,52.33 1 1
9892
github.com/GolangToolKits/grrt/reqRouter.go:38.53,40.3 1 1
99-
github.com/GolangToolKits/grrt/reqRouter.go:44.15,47.22 3 1
100-
github.com/GolangToolKits/grrt/reqRouter.go:47.22,50.4 1 1
101-
github.com/GolangToolKits/grrt/reqRouter.go:52.33,54.3 1 1
102-
github.com/GolangToolKits/grrt/reqRouter.go:54.8,54.42 1 1
103-
github.com/GolangToolKits/grrt/reqRouter.go:54.42,56.3 1 1
104-
github.com/GolangToolKits/grrt/reqRouter.go:56.8,59.3 2 1
105-
github.com/GolangToolKits/grrt/reqRouter.go:63.37,67.2 3 1
106-
github.com/GolangToolKits/grrt/reqRouter.go:70.68,73.16 3 1
107-
github.com/GolangToolKits/grrt/reqRouter.go:87.2,87.11 1 1
108-
github.com/GolangToolKits/grrt/reqRouter.go:73.16,75.3 1 1
109-
github.com/GolangToolKits/grrt/reqRouter.go:75.8,77.28 2 1
110-
github.com/GolangToolKits/grrt/reqRouter.go:83.3,83.12 1 1
111-
github.com/GolangToolKits/grrt/reqRouter.go:77.28,78.55 1 1
112-
github.com/GolangToolKits/grrt/reqRouter.go:78.55,81.5 2 1
113-
github.com/GolangToolKits/grrt/reqRouter.go:83.12,85.4 1 1
114-
github.com/GolangToolKits/grrt/reqRouter.go:91.94,94.16 3 1
115-
github.com/GolangToolKits/grrt/reqRouter.go:108.2,108.11 1 1
116-
github.com/GolangToolKits/grrt/reqRouter.go:94.16,96.3 1 1
117-
github.com/GolangToolKits/grrt/reqRouter.go:96.8,98.28 2 1
118-
github.com/GolangToolKits/grrt/reqRouter.go:104.3,104.12 1 1
119-
github.com/GolangToolKits/grrt/reqRouter.go:98.28,99.55 1 1
120-
github.com/GolangToolKits/grrt/reqRouter.go:99.55,102.5 2 1
121-
github.com/GolangToolKits/grrt/reqRouter.go:104.12,106.4 1 1
122-
github.com/GolangToolKits/grrt/reqRouter.go:112.48,115.16 3 1
123-
github.com/GolangToolKits/grrt/reqRouter.go:118.2,118.11 1 1
124-
github.com/GolangToolKits/grrt/reqRouter.go:115.16,117.3 1 1
125-
github.com/GolangToolKits/grrt/reqRouter.go:122.33,124.2 1 1
126-
github.com/GolangToolKits/grrt/reqRouter.go:127.56,128.28 1 1
127-
github.com/GolangToolKits/grrt/reqRouter.go:128.28,130.20 2 1
128-
github.com/GolangToolKits/grrt/reqRouter.go:133.3,133.55 1 1
129-
github.com/GolangToolKits/grrt/reqRouter.go:130.20,131.12 1 1
130-
github.com/GolangToolKits/grrt/reqRouter.go:138.53,140.2 1 1
131-
github.com/GolangToolKits/grrt/reqRouter.go:143.53,144.28 1 1
132-
github.com/GolangToolKits/grrt/reqRouter.go:144.28,146.20 2 1
133-
github.com/GolangToolKits/grrt/reqRouter.go:149.3,149.55 1 1
134-
github.com/GolangToolKits/grrt/reqRouter.go:146.20,147.12 1 1
135-
github.com/GolangToolKits/grrt/reqRouter.go:153.54,158.2 4 1
136-
github.com/GolangToolKits/grrt/reqRouter.go:160.48,164.2 3 1
137-
github.com/GolangToolKits/grrt/reqRouter.go:166.69,174.23 8 1
138-
github.com/GolangToolKits/grrt/reqRouter.go:195.2,195.19 1 1
139-
github.com/GolangToolKits/grrt/reqRouter.go:174.23,175.13 1 1
140-
github.com/GolangToolKits/grrt/reqRouter.go:180.3,182.17 3 1
141-
github.com/GolangToolKits/grrt/reqRouter.go:175.13,176.12 1 1
142-
github.com/GolangToolKits/grrt/reqRouter.go:177.9,177.19 1 1
143-
github.com/GolangToolKits/grrt/reqRouter.go:177.19,178.9 1 1
144-
github.com/GolangToolKits/grrt/reqRouter.go:182.17,183.28 1 1
145-
github.com/GolangToolKits/grrt/reqRouter.go:183.28,184.38 1 1
146-
github.com/GolangToolKits/grrt/reqRouter.go:184.38,188.11 4 1
147-
github.com/GolangToolKits/grrt/reqRouter.go:191.9,193.4 1 1
148-
github.com/GolangToolKits/grrt/reqRouter.go:198.95,200.36 2 1
149-
github.com/GolangToolKits/grrt/reqRouter.go:205.2,206.27 2 1
150-
github.com/GolangToolKits/grrt/reqRouter.go:200.36,201.32 1 1
151-
github.com/GolangToolKits/grrt/reqRouter.go:201.32,203.4 1 1
93+
github.com/GolangToolKits/grrt/reqRouter.go:40.8,43.16 3 1
94+
github.com/GolangToolKits/grrt/reqRouter.go:51.3,51.34 1 1
95+
github.com/GolangToolKits/grrt/reqRouter.go:43.16,46.23 3 1
96+
github.com/GolangToolKits/grrt/reqRouter.go:46.23,49.5 1 1
97+
github.com/GolangToolKits/grrt/reqRouter.go:51.34,53.4 1 1
98+
github.com/GolangToolKits/grrt/reqRouter.go:53.9,53.43 1 1
99+
github.com/GolangToolKits/grrt/reqRouter.go:53.43,55.4 1 1
100+
github.com/GolangToolKits/grrt/reqRouter.go:55.9,58.4 2 1
101+
github.com/GolangToolKits/grrt/reqRouter.go:82.37,86.2 3 1
102+
github.com/GolangToolKits/grrt/reqRouter.go:89.68,92.16 3 1
103+
github.com/GolangToolKits/grrt/reqRouter.go:106.2,106.11 1 1
104+
github.com/GolangToolKits/grrt/reqRouter.go:92.16,94.3 1 1
105+
github.com/GolangToolKits/grrt/reqRouter.go:94.8,96.28 2 1
106+
github.com/GolangToolKits/grrt/reqRouter.go:102.3,102.12 1 1
107+
github.com/GolangToolKits/grrt/reqRouter.go:96.28,97.55 1 1
108+
github.com/GolangToolKits/grrt/reqRouter.go:97.55,100.5 2 1
109+
github.com/GolangToolKits/grrt/reqRouter.go:102.12,104.4 1 1
110+
github.com/GolangToolKits/grrt/reqRouter.go:110.94,113.16 3 1
111+
github.com/GolangToolKits/grrt/reqRouter.go:127.2,127.11 1 1
112+
github.com/GolangToolKits/grrt/reqRouter.go:113.16,115.3 1 1
113+
github.com/GolangToolKits/grrt/reqRouter.go:115.8,117.28 2 1
114+
github.com/GolangToolKits/grrt/reqRouter.go:123.3,123.12 1 1
115+
github.com/GolangToolKits/grrt/reqRouter.go:117.28,118.55 1 1
116+
github.com/GolangToolKits/grrt/reqRouter.go:118.55,121.5 2 1
117+
github.com/GolangToolKits/grrt/reqRouter.go:123.12,125.4 1 1
118+
github.com/GolangToolKits/grrt/reqRouter.go:131.48,134.16 3 1
119+
github.com/GolangToolKits/grrt/reqRouter.go:137.2,137.11 1 1
120+
github.com/GolangToolKits/grrt/reqRouter.go:134.16,136.3 1 1
121+
github.com/GolangToolKits/grrt/reqRouter.go:141.33,143.2 1 1
122+
github.com/GolangToolKits/grrt/reqRouter.go:146.54,149.28 3 1
123+
github.com/GolangToolKits/grrt/reqRouter.go:149.28,151.20 2 1
124+
github.com/GolangToolKits/grrt/reqRouter.go:154.3,154.55 1 1
125+
github.com/GolangToolKits/grrt/reqRouter.go:151.20,152.12 1 1
126+
github.com/GolangToolKits/grrt/reqRouter.go:159.54,163.2 3 1
127+
github.com/GolangToolKits/grrt/reqRouter.go:166.55,169.28 3 1
128+
github.com/GolangToolKits/grrt/reqRouter.go:169.28,171.20 2 1
129+
github.com/GolangToolKits/grrt/reqRouter.go:174.3,174.55 1 1
130+
github.com/GolangToolKits/grrt/reqRouter.go:171.20,172.12 1 1
131+
github.com/GolangToolKits/grrt/reqRouter.go:178.54,183.2 4 1
132+
github.com/GolangToolKits/grrt/reqRouter.go:185.48,189.2 3 1
133+
github.com/GolangToolKits/grrt/reqRouter.go:191.69,199.23 7 1
134+
github.com/GolangToolKits/grrt/reqRouter.go:220.2,220.19 1 1
135+
github.com/GolangToolKits/grrt/reqRouter.go:199.23,200.13 1 1
136+
github.com/GolangToolKits/grrt/reqRouter.go:205.3,207.17 3 1
137+
github.com/GolangToolKits/grrt/reqRouter.go:200.13,201.12 1 1
138+
github.com/GolangToolKits/grrt/reqRouter.go:202.9,202.19 1 1
139+
github.com/GolangToolKits/grrt/reqRouter.go:202.19,203.9 1 1
140+
github.com/GolangToolKits/grrt/reqRouter.go:207.17,208.28 1 1
141+
github.com/GolangToolKits/grrt/reqRouter.go:208.28,209.38 1 1
142+
github.com/GolangToolKits/grrt/reqRouter.go:209.38,213.11 4 1
143+
github.com/GolangToolKits/grrt/reqRouter.go:216.9,218.4 1 1
144+
github.com/GolangToolKits/grrt/reqRouter.go:223.95,225.36 2 1
145+
github.com/GolangToolKits/grrt/reqRouter.go:230.2,231.27 2 1
146+
github.com/GolangToolKits/grrt/reqRouter.go:225.36,226.32 1 1
147+
github.com/GolangToolKits/grrt/reqRouter.go:226.32,228.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

reqRouter.go

Lines changed: 50 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -37,26 +37,45 @@ func (t ReqRouter) ServeHTTP(w http.ResponseWriter, r *http.Request) {
3737

3838
if t.corsEnabled && r.Method == http.MethodOptions {
3939
t.handleCors(w)
40-
}
41-
42-
path := r.URL.Path
43-
var rt = t.findPrefix(path)
44-
if rt == nil {
45-
frt, fvars := t.findRouteAndVars(path)
46-
rt = frt
47-
if len(*fvars) > 0 {
48-
r = t.requestWithVars(r, rt.GetVarNames(), fvars)
49-
// rt = frt
50-
}
51-
}
52-
if rt == nil || !rt.IsActive() {
53-
w.WriteHeader(http.StatusNotFound)
54-
} else if !rt.IsMethodAllowed(r.Method) {
55-
w.WriteHeader(http.StatusMethodNotAllowed)
5640
} else {
57-
hd := rt.GetHandler()
58-
hd.ServeHTTP(w, r)
41+
path := r.URL.Path
42+
var rt = t.findPrefix(path)
43+
if rt == nil {
44+
frt, fvars := t.findRouteAndVars(path)
45+
rt = frt
46+
if len(*fvars) > 0 {
47+
r = t.requestWithVars(r, rt.GetVarNames(), fvars)
48+
// rt = frt
49+
}
50+
}
51+
if rt == nil || !rt.IsActive() {
52+
w.WriteHeader(http.StatusNotFound)
53+
} else if !rt.IsMethodAllowed(r.Method) {
54+
w.WriteHeader(http.StatusMethodNotAllowed)
55+
} else {
56+
hd := rt.GetHandler()
57+
hd.ServeHTTP(w, r)
58+
}
5959
}
60+
61+
// path := r.URL.Path
62+
// var rt = t.findPrefix(path)
63+
// if rt == nil {
64+
// frt, fvars := t.findRouteAndVars(path)
65+
// rt = frt
66+
// if len(*fvars) > 0 {
67+
// r = t.requestWithVars(r, rt.GetVarNames(), fvars)
68+
// // rt = frt
69+
// }
70+
// }
71+
// if rt == nil || !rt.IsActive() {
72+
// w.WriteHeader(http.StatusNotFound)
73+
// } else if !rt.IsMethodAllowed(r.Method) {
74+
// w.WriteHeader(http.StatusMethodNotAllowed)
75+
// } else {
76+
// hd := rt.GetHandler()
77+
// hd.ServeHTTP(w, r)
78+
// }
6079
}
6180

6281
// NewRoute NewRoute
@@ -123,8 +142,10 @@ func (t ReqRouter) EnableCORS() {
123142
t.corsEnabled = true
124143
}
125144

126-
// SetAllowedHeaders SetAllowedHeaders
127-
func (t ReqRouter) SetAllowedHeaders(headers []string) {
145+
// SetCorsAllowedHeaders SetAllowedHeaders
146+
func (t ReqRouter) SetCorsAllowedHeaders(hdr string) {
147+
hdr = strings.ReplaceAll(hdr, " ", "")
148+
headers := strings.Split(hdr, ",")
128149
for _, v := range headers {
129150
nHeader := http.CanonicalHeaderKey(strings.TrimSpace(v))
130151
if nHeader == "" {
@@ -134,13 +155,17 @@ func (t ReqRouter) SetAllowedHeaders(headers []string) {
134155
}
135156
}
136157

137-
// AllowedOrigins AllowedOrigins
138-
func (t ReqRouter) AllowedOrigins(origins []string) {
158+
// SetCorsAllowedOrigins AllowedOrigins
159+
func (t ReqRouter) SetCorsAllowedOrigins(org string) {
160+
org = strings.ReplaceAll(org, " ", "")
161+
var origins = strings.Split(org, ",")
139162
t.allowedOrigins = origins
140163
}
141164

142-
// AllowedMethods AllowedMethods
143-
func (t ReqRouter) AllowedMethods(methods []string) {
165+
// SetCorsAllowedMethods AllowedMethods
166+
func (t ReqRouter) SetCorsAllowedMethods(mths string) {
167+
mths = strings.ReplaceAll(mths, " ", "")
168+
var methods = strings.Split(mths, ",")
144169
for _, v := range methods {
145170
nMethod := strings.ToUpper(strings.TrimSpace(v))
146171
if nMethod == "" {
@@ -168,7 +193,7 @@ func (t ReqRouter) findRouteAndVars(path string) (Route, *[]string) {
168193
sp := strings.Split(path, "/")
169194
var vars []string
170195
var vcnt = len(sp) - 2
171-
log.Println("sp:", sp)
196+
//log.Println("sp:", sp)
172197
var found = false
173198
var searchPath = ""
174199
for i, p := range sp {

reqRouter_test.go

Lines changed: 33 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -335,15 +335,14 @@ func TestReqRouter_ServeHTTP(t *testing.T) {
335335
w: tw7,
336336
r: tr7,
337337
},
338-
339338
},
340339
}
341340
for _, tt := range tests {
342341
t.Run(tt.name, func(t *testing.T) {
343342
tr := ReqRouter{
344343
namedRoutes: tt.fields.namedRoutes,
345344
prefixRoutes: tt.fields.prefixRoutes,
346-
corsEnabled: true,
345+
corsEnabled: true,
347346
}
348347
tr.ServeHTTP(tt.args.w, tt.args.r)
349348
if tt.name == "test 1" && tw.Code != http.StatusOK {
@@ -765,7 +764,7 @@ func TestReqRouter_SetAllowedHeaders(t *testing.T) {
765764
allowedMethods []string
766765
}
767766
type args struct {
768-
headers []string
767+
headers string
769768
}
770769
tests := []struct {
771770
name string
@@ -776,10 +775,10 @@ func TestReqRouter_SetAllowedHeaders(t *testing.T) {
776775
{
777776
name: "test 1",
778777
fields: fields{
779-
allowedHeaders: []string{"Content-Type"},
778+
allowedHeaders: []string{"Content-Type", "api-key"},
780779
},
781780
args: args{
782-
headers: []string{"Content-Type", ""},
781+
headers: "Content-Type, api-key, ",
783782
},
784783
},
785784
}
@@ -793,7 +792,13 @@ func TestReqRouter_SetAllowedHeaders(t *testing.T) {
793792
allowedOrigins: tt.fields.allowedOrigins,
794793
allowedMethods: tt.fields.allowedMethods,
795794
}
796-
tr.SetAllowedHeaders(tt.args.headers)
795+
tr.SetCorsAllowedHeaders(tt.args.headers)
796+
if tr.allowedHeaders[0] != "Content-Type" {
797+
t.Fail()
798+
}
799+
if tr.allowedHeaders[1] != "api-key" {
800+
t.Fail()
801+
}
797802
})
798803
}
799804
}
@@ -844,7 +849,7 @@ func TestReqRouter_AllowedOrigins(t *testing.T) {
844849
allowedMethods []string
845850
}
846851
type args struct {
847-
origins []string
852+
origins string
848853
}
849854
tests := []struct {
850855
name string
@@ -855,10 +860,10 @@ func TestReqRouter_AllowedOrigins(t *testing.T) {
855860
{
856861
name: "test 1",
857862
fields: fields{
858-
allowedOrigins: []string{"test"},
863+
allowedOrigins: []string{"*", "test"},
859864
},
860865
args: args{
861-
origins: []string{"*"},
866+
origins: "*, test",
862867
},
863868
},
864869
}
@@ -872,7 +877,13 @@ func TestReqRouter_AllowedOrigins(t *testing.T) {
872877
allowedOrigins: tt.fields.allowedOrigins,
873878
allowedMethods: tt.fields.allowedMethods,
874879
}
875-
tr.AllowedOrigins(tt.args.origins)
880+
tr.SetCorsAllowedOrigins(tt.args.origins)
881+
if tr.allowedOrigins[0] != "*" {
882+
t.Fail()
883+
}
884+
if tr.allowedOrigins[1] != "test" {
885+
t.Fail()
886+
}
876887
})
877888
}
878889
}
@@ -887,7 +898,7 @@ func TestReqRouter_AllowedMethods(t *testing.T) {
887898
allowedMethods []string
888899
}
889900
type args struct {
890-
methods []string
901+
methods string
891902
}
892903
tests := []struct {
893904
name string
@@ -898,10 +909,10 @@ func TestReqRouter_AllowedMethods(t *testing.T) {
898909
{
899910
name: "test 1",
900911
fields: fields{
901-
allowedMethods: []string{},
912+
allowedMethods: []string{"POST"},
902913
},
903914
args: args{
904-
methods: []string{"POST", ""},
915+
methods: "POST, ",
905916
},
906917
},
907918
}
@@ -915,7 +926,10 @@ func TestReqRouter_AllowedMethods(t *testing.T) {
915926
allowedOrigins: tt.fields.allowedOrigins,
916927
allowedMethods: tt.fields.allowedMethods,
917928
}
918-
tr.AllowedMethods(tt.args.methods)
929+
tr.SetCorsAllowedMethods(tt.args.methods)
930+
if tr.allowedMethods[0] != "POST" {
931+
t.Fail()
932+
}
919933
})
920934
}
921935
}
@@ -961,19 +975,19 @@ func TestReqRouter_handleCors(t *testing.T) {
961975
allowedOrigins: tt.fields.allowedOrigins,
962976
allowedMethods: tt.fields.allowedMethods,
963977
}
964-
978+
965979
tr.handleCors(tt.args.w)
966980
fmt.Println("headers: ", tw.Header().Get("Access-Control-Allow-Headers"))
967-
if tw.Result().StatusCode != http.StatusOK{
981+
if tw.Result().StatusCode != http.StatusOK {
968982
t.Fail()
969983
}
970-
if tw.Header().Get("Access-Control-Allow-Headers") != "Content-Type"{
984+
if tw.Header().Get("Access-Control-Allow-Headers") != "Content-Type" {
971985
t.Fail()
972986
}
973-
if tw.Header().Get("Access-Control-Allow-Origin") != "test"{
987+
if tw.Header().Get("Access-Control-Allow-Origin") != "test" {
974988
t.Fail()
975989
}
976-
if tw.Header().Get("Access-Control-Allow-Methods") != "POST,GET"{
990+
if tw.Header().Get("Access-Control-Allow-Methods") != "POST,GET" {
977991
t.Fail()
978992
}
979993
})

router.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ type Router interface {
1616

1717
//CORS methods
1818
EnableCORS()
19-
SetAllowedHeaders(headers []string)
20-
AllowedOrigins(origins []string)
21-
AllowedMethods(methods []string)
19+
SetCorsAllowedHeaders(headers string)
20+
SetCorsAllowedOrigins(origins string)
21+
SetCorsAllowedMethods(methods string)
2222
}
2323

2424
// NewRouter NewRouter creates new Router

0 commit comments

Comments
 (0)