9a583323 by smallfish

Add SetBasicAuth function for HTTP Auth

1 parent ff9c8d94
...@@ -147,6 +147,12 @@ func (b *BeegoHttpRequest) Setting(setting BeegoHttpSettings) *BeegoHttpRequest ...@@ -147,6 +147,12 @@ func (b *BeegoHttpRequest) Setting(setting BeegoHttpSettings) *BeegoHttpRequest
147 return b 147 return b
148 } 148 }
149 149
150 // SetBasicAuth sets the request's Authorization header to use HTTP Basic Authentication with the provided username and password.
151 func (b *BeegoHttpRequest) SetBasicAuth(username, password string) *BeegoHttpRequest {
152 b.req.SetBasicAuth(username, password)
153 return b
154 }
155
150 // SetEnableCookie sets enable/disable cookiejar 156 // SetEnableCookie sets enable/disable cookiejar
151 func (b *BeegoHttpRequest) SetEnableCookie(enable bool) *BeegoHttpRequest { 157 func (b *BeegoHttpRequest) SetEnableCookie(enable bool) *BeegoHttpRequest {
152 b.setting.EnableCookie = enable 158 b.setting.EnableCookie = enable
......
...@@ -120,6 +120,18 @@ func TestWithCookie(t *testing.T) { ...@@ -120,6 +120,18 @@ func TestWithCookie(t *testing.T) {
120 } 120 }
121 } 121 }
122 122
123 func TestWithBasicAuth(t *testing.T) {
124 str, err := Get("http://httpbin.org/basic-auth/user/passwd").SetBasicAuth("user", "passwd").String()
125 if err != nil {
126 t.Fatal(err)
127 }
128 t.Log(str)
129 n := strings.Index(str, "authenticated")
130 if n == -1 {
131 t.Fatal("authenticated not found in response")
132 }
133 }
134
123 func TestWithUserAgent(t *testing.T) { 135 func TestWithUserAgent(t *testing.T) {
124 v := "beego" 136 v := "beego"
125 str, err := Get("http://httpbin.org/headers").SetUserAgent(v).String() 137 str, err := Get("http://httpbin.org/headers").SetUserAgent(v).String()
......
Styling with Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!