github.com/insionng/yougam@v0.0.0-20170714101924-2bc18d833463/libraries/igm/sockjs-go.v2/sockjs/mapping_test.gon-place (about)

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