Path: blob/main/components/proxy/plugins/frontend_dev/frontend_dev_test.go
2500 views
// Copyright (c) 2023 Gitpod GmbH. All rights reserved.1// Licensed under the GNU Affero General Public License (AGPL).2// See License.AGPL.txt in the project root for license information.34package frontend_dev56import (7"io/ioutil"8"net/http"9"net/url"10"strings"11"testing"12)1314const index_html = `<!doctype html>15<html lang="en">1617<head>18<meta charset="utf-8" />19<link rel="icon" href="/favicon256.png" />20<meta name="viewport" content="width=device-width,initial-scale=1" />21<meta name="theme-color" content="#000000" />22<meta name="robots" content="noindex">23<meta name="Gitpod" content="Always Ready-to-Code" />24<link rel="apple-touch-icon" href="/favicon192.png" />25<link rel="manifest" href="/manifest.json" />26<title>Dashboard</title>27<script defer="defer" src="/static/js/main.b009793d.js"></script>28<link href="/static/css/main.32e61b25.css" rel="stylesheet">29</head>3031<body><noscript>You need to enable JavaScript to run this app.</noscript>32<div id="root"></div>33</body>3435</html>`3637func Test_MatchAndRewriteRootRequest(t *testing.T) {3839type Test struct {40name string41response *http.Response42newBaseUrl string43expectedBody string44}45tests := []Test{46{47name: "should match and rewrite root request",48response: &http.Response{49StatusCode: 200,50Header: http.Header{51"Content-Type": []string{"text/html"},52},53Body: ioutil.NopCloser(strings.NewReader(index_html)),54},55newBaseUrl: "https://3000-gitpodio-gitpod-hk3453q4csi.ws-eu108.gitpod.io",56expectedBody: `<!doctype html>57<html lang="en">5859<head>60<meta charset="utf-8" />61<link rel="icon" href="https://3000-gitpodio-gitpod-hk3453q4csi.ws-eu108.gitpod.io/favicon256.png" />62<meta name="viewport" content="width=device-width,initial-scale=1" />63<meta name="theme-color" content="#000000" />64<meta name="robots" content="noindex">65<meta name="Gitpod" content="Always Ready-to-Code" />66<link rel="apple-touch-icon" href="https://3000-gitpodio-gitpod-hk3453q4csi.ws-eu108.gitpod.io/favicon192.png" />67<link rel="manifest" href="https://3000-gitpodio-gitpod-hk3453q4csi.ws-eu108.gitpod.io/manifest.json" />68<title>Dashboard</title>69<script defer="defer" src="https://3000-gitpodio-gitpod-hk3453q4csi.ws-eu108.gitpod.io/static/js/main.js"></script>7071</head>7273<body><noscript>You need to enable JavaScript to run this app.</noscript>74<div id="root"></div>75</body>7677</html>`,78},79}8081for _, test := range tests {82t.Run(test.name, func(t *testing.T) {83newBase, err := url.Parse(test.newBaseUrl)84if err != nil {85t.Errorf("error parsing new base url: %v", err)86}87actual := MatchAndRewriteRootRequest(test.response, newBase)88actualBodyBytes, err := ioutil.ReadAll(actual.Body)89if err != nil {90t.Errorf("error reading response body: %v", err)91}92actualBody := string(actualBodyBytes)93if strings.Compare(actualBody, test.expectedBody) != 0 {94t.Errorf("got %v, want %v", actualBody, test.expectedBody)95}96})97}98}99100101