unit tests for extractor

This commit is contained in:
Dave Grijalva 2016-06-06 17:18:24 -07:00
parent de0a819d8d
commit f93fcfd3f9
3 changed files with 112 additions and 7 deletions

86
request/extractor_test.go Normal file
View File

@ -0,0 +1,86 @@
package request
import (
"fmt"
"net/http"
"net/url"
"testing"
)
var extractorTestTokenA = "A"
var extractorTestTokenB = "B"
var extractorTestData = []struct {
name string
extractor Extractor
headers map[string]string
query url.Values
token string
err error
}{
{
name: "simple header",
extractor: HeaderExtractor{"Foo"},
headers: map[string]string{"Foo": extractorTestTokenA},
query: nil,
token: extractorTestTokenA,
err: nil,
},
{
name: "simple argument",
extractor: ArgumentExtractor{"token"},
headers: map[string]string{},
query: url.Values{"token": {extractorTestTokenA}},
token: extractorTestTokenA,
err: nil,
},
{
name: "multiple extractors",
extractor: MultiExtractor{
HeaderExtractor{"Foo"},
ArgumentExtractor{"token"},
},
headers: map[string]string{"Foo": extractorTestTokenA},
query: url.Values{"token": {extractorTestTokenB}},
token: extractorTestTokenA,
err: nil,
},
{
name: "simple miss",
extractor: HeaderExtractor{"This-Header-Is-Not-Set"},
headers: map[string]string{"Foo": extractorTestTokenA},
query: nil,
token: "",
err: ErrNoTokenInRequest,
},
{
name: "filter",
extractor: AuthorizationHeaderExtractor,
headers: map[string]string{"Authorization": "Bearer " + extractorTestTokenA},
query: nil,
token: extractorTestTokenA,
err: nil,
},
}
func TestExtractor(t *testing.T) {
// Bearer token request
for _, data := range extractorTestData {
// Make request from test struct
r, _ := http.NewRequest("GET", fmt.Sprintf("/?%v", data.query.Encode()), nil)
for k, v := range data.headers {
r.Header.Set(k, v)
}
// Test extractor
token, err := data.extractor.ExtractToken(r)
if token != data.token {
t.Errorf("[%v] Expected token '%v'. Got '%v'", data.name, data.token, token)
continue
}
if err != data.err {
t.Errorf("[%v] Expected error '%v'. Got '%v'", data.name, data.err, err)
continue
}
}
}

View File

@ -23,5 +23,5 @@ var OAuth2Extractor = &MultiExtractor{
// Look for authorization token first // Look for authorization token first
AuthorizationHeaderExtractor, AuthorizationHeaderExtractor,
// Extract access_token from form or GET argument // Extract access_token from form or GET argument
&ArgumentExtractor{"access_token"}, ArgumentExtractor{"access_token"},
} }

View File

@ -14,13 +14,23 @@ import (
var requestTestData = []struct { var requestTestData = []struct {
name string name string
claims jwt.MapClaims claims jwt.MapClaims
extractor Extractor
headers map[string]string headers map[string]string
query url.Values query url.Values
valid bool valid bool
}{ }{
{
"authorization bearer token",
jwt.MapClaims{"foo": "bar"},
AuthorizationHeaderExtractor,
map[string]string{"Authorization": "Bearer %v"},
url.Values{},
true,
},
{ {
"oauth bearer token - header", "oauth bearer token - header",
jwt.MapClaims{"foo": "bar"}, jwt.MapClaims{"foo": "bar"},
OAuth2Extractor,
map[string]string{"Authorization": "Bearer %v"}, map[string]string{"Authorization": "Bearer %v"},
url.Values{}, url.Values{},
true, true,
@ -28,10 +38,19 @@ var requestTestData = []struct {
{ {
"oauth bearer token - url", "oauth bearer token - url",
jwt.MapClaims{"foo": "bar"}, jwt.MapClaims{"foo": "bar"},
OAuth2Extractor,
map[string]string{}, map[string]string{},
url.Values{"access_token": {"%v"}}, url.Values{"access_token": {"%v"}},
true, true,
}, },
{
"url token",
jwt.MapClaims{"foo": "bar"},
ArgumentExtractor{"token"},
map[string]string{},
url.Values{"token": {"%v"}},
true,
},
} }
func TestParseRequest(t *testing.T) { func TestParseRequest(t *testing.T) {
@ -65,7 +84,7 @@ func TestParseRequest(t *testing.T) {
r.Header.Set(k, tokenString) r.Header.Set(k, tokenString)
} }
} }
token, err := ParseFromRequestWithClaims(r, OAuth2Extractor, jwt.MapClaims{}, keyfunc) token, err := ParseFromRequestWithClaims(r, data.extractor, jwt.MapClaims{}, keyfunc)
if token == nil { if token == nil {
t.Errorf("[%v] Token was not found: %v", data.name, err) t.Errorf("[%v] Token was not found: %v", data.name, err)