@@ -15,18 +15,28 @@ import (
1515 . "github.com/onsi/ginkgo/v2"
1616)
1717
18+ type Response struct {
19+ Headers http.Header
20+ Body string
21+ StatusCode int
22+ }
23+
24+ type Request struct {
25+ Body io.Reader
26+ Headers map [string ]string
27+ QueryParams map [string ]string
28+ URL string
29+ Address string
30+ Timeout time.Duration
31+ }
32+
1833// Get sends a GET request to the specified url.
1934// It resolves to the specified address instead of using DNS.
20- // The status and body of the response is returned, or an error.
21- func Get (
22- url , address string ,
23- timeout time.Duration ,
24- headers , queryParams map [string ]string ,
25- opts ... Option ,
26- ) (int , string , error ) {
35+ // It returns the response body, headers, and status code.
36+ func Get (request Request , opts ... Option ) (Response , error ) {
2737 options := LogOptions (opts ... )
2838
29- resp , err := makeRequest (http .MethodGet , url , address , nil , timeout , headers , queryParams , opts ... )
39+ resp , err := makeRequest (http .MethodGet , request , opts ... )
3040 if err != nil {
3141 if options .logEnabled {
3242 GinkgoWriter .Printf (
@@ -35,46 +45,39 @@ func Get(
3545 )
3646 }
3747
38- return 0 , "" , err
48+ return Response { StatusCode : 0 } , err
3949 }
4050 defer resp .Body .Close ()
4151
4252 body := new (bytes.Buffer )
4353 _ , err = body .ReadFrom (resp .Body )
4454 if err != nil {
4555 GinkgoWriter .Printf ("ERROR in Body content: %v returning body: ''\n " , err )
46- return resp .StatusCode , "" , err
56+ return Response { StatusCode : resp .StatusCode } , err
4757 }
4858 if options .logEnabled {
4959 GinkgoWriter .Printf ("Successfully received response and parsed body: %s\n " , body .String ())
5060 }
5161
52- return resp .StatusCode , body .String (), nil
62+ return Response {
63+ Body : body .String (),
64+ Headers : resp .Header ,
65+ StatusCode : resp .StatusCode ,
66+ }, nil
5367}
5468
5569// Post sends a POST request to the specified url with the body as the payload.
5670// It resolves to the specified address instead of using DNS.
57- func Post (
58- url , address string ,
59- body io.Reader ,
60- timeout time.Duration ,
61- headers , queryParams map [string ]string ,
62- ) (* http.Response , error ) {
63- response , err := makeRequest (http .MethodPost , url , address , body , timeout , headers , queryParams )
71+ func Post (request Request ) (* http.Response , error ) {
72+ response , err := makeRequest (http .MethodPost , request )
6473 if err != nil {
6574 GinkgoWriter .Printf ("ERROR occurred during getting response, error: %s\n " , err )
6675 }
6776
6877 return response , err
6978}
7079
71- func makeRequest (
72- method , url , address string ,
73- body io.Reader ,
74- timeout time.Duration ,
75- headers , queryParams map [string ]string ,
76- opts ... Option ,
77- ) (* http.Response , error ) {
80+ func makeRequest (method string , request Request , opts ... Option ) (* http.Response , error ) {
7881 dialer := & net.Dialer {}
7982
8083 transport , ok := http .DefaultTransport .(* http.Transport )
@@ -90,50 +93,52 @@ func makeRequest(
9093 ) (net.Conn , error ) {
9194 split := strings .Split (addr , ":" )
9295 port := split [len (split )- 1 ]
93- return dialer .DialContext (ctx , network , fmt .Sprintf ("%s:%s" , address , port ))
96+ return dialer .DialContext (ctx , network , fmt .Sprintf ("%s:%s" , request . Address , port ))
9497 }
9598
96- ctx , cancel := context .WithTimeout (context .Background (), timeout )
99+ ctx , cancel := context .WithTimeout (context .Background (), request . Timeout )
97100 defer cancel ()
98101
99102 options := LogOptions (opts ... )
100103 if options .logEnabled {
101104 requestDetails := fmt .Sprintf (
102105 "Method: %s, URL: %s, Address: %s, Headers: %v, QueryParams: %v\n " ,
103106 strings .ToUpper (method ),
104- url ,
105- address ,
106- headers ,
107- queryParams ,
107+ request . URL ,
108+ request . Address ,
109+ request . Headers ,
110+ request . QueryParams ,
108111 )
109112 GinkgoWriter .Printf ("Sending request: %s" , requestDetails )
110113 }
111114
112- req , err := http .NewRequestWithContext (ctx , method , url , body )
115+ req , err := http .NewRequestWithContext (ctx , method , request . URL , request . Body )
113116 if err != nil {
114117 return nil , err
115118 }
116119
117- for key , value := range headers {
120+ for key , value := range request . Headers {
118121 req .Header .Add (key , value )
119122 }
120123
121- if queryParams != nil {
124+ if request . QueryParams != nil {
122125 q := req .URL .Query ()
123- for key , value := range queryParams {
126+ for key , value := range request . QueryParams {
124127 q .Add (key , value )
125128 }
126129 req .URL .RawQuery = q .Encode ()
127130 }
128131
129132 var resp * http.Response
130- if strings .HasPrefix (url , "https" ) {
133+ if strings .HasPrefix (request . URL , "https" ) {
131134 // similar to how in our examples with https requests we run our curl command
132135 // we turn off verification of the certificate, we do the same here
133136 customTransport .TLSClientConfig = & tls.Config {InsecureSkipVerify : true } //nolint:gosec // for https test traffic
134137 }
135138
136- client := & http.Client {Transport : customTransport }
139+ client := & http.Client {
140+ Transport : customTransport ,
141+ }
137142 resp , err = client .Do (req )
138143 if err != nil {
139144 return nil , err
0 commit comments