55using System . IO ;
66using System . IO . Compression ;
77using System . Linq ;
8- using System . Net ;
9- using System . Net . Http ;
10- using System . Net . Http . Headers ;
118using System . Text ;
129using System . Text . Json ;
1310using System . Threading . Tasks ;
14- using Azure . DataApiBuilder . Core . Models ;
15- using Azure . DataApiBuilder . Core . Services ;
1611using Microsoft . AspNetCore . Builder ;
1712using Microsoft . AspNetCore . Hosting ;
1813using Microsoft . AspNetCore . Http ;
@@ -56,16 +51,16 @@ public async Task TestResponseIsCompressedWithGzip()
5651 {
5752 IHost host = await CreateCompressionConfiguredWebHost ( DabCompressionLevel . Optimal ) ;
5853 TestServer server = host . GetTestServer ( ) ;
59-
54+
6055 HttpContext returnContext = await server . SendAsync ( context =>
6156 {
6257 context . Request . Headers . AcceptEncoding = "gzip" ;
6358 } ) ;
64-
59+
6560 // Verify Content-Encoding header is present
66- Assert . IsTrue ( returnContext . Response . Headers . ContentEncoding . Contains ( "gzip" ) ,
61+ Assert . IsTrue ( returnContext . Response . Headers . ContentEncoding . Contains ( "gzip" ) ,
6762 "Response should have gzip Content-Encoding header" ) ;
68-
63+
6964 // Verify response body exists by checking if we can read it
7065 using ( var reader = new StreamReader ( returnContext . Response . Body ) )
7166 {
@@ -82,13 +77,13 @@ public async Task TestResponseIsCompressedWithBrotli()
8277 {
8378 IHost host = await CreateCompressionConfiguredWebHost ( DabCompressionLevel . Optimal ) ;
8479 TestServer server = host . GetTestServer ( ) ;
85-
80+
8681 HttpContext returnContext = await server . SendAsync ( context =>
8782 {
8883 context . Request . Headers . AcceptEncoding = "br" ;
8984 } ) ;
90-
91- Assert . IsTrue ( returnContext . Response . Headers . ContentEncoding . Contains ( "br" ) ,
85+
86+ Assert . IsTrue ( returnContext . Response . Headers . ContentEncoding . Contains ( "br" ) ,
9287 "Response should have br Content-Encoding header" ) ;
9388 }
9489
@@ -100,37 +95,37 @@ public async Task TestCompressionReducesPayloadSize()
10095 {
10196 IHost host = await CreateCompressionConfiguredWebHost ( DabCompressionLevel . Optimal ) ;
10297 TestServer server = host . GetTestServer ( ) ;
103-
98+
10499 // Get uncompressed response
105100 HttpContext uncompressedContext = await server . SendAsync ( context =>
106101 {
107102 // Don't set Accept-Encoding
108103 } ) ;
109-
104+
110105 using ( var ms = new MemoryStream ( ) )
111106 {
112107 await uncompressedContext . Response . Body . CopyToAsync ( ms ) ;
113108 long uncompressedSize = ms . Length ;
114-
109+
115110 // Get compressed response
116111 HttpContext compressedContext = await server . SendAsync ( context =>
117112 {
118113 context . Request . Headers . AcceptEncoding = "gzip" ;
119114 } ) ;
120-
115+
121116 using ( var cms = new MemoryStream ( ) )
122117 {
123118 await compressedContext . Response . Body . CopyToAsync ( cms ) ;
124119 long compressedSize = cms . Length ;
125-
120+
126121 // Verify compressed size is smaller
127- Assert . IsTrue ( compressedSize < uncompressedSize ,
122+ Assert . IsTrue ( compressedSize < uncompressedSize ,
128123 $ "Compressed size ({ compressedSize } ) should be less than uncompressed size ({ uncompressedSize } )") ;
129-
124+
130125 // Calculate compression ratio
131126 double compressionRatio = ( double ) ( uncompressedSize - compressedSize ) / uncompressedSize * 100 ;
132127 Console . WriteLine ( $ "Compression achieved: { compressionRatio : F2} % reduction (from { uncompressedSize } to { compressedSize } bytes)") ;
133-
128+
134129 // Verify at least some compression occurred (at least 10% for JSON)
135130 Assert . IsTrue ( compressionRatio > 10 , $ "Compression ratio should be at least 10%, got { compressionRatio : F2} %") ;
136131 }
@@ -145,13 +140,13 @@ public async Task TestCompressionDisabledWhenLevelIsNone()
145140 {
146141 IHost host = await CreateCompressionConfiguredWebHost ( DabCompressionLevel . None ) ;
147142 TestServer server = host . GetTestServer ( ) ;
148-
143+
149144 HttpContext returnContext = await server . SendAsync ( context =>
150145 {
151146 context . Request . Headers . AcceptEncoding = "gzip" ;
152147 } ) ;
153-
154- Assert . IsFalse ( returnContext . Response . Headers . ContentEncoding . Any ( ) ,
148+
149+ Assert . IsFalse ( returnContext . Response . Headers . ContentEncoding . Any ( ) ,
155150 "Response should not have Content-Encoding header when compression is disabled" ) ;
156151 }
157152
@@ -163,13 +158,13 @@ public async Task TestNoCompressionWithoutAcceptEncoding()
163158 {
164159 IHost host = await CreateCompressionConfiguredWebHost ( DabCompressionLevel . Optimal ) ;
165160 TestServer server = host . GetTestServer ( ) ;
166-
161+
167162 HttpContext returnContext = await server . SendAsync ( context =>
168163 {
169164 // Don't set Accept-Encoding header
170165 } ) ;
171-
172- Assert . IsFalse ( returnContext . Response . Headers . ContentEncoding . Any ( ) ,
166+
167+ Assert . IsFalse ( returnContext . Response . Headers . ContentEncoding . Any ( ) ,
173168 "Response should not be compressed without Accept-Encoding header" ) ;
174169 }
175170
@@ -181,13 +176,13 @@ public async Task TestCompressionWithFastestLevel()
181176 {
182177 IHost host = await CreateCompressionConfiguredWebHost ( DabCompressionLevel . Fastest ) ;
183178 TestServer server = host . GetTestServer ( ) ;
184-
179+
185180 HttpContext returnContext = await server . SendAsync ( context =>
186181 {
187182 context . Request . Headers . AcceptEncoding = "gzip" ;
188183 } ) ;
189-
190- Assert . IsTrue ( returnContext . Response . Headers . ContentEncoding . Contains ( "gzip" ) ,
184+
185+ Assert . IsTrue ( returnContext . Response . Headers . ContentEncoding . Contains ( "gzip" ) ,
191186 "Response should be compressed with fastest level" ) ;
192187 }
193188
@@ -199,22 +194,22 @@ public async Task TestCompressedContentCanBeDecompressed()
199194 {
200195 IHost host = await CreateCompressionConfiguredWebHost ( DabCompressionLevel . Optimal ) ;
201196 TestServer server = host . GetTestServer ( ) ;
202-
197+
203198 HttpContext returnContext = await server . SendAsync ( context =>
204199 {
205200 context . Request . Headers . AcceptEncoding = "gzip" ;
206201 } ) ;
207-
202+
208203 // Read compressed data
209204 using ( var ms = new MemoryStream ( ) )
210205 {
211206 await returnContext . Response . Body . CopyToAsync ( ms ) ;
212207 byte [ ] compressedData = ms . ToArray ( ) ;
213-
208+
214209 // Decompress
215210 string decompressedContent = await DecompressGzipAsync ( compressedData ) ;
216211 Assert . IsFalse ( string . IsNullOrEmpty ( decompressedContent ) , "Decompressed content should not be empty" ) ;
217-
212+
218213 // Verify it's valid JSON matching our sample
219214 JsonDocument doc = JsonDocument . Parse ( decompressedContent ) ;
220215 Assert . IsTrue ( doc . RootElement . TryGetProperty ( "data" , out _ ) , "Decompressed JSON should contain 'data' property" ) ;
@@ -238,7 +233,7 @@ private static async Task<IHost> CreateCompressionConfiguredWebHost(DabCompressi
238233 . ConfigureServices ( services =>
239234 {
240235 services . AddHttpContextAccessor ( ) ;
241-
236+
242237 // Add response compression based on level
243238 if ( level != DabCompressionLevel . None )
244239 {
@@ -248,19 +243,19 @@ private static async Task<IHost> CreateCompressionConfiguredWebHost(DabCompressi
248243 DabCompressionLevel . Optimal => SystemCompressionLevel . Optimal ,
249244 _ => SystemCompressionLevel . Optimal
250245 } ;
251-
246+
252247 services . AddResponseCompression ( options =>
253248 {
254249 options . EnableForHttps = true ;
255250 options . Providers . Add < GzipCompressionProvider > ( ) ;
256251 options . Providers . Add < BrotliCompressionProvider > ( ) ;
257252 } ) ;
258-
253+
259254 services . Configure < GzipCompressionProviderOptions > ( options =>
260255 {
261256 options . Level = systemLevel ;
262257 } ) ;
263-
258+
264259 services . Configure < BrotliCompressionProviderOptions > ( options =>
265260 {
266261 options . Level = systemLevel ;
@@ -274,7 +269,7 @@ private static async Task<IHost> CreateCompressionConfiguredWebHost(DabCompressi
274269 {
275270 app . UseResponseCompression ( ) ;
276271 }
277-
272+
278273 // Simple endpoint that returns JSON
279274 app . Run ( async context =>
280275 {
0 commit comments