Path: blob/main/components/openvsx-proxy/pkg/openvsxproxy_test.go
2498 views
// Copyright (c) 2020 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 pkg56import (7"bytes"8"fmt"9"io"10"net/http"11"net/http/httptest"12"net/http/httputil"13"net/url"14"testing"15)1617func createFrontend(backendURL string, isDisabledCache bool) (*httptest.Server, *OpenVSXProxy) {18u, _ := url.Parse(backendURL)19cfg := &Config{20URLUpstream: backendURL,21}22if !isDisabledCache {23cfg.AllowCacheDomain = []string{u.Host}24}25openVSXProxy := &OpenVSXProxy{Config: cfg}26openVSXProxy.Setup()2728proxy := httputil.NewSingleHostReverseProxy(openVSXProxy.defaultUpstreamURL)29proxy.ModifyResponse = openVSXProxy.ModifyResponse30handler := http.HandlerFunc(openVSXProxy.Handler(proxy))31frontend := httptest.NewServer(handler)32return frontend, openVSXProxy33}3435func TestAddResponseToCache(t *testing.T) {36backend := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {37bodyBytes, _ := io.ReadAll(r.Body)38rw.Header().Set("Content-Type", "application/json")39rw.Write([]byte(fmt.Sprintf("Hello %s!", string(bodyBytes))))40}))41defer backend.Close()4243frontend, openVSXProxy := createFrontend(backend.URL, false)44defer frontend.Close()4546frontendClient := frontend.Client()4748requestBody := backend.URL49req, _ := http.NewRequest("POST", frontend.URL, bytes.NewBuffer([]byte(requestBody)))50req.Close = true51_, err := frontendClient.Do(req)52if err != nil {53t.Fatal(err)54}55key := fmt.Sprintf("POST / %d %s", len(requestBody), openVSXProxy.hash([]byte(requestBody)))56if _, err = openVSXProxy.cacheManager.Get(key); err != nil {57t.Error(err)58}59if _, ok, err := openVSXProxy.ReadCache(key); ok == false || err != nil {60t.Errorf("key not found or error: %v", err)61}62}6364func TestServeFromCacheOnUpstreamError(t *testing.T) {65backend := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {66rw.WriteHeader(http.StatusInternalServerError)67}))68defer backend.Close()6970frontend, openVSXProxy := createFrontend(backend.URL, false)71defer frontend.Close()7273requestBody := "Request Body Foo"74key := fmt.Sprintf("POST / %d %s", len(requestBody), openVSXProxy.hash([]byte(requestBody)))7576expectedHeader := make(map[string][]string)77expectedHeader["X-Test"] = []string{"Foo Bar"}78expectedResponse := "Response Body Baz"79expectedStatus := 2008081openVSXProxy.StoreCache(key, &CacheObject{82Header: expectedHeader,83Body: []byte(expectedResponse),84StatusCode: expectedStatus,85})8687frontendClient := frontend.Client()8889req, _ := http.NewRequest("POST", frontend.URL, bytes.NewBuffer([]byte(requestBody)))90req.Close = true91res, err := frontendClient.Do(req)92if err != nil {93t.Fatal(err)94}9596if res.StatusCode != expectedStatus {97t.Errorf("got status %d; expected %d", res.StatusCode, expectedStatus)98}99if bodyBytes, _ := io.ReadAll(res.Body); string(bodyBytes) != expectedResponse {100t.Errorf("got body '%s'; expected '%s'", string(bodyBytes), expectedResponse)101}102if h := res.Header.Get("X-Test"); h != "Foo Bar" {103t.Errorf("got header '%s'; expected '%s'", h, "Foo Bar")104}105}106107func TestServeFromNonCacheOnUpstreamError(t *testing.T) {108backend := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {109rw.WriteHeader(http.StatusInternalServerError)110}))111defer backend.Close()112113frontend, openVSXProxy := createFrontend(backend.URL, true)114defer frontend.Close()115116requestBody := "Request Body Foo"117key := fmt.Sprintf("POST / %d %s", len(requestBody), openVSXProxy.hash([]byte(requestBody)))118119expectedHeader := make(map[string][]string)120expectedResponse := ""121expectedStatus := 500122123openVSXProxy.StoreCache(key, &CacheObject{124Header: expectedHeader,125Body: []byte(expectedResponse),126StatusCode: expectedStatus,127})128129frontendClient := frontend.Client()130131req, _ := http.NewRequest("POST", frontend.URL, bytes.NewBuffer([]byte(requestBody)))132req.Close = true133res, err := frontendClient.Do(req)134if err != nil {135t.Fatal(err)136}137138if res.StatusCode != expectedStatus {139t.Errorf("got status %d; expected %d", res.StatusCode, expectedStatus)140}141if bodyBytes, _ := io.ReadAll(res.Body); string(bodyBytes) != expectedResponse {142t.Errorf("got body '%s'; expected '%s'", string(bodyBytes), expectedResponse)143}144}145146147