@@ -85,8 +85,7 @@ fn get_router(options: &Options, output: WasmBindgenOutput) -> Router {
8585 let serve_dir =
8686 get_service ( ServeDir :: new ( options. directory . clone ( ) ) ) . handle_error ( internal_server_error) ;
8787
88- let serve_wasm = |headers : HeaderMap | async move {
89- println ! ( " request headers: {:?}" , headers) ;
88+ let serve_wasm = || async move {
9089 ( [ ( "content-encoding" , "br" ) ] , WithContentType ( "application/wasm" , compressed_wasm) )
9190 } ;
9291
@@ -172,50 +171,45 @@ mod pick_port {
172171
173172#[ cfg( test) ]
174173mod tests {
174+ use std:: collections:: HashMap ;
175+
175176 use crate :: server:: get_router;
176- use crate :: wasm_bindgen;
177+ use crate :: wasm_bindgen:: WasmBindgenOutput ;
177178 use crate :: Options ;
178- use axum:: body:: Bytes ;
179179 use axum:: http:: StatusCode ;
180180 use axum_test_helper:: TestClient ;
181- use std:: path:: Path ;
182-
183- /// Headers for requests from 127.0.0.1 and local IP:
184- ///
185- /// In this request, br is missing from the "accept-encoding" header
186- ///
187- /// request headers: {"host": "192.168.68.107:1334", "connection": "keep-alive",
188- /// "user-agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36",
189- /// "accept": "*/*", "referer": "http://192.168.68.107:1334/",
190- /// "accept-encoding": "gzip, deflate", "accept-language": "en-US,en;q=0.9"}
191-
192- /// request headers: {"host": "127.0.0.1:1334", "connection": "keep-alive",
193- /// "sec-ch-ua": "\"Google Chrome\";v=\"107\", \"Chromium\";v=\"107\", \"Not=A?Brand\";v=\"24\"",
194- /// "sec-ch-ua-mobile": "?0",
195- /// "user-agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36",
196- /// "sec-ch-ua-platform": "\"Linux\"", "accept": "*/*", "sec-fetch-site": "same-origin",
197- /// "sec-fetch-mode": "cors", "sec-fetch-dest": "empty", "referer": "http://127.0.0.1:1334/",
198- /// "accept-encoding": "gzip, deflate, br", "accept-language": "en-US,en;q=0.9"}
199-
200- /// To run this test, it is necessary to first create `example-project.wasm` by these commands:
201- /// cd example-project
202- /// cargo build
203- #[ tokio:: test]
204- async fn test_router ( ) {
205- let options = Options {
181+
182+ const FAKE_BR_COMPRESSED_WASM : [ u8 ; 4 ] = [ 1 , 2 , 3 , 4 ] ;
183+
184+ fn fake_options ( ) -> Options {
185+ Options {
206186 title : "title" . to_string ( ) ,
207187 address : "127.0.0.1:0" . to_string ( ) ,
208188 directory : "." . to_string ( ) ,
209189 https : false ,
210190 no_module : false ,
211- } ;
191+ }
192+ }
212193
213- let wasm_file =
214- Path :: new ( "example-project/target/wasm32-unknown-unknown/debug/example-project.wasm" ) ;
215- let output = wasm_bindgen:: generate ( & options, wasm_file) . unwrap ( ) ;
194+ fn fake_wasm_bindgen_output ( ) -> WasmBindgenOutput {
195+ WasmBindgenOutput {
196+ js : "fake js" . to_string ( ) ,
197+ compressed_wasm : FAKE_BR_COMPRESSED_WASM . to_vec ( ) ,
198+ snippets : HashMap :: < String , Vec < String > > :: new ( ) ,
199+ local_modules : HashMap :: < String , String > :: new ( ) ,
200+ }
201+ }
202+
203+ fn make_test_client ( ) -> TestClient {
204+ let options = fake_options ( ) ;
205+ let output = fake_wasm_bindgen_output ( ) ;
216206 let router = get_router ( & options, output) ;
217- println ! ( "{:?}" , & router) ;
218- let client = TestClient :: new ( router) ;
207+ TestClient :: new ( router)
208+ }
209+
210+ #[ tokio:: test]
211+ async fn test_router ( ) {
212+ let client = make_test_client ( ) ;
219213
220214 // Test with br compression requested
221215 let mut res = client
@@ -225,19 +219,6 @@ mod tests {
225219 . await ;
226220 assert_eq ! ( res. status( ) , StatusCode :: OK ) ;
227221 let result = res. chunk ( ) . await . unwrap ( ) ;
228- assert_ne ! ( result[ 0 ..3 ] , Bytes :: from( vec![ 0x1f , 0x8b , 0x08 ] ) ) ;
229-
230- // Test without br compression
231- // let mut res = client
232- // .get("/api/wasm.wasm")
233- // .header("accept-encoding", "gzip, deflate")
234- // .send()
235- // .await;
236- // assert_eq!(res.status(), StatusCode::OK);
237- // let result = res.chunk().await.unwrap();
238- // // This is the gzip 3-byte file header
239- // assert_eq!(result[0..3], Bytes::from(vec![0x1f, 0x8b, 0x08]));
240-
241- //tokio_test::block_on(server).unwrap();
222+ assert_eq ! ( result. to_vec( ) , FAKE_BR_COMPRESSED_WASM ) ;
242223 }
243224}
0 commit comments