-
Notifications
You must be signed in to change notification settings - Fork 38
Extract get_router and add simple test. #29
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
|
Hi @jakobhellermann and @daxpedda -- I'm starting with this simple refactoring PR with a simple test, but plan to later add on support for serving gzip compressed files based on the |
| let mut address_string = options.address; | ||
| if !address_string.contains(":") { | ||
| address_string += | ||
| &(":".to_owned() + &pick_port::pick_free_port(1334, 10).unwrap_or(1334).to_string()); | ||
| } | ||
| let addr: SocketAddr = address_string.parse().expect("Couldn't parse address"); | ||
|
|
||
| if options.https { | ||
| let certificate = rcgen::generate_simple_self_signed([String::from("localhost")])?; | ||
| let config = RustlsConfig::from_der( | ||
| vec![certificate.serialize_der()?], | ||
| certificate.serialize_private_key_der(), | ||
| ) | ||
| .await?; | ||
|
|
||
| tracing::info!("starting webserver at https://{}", addr); | ||
| axum_server_dual_protocol::bind_dual_protocol(addr, config) | ||
| .set_upgrade(true) | ||
| .serve(app.into_make_service()) | ||
| .await?; | ||
| } else { | ||
| tracing::info!("starting webserver at http://{}", addr); | ||
| axum_server::bind(addr).serve(app.into_make_service()).await?; | ||
| } | ||
|
|
||
| Ok(()) | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This code is unchanged as compared to lines 82-107 of the original file.
daxpedda
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just to clarify, I'm not a maintainer of this repository, so no merge rights, but happy to be pinged to review anyway, thanks!
You should probably not ping jakobhellermann, owners of a repo get notified anyway.
src/server.rs
Outdated
|
|
||
| /// Headers for requests from 127.0.0.1 and local IP: | ||
| /// | ||
| /// In this request, br is missing from the "accept-encoding" header |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is confusing, the test below does also have br in it's accept-encoding header, as far as I can see.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've deleted this whole comment, particular since this is largely captured in #28. For this PR, I'm leaving the br in the accept-encoding in the test just to verify existing behavior and to have the testing scaffolding in place.
src/server.rs
Outdated
| /// "sec-ch-ua-platform": "\"Linux\"", "accept": "*/*", "sec-fetch-site": "same-origin", | ||
| /// "sec-fetch-mode": "cors", "sec-fetch-dest": "empty", "referer": "http://127.0.0.1:1334/", | ||
| /// "accept-encoding": "gzip, deflate, br", "accept-language": "en-US,en;q=0.9"} | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is probably not useful as a comment.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removed
src/server.rs
Outdated
| no_module: false, | ||
| }; | ||
|
|
||
| let wasm_file = |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not exactly ideal. I think generally the issue is that wasm-server-runner has no CI, so I'm not entirely sure if adding tests that are not really straightforward to run makes sense.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Very valid point! I've done some rework to this test so that it no longer calls generate and instead just has a dummy br compressed file with everything else in WasmBindgenOutput either empty or with dummy values.
src/server.rs
Outdated
| Path::new("example-project/target/wasm32-unknown-unknown/debug/example-project.wasm"); | ||
| let output = wasm_bindgen::generate(&options, wasm_file).unwrap(); | ||
| let router = get_router(&options, output); | ||
| println!("{:?}", &router); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Again I'm assuming debugging leftover.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch! Removed
src/server.rs
Outdated
| .await; | ||
| assert_eq!(res.status(), StatusCode::OK); | ||
| let result = res.chunk().await.unwrap(); | ||
| assert_ne!(result[0..3], Bytes::from(vec![0x1f, 0x8b, 0x08])); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You could probably just load the file that you have the path to already and just compare the whole thing.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good idea! I went ahead and just created a short dummy "compressed" file as a Vec and compared against that. Since I'm not really testing the compression itself in this test, I omitted that logic here.
src/server.rs
Outdated
| // Test without br compression | ||
| // let mut res = client | ||
| // .get("/api/wasm.wasm") | ||
| // .header("accept-encoding", "gzip, deflate") | ||
| // .send() | ||
| // .await; | ||
| // assert_eq!(res.status(), StatusCode::OK); | ||
| // let result = res.chunk().await.unwrap(); | ||
| // // This is the gzip 3-byte file header | ||
| // assert_eq!(result[0..3], Bytes::from(vec![0x1f, 0x8b, 0x08])); | ||
|
|
||
| //tokio_test::block_on(server).unwrap(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is probably not useful to leave here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removed
|
Thank you @daxpedda for all your constructive comments! I should have some time after U.S. Thanksgiving to take another pass. |
heisencoder
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you @daxpedda for your comments! I've uploaded changes based on these comments as a new commit for easier tracking, although I'm happy to do a squash rebase if that's easier for this repo (in any case, I'm assuming that this would get squash merged).
src/server.rs
Outdated
|
|
||
| /// Headers for requests from 127.0.0.1 and local IP: | ||
| /// | ||
| /// In this request, br is missing from the "accept-encoding" header |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've deleted this whole comment, particular since this is largely captured in #28. For this PR, I'm leaving the br in the accept-encoding in the test just to verify existing behavior and to have the testing scaffolding in place.
src/server.rs
Outdated
| /// "sec-ch-ua-platform": "\"Linux\"", "accept": "*/*", "sec-fetch-site": "same-origin", | ||
| /// "sec-fetch-mode": "cors", "sec-fetch-dest": "empty", "referer": "http://127.0.0.1:1334/", | ||
| /// "accept-encoding": "gzip, deflate, br", "accept-language": "en-US,en;q=0.9"} | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removed
src/server.rs
Outdated
| no_module: false, | ||
| }; | ||
|
|
||
| let wasm_file = |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Very valid point! I've done some rework to this test so that it no longer calls generate and instead just has a dummy br compressed file with everything else in WasmBindgenOutput either empty or with dummy values.
src/server.rs
Outdated
| Path::new("example-project/target/wasm32-unknown-unknown/debug/example-project.wasm"); | ||
| let output = wasm_bindgen::generate(&options, wasm_file).unwrap(); | ||
| let router = get_router(&options, output); | ||
| println!("{:?}", &router); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch! Removed
src/server.rs
Outdated
| .await; | ||
| assert_eq!(res.status(), StatusCode::OK); | ||
| let result = res.chunk().await.unwrap(); | ||
| assert_ne!(result[0..3], Bytes::from(vec![0x1f, 0x8b, 0x08])); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good idea! I went ahead and just created a short dummy "compressed" file as a Vec and compared against that. Since I'm not really testing the compression itself in this test, I omitted that logic here.
src/server.rs
Outdated
| // Test without br compression | ||
| // let mut res = client | ||
| // .get("/api/wasm.wasm") | ||
| // .header("accept-encoding", "gzip, deflate") | ||
| // .send() | ||
| // .await; | ||
| // assert_eq!(res.status(), StatusCode::OK); | ||
| // let result = res.chunk().await.unwrap(); | ||
| // // This is the gzip 3-byte file header | ||
| // assert_eq!(result[0..3], Bytes::from(vec![0x1f, 0x8b, 0x08])); | ||
|
|
||
| //tokio_test::block_on(server).unwrap(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removed
daxpedda
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Otherwise LGTM.
| fn fake_wasm_bindgen_output() -> WasmBindgenOutput { | ||
| WasmBindgenOutput { | ||
| js: "fake js".to_string(), | ||
| compressed_wasm: FAKE_BR_COMPRESSED_WASM.to_vec(), | ||
| snippets: HashMap::<String, Vec<String>>::new(), | ||
| local_modules: HashMap::<String, String>::new(), | ||
| } | ||
| } | ||
|
|
||
| fn make_test_client() -> TestClient { | ||
| let options = fake_options(); | ||
| let output = fake_wasm_bindgen_output(); | ||
| let router = get_router(&options, output); | ||
| println!("{:?}", &router); | ||
| let client = TestClient::new(router); | ||
| TestClient::new(router) | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think for this test to be useful, we should probably make a fake WASM file and then compress that. At the moment we are providing a fake already compressed WASM file, so we aren't properly testing the code.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I gave this some thought and decided that for this particular case, a more targeted unit test was more appropriate, particularly since the primary goal is to make sure the router is able to choose the correct file. With regard to testing the compression, I was thinking that that could be addressed by another unit test in wasm_bindgen (although logically the compression could probably be extracted into a separate module).
Heavier-weight testing, I think, would be better placed into a separate PR, ideally with corresponding test automation.
This PR extracts the logic from `run_server` that creates a new Router into a helper function named `get_router` to make it easier to test without needing to start the full server. This also adds a simple test that verifies current behavior for returning a br compressed wasm.wasm file. This change is in preparation for adding support dynamically change the available compression.
This PR extracts the logic from
run_serverthat creates a new Router into a helper function namedget_routerto make it easier to test without needing to start the full server.This also adds a simple test that verifies current behavior for returning a br compressed wasm.wasm file.
This change is in preparation for adding support dynamically change the available compression.
See issue #28 for context.