1
2
3
4
5
6
7
8
9
10
11
12
13 package db
14
15 import (
16 "context"
17
18 "github.com/go-kivik/kivik/v4"
19 "github.com/go-kivik/kivik/v4/kiviktest/kt"
20 )
21
22 func init() {
23 kt.Register("GetAttachment", getAttachment)
24 }
25
26 func getAttachment(ctx *kt.Context) {
27 ctx.RunRW(func(ctx *kt.Context) {
28 dbname := ctx.TestDB()
29 adb := ctx.Admin.DB(dbname, ctx.Options("db"))
30 if err := adb.Err(); err != nil {
31 ctx.Fatalf("Failed to open db: %s", err)
32 }
33
34 doc := map[string]interface{}{
35 "_id": "foo",
36 "_attachments": map[string]interface{}{
37 "foo.txt": map[string]interface{}{
38 "content_type": "text/plain",
39 "data": "VGhpcyBpcyBhIGJhc2U2NCBlbmNvZGVkIHRleHQ=",
40 },
41 },
42 }
43 if _, err := adb.Put(context.Background(), "foo", doc); err != nil {
44 ctx.Fatalf("Failed to create doc: %s", err)
45 }
46
47 ddoc := map[string]interface{}{
48 "_id": "_design/foo",
49 "_attachments": map[string]interface{}{
50 "foo.txt": map[string]interface{}{
51 "content_type": "text/plain",
52 "data": "VGhpcyBpcyBhIGJhc2U2NCBlbmNvZGVkIHRleHQ=",
53 },
54 },
55 }
56 if _, err := adb.Put(context.Background(), "_design/foo", ddoc); err != nil {
57 ctx.Fatalf("Failed to create design doc: %s", err)
58 }
59
60 ctx.Run("group", func(ctx *kt.Context) {
61 ctx.RunAdmin(func(ctx *kt.Context) {
62 ctx.Parallel()
63 testGetAttachments(ctx, ctx.Admin, dbname, "foo", "foo.txt")
64 testGetAttachments(ctx, ctx.Admin, dbname, "foo", "NotFound")
65 testGetAttachments(ctx, ctx.Admin, dbname, "_design/foo", "foo.txt")
66 })
67 ctx.RunNoAuth(func(ctx *kt.Context) {
68 ctx.Parallel()
69 testGetAttachments(ctx, ctx.NoAuth, dbname, "foo", "foo.txt")
70 testGetAttachments(ctx, ctx.NoAuth, dbname, "foo", "NotFound")
71 testGetAttachments(ctx, ctx.NoAuth, dbname, "_design/foo", "foo.txt")
72 })
73 })
74 })
75 }
76
77 func testGetAttachments(ctx *kt.Context, client *kivik.Client, dbname, docID, filename string) {
78 ctx.Run(docID+"/"+filename, func(ctx *kt.Context) {
79 ctx.Parallel()
80 db := client.DB(dbname, ctx.Options("db"))
81 if err := db.Err(); err != nil {
82 ctx.Fatalf("Failed to connect to db")
83 }
84 att, err := db.GetAttachment(context.Background(), docID, filename)
85 if !ctx.IsExpectedSuccess(err) {
86 return
87 }
88 if client.Driver() != "pouch" {
89 if att.ContentType != "text/plain" {
90 ctx.Errorf("Content-Type: Expected %s, Actual %s", "text/plain", att.ContentType)
91 }
92 }
93 })
94 }
95
View as plain text