Skip to content

Commit a609e19

Browse files
committed
feat: add HTTP request utility for testing with customizable headers
1 parent cca765a commit a609e19

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

e2e/request/request.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package request_test
2+
3+
import (
4+
"io"
5+
"net/http"
6+
)
7+
8+
type Params struct {
9+
URL string
10+
HeaderKey string
11+
HeaderValue string
12+
}
13+
14+
func Do(params Params) (body []byte, statusCode int, err error) {
15+
client := http.Client{}
16+
req, err := http.NewRequest(http.MethodGet, params.URL, nil)
17+
if err != nil {
18+
return nil, statusCode, err
19+
}
20+
if params.HeaderKey != "" && params.HeaderValue != "" {
21+
req.Header.Add(params.HeaderKey, params.HeaderValue)
22+
}
23+
resp, err := client.Do(req)
24+
if err != nil {
25+
return nil, statusCode, err
26+
}
27+
defer resp.Body.Close()
28+
statusCode = resp.StatusCode
29+
body, err = io.ReadAll(resp.Body)
30+
if err != nil {
31+
return nil, statusCode, err
32+
}
33+
return body, statusCode, nil
34+
}

0 commit comments

Comments
 (0)