Skip to content

Commit 3823708

Browse files
committed
Added BasicAuth tests
1 parent 90a5c6a commit 3823708

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

web_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package web
22

33
import (
44
"bytes"
5+
"encoding/base64"
56
"encoding/json"
67
"errors"
78
"fmt"
@@ -200,6 +201,14 @@ func init() {
200201
ctx.SetHeader("Server", "myserver", true)
201202
return ""
202203
})
204+
205+
Get("/authorization", func(ctx *Context) string {
206+
user, pass, err := ctx.GetBasicAuth()
207+
if err != nil {
208+
return "fail"
209+
}
210+
return user + pass
211+
})
203212
}
204213

205214
var tests = []Test{
@@ -228,6 +237,7 @@ var tests = []Test{
228237
{"GET", "/jsonbytes?a=1&b=2", nil, "", 200, `{"a":"1","b":"2"}`},
229238
{"POST", "/parsejson", map[string][]string{"Content-Type": {"application/json"}}, `{"a":"hello", "b":"world"}`, 200, "hello world"},
230239
//{"GET", "/testenv", "", 200, "hello world"},
240+
{"GET", "/authorization", map[string][]string{"Authorization": {BuildBasicAuthCredentials("foo", "bar")}}, "", 200, "foobar"},
231241
}
232242

233243
func buildTestRequest(method string, path string, body string, headers map[string][]string, cookies []*http.Cookie) *http.Request {
@@ -519,3 +529,8 @@ func TestDuplicateHeader(t *testing.T) {
519529
t.Fatalf("Incorrect header, exp 'myserver', got %q", resp.headers["Server"][0])
520530
}
521531
}
532+
533+
func BuildBasicAuthCredentials(user string, pass string) string {
534+
s := user + ":" + pass
535+
return "Basic "+base64.StdEncoding.EncodeToString([]byte(s))
536+
}

0 commit comments

Comments
 (0)