github.com/greenpau/go-authcrunch@v1.1.4/pkg/redirects/redirect_parser_test.go (about) 1 // Copyright 2024 Paul Greenberg greenpau@outlook.com 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package redirects 16 17 import ( 18 "fmt" 19 20 "testing" 21 22 "github.com/greenpau/go-authcrunch/internal/tests" 23 ) 24 25 func TestParseRedirectURI(t *testing.T) { 26 27 testcases := []struct { 28 name string 29 input string 30 want map[string]interface{} 31 shouldErr bool 32 err error 33 disabled bool 34 }{ 35 { 36 name: "test valid redirect uri", 37 disabled: false, 38 input: "https://authcrunch.com/?redirect_uri=https://authcrunch.com/path/to/login", 39 want: map[string]interface{}{ 40 "redirect_uri": "https://authcrunch.com/path/to/login", 41 }, 42 }, 43 { 44 name: "test parse invalid base uri", 45 disabled: false, 46 input: "http://authcrunch.com.%24/?redirect_uri=https://authcrunch.com/path/to/login", 47 shouldErr: true, 48 err: fmt.Errorf("failed to parse base uri"), 49 }, 50 { 51 name: "test parse non compliant base uri", 52 disabled: false, 53 input: "http:authcrunch.com/?redirect_uri=https://authcrunch.com/path/to/login", 54 shouldErr: true, 55 err: fmt.Errorf("non compliant base uri"), 56 }, 57 // { 58 // name: "test parse invalid redirect uri", 59 // disabled: false, 60 // input: "http://authcrunch.com/?redirect_uri=https://authcrunch.com.%24/path/to/login", 61 // shouldErr: true, 62 // err: fmt.Errorf("failed to parse redirect uri"), 63 // }, 64 { 65 name: "test parse redirect uri without host", 66 disabled: false, 67 input: "https://authcrunch.com/?redirect_uri=/foo/bar", 68 shouldErr: true, 69 err: fmt.Errorf("redirect uri has no scheme and host"), 70 }, 71 { 72 name: "test parse non compliant redirect uri", 73 disabled: false, 74 input: "http://authcrunch.com/?redirect_uri=authcrunch.com/path/to/login", 75 shouldErr: true, 76 err: fmt.Errorf("non compliant redirect uri"), 77 }, 78 } 79 for _, tc := range testcases { 80 t.Run(tc.name, func(t *testing.T) { 81 if tc.disabled { 82 return 83 } 84 var err error 85 got := make(map[string]interface{}) 86 msgs := []string{fmt.Sprintf("test name: %s", tc.name)} 87 msgs = append(msgs, fmt.Sprintf("input:\n%s", tc.input)) 88 89 redirURI, err := ParseRedirectURI(tc.input) 90 if tests.EvalErrWithLog(t, err, "RedirectURI", tc.shouldErr, tc.err, msgs) { 91 return 92 } 93 94 got["redirect_uri"] = redirURI.String() 95 96 tests.EvalObjectsWithLog(t, "RedirectURI", tc.want, got, msgs) 97 }) 98 } 99 }