-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexecute_http.go
81 lines (67 loc) · 2.24 KB
/
execute_http.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package main
import (
"fmt"
"net/http"
"strings"
"github.com/go-resty/resty/v2"
)
type httpClient struct {
baseURL string
}
func newHTTPClient(baseURL string) httpClient {
return httpClient{
baseURL: baseURL,
}
}
func (h httpClient) execute(httpMethod, pathName string, xTestSuiteRequest XTestSuiteRequest) (statusCode int, body []byte, err error) {
if httpMethod == http.MethodGet {
return h.get(pathName, xTestSuiteRequest)
} else if httpMethod == http.MethodPost {
return h.post(pathName, xTestSuiteRequest)
} else {
return 0, nil, err
}
}
// get method will execute HTTP request, if the test suite request has a body, it will ignore
func (h httpClient) get(pathName string, xTestSuiteRequest XTestSuiteRequest) (statusCode int, body []byte, err error) {
// Construct path params
// What this piece of code will do is e.g "/users/{id}/order/{order_id}" -> "/users/1/order/2"
for key, value := range xTestSuiteRequest.PathParam {
stringToReplace := fmt.Sprintf("{%s}", key)
if strings.Contains(pathName, stringToReplace) {
pathName = strings.Replace(pathName, stringToReplace, value, 1)
}
}
client := resty.New().SetBaseURL(h.baseURL)
// TODO@adam: Query param and headers needs validation
// - Is is it eempty
// - Does the key have a valid type
// Set query param
response, err := client.R().
SetQueryParams(xTestSuiteRequest.QueryParam).
SetHeaders(xTestSuiteRequest.Header).
Get(pathName)
if err != nil {
return 0, nil, err
}
return response.StatusCode(), response.Body(), nil
}
func (h httpClient) post(pathName string, xTestSuiteRequest XTestSuiteRequest) (statusCode int, body []byte, err error) {
// Construct path params
// What this piece of code will do is e.g "/users/{id}/order/{order_id}" -> "/users/1/order/2"
for key, value := range xTestSuiteRequest.PathParam {
stringToReplace := fmt.Sprintf("{%s}", key)
if strings.Contains(pathName, stringToReplace) {
pathName = strings.Replace(pathName, stringToReplace, value, 1)
}
}
client := resty.New().SetBaseURL(h.baseURL)
response, respErr := client.R().
SetBody(xTestSuiteRequest.Body).
SetHeaders(xTestSuiteRequest.Header).
Post(pathName)
if respErr != nil {
return 0, nil, respErr
}
return response.StatusCode(), response.Body(), nil
}