Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gitpod-io
GitHub Repository: gitpod-io/gitpod
Path: blob/main/components/openvsx-proxy/pkg/openvsxproxy_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 pkg
6
7
import (
8
"bytes"
9
"fmt"
10
"io"
11
"net/http"
12
"net/http/httptest"
13
"net/http/httputil"
14
"net/url"
15
"testing"
16
)
17
18
func createFrontend(backendURL string, isDisabledCache bool) (*httptest.Server, *OpenVSXProxy) {
19
u, _ := url.Parse(backendURL)
20
cfg := &Config{
21
URLUpstream: backendURL,
22
}
23
if !isDisabledCache {
24
cfg.AllowCacheDomain = []string{u.Host}
25
}
26
openVSXProxy := &OpenVSXProxy{Config: cfg}
27
openVSXProxy.Setup()
28
29
proxy := httputil.NewSingleHostReverseProxy(openVSXProxy.defaultUpstreamURL)
30
proxy.ModifyResponse = openVSXProxy.ModifyResponse
31
handler := http.HandlerFunc(openVSXProxy.Handler(proxy))
32
frontend := httptest.NewServer(handler)
33
return frontend, openVSXProxy
34
}
35
36
func TestAddResponseToCache(t *testing.T) {
37
backend := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
38
bodyBytes, _ := io.ReadAll(r.Body)
39
rw.Header().Set("Content-Type", "application/json")
40
rw.Write([]byte(fmt.Sprintf("Hello %s!", string(bodyBytes))))
41
}))
42
defer backend.Close()
43
44
frontend, openVSXProxy := createFrontend(backend.URL, false)
45
defer frontend.Close()
46
47
frontendClient := frontend.Client()
48
49
requestBody := backend.URL
50
req, _ := http.NewRequest("POST", frontend.URL, bytes.NewBuffer([]byte(requestBody)))
51
req.Close = true
52
_, err := frontendClient.Do(req)
53
if err != nil {
54
t.Fatal(err)
55
}
56
key := fmt.Sprintf("POST / %d %s", len(requestBody), openVSXProxy.hash([]byte(requestBody)))
57
if _, err = openVSXProxy.cacheManager.Get(key); err != nil {
58
t.Error(err)
59
}
60
if _, ok, err := openVSXProxy.ReadCache(key); ok == false || err != nil {
61
t.Errorf("key not found or error: %v", err)
62
}
63
}
64
65
func TestServeFromCacheOnUpstreamError(t *testing.T) {
66
backend := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
67
rw.WriteHeader(http.StatusInternalServerError)
68
}))
69
defer backend.Close()
70
71
frontend, openVSXProxy := createFrontend(backend.URL, false)
72
defer frontend.Close()
73
74
requestBody := "Request Body Foo"
75
key := fmt.Sprintf("POST / %d %s", len(requestBody), openVSXProxy.hash([]byte(requestBody)))
76
77
expectedHeader := make(map[string][]string)
78
expectedHeader["X-Test"] = []string{"Foo Bar"}
79
expectedResponse := "Response Body Baz"
80
expectedStatus := 200
81
82
openVSXProxy.StoreCache(key, &CacheObject{
83
Header: expectedHeader,
84
Body: []byte(expectedResponse),
85
StatusCode: expectedStatus,
86
})
87
88
frontendClient := frontend.Client()
89
90
req, _ := http.NewRequest("POST", frontend.URL, bytes.NewBuffer([]byte(requestBody)))
91
req.Close = true
92
res, err := frontendClient.Do(req)
93
if err != nil {
94
t.Fatal(err)
95
}
96
97
if res.StatusCode != expectedStatus {
98
t.Errorf("got status %d; expected %d", res.StatusCode, expectedStatus)
99
}
100
if bodyBytes, _ := io.ReadAll(res.Body); string(bodyBytes) != expectedResponse {
101
t.Errorf("got body '%s'; expected '%s'", string(bodyBytes), expectedResponse)
102
}
103
if h := res.Header.Get("X-Test"); h != "Foo Bar" {
104
t.Errorf("got header '%s'; expected '%s'", h, "Foo Bar")
105
}
106
}
107
108
func TestServeFromNonCacheOnUpstreamError(t *testing.T) {
109
backend := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
110
rw.WriteHeader(http.StatusInternalServerError)
111
}))
112
defer backend.Close()
113
114
frontend, openVSXProxy := createFrontend(backend.URL, true)
115
defer frontend.Close()
116
117
requestBody := "Request Body Foo"
118
key := fmt.Sprintf("POST / %d %s", len(requestBody), openVSXProxy.hash([]byte(requestBody)))
119
120
expectedHeader := make(map[string][]string)
121
expectedResponse := ""
122
expectedStatus := 500
123
124
openVSXProxy.StoreCache(key, &CacheObject{
125
Header: expectedHeader,
126
Body: []byte(expectedResponse),
127
StatusCode: expectedStatus,
128
})
129
130
frontendClient := frontend.Client()
131
132
req, _ := http.NewRequest("POST", frontend.URL, bytes.NewBuffer([]byte(requestBody)))
133
req.Close = true
134
res, err := frontendClient.Do(req)
135
if err != nil {
136
t.Fatal(err)
137
}
138
139
if res.StatusCode != expectedStatus {
140
t.Errorf("got status %d; expected %d", res.StatusCode, expectedStatus)
141
}
142
if bodyBytes, _ := io.ReadAll(res.Body); string(bodyBytes) != expectedResponse {
143
t.Errorf("got body '%s'; expected '%s'", string(bodyBytes), expectedResponse)
144
}
145
}
146
147