-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreverso_test.go
115 lines (97 loc) · 2.88 KB
/
reverso_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package main
import (
"fmt"
"io"
"log"
"net/http"
"net/http/httptest"
"net/url"
"testing"
)
// Checks if client receives response sent by origin
func TestHandleSimpleRequest(t *testing.T) {
expectedBody := "Hello from the other side"
expectedHeaders := map[string]string{
"X-Test-Header-1": "X-Test-Header-Value-1",
"X-Test-Header-2": "X-Test-Header-Value-2",
}
expectedTrailers := map[string]string{
"X-Test-Trailer-1": "X-Test-Trailer-Value-1",
"X-Test-Trailer-2": "X-Test-Trailer-Value-2",
}
// Setup
req := httptest.NewRequest(http.MethodGet, "/", nil)
rec := httptest.NewRecorder()
svr := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
for key, value := range expectedHeaders {
rw.Header().Set(key, value)
}
for key := range expectedTrailers {
rw.Header().Add("Trailer", key)
}
fmt.Fprint(rw, expectedBody)
for key, value := range expectedTrailers {
rw.Header().Add(key, value)
}
}))
defer svr.Close()
r := &Reverso{originURL: parseServerURL(svr.URL), cache: *NewCacheMiddleware()}
// Test reverse proxy handler
r.ServeHTTP(rec, req)
res := rec.Result()
defer res.Body.Close()
body, err := io.ReadAll(res.Body)
if err != nil {
log.Fatal(err)
}
for key, value := range expectedHeaders {
if res.Header.Get(key) != value {
t.Errorf("Expected header '%v: %v', got '%v'", key, value, res.Header.Get(key))
}
}
if string(body) != expectedBody {
t.Errorf("Expected body '%v', got '%v'", expectedBody, string(body))
}
for key, value := range expectedTrailers {
if res.Trailer.Get(key) != value {
t.Errorf("Expected trailer '%v: %v', got '%v'", key, value, res.Trailer.Get(key))
}
}
}
// Checks if client receives HTTP status code sent by origin
func TestHandleTeapotRequest(t *testing.T) {
// Setup
req := httptest.NewRequest(http.MethodGet, "/teapot", nil)
rec := httptest.NewRecorder()
svr := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusTeapot)
}))
defer svr.Close()
// Test reverse proxy handler
r := &Reverso{originURL: parseServerURL(svr.URL), cache: *NewCacheMiddleware()}
r.ServeHTTP(rec, req)
res := rec.Result()
if res.StatusCode != http.StatusTeapot {
t.Errorf("Expected status code '%v', got '%v'", http.StatusTeapot, res.StatusCode)
}
}
func TestInvalidOriginURL(t *testing.T) {
// Setup
req := httptest.NewRequest(http.MethodGet, "/", nil)
rec := httptest.NewRecorder()
r := &Reverso{} // empty origin URL
// Test reverse proxy handler
r.ServeHTTP(rec, req)
res := rec.Result()
if res.StatusCode != http.StatusInternalServerError {
t.Errorf("Expected status code '%v', got '%v'", http.StatusInternalServerError, res.StatusCode)
}
}
// Parses the given raw URL into a url.URL
func parseServerURL(rawURL string) url.URL {
serverURL, err := url.Parse(rawURL)
if err != nil {
log.Fatal(err)
}
return *serverURL
}