@@ -4,15 +4,42 @@ import (
44 "fmt"
55 "net/http"
66 "net/url"
7+ "strings"
78 "testing"
89
910 cmds "github.com/ipfs/go-ipfs-cmds"
1011)
1112
1213func assertHeaders (t * testing.T , resHeaders http.Header , reqHeaders map [string ]string ) {
14+ t .Helper ()
15+ t .Logf ("headers: %v" , resHeaders )
1316 for name , value := range reqHeaders {
14- if resHeaders .Get (name ) != value {
15- t .Errorf ("Invalid header '%s', wanted '%s', got '%s'" , name , value , resHeaders .Get (name ))
17+ header := resHeaders [http .CanonicalHeaderKey (name )]
18+ switch len (header ) {
19+ case 0 :
20+ if value != "" {
21+ t .Errorf ("expected a header for %s" , name )
22+ }
23+ case 1 :
24+ if header [0 ] != value {
25+ t .Errorf ("Invalid header '%s', wanted '%s', got '%s'" , name , value , header [0 ])
26+ }
27+ default :
28+ values := strings .Split (value , "," )
29+ set := make (map [string ]bool , len (values ))
30+ for _ , v := range values {
31+ set [strings .Trim (v , " " )] = true
32+ }
33+ for _ , got := range header {
34+ if ! set [got ] {
35+ t .Errorf ("found unexpected value %s in header %s" , got , name )
36+ continue
37+ }
38+ delete (set , got )
39+ }
40+ for missing := range set {
41+ t .Errorf ("missing value %s in header %s" , missing , name )
42+ }
1643 }
1744 }
1845}
@@ -27,7 +54,7 @@ func originCfg(origins []string) *ServerConfig {
2754 cfg := NewServerConfig ()
2855 cfg .SetAllowedOrigins (origins ... )
2956 cfg .SetAllowedMethods ("GET" , "PUT" , "POST" )
30- cfg .HandledMethods = [] string { "GET" , "POST" }
57+ cfg .AllowGet = true
3158 return cfg
3259}
3360
@@ -39,18 +66,19 @@ var defaultOrigins = []string{
3966}
4067
4168type httpTestCase struct {
42- Method string
43- Path string
44- Code int
45- Origin string
46- Referer string
47- AllowOrigins []string
48- HandledMethods [] string
49- ReqHeaders map [string ]string
50- ResHeaders map [string ]string
69+ Method string
70+ Path string
71+ Code int
72+ Origin string
73+ Referer string
74+ AllowOrigins []string
75+ AllowGet bool
76+ ReqHeaders map [string ]string
77+ ResHeaders map [string ]string
5178}
5279
5380func (tc * httpTestCase ) test (t * testing.T ) {
81+ t .Helper ()
5482 // defaults
5583 method := tc .Method
5684 if method == "" {
@@ -85,7 +113,7 @@ func (tc *httpTestCase) test(t *testing.T) {
85113 }
86114
87115 // server
88- _ , server := getTestServer (t , tc .AllowOrigins , tc .HandledMethods )
116+ _ , server := getTestServer (t , tc .AllowOrigins , tc .AllowGet )
89117 if server == nil {
90118 return
91119 }
@@ -114,6 +142,7 @@ func TestDisallowedOrigins(t *testing.T) {
114142 return httpTestCase {
115143 Origin : origin ,
116144 AllowOrigins : allowedOrigins ,
145+ AllowGet : true ,
117146 ResHeaders : map [string ]string {
118147 ACAOrigin : "" ,
119148 ACAMethods : "" ,
@@ -144,6 +173,7 @@ func TestAllowedOrigins(t *testing.T) {
144173 return httpTestCase {
145174 Origin : origin ,
146175 AllowOrigins : allowedOrigins ,
176+ AllowGet : true ,
147177 ResHeaders : map [string ]string {
148178 ACAOrigin : origin ,
149179 ACAMethods : "" ,
@@ -171,6 +201,7 @@ func TestWildcardOrigin(t *testing.T) {
171201 gtc := func (origin string , allowedOrigins []string ) httpTestCase {
172202 return httpTestCase {
173203 Origin : origin ,
204+ AllowGet : true ,
174205 AllowOrigins : allowedOrigins ,
175206 ResHeaders : map [string ]string {
176207 ACAOrigin : "*" ,
@@ -204,6 +235,7 @@ func TestDisallowedReferer(t *testing.T) {
204235 return httpTestCase {
205236 Origin : "http://localhost" ,
206237 Referer : referer ,
238+ AllowGet : true ,
207239 AllowOrigins : allowedOrigins ,
208240 ResHeaders : map [string ]string {
209241 ACAOrigin : "http://localhost" ,
@@ -232,6 +264,7 @@ func TestAllowedReferer(t *testing.T) {
232264 return httpTestCase {
233265 Origin : "http://localhost" ,
234266 AllowOrigins : allowedOrigins ,
267+ AllowGet : true ,
235268 ResHeaders : map [string ]string {
236269 ACAOrigin : "http://localhost" ,
237270 ACAMethods : "" ,
@@ -260,6 +293,7 @@ func TestWildcardReferer(t *testing.T) {
260293 return httpTestCase {
261294 Origin : origin ,
262295 AllowOrigins : allowedOrigins ,
296+ AllowGet : true ,
263297 ResHeaders : map [string ]string {
264298 ACAOrigin : "*" ,
265299 ACAMethods : "" ,
@@ -338,6 +372,7 @@ func TestEncoding(t *testing.T) {
338372 return httpTestCase {
339373 Method : "GET" ,
340374 Path : path ,
375+ AllowGet : true ,
341376 Origin : "http://localhost" ,
342377 AllowOrigins : []string {"*" },
343378 ReqHeaders : map [string ]string {
0 commit comments