// Copyright (c) 2023 Gitpod GmbH. All rights reserved. // Licensed under the GNU Affero General Public License (AGPL). // See License.AGPL.txt in the project root for license information. package frontend_dev import ( "io/ioutil" "net/http" "net/url" "strings" "testing" ) const index_html = ` Dashboard
` func Test_MatchAndRewriteRootRequest(t *testing.T) { type Test struct { name string response *http.Response newBaseUrl string expectedBody string } tests := []Test{ { name: "should match and rewrite root request", response: &http.Response{ StatusCode: 200, Header: http.Header{ "Content-Type": []string{"text/html"}, }, Body: ioutil.NopCloser(strings.NewReader(index_html)), }, newBaseUrl: "https://3000-gitpodio-gitpod-hk3453q4csi.ws-eu108.gitpod.io", expectedBody: ` Dashboard
`, }, } for _, test := range tests { t.Run(test.name, func(t *testing.T) { newBase, err := url.Parse(test.newBaseUrl) if err != nil { t.Errorf("error parsing new base url: %v", err) } actual := MatchAndRewriteRootRequest(test.response, newBase) actualBodyBytes, err := ioutil.ReadAll(actual.Body) if err != nil { t.Errorf("error reading response body: %v", err) } actualBody := string(actualBodyBytes) if strings.Compare(actualBody, test.expectedBody) != 0 { t.Errorf("got %v, want %v", actualBody, test.expectedBody) } }) } }