Skip to content

Commit cdddcc9

Browse files
committed
refactor: delete CRequest/CResponse
1 parent 72453bc commit cdddcc9

File tree

3 files changed

+62
-130
lines changed

3 files changed

+62
-130
lines changed

tooling/test/report.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@ Hint: {{.Test.Hint}}
2323
Error: {{.Err}}
2424
2525
Request:
26-
{{.Test.Request.Request() | json}}
26+
{{.Test.Request | json}}
2727
2828
Expected Response:
29-
{{.Test.Response.Response() | json}}
29+
{{.Test.Response | json}}
3030
3131
Actual Request:
3232
{{.Req | dump}}

tooling/test/sugar.go

Lines changed: 26 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,15 @@ import (
88
)
99

1010
type RequestBuilder struct {
11-
Method_ string
12-
Path_ string
13-
URL_ string
14-
Proxy_ string
15-
UseProxyTunnel bool
16-
Headers_ map[string]string
17-
DoNotFollowRedirects_ bool
18-
Query_ url.Values
11+
Method_ string `json:"method,omitempty"`
12+
Path_ string `json:"path,omitempty"`
13+
URL_ string `json:"url,omitempty"`
14+
Proxy_ string `json:"proxy,omitempty"`
15+
UseProxyTunnel_ bool `json:"useProxyTunnel,omitempty"`
16+
Headers_ map[string]string `json:"headers,omitempty"`
17+
DoNotFollowRedirects_ bool `json:"doNotFollowRedirects,omitempty"`
18+
Query_ url.Values `json:"query,omitempty"`
19+
Body_ []byte `json:"body,omitempty"`
1920
}
2021

2122
func Request() RequestBuilder {
@@ -52,7 +53,7 @@ func (r RequestBuilder) Proxy(path string, args ...any) RequestBuilder {
5253
}
5354

5455
func (r RequestBuilder) WithProxyTunnel() RequestBuilder {
55-
r.UseProxyTunnel = true
56+
r.UseProxyTunnel_ = true
5657
return r
5758
}
5859

@@ -81,41 +82,24 @@ func (r RequestBuilder) Headers(hs ...HeaderBuilder) RequestBuilder {
8182
}
8283

8384
for _, h := range hs {
84-
r.Headers_[h.Key] = h.Value
85+
r.Headers_[h.Key_] = h.Value_
8586
}
8687

8788
return r
8889
}
8990

90-
func (r RequestBuilder) Request() CRequest {
91-
if r.URL_ != "" && r.Path_ != "" {
92-
panic("Both 'URL' and 'Path' are set")
93-
}
94-
95-
return CRequest{
96-
Method: r.Method_,
97-
Path: r.Path_,
98-
URL: r.URL_,
99-
Query: r.Query_,
100-
Proxy: r.Proxy_,
101-
UseProxyTunnel: r.UseProxyTunnel,
102-
Headers: r.Headers_,
103-
DoNotFollowRedirects: r.DoNotFollowRedirects_,
104-
}
105-
}
106-
10791
type ExpectBuilder struct {
108-
StatusCode int
109-
Headers_ []HeaderBuilder
110-
Body_ interface{}
92+
StatusCode_ int `json:"statusCode,omitempty"`
93+
Headers_ []HeaderBuilder `json:"headers,omitempty"`
94+
Body_ interface{} `json:"body,omitempty"`
11195
}
11296

11397
func Expect() ExpectBuilder {
11498
return ExpectBuilder{Body_: nil}
11599
}
116100

117101
func (e ExpectBuilder) Status(statusCode int) ExpectBuilder {
118-
e.StatusCode = statusCode
102+
e.StatusCode_ = statusCode
119103
return e
120104
}
121105

@@ -171,50 +155,31 @@ func (e ExpectBuilder) BodyWithHint(hint string, body interface{}) ExpectBuilder
171155
return e
172156
}
173157

174-
func (e ExpectBuilder) Response() CResponse {
175-
headers := make(map[string]interface{})
176-
177-
// TODO: detect collision in keys
178-
for _, h := range e.Headers_ {
179-
if h.Hint_ != "" {
180-
headers[h.Key] = check.WithHint(h.Hint_, h.Check)
181-
} else {
182-
headers[h.Key] = h.Check
183-
}
184-
}
185-
186-
return CResponse{
187-
StatusCode: e.StatusCode,
188-
Headers: headers,
189-
Body: e.Body_,
190-
}
191-
}
192-
193158
type HeaderBuilder struct {
194-
Key string
195-
Value string
196-
Check check.Check[string]
197-
Hint_ string
159+
Key_ string `json:"key,omitempty"`
160+
Value_ string `json:"value,omitempty"`
161+
Check_ check.Check[string] `json:"-"`
162+
Hint_ string `json:"-"`
198163
}
199164

200165
func Header(key string, opts ...string) HeaderBuilder {
201166
if len(opts) > 1 {
202167
panic("too many options")
203168
}
204169
if len(opts) > 0 {
205-
return HeaderBuilder{Key: key, Value: opts[0], Check: check.IsEqual(opts[0])}
170+
return HeaderBuilder{Key_: key, Value_: opts[0], Check_: check.IsEqual(opts[0])}
206171
}
207172

208-
return HeaderBuilder{Key: key}
173+
return HeaderBuilder{Key_: key}
209174
}
210175

211176
func (h HeaderBuilder) Contains(value string, rest ...any) HeaderBuilder {
212-
h.Check = check.Contains(value, rest...)
177+
h.Check_ = check.Contains(value, rest...)
213178
return h
214179
}
215180

216181
func (h HeaderBuilder) Matches(value string, rest ...any) HeaderBuilder {
217-
h.Check = check.Matches(value, rest...)
182+
h.Check_ = check.Matches(value, rest...)
218183
return h
219184
}
220185

@@ -224,17 +189,17 @@ func (h HeaderBuilder) Hint(hint string) HeaderBuilder {
224189
}
225190

226191
func (h HeaderBuilder) Equals(value string, args ...any) HeaderBuilder {
227-
h.Check = check.IsEqual(value, args...)
192+
h.Check_ = check.IsEqual(value, args...)
228193
return h
229194
}
230195

231196
func (h HeaderBuilder) IsEmpty() HeaderBuilder {
232-
h.Check = check.CheckIsEmpty{}
197+
h.Check_ = check.CheckIsEmpty{}
233198
return h
234199
}
235200

236201
func (h HeaderBuilder) Checks(f func(string) bool) HeaderBuilder {
237-
h.Check = check.CheckFunc[string]{
202+
h.Check_ = check.CheckFunc[string]{
238203
Fn: f,
239204
}
240205
return h

tooling/test/test.go

Lines changed: 34 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -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-
3313
type 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

Comments
 (0)