Add a cookie with a valid token to test httpserver.NewReplacer

This commit is contained in:
magikstm
2017-11-09 00:48:12 -05:00
parent fc5e3a317b
commit 4309067e07
+16 -1
View File
@@ -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 {