Skip to content

Commit

Permalink
Return empty string if token does not exist in request context
Browse files Browse the repository at this point in the history
  • Loading branch information
alexedwards committed Jul 19, 2018
1 parent 7182011 commit dffc98a
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
5 changes: 4 additions & 1 deletion context.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@ type csrfContext struct {
// (that is, in another handler that wraps it,
// or after the request has been served)
func Token(req *http.Request) string {
ctx := req.Context().Value(nosurfKey).(*csrfContext)
ctx, ok := req.Context().Value(nosurfKey).(*csrfContext)
if !ok {
return ""
}

return ctx.token
}
Expand Down
14 changes: 14 additions & 0 deletions context_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package nosurf

import (
"testing"
)

func TestTokenForRequestWithoutContext(t *testing.T) {
req := dummyGet()
token := Token(req)

if token != "" {
t.Errorf("Token should be %q but it's %v", "", token)
}
}

0 comments on commit dffc98a

Please sign in to comment.