@@ -157,11 +157,15 @@ mod test {
157157
158158 #[ tokio:: test]
159159 async fn test_precompressed ( ) {
160- // `../public/sub.js` has pre-compressed version: `sub.js.gz` and `sub.js.br`
160+ // `../public/sub.js` and `../public/blog/second.html` have pre-compressed version:
161161 //
162- // * they are used for response when the client accepts gzip or brotli encoding.
163- // * brotli version is smaller than gzip version, so it is preferred
164- // if the client accepts it.
162+ // - `../public/sub.js.gz`
163+ // - `../public/sub.js.br`
164+ // - `../public/blog/second.html.gz`
165+ //
166+ // They are used for response when the client accepts gzip or brotli encoding.
167+ // Then, brotli version is smaller than gzip version, so it is preferred
168+ // when the client accepts it.
165169 let t = ohkami ( Default :: default ( ) ) . test ( ) ;
166170
167171 // sub.js.br is used for requests that accept brotli
@@ -242,5 +246,42 @@ mod test {
242246 assert_eq ! ( res. header( "Content-Encoding" ) , None ) ;
243247 assert_eq ! ( res. content( "text/javascript" ) , None ) ;
244248 }
249+
250+ // precompressed files in subdirectory are used with no problem
251+ {
252+ let req = TestRequest :: GET ( "/blog/second.html" ) ;
253+ let res = t. oneshot ( req) . await ;
254+ assert_eq ! ( res. status( ) . code( ) , 200 ) ;
255+ assert_eq ! ( res. header( "Content-Encoding" ) , Some ( "gzip" ) ) ;
256+ assert_eq ! ( res. content( "text/html" ) , Some ( include_bytes!( "../public/blog/second.html.gz" ) . as_slice( ) ) ) ;
257+
258+ let req = TestRequest :: GET ( "/blog/second.html" )
259+ . header ( "Accept-Encoding" , "deflate, gzip;q=0" ) ;
260+ let res = t. oneshot ( req) . await ;
261+ assert_eq ! ( res. status( ) . code( ) , 200 ) ;
262+ assert_eq ! ( res. header( "Content-Encoding" ) , None ) ;
263+ assert_eq ! ( res. content( "text/html" ) , Some ( include_str!( "../public/blog/second.html" ) . as_bytes( ) ) ) ;
264+
265+ let req = TestRequest :: GET ( "/blog/second.html" )
266+ . header ( "Accept-Encoding" , "br" ) ;
267+ let res = t. oneshot ( req) . await ;
268+ assert_eq ! ( res. status( ) . code( ) , 200 ) ;
269+ assert_eq ! ( res. header( "Content-Encoding" ) , None ) ;
270+ assert_eq ! ( res. content( "text/html" ) , Some ( include_str!( "../public/blog/second.html" ) . as_bytes( ) ) ) ;
271+
272+ let req = TestRequest :: GET ( "/blog/second.html" )
273+ . header ( "Accept-Encoding" , "gzip;q=0, identity;q=0" ) ;
274+ let res = t. oneshot ( req) . await ;
275+ assert_eq ! ( res. status( ) . code( ) , 406 ) ;
276+ assert_eq ! ( res. header( "Content-Encoding" ) , None ) ;
277+ assert_eq ! ( res. content( "text/html" ) , None ) ;
278+
279+ let req = TestRequest :: GET ( "/blog/second.html" )
280+ . header ( "Accept-Encoding" , "*;q=0" ) ;
281+ let res = t. oneshot ( req) . await ;
282+ assert_eq ! ( res. status( ) . code( ) , 406 ) ;
283+ assert_eq ! ( res. header( "Content-Encoding" ) , None ) ;
284+ assert_eq ! ( res. content( "text/html" ) , None ) ;
285+ }
245286 }
246287}
0 commit comments