github.com/jlevesy/mattermost-server@v5.3.2-0.20181003190404-7468f35cb0c8+incompatible/app/authentication_test.go (about) 1 // Copyright (c) 2017-present Mattermost, Inc. All Rights Reserved. 2 // See License.txt for license information. 3 4 package app 5 6 import ( 7 "net/http" 8 "net/http/httptest" 9 "strconv" 10 "testing" 11 12 "github.com/mattermost/mattermost-server/model" 13 "github.com/stretchr/testify/require" 14 ) 15 16 func TestParseAuthTokenFromRequest(t *testing.T) { 17 cases := []struct { 18 header string 19 cookie string 20 query string 21 expectedToken string 22 expectedLocation TokenLocation 23 }{ 24 {"", "", "", "", TokenLocationNotFound}, 25 {"token mytoken", "", "", "mytoken", TokenLocationHeader}, 26 {"BEARER mytoken", "", "", "mytoken", TokenLocationHeader}, 27 {"", "mytoken", "", "mytoken", TokenLocationCookie}, 28 {"", "", "mytoken", "mytoken", TokenLocationQueryString}, 29 } 30 31 for testnum, tc := range cases { 32 pathname := "/test/here" 33 if tc.query != "" { 34 pathname += "?access_token=" + tc.query 35 } 36 req := httptest.NewRequest("GET", pathname, nil) 37 if tc.header != "" { 38 req.Header.Add(model.HEADER_AUTH, tc.header) 39 } 40 if tc.cookie != "" { 41 req.AddCookie(&http.Cookie{ 42 Name: model.SESSION_COOKIE_TOKEN, 43 Value: tc.cookie, 44 }) 45 } 46 47 token, location := ParseAuthTokenFromRequest(req) 48 49 require.Equal(t, tc.expectedToken, token, "Wrong token on test "+strconv.Itoa(testnum)) 50 require.Equal(t, tc.expectedLocation, location, "Wrong location on test "+strconv.Itoa(testnum)) 51 } 52 }