forked from gin-gonic/gin
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
970 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package gin | ||
|
||
import ( | ||
"encoding/base64" | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
) | ||
|
||
func TestBasicAuthSucceed(t *testing.T) { | ||
req, _ := http.NewRequest("GET", "/login", nil) | ||
w := httptest.NewRecorder() | ||
|
||
r := New() | ||
accounts := Accounts{"admin": "password"} | ||
r.Use(BasicAuth(accounts)) | ||
|
||
r.GET("/login", func(c *Context) { | ||
c.String(200, "autorized") | ||
}) | ||
|
||
req.Header.Set("Authorization", "Basic "+base64.StdEncoding.EncodeToString([]byte("admin:password"))) | ||
r.ServeHTTP(w, req) | ||
|
||
if w.Code != 200 { | ||
t.Errorf("Response code should be Ok, was: %s", w.Code) | ||
} | ||
bodyAsString := w.Body.String() | ||
|
||
if bodyAsString != "autorized" { | ||
t.Errorf("Response body should be `autorized`, was %s", bodyAsString) | ||
} | ||
} | ||
|
||
func TestBasicAuth401(t *testing.T) { | ||
req, _ := http.NewRequest("GET", "/login", nil) | ||
w := httptest.NewRecorder() | ||
|
||
r := New() | ||
accounts := Accounts{"foo": "bar"} | ||
r.Use(BasicAuth(accounts)) | ||
|
||
r.GET("/login", func(c *Context) { | ||
c.String(200, "autorized") | ||
}) | ||
|
||
req.Header.Set("Authorization", "Basic "+base64.StdEncoding.EncodeToString([]byte("admin:password"))) | ||
r.ServeHTTP(w, req) | ||
|
||
if w.Code != 401 { | ||
t.Errorf("Response code should be Not autorized, was: %s", w.Code) | ||
} | ||
|
||
if w.HeaderMap.Get("WWW-Authenticate") != "Basic realm=\"Authorization Required\"" { | ||
t.Errorf("WWW-Authenticate header is incorrect: %s", w.HeaderMap.Get("Content-Type")) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.