github.com/go-kivik/kivik/v4@v4.3.2/x/kivikd/auth/cookie/cookie_test.go (about) 1 // Licensed under the Apache License, Version 2.0 (the "License"); you may not 2 // use this file except in compliance with the License. You may obtain a copy of 3 // the License at 4 // 5 // http://www.apache.org/licenses/LICENSE-2.0 6 // 7 // Unless required by applicable law or agreed to in writing, software 8 // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 9 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 10 // License for the specific language governing permissions and limitations under 11 // the License. 12 13 //go:build !js 14 15 package cookie 16 17 import ( 18 "net/http/httptest" 19 "testing" 20 ) 21 22 type redirTest struct { 23 Name string 24 Input string 25 Expected string 26 Err string 27 } 28 29 func TestRedirectURL(t *testing.T) { 30 tests := []redirTest{ 31 {Name: "NoURL", Input: "-"}, 32 {Name: "EmptyValue", Input: "", Err: "redirection url must be relative to server root"}, 33 {Name: "Absolute", Input: "http://google.com/", Err: "redirection url must be relative to server root"}, 34 {Name: "InvalidURL", Input: "://google.com/", Err: "redirection url must be relative to server root"}, 35 {Name: "NoSlash", Input: "foobar", Err: "redirection url must be relative to server root"}, 36 {Name: "Relative", Input: "/_session", Expected: "/_session"}, 37 {Name: "InvalidRelative", Input: "/session%25%26%26", Err: "invalid redirection url"}, 38 {Name: "Schemaless", Input: "//evil.org", Err: "invalid redirection url"}, 39 } 40 for _, test := range tests { 41 t.Run(test.Name, func(t *testing.T) { 42 url := "/" 43 if test.Input != "-" { 44 url += "?next=" + test.Input 45 } 46 r := httptest.NewRequest("GET", url, nil) 47 result, err := redirectURL(r) 48 var errMsg string 49 if err != nil { 50 errMsg = err.Error() 51 } 52 if test.Err != errMsg { 53 t.Errorf("Unexpected error result. Expected '%s', got '%s'", test.Err, errMsg) 54 } 55 if test.Expected != result { 56 t.Errorf("Unexpected result. Expected '%s', got '%s'", test.Expected, result) 57 } 58 }) 59 } 60 }