Skip to content
This repository has been archived by the owner on Dec 23, 2023. It is now read-only.

Commit

Permalink
Add Context.BasicAuth
Browse files Browse the repository at this point in the history
  • Loading branch information
razonyang committed Apr 11, 2020
1 parent 41a219e commit eeb432a
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ under development
- Router.ServeFiles accepts route options.
- Add Context.ServeFile: a shortcut of http.ServeFile.
- Add Context.ServeContent: a shortcut of http.ServeContent.
- Add Context.BasicAuth: a shortcut of http.Request.BasicAuth.

v1.9.0 April 4, 2020
--------------------
Expand Down
5 changes: 5 additions & 0 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -385,3 +385,8 @@ func (ctx *Context) DefaultQuery(key, defaultVlue string) string {
func (ctx *Context) RouteURL(name string, args ...string) (*url.URL, error) {
return ctx.router.URL(name, args...)
}

// BasicAuth is a shortcut of http.Request.BasicAuth.
func (ctx *Context) BasicAuth() (username, password string, ok bool) {
return ctx.Request.BasicAuth()
}
18 changes: 18 additions & 0 deletions context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -668,3 +668,21 @@ func TestContext_ServeContent(t *testing.T) {
http.ServeContent(w1, httptest.NewRequest(http.MethodGet, "/", nil), "foo", now, buf)
assert.Equal(t, w1, w2)
}

func TestContext_BasicAuth(t *testing.T) {
requests := []*http.Request{
httptest.NewRequest(http.MethodGet, "/", nil),
}
req := httptest.NewRequest(http.MethodGet, "/", nil)
req.SetBasicAuth("foo", "bar")
requests = append(requests, req)
for _, req := range requests {
ctx := newContext(nil, req)
user1, pass1, ok1 := req.BasicAuth()
user2, pass2, ok2 := ctx.BasicAuth()
assert.Equal(t, user1, user2)
assert.Equal(t, pass1, pass2)
assert.Equal(t, ok1, ok2)

}
}

0 comments on commit eeb432a

Please sign in to comment.