Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gitpod-io
GitHub Repository: gitpod-io/gitpod
Path: blob/main/components/proxy/plugins/frontend_dev/frontend_dev_test.go
2500 views
1
// Copyright (c) 2023 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 frontend_dev
6
7
import (
8
"io/ioutil"
9
"net/http"
10
"net/url"
11
"strings"
12
"testing"
13
)
14
15
const index_html = `<!doctype html>
16
<html lang="en">
17
18
<head>
19
<meta charset="utf-8" />
20
<link rel="icon" href="/favicon256.png" />
21
<meta name="viewport" content="width=device-width,initial-scale=1" />
22
<meta name="theme-color" content="#000000" />
23
<meta name="robots" content="noindex">
24
<meta name="Gitpod" content="Always Ready-to-Code" />
25
<link rel="apple-touch-icon" href="/favicon192.png" />
26
<link rel="manifest" href="/manifest.json" />
27
<title>Dashboard</title>
28
<script defer="defer" src="/static/js/main.b009793d.js"></script>
29
<link href="/static/css/main.32e61b25.css" rel="stylesheet">
30
</head>
31
32
<body><noscript>You need to enable JavaScript to run this app.</noscript>
33
<div id="root"></div>
34
</body>
35
36
</html>`
37
38
func Test_MatchAndRewriteRootRequest(t *testing.T) {
39
40
type Test struct {
41
name string
42
response *http.Response
43
newBaseUrl string
44
expectedBody string
45
}
46
tests := []Test{
47
{
48
name: "should match and rewrite root request",
49
response: &http.Response{
50
StatusCode: 200,
51
Header: http.Header{
52
"Content-Type": []string{"text/html"},
53
},
54
Body: ioutil.NopCloser(strings.NewReader(index_html)),
55
},
56
newBaseUrl: "https://3000-gitpodio-gitpod-hk3453q4csi.ws-eu108.gitpod.io",
57
expectedBody: `<!doctype html>
58
<html lang="en">
59
60
<head>
61
<meta charset="utf-8" />
62
<link rel="icon" href="https://3000-gitpodio-gitpod-hk3453q4csi.ws-eu108.gitpod.io/favicon256.png" />
63
<meta name="viewport" content="width=device-width,initial-scale=1" />
64
<meta name="theme-color" content="#000000" />
65
<meta name="robots" content="noindex">
66
<meta name="Gitpod" content="Always Ready-to-Code" />
67
<link rel="apple-touch-icon" href="https://3000-gitpodio-gitpod-hk3453q4csi.ws-eu108.gitpod.io/favicon192.png" />
68
<link rel="manifest" href="https://3000-gitpodio-gitpod-hk3453q4csi.ws-eu108.gitpod.io/manifest.json" />
69
<title>Dashboard</title>
70
<script defer="defer" src="https://3000-gitpodio-gitpod-hk3453q4csi.ws-eu108.gitpod.io/static/js/main.js"></script>
71
72
</head>
73
74
<body><noscript>You need to enable JavaScript to run this app.</noscript>
75
<div id="root"></div>
76
</body>
77
78
</html>`,
79
},
80
}
81
82
for _, test := range tests {
83
t.Run(test.name, func(t *testing.T) {
84
newBase, err := url.Parse(test.newBaseUrl)
85
if err != nil {
86
t.Errorf("error parsing new base url: %v", err)
87
}
88
actual := MatchAndRewriteRootRequest(test.response, newBase)
89
actualBodyBytes, err := ioutil.ReadAll(actual.Body)
90
if err != nil {
91
t.Errorf("error reading response body: %v", err)
92
}
93
actualBody := string(actualBodyBytes)
94
if strings.Compare(actualBody, test.expectedBody) != 0 {
95
t.Errorf("got %v, want %v", actualBody, test.expectedBody)
96
}
97
})
98
}
99
}
100
101