-
Notifications
You must be signed in to change notification settings - Fork 0
/
server_test.go
158 lines (129 loc) · 4.51 KB
/
server_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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package main
import (
"io"
"net/http"
"net/http/httptest"
"net/url"
"strings"
"testing"
)
func newTestRequest(method, targetURL, body string) *http.Request {
req := httptest.NewRequest(method, "/?url="+url.QueryEscape(targetURL), strings.NewReader(body))
req.Header.Set("Content-Type", "application/json")
return req
}
func checkCorsHeaders(t *testing.T, w *httptest.ResponseRecorder) {
if w.Header().Get("Access-Control-Allow-Origin") != "*" {
t.Error("CORS header Access-Control-Allow-Origin is missing or incorrect")
}
if w.Header().Get("Access-Control-Allow-Methods") != "GET, POST, OPTIONS" {
t.Error("CORS header Access-Control-Allow-Methods is missing or incorrect")
}
if w.Header().Get("Access-Control-Allow-Headers") != "Content-Type" {
t.Error("CORS header Access-Control-Allow-Headers is missing or incorrect")
}
}
func TestProxyHandler(t *testing.T) {
backend := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method == http.MethodPost {
body, _ := io.ReadAll(r.Body)
w.Write(body)
} else {
w.Write([]byte("backend response"))
}
}))
defer backend.Close()
t.Run("GET request", func(t *testing.T) {
req := newTestRequest(http.MethodGet, backend.URL, "")
w := httptest.NewRecorder()
proxyHandler(w, req)
resp := w.Result()
body, _ := io.ReadAll(resp.Body)
if string(body) != "backend response" {
t.Errorf("Expected 'backend response', got '%s'", string(body))
}
checkCorsHeaders(t, w)
})
t.Run("POST request", func(t *testing.T) {
req := newTestRequest(http.MethodPost, backend.URL, "test body")
w := httptest.NewRecorder()
proxyHandler(w, req)
resp := w.Result()
body, _ := io.ReadAll(resp.Body)
if string(body) != "test body" {
t.Errorf("Expected 'test body', got '%s'", string(body))
}
checkCorsHeaders(t, w)
})
t.Run("Missing URL parameter", func(t *testing.T) {
req := httptest.NewRequest(http.MethodGet, "/", nil)
w := httptest.NewRecorder()
proxyHandler(w, req)
resp := w.Result()
if resp.StatusCode != http.StatusBadRequest {
t.Errorf("Expected status 400, got %d", resp.StatusCode)
}
body, _ := io.ReadAll(resp.Body)
if string(body) != "Missing url parameter\n" {
t.Errorf("Expected 'Missing url parameter', got '%s'", string(body))
}
checkCorsHeaders(t, w)
})
t.Run("Invalid URL parameter", func(t *testing.T) {
req := newTestRequest(http.MethodGet, "http://%", "")
w := httptest.NewRecorder()
proxyHandler(w, req)
resp := w.Result()
if resp.StatusCode != http.StatusBadRequest {
t.Errorf("Expected status 400, got %d", resp.StatusCode)
}
body, _ := io.ReadAll(resp.Body)
if string(body) != "Invalid url parameter\n" {
t.Errorf("Expected 'Invalid url parameter', got '%s'", string(body))
}
checkCorsHeaders(t, w)
})
t.Run("Valid URL with JSON response", func(t *testing.T) {
req := newTestRequest(http.MethodGet, "https://jsonplaceholder.typicode.com/posts/1", "")
w := httptest.NewRecorder()
proxyHandler(w, req)
resp := w.Result()
body, _ := io.ReadAll(resp.Body)
expectedBody := `{
"userId": 1,
"id": 1,
"title": "sunt aut facere repellat provident occaecati excepturi optio reprehenderit",
"body": "quia et suscipit\nsuscipit recusandae consequuntur expedita et cum\nreprehenderit molestiae ut ut quas totam\nnostrum rerum est autem sunt rem eveniet architecto"
}`
if string(body) != expectedBody {
t.Errorf("Expected JSON body, got '%s'", string(body))
}
if resp.Header.Get("Content-Type") != "application/json; charset=utf-8" {
t.Errorf("Expected content type 'application/json; charset=utf-8', got '%s'", resp.Header.Get("Content-Type"))
}
checkCorsHeaders(t, w)
})
t.Run("POST to valid URL with JSON response", func(t *testing.T) {
reqBody := `{
"title": "foo",
"body": "bar",
"userId": 1
}`
req := newTestRequest(http.MethodPost, "https://jsonplaceholder.typicode.com/posts", reqBody)
w := httptest.NewRecorder()
proxyHandler(w, req)
resp := w.Result()
body, _ := io.ReadAll(resp.Body)
// Check if the response body contains the expected fields
expectedFields := []string{"title", "body", "userId", "id"}
for _, field := range expectedFields {
if !strings.Contains(string(body), field) {
t.Errorf("Expected JSON body to contain field '%s', got '%s'", field, string(body))
}
}
if resp.Header.Get("Content-Type") != "application/json; charset=utf-8" {
t.Errorf("Expected content type 'application/json; charset=utf-8', got '%s'", resp.Header.Get("Content-Type"))
}
checkCorsHeaders(t, w)
})
}