56 lines
1.1 KiB
Go
56 lines
1.1 KiB
Go
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)
|
|
}
|