-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrequest.go
48 lines (40 loc) · 917 Bytes
/
request.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
package gqltest
import (
"bytes"
"encoding/json"
"net/http"
"net/http/httptest"
"os"
)
func NewRequest(query string, options ...*RequestOption) (*http.Request, error) {
body := &RequestBody{
Query: query,
}
method := "POST"
target := "/"
for _, option := range options {
if option.Method != "" {
method = option.Method
}
if option.Target != "" {
target = option.Target
}
if option.Variables != nil {
body.Variables = option.Variables
}
}
bodyBytes, err := json.Marshal(body)
if err != nil {
return nil, err
}
r := httptest.NewRequest(method, target, bytes.NewReader(bodyBytes))
r.Header.Add("content-type", "application/json")
return r, nil
}
func NewRequestFromFile(filename string, options ...*RequestOption) (*http.Request, error) {
fileBytes, err := os.ReadFile(filename)
if err != nil {
return nil, err
}
return NewRequest(string(fileBytes), options...)
}