-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.go
163 lines (147 loc) · 3.95 KB
/
helpers.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package golden
import (
"bufio"
"bytes"
"encoding/json"
"io"
"io/ioutil"
"net/http"
"net/textproto"
"net/url"
"strings"
"text/template"
"github.com/gorilla/schema"
)
// tplOpt represents template parsing option.
type tplOpt func(tpl *template.Template) *template.Template
// TplDelims sets the template action delimiters to the specified strings.
func TplDelims(left, right string) tplOpt {
return func(tpl *template.Template) *template.Template {
return tpl.Delims(left, right)
}
}
// Open reads golden file pointed by pth and returns it as a byte slice.
//
// If data is not nil the golden file pointed by pth is treated as a template
// and applies a parsed template to the specified data object.
//
// You can set template action delimiters using TplDelims function:
//
// Open(t, "golden.yml", data, TplDelims("[[", "]]"))
//
func Open(t T, pth string, data interface{}, opts ...tplOpt) (T, io.Reader) {
content, err := ioutil.ReadFile(pth)
if err != nil {
t.Fatal(err)
return t, nil
}
if data != nil {
tpl := template.New("golden")
for _, opt := range opts {
tpl = opt(tpl)
}
tpl, err := tpl.Parse(string(content))
if err != nil {
t.Fatal(err)
return t, nil
}
buf := &bytes.Buffer{}
if err := tpl.Execute(buf, data); err != nil {
t.Fatal(err)
return t, nil
}
return t, buf
}
return t, bytes.NewReader(content)
}
// Map is a helper type for constructing template data.
type Map map[string]interface{}
// Add adds key and val to map and returns map for chaining.
func (m Map) Add(key string, val interface{}) Map {
m[key] = val
return m
}
// headers2Lines returns headers in wire format as slice of strings.
// Returned lines do not have trailing \r\n characters and the last
// empty line is removed.
func headers2Lines(t T, hs http.Header) []string {
buf := &bytes.Buffer{}
if err := hs.Write(buf); err != nil {
t.Fatal(err)
return nil
}
lns := strings.Split(buf.String(), "\r\n")
if len(lns) > 0 {
lns = lns[:len(lns)-1]
}
return lns
}
// lines2Headers creates http.Header from header lines. It does exactly
// opposite of headers2Lines function.
func lines2Headers(t T, lines ...string) http.Header {
rdr := strings.NewReader(strings.Join(lines, "\r\n") + "\r\n\r\n")
tp := textproto.NewReader(bufio.NewReader(rdr))
hs, err := tp.ReadMIMEHeader()
if err != nil {
t.Fatal(err)
return nil
}
return http.Header(hs)
}
// readBody reads all from rc and returns read data as a byte slice
// and io.ReadCloser with the same data so it can be used to for example
// "reset" body of a http.Request or http.Response instances.
func readBody(t T, rc io.ReadCloser) ([]byte, io.ReadCloser) {
buf := &bytes.Buffer{}
tee := io.TeeReader(rc, buf)
data, err := ioutil.ReadAll(tee)
if err != nil {
t.Fatal(err)
return nil, nil
}
_ = rc.Close()
lns := strings.Split(string(data), "\n")
for i := range lns {
lns[i] = strings.TrimRight(lns[i], "\r")
}
return []byte(strings.Join(lns, "\n")), ioutil.NopCloser(buf)
}
// bindQuery decodes HTTP query string to a struct v.
// The tag is used to locate custom field aliases. See
// https://github.com/gorilla/schema for details.
func bindQuery(t T, query, tag string, v interface{}) {
vs, err := url.ParseQuery(query)
if err != nil {
t.Fatal(err)
return
}
dec := schema.NewDecoder()
dec.SetAliasTag(tag)
if err := dec.Decode(v, vs); err != nil {
t.Fatal(err)
return
}
}
// unmarshalBody unmarshalls golden file body to v based on body type. When
// body type is set to text v can be pointer to sting or byte slice (with
// enough space to fit body). Calls Fatal if body cannot be unmarshalled.
func unmarshalBody(t T, bt string, body string, v interface{}) {
switch bt {
case TypeJSON:
if err := json.Unmarshal([]byte(body), v); err != nil {
t.Fatal(err)
return
}
case TypeText:
switch vt := v.(type) {
case *string:
*vt = body
case *[]byte:
copy(*vt, body)
default:
t.Fatal(ErrUnknownUnmarshaler)
}
default:
t.Fatal(ErrUnknownUnmarshaler)
}
}