-
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathProxyCacheTests.cs
More file actions
344 lines (299 loc) · 12.6 KB
/
Copy pathProxyCacheTests.cs
File metadata and controls
344 lines (299 loc) · 12.6 KB
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
using WireMock.Server;
using WireMock.RequestBuilders;
using WireMock.ResponseBuilders;
namespace NpgsqlRestTests;
public static partial class Database
{
public static void ProxyCacheTests()
{
script.Append(@"
-- Passthrough proxy with caching (no proxy response parameters)
create function proxy_cache_passthrough()
returns void
language plpgsql
as
$$
begin
raise exception 'This should not be called - passthrough proxy';
end;
$$;
comment on function proxy_cache_passthrough() is 'HTTP GET
proxy
cached';
-- Passthrough proxy with caching and cache key parameter
create function proxy_cache_passthrough_with_key(_key text)
returns void
language plpgsql
as
$$
begin
raise exception 'This should not be called - passthrough proxy with key';
end;
$$;
comment on function proxy_cache_passthrough_with_key(text) is 'HTTP GET
proxy
cached _key';
-- Passthrough proxy with caching and expiration
create function proxy_cache_passthrough_expires()
returns void
language plpgsql
as
$$
begin
raise exception 'This should not be called - passthrough proxy expires';
end;
$$;
comment on function proxy_cache_passthrough_expires() is 'HTTP GET
proxy
cached
cache_expires_in 1 second';
-- Passthrough proxy without caching (for comparison)
create function proxy_no_cache_passthrough()
returns void
language plpgsql
as
$$
begin
raise exception 'This should not be called - no cache passthrough';
end;
$$;
comment on function proxy_no_cache_passthrough() is 'HTTP GET
proxy';
-- Separate endpoint for status code preservation test
create function proxy_cache_status_test()
returns void
language plpgsql
as
$$
begin
raise exception 'This should not be called - status test';
end;
$$;
comment on function proxy_cache_status_test() is 'HTTP GET
proxy
cached';
-- Separate endpoint for content type preservation test
create function proxy_cache_content_type_test()
returns void
language plpgsql
as
$$
begin
raise exception 'This should not be called - content type test';
end;
$$;
comment on function proxy_cache_content_type_test() is 'HTTP GET
proxy
cached';
");
}
}
[Collection("TestFixture")]
public class ProxyCacheTests : IClassFixture<ProxyWireMockFixture>
{
private readonly TestFixture _test;
private readonly WireMockServer _server;
private static int _requestCounter = 0;
public ProxyCacheTests(TestFixture test, ProxyWireMockFixture wireMock)
{
_test = test;
_server = wireMock.Server;
_server.Reset();
}
[Fact]
public async Task Test_Proxy_Cache_Returns_Same_Response_On_Subsequent_Calls()
{
var counter = Interlocked.Increment(ref _requestCounter);
_server
.Given(Request.Create().WithPath("/api/proxy-cache-passthrough/").UsingGet())
.RespondWith(Response.Create()
.WithStatusCode(200)
.WithHeader("Content-Type", "application/json")
.WithBody($"{{\"counter\": {counter}, \"timestamp\": \"{DateTime.UtcNow:O}\"}}"));
// First request - should hit the proxy
using var result1 = await _test.Client.GetAsync("/api/proxy-cache-passthrough/");
var response1 = await result1.Content.ReadAsStringAsync();
result1?.StatusCode.Should().Be(HttpStatusCode.OK);
// Update the mock to return different value
_server.Reset();
var counter2 = Interlocked.Increment(ref _requestCounter);
_server
.Given(Request.Create().WithPath("/api/proxy-cache-passthrough/").UsingGet())
.RespondWith(Response.Create()
.WithStatusCode(200)
.WithHeader("Content-Type", "application/json")
.WithBody($"{{\"counter\": {counter2}, \"timestamp\": \"{DateTime.UtcNow:O}\"}}"));
await Task.Delay(10);
// Second request - should return cached response
using var result2 = await _test.Client.GetAsync("/api/proxy-cache-passthrough/");
var response2 = await result2.Content.ReadAsStringAsync();
result2?.StatusCode.Should().Be(HttpStatusCode.OK);
response1.Should().Be(response2, "cached proxy endpoint should return same value");
}
[Fact]
public async Task Test_Proxy_Cache_With_Key_Same_Key_Returns_Cached()
{
_server
.Given(Request.Create().WithPath("/api/proxy-cache-passthrough-with-key/").UsingGet())
.RespondWith(Response.Create()
.WithStatusCode(200)
.WithHeader("Content-Type", "application/json")
.WithBody($"{{\"value\": \"first-{Guid.NewGuid()}\"}}"));
// First request with key=test1
using var result1 = await _test.Client.GetAsync("/api/proxy-cache-passthrough-with-key/?key=test1");
var response1 = await result1.Content.ReadAsStringAsync();
result1?.StatusCode.Should().Be(HttpStatusCode.OK);
// Update mock
_server.Reset();
_server
.Given(Request.Create().WithPath("/api/proxy-cache-passthrough-with-key/").UsingGet())
.RespondWith(Response.Create()
.WithStatusCode(200)
.WithHeader("Content-Type", "application/json")
.WithBody($"{{\"value\": \"second-{Guid.NewGuid()}\"}}"));
// Second request with same key - should return cached
using var result2 = await _test.Client.GetAsync("/api/proxy-cache-passthrough-with-key/?key=test1");
var response2 = await result2.Content.ReadAsStringAsync();
result2?.StatusCode.Should().Be(HttpStatusCode.OK);
response1.Should().Be(response2, "same cache key should return cached proxy response");
}
[Fact]
public async Task Test_Proxy_Cache_With_Key_Different_Keys_Return_Different()
{
var guid1 = Guid.NewGuid().ToString();
_server
.Given(Request.Create().WithPath("/api/proxy-cache-passthrough-with-key/").UsingGet())
.RespondWith(Response.Create()
.WithStatusCode(200)
.WithHeader("Content-Type", "application/json")
.WithBody($"{{\"value\": \"{guid1}\"}}"));
// First request with key=keyA
using var result1 = await _test.Client.GetAsync("/api/proxy-cache-passthrough-with-key/?key=keyA");
var response1 = await result1.Content.ReadAsStringAsync();
result1?.StatusCode.Should().Be(HttpStatusCode.OK);
// Update mock for different response
_server.Reset();
var guid2 = Guid.NewGuid().ToString();
_server
.Given(Request.Create().WithPath("/api/proxy-cache-passthrough-with-key/").UsingGet())
.RespondWith(Response.Create()
.WithStatusCode(200)
.WithHeader("Content-Type", "application/json")
.WithBody($"{{\"value\": \"{guid2}\"}}"));
// Second request with different key - should hit proxy again
using var result2 = await _test.Client.GetAsync("/api/proxy-cache-passthrough-with-key/?key=keyB");
var response2 = await result2.Content.ReadAsStringAsync();
result2?.StatusCode.Should().Be(HttpStatusCode.OK);
response1.Should().NotBe(response2, "different cache keys should return different proxy responses");
}
[Fact]
public async Task Test_Proxy_Cache_Expires_Returns_Fresh_After_Expiration()
{
var guid1 = Guid.NewGuid().ToString();
_server
.Given(Request.Create().WithPath("/api/proxy-cache-passthrough-expires/").UsingGet())
.RespondWith(Response.Create()
.WithStatusCode(200)
.WithHeader("Content-Type", "application/json")
.WithBody($"{{\"value\": \"{guid1}\"}}"));
// First request
using var result1 = await _test.Client.GetAsync("/api/proxy-cache-passthrough-expires/");
var response1 = await result1.Content.ReadAsStringAsync();
result1?.StatusCode.Should().Be(HttpStatusCode.OK);
// Wait for cache to expire
await Task.Delay(1500);
// Update mock
_server.Reset();
var guid2 = Guid.NewGuid().ToString();
_server
.Given(Request.Create().WithPath("/api/proxy-cache-passthrough-expires/").UsingGet())
.RespondWith(Response.Create()
.WithStatusCode(200)
.WithHeader("Content-Type", "application/json")
.WithBody($"{{\"value\": \"{guid2}\"}}"));
// Second request after expiration - should hit proxy again
using var result2 = await _test.Client.GetAsync("/api/proxy-cache-passthrough-expires/");
var response2 = await result2.Content.ReadAsStringAsync();
result2?.StatusCode.Should().Be(HttpStatusCode.OK);
response1.Should().NotBe(response2, "cache should expire and return fresh proxy response");
}
[Fact]
public async Task Test_Proxy_No_Cache_Returns_Different_On_Each_Call()
{
var guid1 = Guid.NewGuid().ToString();
_server
.Given(Request.Create().WithPath("/api/proxy-no-cache-passthrough/").UsingGet())
.RespondWith(Response.Create()
.WithStatusCode(200)
.WithHeader("Content-Type", "application/json")
.WithBody($"{{\"value\": \"{guid1}\"}}"));
// First request
using var result1 = await _test.Client.GetAsync("/api/proxy-no-cache-passthrough/");
var response1 = await result1.Content.ReadAsStringAsync();
result1?.StatusCode.Should().Be(HttpStatusCode.OK);
// Update mock
_server.Reset();
var guid2 = Guid.NewGuid().ToString();
_server
.Given(Request.Create().WithPath("/api/proxy-no-cache-passthrough/").UsingGet())
.RespondWith(Response.Create()
.WithStatusCode(200)
.WithHeader("Content-Type", "application/json")
.WithBody($"{{\"value\": \"{guid2}\"}}"));
// Second request - should hit proxy again (no caching)
using var result2 = await _test.Client.GetAsync("/api/proxy-no-cache-passthrough/");
var response2 = await result2.Content.ReadAsStringAsync();
result2?.StatusCode.Should().Be(HttpStatusCode.OK);
response1.Should().NotBe(response2, "non-cached proxy should return different values");
}
[Fact]
public async Task Test_Proxy_Cache_Preserves_Status_Code()
{
_server
.Given(Request.Create().WithPath("/api/proxy-cache-status-test/").UsingGet())
.RespondWith(Response.Create()
.WithStatusCode(201)
.WithHeader("Content-Type", "application/json")
.WithBody("{\"created\": true}"));
// First request
using var result1 = await _test.Client.GetAsync("/api/proxy-cache-status-test/");
result1?.StatusCode.Should().Be(HttpStatusCode.Created);
// Update mock to different status
_server.Reset();
_server
.Given(Request.Create().WithPath("/api/proxy-cache-status-test/").UsingGet())
.RespondWith(Response.Create()
.WithStatusCode(200)
.WithHeader("Content-Type", "application/json")
.WithBody("{\"ok\": true}"));
// Second request - should return cached status code
using var result2 = await _test.Client.GetAsync("/api/proxy-cache-status-test/");
result2?.StatusCode.Should().Be(HttpStatusCode.Created, "cached proxy should preserve status code");
}
[Fact]
public async Task Test_Proxy_Cache_Preserves_Content_Type()
{
_server
.Given(Request.Create().WithPath("/api/proxy-cache-content-type-test/").UsingGet())
.RespondWith(Response.Create()
.WithStatusCode(200)
.WithHeader("Content-Type", "text/xml")
.WithBody("<data>test</data>"));
// First request
using var result1 = await _test.Client.GetAsync("/api/proxy-cache-content-type-test/");
var contentType1 = result1?.Content.Headers.ContentType?.ToString();
contentType1.Should().Contain("text/xml");
// Update mock to different content type
_server.Reset();
_server
.Given(Request.Create().WithPath("/api/proxy-cache-content-type-test/").UsingGet())
.RespondWith(Response.Create()
.WithStatusCode(200)
.WithHeader("Content-Type", "application/json")
.WithBody("{\"data\": \"test\"}"));
// Second request - should return cached content type
using var result2 = await _test.Client.GetAsync("/api/proxy-cache-content-type-test/");
var contentType2 = result2?.Content.Headers.ContentType?.ToString();
contentType2.Should().Contain("text/xml", "cached proxy should preserve content type");
}
}