Beispiel #1
0
        public void Test_response_with_CompressedResult()
        {
            using (new BasicAppHost(typeof(CompressionTests).GetAssembly()).Init())
            {
                var mockResponse = new MockHttpResponse();

                var simpleDto = new TestCompress(1, "name");

                var simpleDtoXml = DataContractSerializer.Instance.SerializeToString(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>";
                const string expectedXmlNetCore = "<TestCompress xmlns=\"http://schemas.ddnglobal.com/types/\" xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\"><Id>1</Id><Name>name</Name></TestCompress>";

                Assert.That(simpleDtoXml, Is.EqualTo(expectedXml).Or.EqualTo(expectedXmlNetCore));

                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.Result, Is.True);

                //var bytesToWriteToResponseStream = new byte[simpleDtoZip.Length - 4];
                //Array.Copy(simpleDtoZip, CompressedResult.Adler32ChecksumLength, bytesToWriteToResponseStream, 0, bytesToWriteToResponseStream.Length);

                var bytesToWriteToResponseStream = simpleDtoZip;

                var writtenBytes = mockResponse.ReadAsBytes();
                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));
            }
        }
        private void ThrowIfError <TResponse>(MockHttpResponse httpRes)
        {
            if (httpRes.StatusCode >= 400)
            {
                var webEx = new WebServiceException("WebServiceException, StatusCode: " + httpRes.StatusCode)
                {
                    StatusCode        = httpRes.StatusCode,
                    StatusDescription = httpRes.StatusDescription,
                };

                try
                {
                    var deserializer = HostContext.ContentTypes.GetStreamDeserializer(httpReq.ResponseContentType);
                    webEx.ResponseDto = deserializer(typeof(TResponse), httpRes.ReadAsBytes().InMemoryStream());
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex);
                }

                throw webEx;
            }
        }