Add SetBasicAuth function for HTTP Auth
Showing
2 changed files
with
18 additions
and
0 deletions
| ... | @@ -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() | ... | ... |
-
Please register or sign in to post a comment