-
Notifications
You must be signed in to change notification settings - Fork 6
/
request_test.go
69 lines (66 loc) · 1.79 KB
/
request_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
package dynjson
import (
"net/http"
"testing"
)
func TestFieldsFromRequestNoError(t *testing.T) {
{
r, err := http.NewRequest(http.MethodGet, "http://api.example.com/endpoint", nil)
if err != nil {
t.Error("Should not have returned", err)
}
fields := FieldsFromRequest(r)
if len(fields) != 0 {
t.Error("0 fields were expected")
}
}
{
r, err := http.NewRequest(http.MethodGet, "http://api.example.com/endpoint?select=foo&select=bar", nil)
if err != nil {
t.Error("Should not have returned", err)
}
fields := FieldsFromRequest(r)
if len(fields) != 2 {
t.Error("2 fields were expected")
}
if fields[0] != "foo" || fields[1] != "bar" {
t.Errorf("Expected [foo bar] but got %v", fields)
}
}
{
r, err := http.NewRequest(http.MethodGet, "http://api.example.com/endpoint?select=foo&select=bar", nil)
if err != nil {
t.Error("Should not have returned", err)
}
fields := FieldsFromRequest(r, OptionMultipleFields)
if len(fields) != 2 {
t.Error("2 fields were expected")
}
if fields[0] != "foo" || fields[1] != "bar" {
t.Errorf("Expected [foo bar] but got %v", fields)
}
}
{
r, err := http.NewRequest(http.MethodGet, "http://api.example.com/endpoint?select=foo,bar", nil)
if err != nil {
t.Error("Should not have returned", err)
}
fields := FieldsFromRequest(r, OptionCommaList)
if len(fields) != 2 {
t.Error("2 fields were expected")
}
if fields[0] != "foo" || fields[1] != "bar" {
t.Errorf("Expected [foo bar] but got %v", fields)
}
}
}
func TestFieldsFromRequestError(t *testing.T) {
r, err := http.NewRequest(http.MethodGet, "http://api.example.com/endpoint?select=ad%f", nil)
if err != nil {
t.Error("Should not have returned", err)
}
fields := FieldsFromRequest(r)
if len(fields) != 0 {
t.Error("0 fields were expected")
}
}