/
CompressResponseTests.cs
357 lines (304 loc) · 12.1 KB
/
CompressResponseTests.cs
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
345
346
347
348
349
350
351
352
353
354
355
356
357
using System.Collections.Generic;
using System.Net;
using System.Threading.Tasks;
using Funq;
using NUnit.Framework;
using ServiceStack.IO;
using ServiceStack.Text;
using ServiceStack.VirtualPath;
namespace ServiceStack.WebHost.Endpoints.Tests
{
public class CompressData : IReturn<CompressData>
{
public string String { get; set; }
public byte[] Bytes { get; set; }
}
public class CompressString : IReturn<string>
{
public string String { get; set; }
}
public class CompressBytes : IReturn<byte[]>
{
public byte[] Bytes { get; set; }
}
[Route("/compress/{Path*}")]
public class CompressFile
{
public string Path { get; set; }
}
public class CompressError : IReturn<CompressError> { }
[Route("/compress/dto-result/{Name}")]
public class CompressDtoResult : IReturn<CompressDtoResult>
{
public string Name { get; set; }
}
[CompressResponse]
public class CompressedServices : Service
{
public object Any(CompressData request) => request;
public object Any(CompressString request) => request.String;
public object Any(CompressBytes request) => request.Bytes;
public object Any(CompressFile request)
{
var file = VirtualFileSources.GetFile(request.Path);
if (file == null)
throw HttpError.NotFound($"{request.Path} does not exist");
return new HttpResult(file);
}
public object Any(CompressDtoResult request) => new HttpResult(request, MimeTypes.Xml);
public object Any(CompressError request)
{
throw HttpError.NotFound("Always NotFound");
}
}
public class CompressResponseTests
{
class AppHost : AppSelfHostBase
{
public AppHost()
: base(nameof(CompressResponseTests), typeof(CompressedServices).Assembly) { }
public override void Configure(Container container)
{
SetConfig(new HostConfig
{
CompressFilesWithExtensions = { "html", "css" }
});
}
public override List<IVirtualPathProvider> GetVirtualFileSources()
{
var existingProviders = base.GetVirtualFileSources();
var memFs = new MemoryVirtualFiles();
memFs.WriteFile("/file.js", "console.log('foo')");
memFs.WriteFile("/file.css", ".foo{}");
memFs.WriteFile("/file.txt", "foo");
memFs.WriteFile("/default.html", "<body>foo</body>");
//Give new Memory FS highest priority
existingProviders.Insert(0, memFs);
return existingProviders;
}
}
private ServiceStackHost appHost;
public CompressResponseTests()
{
appHost = new AppHost()
.Init()
.Start(Config.ListeningOn);
}
[OneTimeTearDown]
public void OneTimeTearDown() => appHost.Dispose();
[Test]
public void Does_compress_RequestDto_responses()
{
var client = new JsonServiceClient(Config.ListeningOn);
var response = client.Post(new CompressData
{
String = "Hello",
Bytes = "World".ToUtf8Bytes()
});
Assert.That(response.String, Is.EqualTo("Hello"));
Assert.That(response.Bytes, Is.EqualTo("World".ToUtf8Bytes()));
}
[Test]
public async Task Does_compress_RequestDto_responses_HttpClient()
{
var client = new JsonHttpClient(Config.ListeningOn);
var response = await client.PostAsync(new CompressData
{
String = "Hello",
Bytes = "World".ToUtf8Bytes()
});
Assert.That(response.String, Is.EqualTo("Hello"));
Assert.That(response.Bytes, Is.EqualTo("World".ToUtf8Bytes()));
}
[Test]
public void Does_compress_raw_String_responses()
{
var client = new JsonServiceClient(Config.ListeningOn);
var response = client.Post(new CompressString
{
String = "foo",
});
Assert.That(response, Is.EqualTo("foo"));
}
[Test]
public async Task Does_compress_raw_String_responses_HttpClient()
{
var client = new JsonHttpClient(Config.ListeningOn);
var response = await client.PostAsync(new CompressString
{
String = "foo",
});
Assert.That(response, Is.EqualTo("foo"));
}
[Test]
public void Does_compress_raw_Bytes_responses()
{
var client = new JsonServiceClient(Config.ListeningOn);
var response = client.Post(new CompressBytes
{
Bytes = "foo".ToUtf8Bytes(),
});
Assert.That(response, Is.EquivalentTo("foo".ToUtf8Bytes()));
}
[Test]
public async Task Does_compress_raw_Bytes_responses_HttpClient()
{
var client = new JsonHttpClient(Config.ListeningOn);
var response = await client.PostAsync(new CompressBytes
{
Bytes = "foo".ToUtf8Bytes(),
});
Assert.That(response, Is.EquivalentTo("foo".ToUtf8Bytes()));
}
[Test]
public void Does_not_compress_error_responses()
{
var client = new JsonServiceClient(Config.ListeningOn);
try
{
client.Post(new CompressError());
Assert.Fail("Should throw");
}
catch (WebServiceException ex)
{
Assert.That(ex.StatusCode, Is.EqualTo(404));
Assert.That(ex.ErrorCode, Is.EqualTo("NotFound"));
Assert.That(ex.ErrorMessage, Is.EqualTo("Always NotFound"));
}
}
[Test]
public async Task Does_not_compress_error_responses_HttpClient()
{
var client = new JsonHttpClient(Config.ListeningOn);
try
{
await client.PostAsync(new CompressError());
Assert.Fail("Should throw");
}
catch (WebServiceException ex)
{
Assert.That(ex.StatusCode, Is.EqualTo(404));
Assert.That(ex.ErrorCode, Is.EqualTo("NotFound"));
Assert.That(ex.ErrorMessage, Is.EqualTo("Always NotFound"));
}
}
[Test]
public void Does_compress_using_ContenType_in_HttpResult()
{
var url = Config.ListeningOn.CombineWith(new CompressDtoResult { Name = "foo" }.ToGetUrl());
var xml = url.GetJsonFromUrl(responseFilter: res =>
{
Assert.That(res.GetHeader(HttpHeaders.ContentType), Does.StartWith(MimeTypes.Xml));
});
Assert.That(xml, Does.StartWith("<?xml"));
}
[Test]
public async Task Does_compress_using_ContenType_in_HttpResult_Async()
{
var url = Config.ListeningOn.CombineWith(new CompressDtoResult { Name = "foo" }.ToGetUrl());
var xml = await url.GetJsonFromUrlAsync(responseFilter: res =>
{
Assert.That(res.GetHeader(HttpHeaders.ContentType), Does.StartWith(MimeTypes.Xml));
});
Assert.That(xml, Does.StartWith("<?xml"));
}
#if NETFX
[Test]
public void Does_compress_file_returned_in_HttpResult()
{
var url = Config.ListeningOn.CombineWith("/compress/file.js");
var zipBytes = url.GetBytesFromUrl(
requestFilter: req =>
{
req.AutomaticDecompression = DecompressionMethods.None;
req.Headers[HttpRequestHeader.AcceptEncoding] = "deflate";
}, responseFilter: res =>
{
Assert.That(res.Headers[HttpResponseHeader.ContentEncoding], Is.EqualTo("deflate"));
});
var bytes = zipBytes.DecompressBytes("deflate");
Assert.That(bytes.FromUtf8Bytes(), Is.EqualTo("console.log('foo')"));
}
[Test]
public async Task Does_compress_file_returned_in_HttpResult_Async()
{
var url = Config.ListeningOn.CombineWith("/compress/file.js");
var zipBytes = await url.GetBytesFromUrlAsync(
requestFilter: req =>
{
req.AutomaticDecompression = DecompressionMethods.None;
req.Headers[HttpRequestHeader.AcceptEncoding] = "deflate";
}, responseFilter: res =>
{
Assert.That(res.Headers[HttpResponseHeader.ContentEncoding], Is.EqualTo("deflate"));
});
var bytes = zipBytes.DecompressBytes("deflate");
Assert.That(bytes.FromUtf8Bytes(), Is.EqualTo("console.log('foo')"));
}
[Test]
public void Does_compress_static_file_in_CompressFilesWithExtensions()
{
var url = Config.ListeningOn.CombineWith("/file.css");
var zipBytes = url.GetBytesFromUrl(
requestFilter: req =>
{
req.AutomaticDecompression = DecompressionMethods.None;
req.Headers[HttpRequestHeader.AcceptEncoding] = "deflate";
}, responseFilter: res =>
{
Assert.That(res.Headers[HttpResponseHeader.ContentEncoding], Is.EqualTo("deflate"));
});
var bytes = zipBytes.DecompressBytes("deflate");
Assert.That(bytes.FromUtf8Bytes(), Is.EqualTo(".foo{}"));
}
[Test]
public async Task Does_compress_static_file_in_CompressFilesWithExtensions_Async()
{
var url = Config.ListeningOn.CombineWith("/file.css");
var zipBytes = await url.GetBytesFromUrlAsync(
requestFilter: req =>
{
req.AutomaticDecompression = DecompressionMethods.None;
req.Headers[HttpRequestHeader.AcceptEncoding] = "deflate";
}, responseFilter: res =>
{
Assert.That(res.Headers[HttpResponseHeader.ContentEncoding], Is.EqualTo("deflate"));
});
var bytes = zipBytes.DecompressBytes("deflate");
Assert.That(bytes.FromUtf8Bytes(), Is.EqualTo(".foo{}"));
}
[Test]
public void Does_compress_default_page_in_CompressFilesWithExtensions()
{
var url = Config.ListeningOn.CombineWith("/default.html");
var zipBytes = url.GetBytesFromUrl(
requestFilter: req =>
{
req.AutomaticDecompression = DecompressionMethods.None;
req.Headers[HttpRequestHeader.AcceptEncoding] = "deflate";
}, responseFilter: res =>
{
Assert.That(res.Headers[HttpResponseHeader.ContentEncoding], Is.EqualTo("deflate"));
});
var bytes = zipBytes.DecompressBytes("deflate");
Assert.That(bytes.FromUtf8Bytes(), Is.EqualTo("<body>foo</body>"));
}
[Test]
public async Task Does_compress_default_page_in_CompressFilesWithExtensions_Async()
{
var url = Config.ListeningOn.CombineWith("/default.html");
var zipBytes = await url.GetBytesFromUrlAsync(
requestFilter: req =>
{
req.AutomaticDecompression = DecompressionMethods.None;
req.Headers[HttpRequestHeader.AcceptEncoding] = "deflate";
}, responseFilter: res =>
{
Assert.That(res.Headers[HttpResponseHeader.ContentEncoding], Is.EqualTo("deflate"));
});
var bytes = zipBytes.DecompressBytes("deflate");
Assert.That(bytes.FromUtf8Bytes(), Is.EqualTo("<body>foo</body>"));
}
#endif
}
}