-
Notifications
You must be signed in to change notification settings - Fork 1
/
request.go
99 lines (90 loc) · 2.29 KB
/
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
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package httpbaselinetest
import (
"bytes"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/http/httptest"
"sort"
"strings"
)
func formatRequest(r *http.Request) (string, []byte, error) {
// Create return string
var request []string
// Add the request string
url := fmt.Sprintf("%v %v %v", r.Method, r.URL, r.Proto)
request = append(request, url)
// Add the host
request = append(request, fmt.Sprintf("Host: %v", r.Host))
if len(r.TransferEncoding) > 0 {
request = append(request, fmt.Sprintf("Transfer-Encoding: %s", strings.Join(r.TransferEncoding, ",")))
}
if r.Close {
request = append(request, "Connection: close")
}
// Sort headers for deterministic output
keys := make([]string, 0, len(r.Header))
for k := range r.Header {
keys = append(keys, k)
}
sort.Strings(keys)
// Loop through headers
for _, k := range keys {
for _, h := range r.Header.Values(k) {
request = append(request, fmt.Sprintf("%v: %v", k, h))
}
}
body, err := ioutil.ReadAll(r.Body)
if err != nil {
return "", nil, err
}
if len(body) > 0 {
request = append(request, fmt.Sprintf("Content-Length: %d", len(body)))
// make the body readable when the request is processed
r.Body = ioutil.NopCloser(bytes.NewBuffer(body))
}
request = append(request, "")
ctype := r.Header.Get("Content-Type")
if ctype == "" {
ctype = r.Header.Get("Content-type")
}
if len(body) > 0 {
formattedBody, err := formatBody(ctype, body)
if err != nil {
return "", nil, err
}
request = append(request, formattedBody)
}
// Return the request as a string
return strings.Join(request, "\n"), body, nil
}
func (r *httpBaselineTestRunner) buildRequest() *http.Request {
var bodyReader io.Reader
switch v := r.btest.Body.(type) {
case io.Reader:
bodyReader = v
case string:
bodyReader = strings.NewReader(v)
case nil:
// bodyReader is all set
default:
data, err := json.MarshalIndent(r.btest.Body, "", " ")
if err != nil {
r.t.Fatalf("Error marshaling json body: %s", err)
}
bodyReader = bytes.NewReader(data)
}
req := httptest.NewRequest(r.btest.Method, r.btest.Path, bodyReader)
if r.btest.Host != "" {
req.Host = r.btest.Host
}
for key, val := range r.btest.Headers {
req.Header.Add(key, val)
}
for i := range r.btest.Cookies {
req.AddCookie(&r.btest.Cookies[i])
}
return req
}