-
Notifications
You must be signed in to change notification settings - Fork 139
/
Copy pathmultipart_test.go
185 lines (155 loc) · 6.78 KB
/
multipart_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
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/*
Copyright 2019-Present Couchbase, Inc.
Use of this software is governed by the Business Source License included in
the file licenses/BSL-Couchbase.txt. As of the Change Date specified in that
file, in accordance with the Business Source License, use of this software will
be governed by the Apache License, Version 2.0, included in the file
licenses/APL2.txt.
*/
package rest
import (
"bytes"
"compress/gzip"
"fmt"
"io"
"log"
"mime/multipart"
"net/http"
"strconv"
"strings"
"testing"
"github.com/couchbase/sync_gateway/base"
"github.com/couchbase/sync_gateway/db"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestWriteMultipartDocument(t *testing.T) {
rt := NewRestTester(t, nil)
defer rt.Close()
reqHeaders := map[string]string{
"MIME-Version": "1.0",
"Content-Type": "multipart/related; boundary=0123456789"}
bodyText := `--0123456789
Content-Type: application/json
{"key":"foo","value":"bar"}
--0123456789--`
response := rt.SendAdminRequestWithHeaders(http.MethodPut, "/{{.keyspace}}/doc1", bodyText, reqHeaders)
RequireStatus(t, response, http.StatusCreated)
response = rt.SendAdminRequestWithHeaders(http.MethodGet, "/{{.keyspace}}/doc1", "", reqHeaders)
log.Printf("response: %v", string(response.BodyBytes()))
RequireStatus(t, response, http.StatusOK)
}
func TestReadJSONFromMIME(t *testing.T) {
// Read JSON from MIME by specifying non-JSON content type
header := http.Header{}
header.Add("MIME-Version", "1.0")
header.Add("Content-Type", "multipart/related; boundary=0123456789")
input := io.NopCloser(strings.NewReader(`{"key":"foo","value":"bar"}`))
var body db.Body
err := ReadJSONFromMIME(header, input, &body)
assert.Error(t, err, "Can't read JSON from MIME by specifying non-JSON content type")
assert.Contains(t, err.Error(), strconv.Itoa(http.StatusUnsupportedMediaType))
// Read valid JSON from MIME
header = http.Header{}
header.Add("MIME-Version", "1.0")
header.Add("Content-Type", "application/json")
input = io.NopCloser(strings.NewReader(`{"key":"foo","value":"bar"}`))
err = ReadJSONFromMIME(header, input, &body)
assert.NoError(t, err, "Should read valid JSON from MIME")
assert.Equal(t, "foo", body["key"])
assert.Equal(t, "bar", body["value"])
// Read JSON from MIME with illegal JSON body content
header = http.Header{}
header.Add("MIME-Version", "1.0")
header.Add("Content-Type", "application/json")
input = io.NopCloser(strings.NewReader(`"key":"foo","value":"bar"`))
err = ReadJSONFromMIME(header, input, &body)
assert.Error(t, err, "Can't read JSON from MIME with illegal JSON body content")
assert.Contains(t, err.Error(), strconv.Itoa(http.StatusBadRequest))
// Read JSON from MIME with gzip content encoding and illegal content type (application/json)
header = http.Header{}
header.Add("MIME-Version", "1.0")
header.Add("Content-Type", "application/json")
header.Add("Content-Encoding", "gzip")
input = io.NopCloser(strings.NewReader(`{"key":"foo","value":"bar"}`))
err = ReadJSONFromMIME(header, input, &body)
assert.Error(t, err, "Can't read JSON from MIME with gzip content encoding and illegal content type")
assert.Contains(t, err.Error(), "invalid header")
// Read JSON from MIME with unsupported content encoding.
header = http.Header{}
header.Add("MIME-Version", "1.0")
header.Add("Content-Encoding", "zip")
input = io.NopCloser(strings.NewReader(`{"key":"foo","value":"bar"}`))
err = ReadJSONFromMIME(header, input, &body)
assert.Error(t, err, "Can't read JSON from MIME with unsupported content encoding")
assert.Contains(t, err.Error(), strconv.Itoa(http.StatusUnsupportedMediaType))
// Read JSON from MIME with gzip content encoding.
header = http.Header{}
header.Add("MIME-Version", "1.0")
header.Add("Content-Encoding", "gzip")
var buffer bytes.Buffer
gz := gzip.NewWriter(&buffer)
_, err = gz.Write([]byte(`{"key":"foo","value":"bar"}`))
assert.NoError(t, err, "Writes a compressed form of bytes to the underlying io.Writer")
assert.NoError(t, gz.Flush(), "Flushes any pending compressed data to the underlying writer")
assert.NoError(t, gz.Close(), "Closes the Writer by flushing any unwritten data")
input = io.NopCloser(&buffer)
err = ReadJSONFromMIME(header, input, &body)
assert.NoError(t, err, "Should read JSON from MIME with gzip content encoding")
assert.Equal(t, "foo", body["key"])
assert.Equal(t, "bar", body["value"])
}
func TestReadMultipartDocument(t *testing.T) {
rt := NewRestTester(t, nil)
defer rt.Close()
reqHeaders := map[string]string{
"MIME-Version": "1.0",
"Content-Type": "multipart/related; boundary=123"}
bodyText := `--123
Content-Type: application/json
{"key":"foo","value":"bar","_attachments":{"att.txt": {"type": "text/plain", "length": 35, "follows": true, "digest":"sha1-6RU4WkyC+YYARHkO052YJ/dw1Zk="}}}
--123
Content-Type: application/json
Content-Disposition: attachment; filename=att.txt
{"root":"Jacques' JSON attachment"}
--123--`
response := rt.SendAdminRequestWithHeaders(http.MethodPut, "/{{.keyspace}}/doc1", bodyText, reqHeaders)
RequireStatus(t, response, http.StatusCreated)
response = rt.SendAdminRequestWithHeaders(http.MethodGet, "/{{.keyspace}}/doc1", "", reqHeaders)
RequireStatus(t, response, http.StatusOK)
var body db.Body
assert.NoError(t, base.JSONUnmarshal(response.BodyBytes(), &body))
log.Printf("body: %v", body)
assert.Equal(t, "doc1", body["_id"])
assert.Equal(t, "foo", body["key"])
assert.Equal(t, "bar", body["value"])
attachments := body["_attachments"].(map[string]interface{})
attachment := attachments["att.txt"].(map[string]interface{})
assert.Equal(t, float64(35), attachment["length"])
assert.Equal(t, float64(1), attachment["revpos"])
assert.True(t, attachment["stub"].(bool))
assert.Equal(t, "sha1-6RU4WkyC+YYARHkO052YJ/dw1Zk=", attachment["digest"])
}
func TestWriteJSONPart(t *testing.T) {
// writeJSONPart toggles compression to false if the incoming body is less than 300 bytes, so creating
// a body larger than 300 bytes to test writeJSONPart with compression=true and compression=false
mockFakeBody := func() db.Body {
bytes := base.FastRandBytes(t, 139)
value := fmt.Sprintf("%x", bytes)
return db.Body{"key": "foo", "value": value}
}
body := mockFakeBody()
log.Printf("body: %v", body)
buffer := &bytes.Buffer{}
writer := multipart.NewWriter(buffer)
bytes, err := base.JSONMarshalCanonical(body)
require.NoError(t, err)
log.Printf("len(bytes): %v", len(bytes))
assert.NoError(t, writeJSONPart(writer, "application/json", body, true))
assert.NoError(t, writeJSONPart(writer, "application/json", body, false))
}
func TestMd5DigestKey(t *testing.T) {
assert.Equal(t, "md5-rL0Y20zC+Fzt72VPzMSk2A==", md5DigestKey([]byte("foo")))
assert.Equal(t, "md5-N7UdGUp1E+RbVvZSTy1R8g==", md5DigestKey([]byte("bar")))
assert.Equal(t, "md5-xWvVSA9uVBPLYqCtlmZhOg==", md5DigestKey([]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}))
}