-
Notifications
You must be signed in to change notification settings - Fork 25
/
request_test.go
81 lines (61 loc) · 2.06 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
70
71
72
73
74
75
76
77
78
79
80
81
package documentdb
import (
"bytes"
"encoding/json"
"net/http"
"testing"
"github.com/stretchr/testify/assert"
)
type TestPartitionKey struct {
Prop string `json:"prop"`
}
func (t *TestPartitionKey) MarshalJSON() ([]byte, error) {
return json.Marshal(&struct {
NewProp string `json:"newProp"`
}{NewProp: t.Prop})
}
func TestResourceRequest(t *testing.T) {
assert := assert.New(t)
req := ResourceRequest("/dbs/b5NCAA==/", &http.Request{})
assert.Equal(req.rType, "dbs")
assert.Equal(req.rId, "b5NCAA==")
}
func TestDefaultHeaders(t *testing.T) {
testUserAgent := "test/user agent"
r, _ := http.NewRequest("GET", "link", &bytes.Buffer{})
req := ResourceRequest("/dbs/b5NCAA==/", r)
_ = req.DefaultHeaders(&Config{MasterKey: &Key{Key: "YXJpZWwNCg=="}}, testUserAgent)
assert := assert.New(t)
assert.NotEqual(req.Header.Get(HeaderAuth), "")
assert.NotEqual(req.Header.Get(HeaderXDate), "")
assert.NotEqual(req.Header.Get(HeaderVersion), "")
assert.Equal(req.Header.Get(HeaderUserAgent), testUserAgent)
}
func TestUpsertHeaders(t *testing.T) {
r, _ := http.NewRequest("POST", "link", &bytes.Buffer{})
req := ResourceRequest("/dbs/b5NCAA==/", r)
Upsert()(req)
assert := assert.New(t)
assert.Equal(req.Header.Get(HeaderUpsert), "true")
}
func TestPartitionKeyMarshalJSON(t *testing.T) {
r, _ := http.NewRequest("GET", "link", &bytes.Buffer{})
req := ResourceRequest("/dbs/b5NCAA==/", r)
PartitionKey(&TestPartitionKey{"test"})(req)
assert := assert.New(t)
assert.Equal([]string{"{\"newProp\":\"test\"}"}, req.Header[HeaderPartitionKey])
}
func TestPartitionKeyAsInt(t *testing.T) {
r, _ := http.NewRequest("GET", "link", &bytes.Buffer{})
req := ResourceRequest("/dbs/b5NCAA==/", r)
PartitionKey(1)(req)
assert := assert.New(t)
assert.Equal([]string{"[1]"}, req.Header[HeaderPartitionKey])
}
func TestPartitionKeyAsString(t *testing.T) {
r, _ := http.NewRequest("GET", "link", &bytes.Buffer{})
req := ResourceRequest("/dbs/b5NCAA==/", r)
PartitionKey("1")(req)
assert := assert.New(t)
assert.Equal([]string{"[\"1\"]"}, req.Header[HeaderPartitionKey])
}