Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gitpod-io
GitHub Repository: gitpod-io/gitpod
Path: blob/main/components/gitpod-cli/cmd/preview_test.go
2498 views
1
// Copyright (c) 2020 Gitpod GmbH. All rights reserved.
2
// Licensed under the GNU Affero General Public License (AGPL).
3
// See License.AGPL.txt in the project root for license information.
4
5
package cmd
6
7
import (
8
"os"
9
"testing"
10
11
"github.com/golang/mock/gomock"
12
)
13
14
func TestReplaceLocalhostInURL(t *testing.T) {
15
tests := []struct {
16
Desc string
17
Input string
18
ExpectedPort uint16
19
PortURL string
20
Expectation string
21
}{
22
{"just localhost", "localhost", 80, "80-workspace-url", "80-workspace-url"},
23
{"just localhost w port", "localhost:1234", 1234, "1234-workspace-url", "1234-workspace-url"},
24
{"just localhost w http", "http://localhost", 80, "https://80-workspace-url", "https://80-workspace-url"},
25
{"just localhost w https", "https://localhost", 80, "https://80-workspace-url", "https://80-workspace-url"},
26
{"just localhost w port/http", "http://localhost:1234", 1234, "https://1234-workspace-url", "https://1234-workspace-url"},
27
{"just localhost w port/https", "https://localhost:1234", 1234, "https://1234-workspace-url", "https://1234-workspace-url"},
28
{"localhost param", "https://something.org?cb=localhost", 0, "", "https://something.org?cb=localhost"},
29
{"localhost param w port", "https://something.org?cb=localhost:8080", 0, "", "https://something.org?cb=localhost:8080"},
30
{"localhost param w http", "https://something.org?cb=http://localhost", 80, "https://80-workspace-url", "https://something.org?cb=https://80-workspace-url"},
31
{"localhost param w http/path", "https://something.org?cb=http://localhost/foo", 80, "https://80-workspace-url", "https://something.org?cb=https://80-workspace-url/foo"},
32
{"localhost param w https", "https://something.org?cb=https://localhost", 80, "https://80-workspace-url", "https://something.org?cb=https://80-workspace-url"},
33
{"localhost param w port/http", "https://something.org?cb=http://localhost:8080", 8080, "https://8080-workspace-url", "https://something.org?cb=https://8080-workspace-url"},
34
{"localhost param w port/https", "https://something.org?cb=https://localhost:8080", 8080, "https://8080-workspace-url", "https://something.org?cb=https://8080-workspace-url"},
35
{"just 127.0.0.1", "127.0.0.1", 80, "80-workspace-url", "80-workspace-url"},
36
{"just 127.0.0.1 w port", "127.0.0.1:1234", 1234, "1234-workspace-url", "1234-workspace-url"},
37
{"just 127.0.0.1 w http", "http://127.0.0.1", 80, "https://80-workspace-url", "https://80-workspace-url"},
38
{"just 127.0.0.1 w https", "https://127.0.0.1", 80, "https://80-workspace-url", "https://80-workspace-url"},
39
{"just 127.0.0.1 w port/http", "http://127.0.0.1:1234", 1234, "https://1234-workspace-url", "https://1234-workspace-url"},
40
{"just 127.0.0.1 w port/https", "https://127.0.0.1:1234", 1234, "https://1234-workspace-url", "https://1234-workspace-url"},
41
{"127.0.0.1 param", "https://something.org?cb=127.0.0.1", 0, "", "https://something.org?cb=127.0.0.1"},
42
{"127.0.0.1 param w port", "https://something.org?cb=127.0.0.1:8080", 0, "", "https://something.org?cb=127.0.0.1:8080"},
43
{"127.0.0.1 param w http", "https://something.org?cb=http://127.0.0.1", 80, "https://80-workspace-url", "https://something.org?cb=https://80-workspace-url"},
44
{"127.0.0.1 param w http/path", "https://something.org?cb=http://127.0.0.1/foo", 80, "https://80-workspace-url", "https://something.org?cb=https://80-workspace-url/foo"},
45
{"127.0.0.1 param w https", "https://something.org?cb=https://127.0.0.1", 80, "https://80-workspace-url", "https://something.org?cb=https://80-workspace-url"},
46
{"127.0.0.1 param w port/http", "https://something.org?cb=http://127.0.0.1:8080", 8080, "https://8080-workspace-url", "https://something.org?cb=https://8080-workspace-url"},
47
{"127.0.0.1 param w port/https", "https://something.org?cb=https://127.0.0.1:8080", 8080, "https://8080-workspace-url", "https://something.org?cb=https://8080-workspace-url"},
48
}
49
50
os.Setenv("GITPOD_WORKSPACE_URL", "https://workspace-url")
51
for _, test := range tests {
52
t.Run(test.Desc, func(t *testing.T) {
53
ctrl := gomock.NewController(t)
54
defer ctrl.Finish()
55
56
act := replaceLocalhostInURL(test.Input)
57
if act != test.Expectation {
58
t.Errorf("unexpected result: %s, expected %s", act, test.Expectation)
59
}
60
})
61
}
62
}
63
64