1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
|
package sockjs
import (
"net/http"
"regexp"
"testing"
)
func TestMappingMatcher(t *testing.T) {
mappingPrefix := mapping{"GET", regexp.MustCompile("prefix/$"), nil}
mappingPrefixRegExp := mapping{"GET", regexp.MustCompile(".*x/$"), nil}
var testRequests = []struct {
mapping mapping
method string
url string
expectedMatch matchType
}{
{mappingPrefix, "GET", "http://foo/prefix/", fullMatch},
{mappingPrefix, "POST", "http://foo/prefix/", pathMatch},
{mappingPrefix, "GET", "http://foo/prefix_not_mapped", noMatch},
{mappingPrefixRegExp, "GET", "http://foo/prefix/", fullMatch},
}
for _, request := range testRequests {
req, _ := http.NewRequest(request.method, request.url, nil)
m := request.mapping
match, method := m.matches(req)
if match != request.expectedMatch {
t.Errorf("mapping %s should match url=%s", m.path, request.url)
}
if request.expectedMatch == pathMatch {
if method != m.method {
t.Errorf("Matcher method should be %s, but got %s", m.method, method)
}
}
}
}
|