-
Notifications
You must be signed in to change notification settings - Fork 1.2k
[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
base: main
Are you sure you want to change the base?
Conversation
b058ad4
to
0311d03
Compare
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.
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) { |
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.
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(), |
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.
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; |
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 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.
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"; |
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.
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.
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.
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?
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 don't see how to have both, but more lines with more descriptive names are to me better than the alternative.
8ccd273
to
aff31c8
Compare
This time I put the tests in a separate file to reduce the cognitive load for someone learning axum.
See #3263