-
Notifications
You must be signed in to change notification settings - Fork 1
/
internal_test.go
74 lines (65 loc) · 2.05 KB
/
internal_test.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
package http
import (
"io/ioutil"
"net/http/httptest"
"strings"
"testing"
"github.com/1set/starlet/dataconv/types"
"go.starlark.net/starlark"
)
// we're ok with testing private functions if it simplifies the test :)
func TestSetBody(t *testing.T) {
fd := map[string]string{
"foo": "bar baz",
}
cases := []struct {
rawBody *types.NullableStringOrBytes
formData map[string]string
formEncoding starlark.String
jsonData starlark.Value
body string
err string
}{
{types.NewNullableStringOrBytes("hallo"), nil, starlark.String(""), nil, "hallo", ""},
{types.NewNullableStringOrBytes(""), fd, starlark.String(""), nil, "foo=bar+baz", ""},
// TODO - this should check multipart form data is being set
{nil, fd, starlark.String("multipart/form-data"), nil, "", ""},
{nil, nil, starlark.String(""), starlark.Tuple{starlark.Bool(true), starlark.MakeInt(1), starlark.String("der")}, "[true,1,\"der\"]", ""},
}
for i, c := range cases {
var formData *starlark.Dict
if c.formData != nil {
formData = starlark.NewDict(len(c.formData))
for k, v := range c.formData {
if err := formData.SetKey(starlark.String(k), starlark.String(v)); err != nil {
t.Fatal(err)
}
}
}
req := httptest.NewRequest("get", "https://example.com", nil)
err := setBody(req, c.rawBody, formData, c.formEncoding, c.jsonData)
if !(err == nil && c.err == "" || (err != nil && err.Error() == c.err)) {
t.Errorf("case %d error mismatch. expected: %s, got: %s", i, c.err, err)
continue
}
if strings.HasPrefix(req.Header.Get("Content-Type"), "multipart/form-data;") {
if err := req.ParseMultipartForm(0); err != nil {
t.Fatal(err)
}
for k, v := range c.formData {
fv := req.FormValue(k)
if fv != v {
t.Errorf("case %d error mismatch. expected %s=%s, got: %s", i, k, v, fv)
}
}
} else {
body, err := ioutil.ReadAll(req.Body)
if err != nil {
t.Fatal(err)
}
if string(body) != c.body {
t.Errorf("case %d body mismatch. expected: %s, got: %s", i, c.body, string(body))
}
}
}
}