@@ -2,11 +2,13 @@ package appurl_test
22
33import (
44 "encoding/json"
5+ "regexp"
56 "testing"
67
78 "github.com/stretchr/testify/require"
89
910 "github.com/coder/coder/v2/coderd/workspaceapps/appurl"
11+ "github.com/coder/coder/v2/provisioner"
1012)
1113
1214func TestNewPreviewID (t * testing.T ) {
@@ -27,3 +29,90 @@ func TestPreviewIDUnmarshalText(t *testing.T) {
2729 require .Error (t , json .Unmarshal ([]byte (raw ), & id ), raw )
2830 }
2931}
32+
33+ func TestPreviewSlugIsNeverAnAppSlug (t * testing.T ) {
34+ t .Parallel ()
35+
36+ for _ , id := range []appurl.PreviewID {"00000000" , "ffffffff" , appurl .NewPreviewID ("/home/coder/site" )} {
37+ require .False (t , provisioner .AppSlugRegex .MatchString (id .Slug ()), id .Slug ())
38+ }
39+ for _ , slug := range []string {"preview--9" , "preview--8080" , "preview--65535" } {
40+ require .False (t , provisioner .AppSlugRegex .MatchString (slug ), slug )
41+ }
42+ }
43+
44+ func TestParsePreviewSlug (t * testing.T ) {
45+ t .Parallel ()
46+
47+ for slug , want := range map [string ]appurl.PreviewTarget {
48+ "preview--0123abcd" : {ID : "0123abcd" },
49+ "preview--8080" : {Port : 8080 },
50+ "preview--9" : {Port : 9 },
51+ "preview--65535" : {Port : 65535 },
52+ } {
53+ got , ok := appurl .ParsePreviewSlug (slug )
54+ require .True (t , ok , slug )
55+ require .Equal (t , want , got , slug )
56+ }
57+
58+ // A port has one hostname, so leading zeros are refused.
59+ for _ , slug := range []string {"preview--08080" , "preview--0" , "preview--65536" , "preview--8080s" , "preview--" , "8080" , "preview--0123ABCD" } {
60+ _ , ok := appurl .ParsePreviewSlug (slug )
61+ require .False (t , ok , slug )
62+ }
63+ }
64+
65+ func TestParsePreviewHostname (t * testing.T ) {
66+ t .Parallel ()
67+
68+ dotPattern , err := appurl .CompileHostnamePattern ("*.apps.example.com" )
69+ require .NoError (t , err )
70+ suffixPattern , err := appurl .CompileHostnamePattern ("*--apps.example.com" )
71+ require .NoError (t , err )
72+ portPreview := appurl.ApplicationURL {AppSlugOrPort : "preview--8080" , AgentName : "agent" , WorkspaceName : "workspace" , Username : "user" }
73+ for _ , tc := range []struct {
74+ name string
75+ pattern * regexp.Regexp
76+ hostname string
77+ wantApp appurl.ApplicationURL
78+ wantTarget appurl.PreviewTarget
79+ }{
80+ {
81+ name : "FilePreview" ,
82+ pattern : dotPattern ,
83+ hostname : "preview--0123abcd--agent--workspace--user.apps.example.com" ,
84+ wantApp : appurl.ApplicationURL {AppSlugOrPort : "preview--0123abcd" , AgentName : "agent" , WorkspaceName : "workspace" , Username : "user" },
85+ wantTarget : appurl.PreviewTarget {ID : "0123abcd" },
86+ },
87+ {name : "PortPreview" , pattern : dotPattern , hostname : "preview--8080--agent--workspace--user.apps.example.com" , wantApp : portPreview , wantTarget : appurl.PreviewTarget {Port : 8080 }},
88+ {name : "UpperCaseWithPort" , pattern : dotPattern , hostname : "PREVIEW--8080--agent--workspace--user.apps.example.com:8443" , wantApp : portPreview , wantTarget : appurl.PreviewTarget {Port : 8080 }},
89+ {name : "SuffixPattern" , pattern : suffixPattern , hostname : "preview--8080--agent--workspace--user--apps.example.com" , wantApp : portPreview , wantTarget : appurl.PreviewTarget {Port : 8080 }},
90+ {
91+ name : "Prefixed" ,
92+ pattern : dotPattern ,
93+ hostname : "x---preview--8080--agent--workspace--user.apps.example.com" ,
94+ wantApp : appurl.ApplicationURL {Prefix : "x---" , AppSlugOrPort : "preview--8080" , AgentName : "agent" , WorkspaceName : "workspace" , Username : "user" },
95+ wantTarget : appurl.PreviewTarget {Port : 8080 },
96+ },
97+ // An app whose slug is "preview" is not a preview, whatever its agent
98+ // is named.
99+ {name : "AppNamedPreview" , pattern : dotPattern , hostname : "preview--agent--workspace--user.apps.example.com" },
100+ {name : "AppNamedPreviewWithoutAgent" , pattern : dotPattern , hostname : "preview--workspace--user.apps.example.com" },
101+ {name : "AppNamedPreviewOnAgentNamedLikePort" , pattern : dotPattern , hostname : "preview--8080--workspace--user.apps.example.com" },
102+ {name : "AppNamedPreviewUnderSuffixPattern" , pattern : suffixPattern , hostname : "preview--8080--workspace--user--apps.example.com" },
103+ {name : "InvalidPreviewSlug" , pattern : dotPattern , hostname : "preview--garbage--agent--workspace--user.apps.example.com" },
104+ {name : "PortWithLeadingZero" , pattern : dotPattern , hostname : "preview--08080--agent--workspace--user.apps.example.com" },
105+ {name : "ExtraPart" , pattern : dotPattern , hostname : "preview--8080--agent--workspace--user--extra.apps.example.com" },
106+ {name : "PortApp" , pattern : dotPattern , hostname : "8080--agent--workspace--user.apps.example.com" },
107+ {name : "OtherHostname" , pattern : dotPattern , hostname : "preview--8080--agent--workspace--user.other.example.com" },
108+ } {
109+ t .Run (tc .name , func (t * testing.T ) {
110+ t .Parallel ()
111+
112+ app , target , ok := appurl .ParsePreviewHostname (tc .pattern , tc .hostname )
113+ require .Equal (t , tc .wantApp != appurl.ApplicationURL {}, ok )
114+ require .Equal (t , tc .wantApp , app )
115+ require .Equal (t , tc .wantTarget , target )
116+ })
117+ }
118+ }
0 commit comments