mirror of
https://github.com/wahyd4/loginsrv.git
synced 2026-08-09 04:46:29 +10:00
Add a cookie with a valid token to test httpserver.NewReplacer
This commit is contained in:
+16
-1
@@ -1,14 +1,17 @@
|
||||
package caddy
|
||||
|
||||
import (
|
||||
"github.com/dgrijalva/jwt-go"
|
||||
"github.com/mholt/caddy/caddyhttp/httpserver"
|
||||
"github.com/tarent/loginsrv/login"
|
||||
"github.com/tarent/loginsrv/model"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func Test_ServeHTTP_200(t *testing.T) {
|
||||
func Test_ServeHTTP_200(t *testing.T) {
|
||||
//Set the ServeHTTP *http.Request
|
||||
r, err := http.NewRequest("GET", "/", nil)
|
||||
if err != nil {
|
||||
@@ -35,6 +38,18 @@ func Test_ServeHTTP_200(t *testing.T) {
|
||||
loginHandler: loginh,
|
||||
}
|
||||
|
||||
//Set user token
|
||||
userInfo := model.UserInfo{Sub: "bob", Expiry: time.Now().Add(time.Second).Unix()}
|
||||
token := jwt.NewWithClaims(jwt.SigningMethodHS512, userInfo)
|
||||
validToken, err := token.SignedString([]byte(h.config.JwtSecret))
|
||||
if err != nil {
|
||||
t.Errorf("Expected nil error, got: %v", err)
|
||||
}
|
||||
|
||||
//Set cookie for user token on the ServeHTTP http.ResponseWriter
|
||||
cookie := http.Cookie{Name: "jwt_token",Value: validToken}
|
||||
http.SetCookie(w, &cookie)
|
||||
|
||||
status, err := h.ServeHTTP(w, r)
|
||||
|
||||
if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user