|
| 1 | +package agentapi |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "net/url" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/stretchr/testify/require" |
| 9 | + |
| 10 | + "github.com/coder/coder/v2/coderd/workspaceapps/appurl" |
| 11 | +) |
| 12 | + |
| 13 | +func Test_vscodeProxyURI(t *testing.T) { |
| 14 | + t.Parallel() |
| 15 | + |
| 16 | + coderAccessURL, err := url.Parse("https://coder.com") |
| 17 | + require.NoError(t, err) |
| 18 | + |
| 19 | + accessURLWithPort, err := url.Parse("https://coder.com:8080") |
| 20 | + require.NoError(t, err) |
| 21 | + |
| 22 | + basicApp := appurl.ApplicationURL{ |
| 23 | + Prefix: "prefix", |
| 24 | + AppSlugOrPort: "slug", |
| 25 | + AgentName: "agent", |
| 26 | + WorkspaceName: "workspace", |
| 27 | + Username: "user", |
| 28 | + } |
| 29 | + |
| 30 | + cases := []struct { |
| 31 | + Name string |
| 32 | + App appurl.ApplicationURL |
| 33 | + AccessURL *url.URL |
| 34 | + AppHostname string |
| 35 | + Expected string |
| 36 | + }{ |
| 37 | + { |
| 38 | + // No hostname proxies through the access url. |
| 39 | + Name: "NoHostname", |
| 40 | + AccessURL: coderAccessURL, |
| 41 | + AppHostname: "", |
| 42 | + App: basicApp, |
| 43 | + Expected: coderAccessURL.String(), |
| 44 | + }, |
| 45 | + { |
| 46 | + Name: "NoHostnameAccessURLPort", |
| 47 | + AccessURL: accessURLWithPort, |
| 48 | + AppHostname: "", |
| 49 | + App: basicApp, |
| 50 | + Expected: accessURLWithPort.String(), |
| 51 | + }, |
| 52 | + { |
| 53 | + Name: "Hostname", |
| 54 | + AccessURL: coderAccessURL, |
| 55 | + AppHostname: "*.apps.coder.com", |
| 56 | + App: basicApp, |
| 57 | + Expected: fmt.Sprintf("https://%s.apps.coder.com", basicApp.String()), |
| 58 | + }, |
| 59 | + { |
| 60 | + Name: "HostnameWithAccessURLPort", |
| 61 | + AccessURL: accessURLWithPort, |
| 62 | + AppHostname: "*.apps.coder.com", |
| 63 | + App: basicApp, |
| 64 | + Expected: fmt.Sprintf("https://%s.apps.coder.com:%s", basicApp.String(), accessURLWithPort.Port()), |
| 65 | + }, |
| 66 | + { |
| 67 | + Name: "HostnameWithPort", |
| 68 | + AccessURL: coderAccessURL, |
| 69 | + AppHostname: "*.apps.coder.com:4444", |
| 70 | + App: basicApp, |
| 71 | + Expected: fmt.Sprintf("https://%s.apps.coder.com:%s", basicApp.String(), "4444"), |
| 72 | + }, |
| 73 | + { |
| 74 | + // Port from hostname takes precedence over access url port. |
| 75 | + Name: "HostnameWithPortAccessURLWithPort", |
| 76 | + AccessURL: accessURLWithPort, |
| 77 | + AppHostname: "*.apps.coder.com:4444", |
| 78 | + App: basicApp, |
| 79 | + Expected: fmt.Sprintf("https://%s.apps.coder.com:%s", basicApp.String(), "4444"), |
| 80 | + }, |
| 81 | + } |
| 82 | + |
| 83 | + for _, c := range cases { |
| 84 | + c := c |
| 85 | + t.Run(c.Name, func(t *testing.T) { |
| 86 | + t.Parallel() |
| 87 | + |
| 88 | + require.NotNilf(t, c.AccessURL, "AccessURL is required") |
| 89 | + |
| 90 | + output := vscodeProxyURI(c.App, c.AccessURL, c.AppHostname) |
| 91 | + require.Equal(t, c.Expected, output) |
| 92 | + }) |
| 93 | + } |
| 94 | +} |
0 commit comments