|
| 1 | +package client |
| 2 | + |
| 3 | +import ( |
| 4 | + "net/url" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/ibm-cloud-security/app-identity-and-access-adapter/tests/fake" |
| 8 | +) |
| 9 | + |
| 10 | +func TestCallbackURLForTarget(t *testing.T) { |
| 11 | + tests := []struct { |
| 12 | + CallbackDef string |
| 13 | + Scheme, Host, Path string |
| 14 | + Result string |
| 15 | + }{ |
| 16 | + { // http scheme test |
| 17 | + "", |
| 18 | + "http", "test.com", "/admin", |
| 19 | + "http://test.com/admin/oidc/callback", |
| 20 | + }, |
| 21 | + { // default relative callback |
| 22 | + "", |
| 23 | + "https", "test.com", "/admin", |
| 24 | + "https://test.com/admin/oidc/callback", |
| 25 | + }, |
| 26 | + { // relative URI |
| 27 | + "custom/relative/path", |
| 28 | + "https", "test.com", "/admin", |
| 29 | + "https://test.com/admin/custom/relative/path", |
| 30 | + }, |
| 31 | + { // relative URI already appended |
| 32 | + "custom/relative/path", |
| 33 | + "https", "test.com", "/admin/custom/relative/path", |
| 34 | + "https://test.com/admin/custom/relative/path", |
| 35 | + }, |
| 36 | + { // absolute URI |
| 37 | + "/absolute/uri/callback", |
| 38 | + "https", "test.com", "/admin", |
| 39 | + "https://test.com/absolute/uri/callback", |
| 40 | + }, |
| 41 | + { // full URL (http) |
| 42 | + "http://test.com/my/callback", |
| 43 | + "http", "test.com", "/admin", |
| 44 | + "http://test.com/my/callback", |
| 45 | + }, |
| 46 | + { // full URL (https) |
| 47 | + "https://test.com/my/callback", |
| 48 | + "https", "test.com", "/admin", |
| 49 | + "https://test.com/my/callback", |
| 50 | + }, |
| 51 | + } |
| 52 | + |
| 53 | + for n, tst := range tests { |
| 54 | + cli := fake.NewClientWithCallback(nil, tst.CallbackDef) |
| 55 | + |
| 56 | + res := CallbackURLForTarget(cli, tst.Scheme, tst.Host, tst.Path) |
| 57 | + if res != tst.Result { |
| 58 | + t.Fatalf("TestCallbackURLForTarget#%d CallbackURLForTarget failed.\n"+ |
| 59 | + " Expected: %s\n Got: %s", n, tst.Result, res) |
| 60 | + } |
| 61 | + |
| 62 | + resURL, err := url.Parse(res) |
| 63 | + if err != nil { |
| 64 | + t.Fatalf("TestCallbackURLForTarget#%d failed to parse URL returned from CallbackURLForTarget %s: %s", |
| 65 | + n, res, err.Error()) |
| 66 | + } |
| 67 | + |
| 68 | + if !IsCallbackRequest(cli, resURL.Scheme, resURL.Host, resURL.Path) { |
| 69 | + t.Fatalf("TestCallbackURLForTarget#%d IsCallbackRequest check not returning true\n"+ |
| 70 | + " scheme: %s, host: %s, path: %s", n, resURL.Scheme, resURL.Host, resURL.Path) |
| 71 | + } |
| 72 | + } |
| 73 | +} |
0 commit comments