@@ -5,31 +5,11 @@ import (
55 "fmt"
66 "io"
77 "net/http"
8- "net/url"
98 "testing"
109
1110 "github.com/ipfs/gateway-conformance/tooling/check"
1211)
1312
14- type CRequest struct {
15- Method string `json:"method,omitempty"`
16- URL string `json:"url,omitempty"`
17- Query url.Values `json:"query,omitempty"`
18- Proxy string `json:"proxy,omitempty"`
19- UseProxyTunnel bool `json:"useProxyTunnel,omitempty"`
20- DoNotFollowRedirects bool `json:"doNotFollowRedirects,omitempty"`
21- Path string `json:"path,omitempty"`
22- Subdomain string `json:"subdomain,omitempty"`
23- Headers map [string ]string `json:"headers,omitempty"`
24- Body []byte `json:"body,omitempty"`
25- }
26-
27- type CResponse struct {
28- StatusCode int `json:"statusCode,omitempty"`
29- Headers map [string ]interface {} `json:"headers,omitempty"`
30- Body interface {} `json:"body,omitempty"`
31- }
32-
3313type SugarTest struct {
3414 Name string
3515 Hint string
@@ -44,28 +24,28 @@ func Run(t *testing.T, tests SugarTests) {
4424
4525 for _ , test := range tests {
4626 t .Run (test .Name , func (t * testing.T ) {
47- request := test .Request . Request ()
48- response := test .Response . Response ()
27+ request := test .Request
28+ response := test .Response
4929
50- method := request .Method
30+ method := request .Method_
5131 if method == "" {
5232 method = "GET"
5333 }
5434
5535 // Prepare a client,
5636 // use proxy, deal with redirects, etc.
5737 client := & http.Client {}
58- if request .UseProxyTunnel {
59- if request .Proxy == "" {
38+ if request .UseProxyTunnel_ {
39+ if request .Proxy_ == "" {
6040 t .Fatal ("ProxyTunnel requires a proxy" )
6141 }
6242
63- client = NewProxyTunnelClient (request .Proxy )
64- } else if request .Proxy != "" {
65- client = NewProxyClient (request .Proxy )
43+ client = NewProxyTunnelClient (request .Proxy_ )
44+ } else if request .Proxy_ != "" {
45+ client = NewProxyClient (request .Proxy_ )
6646 }
6747
68- if request .DoNotFollowRedirects {
48+ if request .DoNotFollowRedirects_ {
6949 client .CheckRedirect = func (req * http.Request , via []* http.Request ) error {
7050 return http .ErrUseLastResponse
7151 }
@@ -89,27 +69,27 @@ func Run(t *testing.T, tests SugarTests) {
8969 }
9070
9171 var url string
92- if request .URL != "" && request .Path != "" {
72+ if request .URL_ != "" && request .Path_ != "" {
9373 localReport (t , "Both 'URL' and 'Path' are set" )
9474 }
95- if request .URL == "" && request .Path == "" {
75+ if request .URL_ == "" && request .Path_ == "" {
9676 localReport (t , "Neither 'URL' nor 'Path' are set" )
9777 }
98- if request .URL != "" {
99- url = request .URL
78+ if request .URL_ != "" {
79+ url = request .URL_
10080 }
101- if request .Path != "" {
102- url = fmt .Sprintf ("%s/%s" , GatewayURL , request .Path )
81+ if request .Path_ != "" {
82+ url = fmt .Sprintf ("%s/%s" , GatewayURL , request .Path_ )
10383 }
10484
105- query := request .Query .Encode ()
85+ query := request .Query_ .Encode ()
10686 if query != "" {
10787 url = fmt .Sprintf ("%s?%s" , url , query )
10888 }
10989
11090 var body io.Reader
111- if request .Body != nil {
112- body = bytes .NewBuffer (request .Body )
91+ if request .Body_ != nil {
92+ body = bytes .NewBuffer (request .Body_ )
11393 }
11494
11595 // create a request
@@ -119,7 +99,7 @@ func Run(t *testing.T, tests SugarTests) {
11999 }
120100
121101 // add headers
122- for key , value := range request .Headers {
102+ for key , value := range request .Headers_ {
123103 req .Header .Add (key , value )
124104
125105 // https://github.com/golang/go/issues/7682
@@ -135,49 +115,36 @@ func Run(t *testing.T, tests SugarTests) {
135115 localReport (t , "Querying %s failed: %s" , url , err )
136116 }
137117
138- if response .StatusCode != 0 {
139- if res .StatusCode != response .StatusCode {
140- localReport (t , "Status code is not %d. It is %d" , response .StatusCode , res .StatusCode )
118+ if response .StatusCode_ != 0 {
119+ if res .StatusCode != response .StatusCode_ {
120+ localReport (t , "Status code is not %d. It is %d" , response .StatusCode_ , res .StatusCode )
141121 }
142122 }
143123
144- for key , value := range response .Headers {
145- t .Run (fmt .Sprintf ("Header %s" , key ), func (t * testing.T ) {
146- actual := res .Header .Get (key )
147-
148- var output check.CheckOutput
149- var hint string
150-
151- switch v := value .(type ) {
152- case check.Check [string ]:
153- output = v .Check (actual )
154- case check.CheckWithHint [string ]:
155- output = v .Check .Check (actual )
156- hint = v .Hint
157- case string :
158- output = check .IsEqual (v ).Check (actual )
159- default :
160- localReport (t , "Header check '%s' has an invalid type: %T" , key , value )
161- }
124+ for _ , header := range response .Headers_ {
125+ t .Run (fmt .Sprintf ("Header %s" , header .Key_ ), func (t * testing.T ) {
126+ actual := res .Header .Get (header .Key_ )
127+ output := header .Check_ .Check (actual )
128+ hint := header .Hint_
162129
163130 if ! output .Success {
164131 if hint == "" {
165- localReport (t , "Header '%s' %s" , key , output .Reason )
132+ localReport (t , "Header '%s' %s" , header . Key_ , output .Reason )
166133 } else {
167- localReport (t , "Header '%s' %s (%s)" , key , output .Reason , hint )
134+ localReport (t , "Header '%s' %s (%s)" , header . Key_ , output .Reason , hint )
168135 }
169136 }
170137 })
171138 }
172139
173- if response .Body != nil {
140+ if response .Body_ != nil {
174141 defer res .Body .Close ()
175142 resBody , err := io .ReadAll (res .Body )
176143 if err != nil {
177144 localReport (t , err )
178145 }
179146
180- switch v := response .Body .(type ) {
147+ switch v := response .Body_ .(type ) {
181148 case check.Check [string ]:
182149 output := v .Check (string (resBody ))
183150 if ! output .Success {
@@ -195,13 +162,13 @@ func Run(t *testing.T, tests SugarTests) {
195162 case []byte :
196163 if ! bytes .Equal (resBody , v ) {
197164 if res .Header .Get ("Content-Type" ) == "application/vnd.ipld.raw" {
198- localReport (t , "Body is not '%+v'. It is: '%+v'" , response .Body , resBody )
165+ localReport (t , "Body is not '%+v'. It is: '%+v'" , response .Body_ , resBody )
199166 } else {
200- localReport (t , "Body is not '%s'. It is: '%s'" , response .Body , resBody )
167+ localReport (t , "Body is not '%s'. It is: '%s'" , response .Body_ , resBody )
201168 }
202169 }
203170 default :
204- localReport (t , "Body check has an invalid type: %T" , response .Body )
171+ localReport (t , "Body check has an invalid type: %T" , response .Body_ )
205172 }
206173 }
207174 })
0 commit comments