...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package couchdb
18
19 import (
20 "context"
21 "net/http"
22 "testing"
23
24 "gitlab.com/flimzy/testy"
25
26 kivik "github.com/go-kivik/kivik/v4"
27 internal "github.com/go-kivik/kivik/v4/int/errors"
28 "github.com/go-kivik/kivik/v4/internal/nettest"
29 )
30
31 func TestSession(t *testing.T) {
32 tests := []struct {
33 name string
34 status int
35 body string
36 expected interface{}
37 errStatus int
38 err string
39 }{
40 {
41 name: "valid",
42 status: http.StatusOK,
43 body: `{"ok":true,"userCtx":{"name":"admin","roles":["_admin"]},"info":{"authentication_db":"_users","authentication_handlers":["oauth","cookie","default"],"authenticated":"cookie"}}`,
44 expected: &kivik.Session{
45 Name: "admin",
46 Roles: []string{"_admin"},
47 AuthenticationMethod: "cookie",
48 AuthenticationHandlers: []string{"oauth", "cookie", "default"},
49 RawResponse: []byte(`{"ok":true,"userCtx":{"name":"admin","roles":["_admin"]},"info":{"authentication_db":"_users","authentication_handlers":["oauth","cookie","default"],"authenticated":"cookie"}}`),
50 },
51 },
52 {
53 name: "invalid response",
54 status: http.StatusOK,
55 body: `{"userCtx":"asdf"}`,
56 errStatus: http.StatusBadGateway,
57 err: "json: cannot unmarshal string into Go ",
58 },
59 }
60 for _, test := range tests {
61 t.Run(test.name, func(t *testing.T) {
62 s := nettest.NewHTTPTestServer(t, http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
63 w.Header().Set("Content-Type", "application/json")
64 w.WriteHeader(test.status)
65 _, _ = w.Write([]byte(test.body))
66 }))
67 client, err := kivik.New("couch", s.URL)
68 if err != nil {
69 t.Fatal(err)
70 }
71 session, err := client.Session(context.Background())
72 if d := internal.StatusErrorDiffRE(test.err, test.errStatus, err); d != "" {
73 t.Error(d)
74 }
75 if err != nil {
76 return
77 }
78 if d := testy.DiffInterface(test.expected, session); d != nil {
79 t.Error(d)
80 }
81 })
82 }
83 }
84
View as plain text