initial commit
This commit is contained in:
commit
e7b1ca79ac
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
||||
.idea/
|
||||
55
testutils/testutils.go
Normal file
55
testutils/testutils.go
Normal file
@ -0,0 +1,55 @@
|
||||
package testutils
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func NewHTTPRequestJSON(method, url string, body interface{}, queryParams map[string]string) (*http.Request, error) {
|
||||
bodyBytes, err := json.Marshal(body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
request, err := http.NewRequest(method, url, bytes.NewReader(bodyBytes))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
request.Header.Set("Content-Type", "application/json")
|
||||
|
||||
if len(queryParams) > 0 {
|
||||
query := request.URL.Query()
|
||||
for k, v := range queryParams {
|
||||
query.Add(k, v)
|
||||
}
|
||||
request.URL.RawQuery = query.Encode()
|
||||
}
|
||||
|
||||
return request, nil
|
||||
}
|
||||
|
||||
func NewHTTPRequest(method, url string, params map[string]string) (*http.Request, error) {
|
||||
request, err := http.NewRequest(method, url, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if len(params) > 0 {
|
||||
query := request.URL.Query()
|
||||
for k, v := range params {
|
||||
query.Add(k, v)
|
||||
}
|
||||
request.URL.RawQuery = query.Encode()
|
||||
}
|
||||
|
||||
return request, nil
|
||||
}
|
||||
|
||||
func Decode(input interface{}, output interface{}) error {
|
||||
bytes, err := json.Marshal(input)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return json.Unmarshal(bytes, output)
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user