public void Register(IAppHost appHost) { appHost.ResponseFilters.Add((request, response, dto) => { if (dto == null || dto is AuthResponse || dto is CompressedResult || dto is Exception) { return; } using (var serializationContext = new HttpRequestContext(request, response, dto)) { if (!serializationContext.RequestAttributes.AcceptsDeflate && !serializationContext.RequestAttributes.AcceptsGzip) { return; } var serializedDto = EndpointHost.ContentTypeFilter.SerializeToString(serializationContext, dto); var callback = request.GetJsonpCallback(); var isJsonpRequest = EndpointHost.Config.AllowJsonpRequests && !string.IsNullOrEmpty(callback); if (isJsonpRequest) { serializedDto = (callback + "(") + serializedDto + ")"; serializationContext.ResponseContentType = ContentType.JavaScript; } var compressedBytes = serializedDto.Compress(serializationContext.CompressionType); var compressedResult = new CompressedResult(compressedBytes, serializationContext.CompressionType, serializationContext.ResponseContentType); response.WriteToResponse(compressedResult, serializationContext.ResponseContentType); } }); }
public void Test_response_with_CompressedResult() { EndpointHost.Config = new EndpointHostConfig( "ServiceName", new ServiceManager(GetType().Assembly)); var assembly = typeof(CompressionTests).Assembly; EndpointHost.ConfigureHost( new TestAppHost(new Container(), assembly), "Name", new ServiceManager(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)); }
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)); } }
public async Task CacheExport(ExportRequestType type, CompressedResult result) { //TODO: create settings for results. await _provider.CacheObject($"{ExportPartial}{type}", result, _settings.TranslationCacheTtlSeconds); }