github.com/go-kivik/kivik/v4@v4.3.2/couchdb/chttp/basicauth_test.go (about) 1 // Licensed under the Apache License, Version 2.0 (the "License"); you may not 2 // use this file except in compliance with the License. You may obtain a copy of 3 // the License at 4 // 5 // http://www.apache.org/licenses/LICENSE-2.0 6 // 7 // Unless required by applicable law or agreed to in writing, software 8 // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 9 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 10 // License for the specific language governing permissions and limitations under 11 // the License. 12 13 package chttp 14 15 import ( 16 "net/http" 17 "net/http/httptest" 18 "testing" 19 20 "gitlab.com/flimzy/testy" 21 22 "github.com/go-kivik/kivik/v4/internal/nettest" 23 ) 24 25 func TestBasicAuthRoundTrip(t *testing.T) { 26 type rtTest struct { 27 name string 28 auth *basicAuth 29 req *http.Request 30 expected *http.Response 31 cleanup func() 32 } 33 tests := []rtTest{ 34 { 35 name: "Provided transport", 36 req: httptest.NewRequest("GET", "/", nil), 37 auth: &basicAuth{ 38 Username: "foo", 39 Password: "bar", 40 transport: customTransport(func(req *http.Request) (*http.Response, error) { 41 u, p, ok := req.BasicAuth() 42 if !ok { 43 t.Error("BasicAuth not set in request") 44 } 45 if u != "foo" || p != "bar" { // nolint: goconst 46 t.Errorf("Unexpected user/password: %s/%s", u, p) 47 } 48 return &http.Response{StatusCode: 200}, nil 49 }), 50 }, 51 expected: &http.Response{StatusCode: 200}, 52 }, 53 func() rtTest { 54 h := func(w http.ResponseWriter, r *http.Request) { 55 u, p, ok := r.BasicAuth() 56 if !ok { 57 t.Error("BasicAuth not set in request") 58 } 59 if u != "foo" || p != "bar" { 60 t.Errorf("Unexpected user/password: %s/%s", u, p) 61 } 62 w.Header().Set("Date", "Wed, 01 Nov 2017 19:32:41 GMT") 63 w.Header().Set("Content-Type", "application/json") 64 } 65 s := nettest.NewHTTPTestServer(t, http.HandlerFunc(h)) 66 return rtTest{ 67 name: "default transport", 68 auth: &basicAuth{ 69 Username: "foo", 70 Password: "bar", 71 transport: http.DefaultTransport, 72 }, 73 req: httptest.NewRequest("GET", s.URL, nil), 74 expected: &http.Response{ 75 Status: "200 OK", 76 StatusCode: 200, 77 Proto: "HTTP/1.1", 78 ProtoMajor: 1, 79 ProtoMinor: 1, 80 Header: http.Header{ 81 "Content-Length": {"0"}, 82 "Content-Type": {"application/json"}, 83 "Date": {"Wed, 01 Nov 2017 19:32:41 GMT"}, 84 }, 85 }, 86 cleanup: func() { s.Close() }, 87 } 88 }(), 89 } 90 for _, test := range tests { 91 t.Run(test.name, func(t *testing.T) { 92 res, err := test.auth.RoundTrip(test.req) 93 if err != nil { 94 t.Fatal(err) 95 } 96 res.Body = nil 97 res.Request = nil 98 if d := testy.DiffInterface(test.expected, res); d != nil { 99 t.Error(d) 100 } 101 }) 102 } 103 } 104 105 func TestJWTAuthRoundTrip(t *testing.T) { 106 type rtTest struct { 107 name string 108 auth *jwtAuth 109 req *http.Request 110 expected *http.Response 111 cleanup func() 112 } 113 tests := []rtTest{ 114 { 115 name: "Provided transport", 116 req: httptest.NewRequest("GET", "/", nil), 117 auth: &jwtAuth{ 118 Token: "token", 119 transport: customTransport(func(req *http.Request) (*http.Response, error) { 120 if h := req.Header.Get("Authorization"); h != "Bearer token" { 121 t.Errorf("Unexpected authorization header: %s", h) 122 } 123 return &http.Response{StatusCode: 200}, nil 124 }), 125 }, 126 expected: &http.Response{StatusCode: 200}, 127 }, 128 func() rtTest { 129 h := func(w http.ResponseWriter, r *http.Request) { 130 if h := r.Header.Get("Authorization"); h != "Bearer token" { 131 t.Errorf("Unexpected authorization header: %s", h) 132 } 133 w.Header().Set("Date", "Wed, 01 Nov 2017 19:32:41 GMT") 134 w.Header().Set("Content-Type", "application/json") 135 } 136 s := nettest.NewHTTPTestServer(t, http.HandlerFunc(h)) 137 return rtTest{ 138 name: "default transport", 139 auth: &jwtAuth{ 140 Token: "token", 141 transport: http.DefaultTransport, 142 }, 143 req: httptest.NewRequest("GET", s.URL, nil), 144 expected: &http.Response{ 145 Status: "200 OK", 146 StatusCode: 200, 147 Proto: "HTTP/1.1", 148 ProtoMajor: 1, 149 ProtoMinor: 1, 150 Header: http.Header{ 151 "Content-Length": {"0"}, 152 "Content-Type": {"application/json"}, 153 "Date": {"Wed, 01 Nov 2017 19:32:41 GMT"}, 154 }, 155 }, 156 cleanup: func() { s.Close() }, 157 } 158 }(), 159 } 160 for _, test := range tests { 161 t.Run(test.name, func(t *testing.T) { 162 res, err := test.auth.RoundTrip(test.req) 163 if err != nil { 164 t.Fatal(err) 165 } 166 res.Body = nil 167 res.Request = nil 168 if d := testy.DiffInterface(test.expected, res); d != nil { 169 t.Error(d) 170 } 171 }) 172 } 173 }