Baked in header. Consistent test verbiage.
This commit is contained in:
parent
1dec4498b0
commit
c102229487
2 changed files with 14 additions and 8 deletions
12
jwt.go
12
jwt.go
|
@ -1,6 +1,7 @@
|
|||
package jwt
|
||||
|
||||
import (
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"net/http"
|
||||
|
@ -42,6 +43,7 @@ func NewMiddleware(c *Config) (*JWTMiddleware, error) {
|
|||
}
|
||||
|
||||
func (m *JWTMiddleware) Secure(h http.Handler) http.Handler {
|
||||
// This is just a placeholder for now
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
h.ServeHTTP(w, r)
|
||||
})
|
||||
|
@ -57,9 +59,13 @@ func (m *JWTMiddleware) GenerateToken(w http.ResponseWriter, r *http.Request) {
|
|||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
resp := "failure"
|
||||
if result {
|
||||
resp = "success"
|
||||
if !result {
|
||||
panic("deal with this")
|
||||
}
|
||||
|
||||
// For now, the header will be static
|
||||
resp := `{"typ":"JWT","alg":"HS256"}`
|
||||
resp = base64.StdEncoding.EncodeToString([]byte(resp))
|
||||
|
||||
w.Write([]byte(resp))
|
||||
}
|
||||
|
|
10
jwt_test.go
10
jwt_test.go
|
@ -32,14 +32,14 @@ func newJWTMiddlewareOrFatal(t *testing.T) *JWTMiddleware {
|
|||
func TestNewJWTMiddleware(t *testing.T) {
|
||||
middleware := newJWTMiddlewareOrFatal(t)
|
||||
if middleware.secret != "password" {
|
||||
t.Errorf("expected 'password', got %v", middleware.secret)
|
||||
t.Errorf("wanted password, got %v", middleware.secret)
|
||||
}
|
||||
val, err := middleware.auth("", "")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if val != true {
|
||||
t.Errorf("expected true, %v", val)
|
||||
t.Errorf("wanted true, got %v", val)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -64,7 +64,7 @@ func TestSecureHandler(t *testing.T) {
|
|||
req, _ := http.NewRequest("GET", "http://example.com", nil)
|
||||
middleware.Secure(testHandler).ServeHTTP(resp, req)
|
||||
if resp.Body.String() != "test" {
|
||||
t.Errorf("expected 'test', got %v", resp.Body.String())
|
||||
t.Errorf("wanted test, got %v", resp.Body.String())
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -86,7 +86,7 @@ func TestGenerateTokenHandler(t *testing.T) {
|
|||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
if string(respBody) != "success" {
|
||||
t.Errorf("expected 'success', got %v", string(respBody))
|
||||
if string(respBody) != "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9" {
|
||||
t.Errorf("wanted eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9, got %v", string(respBody))
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue