@@ -205,73 +205,78 @@ mod pick_port {
205205
206206#[ cfg( test) ]
207207mod tests {
208+ use std:: collections:: HashMap ;
209+
208210 use crate :: server:: get_router;
209- use crate :: wasm_bindgen;
211+ use crate :: wasm_bindgen:: WasmBindgenOutput ;
210212 use crate :: Options ;
211- use axum:: body:: Bytes ;
212213 use axum:: http:: StatusCode ;
213214 use axum_test_helper:: TestClient ;
214- use std:: path:: Path ;
215-
216- /// Headers for requests from 127.0.0.1 and local IP:
217- ///
218- /// In this request, br is missing from the "accept-encoding" header
219- ///
220- /// request headers: {"host": "192.168.68.107:1334", "connection": "keep-alive",
221- /// "user-agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36",
222- /// "accept": "*/*", "referer": "http://192.168.68.107:1334/",
223- /// "accept-encoding": "gzip, deflate", "accept-language": "en-US,en;q=0.9"}
224-
225- /// request headers: {"host": "127.0.0.1:1334", "connection": "keep-alive",
226- /// "sec-ch-ua": "\"Google Chrome\";v=\"107\", \"Chromium\";v=\"107\", \"Not=A?Brand\";v=\"24\"",
227- /// "sec-ch-ua-mobile": "?0",
228- /// "user-agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36",
229- /// "sec-ch-ua-platform": "\"Linux\"", "accept": "*/*", "sec-fetch-site": "same-origin",
230- /// "sec-fetch-mode": "cors", "sec-fetch-dest": "empty", "referer": "http://127.0.0.1:1334/",
231- /// "accept-encoding": "gzip, deflate, br", "accept-language": "en-US,en;q=0.9"}
232-
233- /// To run this test, it is necessary to first create `example-project.wasm` by these commands:
234- /// cd example-project
235- /// cargo build
236- #[ tokio:: test]
237- async fn test_router ( ) {
238- let options = Options {
215+
216+ const FAKE_BR_COMPRESSED_WASM : [ u8 ; 4 ] = [ 1 , 2 , 3 , 4 ] ;
217+ const FAKE_GZIP_COMPRESSED_WASM : [ u8 ; 4 ] = [ 0x1f , 0x8b , 0x08 , 0x08 ] ;
218+
219+ fn fake_options ( ) -> Options {
220+ Options {
239221 title : "title" . to_string ( ) ,
240222 address : "127.0.0.1:0" . to_string ( ) ,
241223 directory : "." . to_string ( ) ,
242224 https : false ,
243225 no_module : false ,
244- } ;
226+ }
227+ }
228+
229+ fn fake_wasm_bindgen_output ( ) -> WasmBindgenOutput {
230+ WasmBindgenOutput {
231+ js : "fake js" . to_string ( ) ,
232+ compressed_wasm : FAKE_BR_COMPRESSED_WASM . to_vec ( ) ,
233+ gzip_compressed_wasm : FAKE_GZIP_COMPRESSED_WASM . to_vec ( ) ,
234+ snippets : HashMap :: < String , Vec < String > > :: new ( ) ,
235+ local_modules : HashMap :: < String , String > :: new ( ) ,
236+ }
237+ }
245238
246- let wasm_file =
247- Path :: new ( "example-project/target/wasm32-unknown-unknown/debug/example-project.wasm" ) ;
248- let output = wasm_bindgen :: generate ( & options , wasm_file ) . unwrap ( ) ;
239+ fn make_test_client ( ) -> TestClient {
240+ let options = fake_options ( ) ;
241+ let output = fake_wasm_bindgen_output ( ) ;
249242 let router = get_router ( & options, output) ;
250- println ! ( "{:?}" , & router) ;
251- let client = TestClient :: new ( router) ;
243+ TestClient :: new ( router)
244+ }
245+
246+ #[ tokio:: test]
247+ async fn test_router_bad_request ( ) {
248+ let client = make_test_client ( ) ;
252249
253250 // Test without any supported compression
254251 let mut res =
255252 client. get ( "/api/wasm.wasm" ) . header ( "accept-encoding" , "deflate" ) . send ( ) . await ;
256253 assert_eq ! ( res. status( ) , StatusCode :: BAD_REQUEST ) ;
257254 let result = res. chunk ( ) . await . unwrap ( ) ;
258255 println ! ( "Bad Request result: {:?}" , result) ;
256+ }
259257
258+ #[ tokio:: test]
259+ async fn test_router_br ( ) {
260+ let client = make_test_client ( ) ;
260261 let mut res = client
261262 . get ( "/api/wasm.wasm" )
262263 . header ( "accept-encoding" , "gzip, deflate, br" )
263264 . send ( )
264265 . await ;
265266 assert_eq ! ( res. status( ) , StatusCode :: OK ) ;
266267 let result = res. chunk ( ) . await . unwrap ( ) ;
267- assert_ne ! ( result[ 0 ..3 ] , Bytes :: from( vec![ 0x1f , 0x8b , 0x08 ] ) ) ;
268+ assert_eq ! ( result. to_vec( ) , FAKE_BR_COMPRESSED_WASM ) ;
269+ }
268270
271+ #[ tokio:: test]
272+ async fn test_router_gzip ( ) {
273+ let client = make_test_client ( ) ;
269274 // Test without br compression, defaulting to gzip
270275 let mut res =
271276 client. get ( "/api/wasm.wasm" ) . header ( "accept-encoding" , "gzip, deflate" ) . send ( ) . await ;
272277 assert_eq ! ( res. status( ) , StatusCode :: OK ) ;
273278 let result = res. chunk ( ) . await . unwrap ( ) ;
274279 // This is the gzip 3-byte file header
275- assert_eq ! ( result[ 0 .. 3 ] , Bytes :: from ( vec! [ 0x1f , 0x8b , 0x08 ] ) ) ;
280+ assert_eq ! ( result. to_vec ( ) , FAKE_GZIP_COMPRESSED_WASM ) ;
276281 }
277282}
0 commit comments