forked from ServiceStack/ServiceStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCompressionTests.cs
More file actions
137 lines (96 loc) · 4.13 KB
/
CompressionTests.cs
File metadata and controls
137 lines (96 loc) · 4.13 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
using System;
using System.Runtime.Serialization;
using Funq;
using Moq;
using NUnit.Framework;
using ServiceStack.Common;
using ServiceStack.Common.Extensions;
using ServiceStack.Common.Web;
using ServiceStack.Logging;
using ServiceStack.ServiceHost;
using ServiceStack.ServiceInterface.Testing;
using ServiceStack.ServiceModel.Serialization;
using ServiceStack.WebHost.Endpoints.Extensions;
using ServiceStack.WebHost.Endpoints.Tests.Mocks;
using DataContractSerializer = ServiceStack.ServiceModel.Serialization.DataContractSerializer;
namespace ServiceStack.WebHost.Endpoints.Tests
{
[DataContract(Namespace = "http://schemas.ddnglobal.com/types/")]
public class TestCompress
{
[DataMember]
public int Id { get; set; }
[DataMember]
public string Name { get; set; }
public TestCompress()
{
}
public TestCompress(int id, string name)
{
Id = id;
Name = name;
}
}
[TestFixture]
public class CompressionTests
{
private static readonly ILog Log = LogManager.GetLogger(typeof(CompressionTests));
[Test]
public void Can_compress_and_decompress_SimpleDto()
{
var simpleDto = new TestCompress(1, "name");
var simpleDtoXml = DataContractSerializer.Instance.Parse(simpleDto);
var simpleDtoZip = simpleDtoXml.Deflate();
Assert.That(simpleDtoZip.Length, Is.GreaterThan(0));
var deserializedSimpleDtoXml = simpleDtoZip.Inflate();
Assert.That(deserializedSimpleDtoXml, Is.Not.Empty);
var deserializedSimpleDto = DataContractDeserializer.Instance.Parse<TestCompress>(
deserializedSimpleDtoXml);
Assert.That(deserializedSimpleDto, Is.Not.Null);
Assert.That(deserializedSimpleDto.Id, Is.EqualTo(simpleDto.Id));
Assert.That(deserializedSimpleDto.Name, Is.EqualTo(simpleDto.Name));
}
[Test]
public void Test_response_with_CompressedResult()
{
EndpointHost.Config = new EndpointHostConfig(
"ServiceName",
new ServiceManager(GetType().Assembly));
var mockResponse = new HttpResponseMock();
var simpleDto = new TestCompress(1, "name");
var simpleDtoXml = DataContractSerializer.Instance.Parse(simpleDto);
const string expectedXml = "<TestCompress xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns=\"http://schemas.ddnglobal.com/types/\"><Id>1</Id><Name>name</Name></TestCompress>";
Assert.That(simpleDtoXml, Is.EqualTo(expectedXml));
var simpleDtoZip = simpleDtoXml.Deflate();
Assert.That(simpleDtoZip.Length, Is.GreaterThan(0));
var compressedResult = new CompressedResult(simpleDtoZip);
var reponseWasAutoHandled = mockResponse.WriteToResponse(
compressedResult, CompressionTypes.Deflate);
Assert.That(reponseWasAutoHandled, Is.True);
//var bytesToWriteToResponseStream = new byte[simpleDtoZip.Length - 4];
//Array.Copy(simpleDtoZip, CompressedResult.Adler32ChecksumLength, bytesToWriteToResponseStream, 0, bytesToWriteToResponseStream.Length);
var bytesToWriteToResponseStream = simpleDtoZip;
var writtenBytes = mockResponse.GetOutputStreamAsBytes();
Assert.That(writtenBytes, Is.EqualTo(bytesToWriteToResponseStream));
Assert.That(mockResponse.ContentType, Is.EqualTo(MimeTypes.Xml));
Assert.That(mockResponse.Headers[HttpHeaders.ContentEncoding], Is.EqualTo(CompressionTypes.Deflate));
Log.Debug("Content-length: " + writtenBytes.Length);
Log.Debug(BitConverter.ToString(writtenBytes));
}
[Test]
public void Can_gzip_and_gunzip_SimpleDto()
{
var simpleDto = new TestCompress(1, "name");
var simpleDtoXml = DataContractSerializer.Instance.Parse(simpleDto);
var simpleDtoZip = simpleDtoXml.GZip();
Assert.That(simpleDtoZip.Length, Is.GreaterThan(0));
var deserializedSimpleDtoXml = simpleDtoZip.GUnzip();
Assert.That(deserializedSimpleDtoXml, Is.Not.Empty);
var deserializedSimpleDto = DataContractDeserializer.Instance.Parse<TestCompress>(
deserializedSimpleDtoXml);
Assert.That(deserializedSimpleDto, Is.Not.Null);
Assert.That(deserializedSimpleDto.Id, Is.EqualTo(simpleDto.Id));
Assert.That(deserializedSimpleDto.Name, Is.EqualTo(simpleDto.Name));
}
}
}