-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathproxy_test.go
237 lines (215 loc) · 6.63 KB
/
proxy_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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
package main
import (
"net/http"
"net/http/httptest"
"time"
"github.com/valyala/fasthttp"
)
func (suite *Tests) Test_proxyTheRequest() {
supplied_headers := map[string]string{
"X-Forwarded-For": "127.0.0.1",
"Content-Type": "application/json",
}
tests := []struct {
headers map[string]string
name string
body string
host string
hostRO string
path string
wantErr bool
wantEndpoint string
}{
{
name: "test_empty",
body: `{"query":"query {\n __type(name: \"Query\") {\n name\n }\n }"}`,
host: "https://telegram-bot.app/",
path: "/v1/graphql",
headers: supplied_headers,
wantErr: false,
wantEndpoint: "https://telegram-bot.app/",
},
{
name: "test_wrong_url",
body: `{"query":"query {\n __type(name: \"Query\") {\n name\n }\n }"}`,
host: "https://google.com/",
path: "/v1/wrongURL",
headers: supplied_headers,
wantErr: true,
wantEndpoint: "https://google.com/",
},
{
name: "Test read only mode",
body: `{"query":"query {\n __type(name: \"Query\") {\n name\n }\n }"}`,
host: "https://google.com/",
hostRO: "https://telegram-bot.app/",
path: "/v1/graphql",
headers: supplied_headers,
wantErr: false,
wantEndpoint: "https://telegram-bot.app/",
},
{
name: "Test read only mode wrong host",
body: `{"query":"query {\n __type(name: \"Query\") {\n name\n }\n }"}`,
host: "https://telegram-bot.app/",
hostRO: "https://google.com/",
path: "/v1/graphql",
headers: supplied_headers,
wantErr: true,
wantEndpoint: "https://google.com/",
},
{
name: "Test mutation with endpoint flip",
body: `{"query":"mutation {\n __type(name: \"Query\") {\n name\n }\n }"}`,
host: "https://telegram-bot.app/",
hostRO: "https://google.com/",
path: "/v1/graphql",
headers: supplied_headers,
wantErr: false,
wantEndpoint: "https://telegram-bot.app/",
},
}
for _, tt := range tests {
suite.Run(tt.name, func() {
cfg = &config{}
parseConfig()
cfg.Server.HostGraphQL = tt.host
if tt.hostRO != "" {
cfg.Server.HostGraphQLReadOnly = tt.hostRO
}
ctx_headers := func() *fasthttp.RequestHeader {
h := fasthttp.RequestHeader{}
for k, v := range tt.headers {
h.Add(k, v)
}
return &h
}()
ctx_request := fasthttp.Request{
Header: *ctx_headers,
}
ctx_request.SetBody([]byte(tt.body))
ctx_request.SetRequestURI(tt.path)
ctx_request.Header.SetMethod("POST")
ctx := suite.app.AcquireCtx(&fasthttp.RequestCtx{
Request: ctx_request,
})
res := parseGraphQLQuery(ctx)
assert.NotNil(ctx, "Fiber context is nil", tt.name)
err := proxyTheRequest(ctx, res.activeEndpoint)
if tt.wantErr {
assert.NotNil(err, "Error is nil", tt.name)
} else {
assert.Nil(err, "Error is not nil", tt.name)
}
assert.Equal(tt.wantEndpoint, res.activeEndpoint, "Unexpected endpoint", tt.name)
})
}
}
func (suite *Tests) Test_proxyTheRequestWithPayloads() {
tests := []struct {
name string
payload string
url string
wantErr bool
}{
{
name: "Test with invalid URL",
payload: `{"query":"query {\n __type(name: \"Query\") {\n name\n }\n }"}`,
url: "://invalid-url",
wantErr: true,
},
{
name: "Test with network error",
payload: `{"query":"query {\n __type(name: \"Query\") {\n name\n }\n }"}`,
url: "http://non-existent-host.invalid",
wantErr: true,
},
// {
// name: "Test with large payload",
// payload: strings.Repeat("a", 10*1024*1024), // 10MB payload
// url: "https://google.com/",
// wantErr: false,
// },
}
for _, tt := range tests {
suite.Run(tt.name, func() {
cfg.Server.HostGraphQL = tt.url
ctx := suite.app.AcquireCtx(&fasthttp.RequestCtx{})
err := proxyTheRequest(ctx, cfg.Server.HostGraphQL)
if tt.wantErr {
assert.NotNil(err)
} else {
assert.Nil(err)
}
})
}
}
func (suite *Tests) Test_proxyTheRequestWithTimeouts() {
originalTimeout := cfg.Client.ClientTimeout
defer func() {
cfg.Client.ClientTimeout = originalTimeout
cfg.Client.FastProxyClient = createFasthttpClient(cfg.Client.ClientTimeout)
}()
// Create a mock server
mockServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
sleepDuration, _ := time.ParseDuration(r.Header.Get("X-Sleep-Duration"))
time.Sleep(sleepDuration)
w.WriteHeader(http.StatusOK)
w.Write([]byte(`{"data":{"test":"response"}}`))
}))
defer mockServer.Close()
tests := []struct {
name string
clientTimeout int
sleepDuration string
body string
wantErr bool
}{
{
name: "Short timeout, long wait for response",
clientTimeout: 1,
sleepDuration: "2s",
body: `{"query":"query { test }"}`,
wantErr: true,
},
{
name: "Short timeout, short wait for response",
clientTimeout: 2,
sleepDuration: "500ms",
body: `{"query":"query { test }"}`,
wantErr: false,
},
{
name: "Long timeout, short wait for response",
clientTimeout: 10,
sleepDuration: "1s",
body: `{"query":"query { test }"}`,
wantErr: false,
},
}
for _, tt := range tests {
suite.Run(tt.name, func() {
cfg.Client.ClientTimeout = tt.clientTimeout
cfg.Client.FastProxyClient = createFasthttpClient(cfg.Client.ClientTimeout)
cfg.Server.HostGraphQL = mockServer.URL
req := &fasthttp.Request{}
req.SetBody([]byte(tt.body))
req.SetRequestURI("/v1/graphql")
req.Header.SetMethod("POST")
req.Header.Set("Content-Type", "application/json")
req.Header.Set("X-Sleep-Duration", tt.sleepDuration)
ctx := suite.app.AcquireCtx(&fasthttp.RequestCtx{})
ctx.Request().Header.SetMethod("POST")
ctx.Request().SetBody(req.Body())
ctx.Request().SetRequestURI(string(req.RequestURI())) // Convert []byte to string
ctx.Request().Header.SetContentType("application/json")
ctx.Request().Header.Set("X-Sleep-Duration", tt.sleepDuration)
err := proxyTheRequest(ctx, cfg.Server.HostGraphQL)
if tt.wantErr {
assert.NotNil(err, "Expected an error for test: %s", tt.name)
} else {
assert.Nil(err, "Expected no error for test: %s", tt.name)
}
})
}
}