MCPcopy Create free account
hub / github.com/couchbase/sync_gateway / TestBytesReadPutAttachment

Function TestBytesReadPutAttachment

rest/bytes_read_public_api_test.go:124–168  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

122}
123
124func TestBytesReadPutAttachment(t *testing.T) {
125 rt := NewRestTester(t, &RestTesterConfig{SyncFn: channels.DocChannelsSyncFunction})
126 defer rt.Close()
127
128 // create user
129 rt.CreateUser("alice", []string{"ABC"})
130
131 // add a doc for an attachment to be added to
132 resp := rt.SendAdminRequest(http.MethodPut, fmt.Sprintf("/%s/%s", rt.GetSingleKeyspace(), "doc1"), `{"channels":["ABC"]}`)
133 RequireStatus(t, resp, http.StatusCreated)
134 var body db.Body
135 require.NoError(t, base.JSONUnmarshal(resp.Body.Bytes(), &body))
136 require.Equal(t, true, body["ok"])
137 revid := body["rev"].(string)
138
139 resp = rt.SendUserRequest(http.MethodGet, fmt.Sprintf("/%s/%s", rt.GetSingleKeyspace(), "doc1"), ``, "alice")
140 RequireStatus(t, resp, http.StatusOK)
141
142 attachmentBody := "this is the body of attachment"
143 attachmentContentType := "content/type"
144 reqHeaders := map[string]string{
145 "Content-Type": attachmentContentType,
146 }
147 byteArrayAttachmentBody := []byte(attachmentBody)
148
149 // attach to existing document created above
150 resp = rt.SendUserRequestWithHeaders("PUT", "/{{.keyspace}}/doc1/attach1?rev="+revid, attachmentBody, reqHeaders, "alice", "letmein")
151 RequireStatus(t, resp, http.StatusCreated)
152
153 // assert the stat has increased by the attachment endpoint input
154 base.RequireWaitForStat(t, func() int64 {
155 return rt.GetDatabase().DbStats.DatabaseStats.PublicRestBytesRead.Value()
156 }, int64(len(byteArrayAttachmentBody)))
157
158 // test incorrect user still increments count
159 resp = rt.SendUserRequestWithHeaders("PUT", "/{{.keyspace}}/doc1/attach1?rev="+revid, attachmentBody, reqHeaders, "bob", "letmein")
160 RequireStatus(t, resp, http.StatusUnauthorized)
161
162 newStatNum := len(byteArrayAttachmentBody) * 2
163
164 base.RequireWaitForStat(t, func() int64 {
165 return rt.GetDatabase().DbStats.DatabaseStats.PublicRestBytesRead.Value()
166 }, int64(newStatNum))
167
168}
169
170func TestBytesReadRevDiff(t *testing.T) {
171 rt := NewRestTester(t, nil)

Callers

nothing calls this directly

Calls 13

CloseMethod · 0.95
CreateUserMethod · 0.95
SendAdminRequestMethod · 0.95
GetSingleKeyspaceMethod · 0.95
SendUserRequestMethod · 0.95
GetDatabaseMethod · 0.95
JSONUnmarshalFunction · 0.92
RequireWaitForStatFunction · 0.92
NewRestTesterFunction · 0.85
RequireStatusFunction · 0.85
EqualMethod · 0.45

Tested by

no test coverage detected