forked from fjl/go-couchdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
attachments_test.go
126 lines (109 loc) · 3.36 KB
/
attachments_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
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
package couchdb_test
import (
"bytes"
"encoding/base64"
"encoding/json"
"io"
"io/ioutil"
. "net/http"
"testing"
"github.com/cabify/go-couchdb"
)
var (
md5string = "2mGd+/VXL8dJsUlrD//Xag=="
md5bytes, _ = base64.StdEncoding.DecodeString(md5string)
)
func TestAttachment(t *testing.T) {
c := newTestClient(t)
c.Handle("GET /db/doc/attachment/1",
func(resp ResponseWriter, req *Request) {
resp.Header().Set("content-md5", "2mGd+/VXL8dJsUlrD//Xag==")
resp.Header().Set("content-type", "text/plain")
io.WriteString(resp, "the content")
})
att, err := c.DB("db").Attachment("doc", "attachment/1", "")
if err != nil {
t.Fatal(err)
}
body, err := ioutil.ReadAll(att.Body)
if err != nil {
t.Fatalf("error reading body: %v", err)
}
check(t, "att.Name", "attachment/1", att.Name)
check(t, "att.Type", "text/plain", att.Type)
check(t, "att.MD5", md5bytes, att.MD5)
check(t, "att.Body content", "the content", string(body))
}
func TestAttachmentMeta(t *testing.T) {
c := newTestClient(t)
c.Handle("HEAD /db/doc/attachment/1",
func(resp ResponseWriter, req *Request) {
resp.Header().Set("content-md5", "2mGd+/VXL8dJsUlrD//Xag==")
resp.Header().Set("content-type", "text/plain")
resp.WriteHeader(StatusOK)
})
att, err := c.DB("db").AttachmentMeta("doc", "attachment/1", "")
if err != nil {
t.Fatal(err)
}
check(t, "att.Name", "attachment/1", att.Name)
check(t, "att.Type", "text/plain", att.Type)
check(t, "att.MD5", md5bytes, att.MD5)
check(t, "att.Body", nil, att.Body)
}
func TestPutAttachment(t *testing.T) {
c := newTestClient(t)
c.Handle("PUT /db/doc/attachment/1",
func(resp ResponseWriter, req *Request) {
reqBodyContent, err := ioutil.ReadAll(req.Body)
if err != nil {
t.Fatal(err)
}
ctype := req.Header.Get("Content-Type")
check(t, "request content type", "text/plain", ctype)
check(t, "request body", "the content", string(reqBodyContent))
check(t, "request query string",
"rev=1-619db7ba8551c0de3f3a178775509611",
req.URL.RawQuery)
resp.Header().Set("content-md5", md5string)
resp.Header().Set("content-type", "application/json")
json.NewEncoder(resp).Encode(map[string]interface{}{
"ok": true,
"id": "doc",
"rev": "2-619db7ba8551c0de3f3a178775509611",
})
})
att := &couchdb.Attachment{
Name: "attachment/1",
Type: "text/plain",
Body: bytes.NewBufferString("the content"),
}
newrev, err := c.DB("db").PutAttachment("doc", att, "1-619db7ba8551c0de3f3a178775509611")
if err != nil {
t.Fatal(err)
}
check(t, "newrev", "2-619db7ba8551c0de3f3a178775509611", newrev)
check(t, "att.Name", "attachment/1", att.Name)
check(t, "att.Type", "text/plain", att.Type)
check(t, "att.MD5", []byte(nil), att.MD5)
}
func TestDeleteAttachment(t *testing.T) {
c := newTestClient(t)
c.Handle("DELETE /db/doc/attachment/1",
func(resp ResponseWriter, req *Request) {
check(t, "request query string",
"rev=1-619db7ba8551c0de3f3a178775509611",
req.URL.RawQuery)
resp.Header().Set("etag", `"2-619db7ba8551c0de3f3a178775509611"`)
json.NewEncoder(resp).Encode(map[string]interface{}{
"ok": true,
"id": "doc",
"rev": "2-619db7ba8551c0de3f3a178775509611",
})
})
newrev, err := c.DB("db").DeleteAttachment("doc", "attachment/1", "1-619db7ba8551c0de3f3a178775509611")
if err != nil {
t.Fatal(err)
}
check(t, "newrev", "2-619db7ba8551c0de3f3a178775509611", newrev)
}