Skip to content

[test] the static-file-server example #3297

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

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

szabgab
Copy link
Contributor

@szabgab szabgab commented Mar 31, 2025

This time I put the tests in a separate file to reduce the cognitive load for someone learning axum.

See #3263

@szabgab szabgab force-pushed the test/static-file-server branch 2 times, most recently from b058ad4 to 0311d03 Compare March 31, 2025 13:28
Copy link
Collaborator

@mladedav mladedav left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since these are supposed to be long-term learning materials, I'm a bit stricter. But thanks for doing this.

I think that separating it like this is much better than when it was in the same file as the example itself.

const JS: &str = "text/javascript";
const HTML: &str = "text/html";

async fn get_page(app: Router, path: &str) -> (StatusCode, String, String) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use headers::ContentType instead of String.

And I'd argue returning Bytes or Vec<u8> for content is also better than forcing this to be String though I could see an argument for making sure that the returned content is valid UTF-8 before also checking that the contents match though I wouldn't subscribe to that. But feel free to ignore this.

let status = response.status();
let content_type = match response.headers().get("content-type") {
Some(content_type) => content_type.to_str().unwrap().to_owned(),
None => String::new(),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Empty header value is not the same thing as a missing header so I'd generally not recommend anyone to do this. returning Option would be more correct.

You even later in this file check for "" which probably is trying to check for the missing header.

@@ -0,0 +1,64 @@
use super::*;
use axum::http::StatusCode as SC;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is really just an opinion, but I don't like this rename. StatusCode is not that long of a name, it's a bit clearer to people what this is when they read the code without also reading imports and you even use both StatusCode and SC in this file. Which happens to work just because you also have use super::*.

This might be even more contentious opinion especially in tests, but I'd be in favor of not using * in imports, even here.

Comment on lines 7 to 11
const INDEX_HTML: &str = include_str!("../assets/index.html");
const SCRIPT_JS: &str = include_str!("../assets/script.js");

const JS: &str = "text/javascript";
const HTML: &str = "text/html";
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Personally, I'd prefer more descriptive names like INDEX_HTML_CONTENT and CONTENT_TYPE_JAVASCRIPT. It's much clearer what you're doing especially in calls like check(app(), "/assets/", SC::OK, HTML, INDEX_HTML).await;.

Also use ContentType as the type instead of &str for the latter two. I don't think it can be const then but static really is good enough.

Copy link
Contributor Author

@szabgab szabgab Mar 31, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the feedback.
Right, so I shortened all the names to let fmt put them (most of them) on one line, but I guess it is better to have longer names even if that means the tests are then in multiple lines.

Or maybe you have an idea how to arrange things to have both?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see how to have both, but more lines with more descriptive names are to me better than the alternative.

@szabgab szabgab force-pushed the test/static-file-server branch from 8ccd273 to aff31c8 Compare April 15, 2025 08:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants