@@ -2,6 +2,7 @@ package web
2
2
3
3
import (
4
4
"bytes"
5
+ "encoding/base64"
5
6
"encoding/json"
6
7
"errors"
7
8
"fmt"
@@ -200,6 +201,14 @@ func init() {
200
201
ctx .SetHeader ("Server" , "myserver" , true )
201
202
return ""
202
203
})
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
+ })
203
212
}
204
213
205
214
var tests = []Test {
@@ -228,6 +237,7 @@ var tests = []Test{
228
237
{"GET" , "/jsonbytes?a=1&b=2" , nil , "" , 200 , `{"a":"1","b":"2"}` },
229
238
{"POST" , "/parsejson" , map [string ][]string {"Content-Type" : {"application/json" }}, `{"a":"hello", "b":"world"}` , 200 , "hello world" },
230
239
//{"GET", "/testenv", "", 200, "hello world"},
240
+ {"GET" , "/authorization" , map [string ][]string {"Authorization" : {BuildBasicAuthCredentials ("foo" , "bar" )}}, "" , 200 , "foobar" },
231
241
}
232
242
233
243
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) {
519
529
t .Fatalf ("Incorrect header, exp 'myserver', got %q" , resp .headers ["Server" ][0 ])
520
530
}
521
531
}
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