Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
go
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
go
Commits
cd4d0004
Commit
cd4d0004
authored
May 13, 2011
by
Brad Fitzpatrick
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
http: add Request.SetBasicAuth method
R=golang-dev, dsymonds, rsc CC=golang-dev
https://golang.org/cl/4543050
parent
36cec789
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
21 additions
and
0 deletions
+21
-0
src/pkg/http/request.go
src/pkg/http/request.go
+13
-0
src/pkg/http/request_test.go
src/pkg/http/request_test.go
+8
-0
No files found.
src/pkg/http/request.go
View file @
cd4d0004
...
...
@@ -12,6 +12,7 @@ import (
"bufio"
"crypto/tls"
"container/vector"
"encoding/base64"
"fmt"
"io"
"io/ioutil"
...
...
@@ -479,6 +480,18 @@ func NewRequest(method, url string, body io.Reader) (*Request, os.Error) {
return
req
,
nil
}
// SetBasicAuth sets the request's Authorization header to use HTTP
// Basic Authentication with the provided username and password.
//
// With HTTP Basic Authentication the provided username and password
// are not encrypted.
func
(
r
*
Request
)
SetBasicAuth
(
username
,
password
string
)
{
s
:=
username
+
":"
+
password
buf
:=
make
([]
byte
,
base64
.
StdEncoding
.
EncodedLen
(
len
(
s
)))
base64
.
StdEncoding
.
Encode
(
buf
,
[]
byte
(
s
))
r
.
Header
.
Set
(
"Authorization"
,
"Basic "
+
string
(
buf
))
}
// ReadRequest reads and parses a request from b.
func
ReadRequest
(
b
*
bufio
.
Reader
)
(
req
*
Request
,
err
os
.
Error
)
{
...
...
src/pkg/http/request_test.go
View file @
cd4d0004
...
...
@@ -173,6 +173,14 @@ func TestRedirect(t *testing.T) {
}
}
func
TestSetBasicAuth
(
t
*
testing
.
T
)
{
r
,
_
:=
NewRequest
(
"GET"
,
"http://example.com/"
,
nil
)
r
.
SetBasicAuth
(
"Aladdin"
,
"open sesame"
)
if
g
,
e
:=
r
.
Header
.
Get
(
"Authorization"
),
"Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ=="
;
g
!=
e
{
t
.
Errorf
(
"got header %q, want %q"
,
g
,
e
)
}
}
func
TestMultipartRequest
(
t
*
testing
.
T
)
{
// Test that we can read the values and files of a
// multipart request with FormValue and FormFile,
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment