forked from restsharp/RestSharp
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCompressionTests.cs
83 lines (70 loc) · 2.84 KB
/
CompressionTests.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
using System;
using System.IO.Compression;
using System.Net;
using NUnit.Framework;
using RestSharp.IntegrationTests.Helpers;
namespace RestSharp.IntegrationTests
{
[TestFixture]
public class CompressionTests
{
[Test]
public void Can_Handle_Gzip_Compressed_Content()
{
Uri baseUrl = new Uri("http://localhost:8888/");
using (SimpleServer.Create(baseUrl.AbsoluteUri, GzipEchoValue("This is some gzipped content")))
{
RestClient client = new RestClient(baseUrl);
RestRequest request = new RestRequest("");
IRestResponse response = client.Execute(request);
Assert.AreEqual("This is some gzipped content", response.Content);
}
}
[Test]
public void Can_Handle_Deflate_Compressed_Content()
{
Uri baseUrl = new Uri("http://localhost:8888/");
using (SimpleServer.Create(baseUrl.AbsoluteUri, DeflateEchoValue("This is some deflated content")))
{
RestClient client = new RestClient(baseUrl);
RestRequest request = new RestRequest("");
IRestResponse response = client.Execute(request);
Assert.AreEqual("This is some deflated content", response.Content);
}
}
[Test]
public void Can_Handle_Uncompressed_Content()
{
Uri baseUrl = new Uri("http://localhost:8888/");
using (SimpleServer.Create(baseUrl.AbsoluteUri, Handlers.EchoValue("This is some sample content")))
{
RestClient client = new RestClient(baseUrl);
RestRequest request = new RestRequest("");
IRestResponse response = client.Execute(request);
Assert.AreEqual("This is some sample content", response.Content);
}
}
private static Action<HttpListenerContext> GzipEchoValue(string value)
{
return context =>
{
context.Response.Headers.Add("Content-encoding", "gzip");
using (GZipStream gzip = new GZipStream(context.Response.OutputStream, CompressionMode.Compress, true))
{
gzip.WriteStringUtf8(value);
}
};
}
private static Action<HttpListenerContext> DeflateEchoValue(string value)
{
return context =>
{
context.Response.Headers.Add("Content-encoding", "deflate");
using (DeflateStream gzip = new DeflateStream(context.Response.OutputStream, CompressionMode.Compress, true))
{
gzip.WriteStringUtf8(value);
}
};
}
}
}