2021-04-05 17:00:21 +03:00
|
|
|
package testutil
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
2021-06-21 17:34:10 +03:00
|
|
|
func SendHTTPRequestWithFileAsBody(t *testing.T, filepath, url string) *http.Response {
|
2021-04-05 17:00:21 +03:00
|
|
|
b, err := ioutil.ReadFile(filepath)
|
|
|
|
require.NoError(t, err)
|
|
|
|
var body map[string]interface{}
|
|
|
|
err = json.Unmarshal(b, &body)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2021-08-26 09:09:06 +03:00
|
|
|
req := NewRequest(t, "POST", url, body)
|
2021-04-05 17:00:21 +03:00
|
|
|
|
|
|
|
c := http.Client{}
|
|
|
|
resp, err := c.Do(req)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2021-06-21 17:34:10 +03:00
|
|
|
return resp
|
2021-04-05 17:00:21 +03:00
|
|
|
}
|
|
|
|
|
2021-08-26 09:09:06 +03:00
|
|
|
func NewRequest(t *testing.T, method, urlStr string, body interface{}) *http.Request {
|
2021-04-05 17:00:21 +03:00
|
|
|
u, err := url.ParseRequestURI(urlStr)
|
2021-06-21 17:34:10 +03:00
|
|
|
require.NoError(t, err)
|
2021-04-05 17:00:21 +03:00
|
|
|
var buf io.ReadWriter
|
|
|
|
if body != nil {
|
|
|
|
buf = &bytes.Buffer{}
|
|
|
|
enc := json.NewEncoder(buf)
|
|
|
|
enc.SetEscapeHTML(false)
|
|
|
|
err := enc.Encode(body)
|
2021-06-21 17:34:10 +03:00
|
|
|
require.NoError(t, err)
|
2021-04-05 17:00:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
req, err := http.NewRequest(method, u.String(), buf)
|
2021-06-21 17:34:10 +03:00
|
|
|
require.NoError(t, err)
|
2021-04-05 17:00:21 +03:00
|
|
|
|
|
|
|
if body != nil {
|
|
|
|
req.Header.Set("Content-Type", "application/json")
|
|
|
|
}
|
2021-08-26 09:09:06 +03:00
|
|
|
if len(TestAdminSecret) > 0 {
|
|
|
|
req.Header.Set("x-hasura-admin-secret", TestAdminSecret)
|
|
|
|
}
|
2021-06-21 17:34:10 +03:00
|
|
|
return req
|
2021-04-05 17:00:21 +03:00
|
|
|
}
|