Skip to content

Rewrite your app in Rust, piece by piece, checked against the original at every step. Works with TypeScript, JavaScript, Python, Go, Ruby, Java, C, PHP, C#, Kotlin, PowerShell and Swift.

test: cover infer_content_type and status_reason (#1384)

jakalijey9de09dfabbf8
diff --git a/src/effects.rs b/src/effects.rs
index 4058a172..751ef3f5 100644
--- a/src/effects.rs
+++ b/src/effects.rs
@@ -1856,4 +1856,71 @@ mod merge_tests {
assert!(!glob_match("/api/*/b", "/api/x/c"));
assert!(!glob_match("/a", ""));
}
+
+ #[test]
+ fn infer_content_type_reads_the_extension_then_the_body() {
+ const JSON: &str = "application/json";
+ const TEXT: &str = "text/plain; charset=utf-8";
+ // The extension wins over the body.
+ assert_eq!(infer_content_type(Some("json"), "ok"), JSON);
+ assert_eq!(infer_content_type(Some("xml"), "{}"), "application/xml");
+ assert_eq!(
+ infer_content_type(Some("html"), "{}"),
+ "text/html; charset=utf-8"
+ );
+ assert_eq!(
+ infer_content_type(Some("htm"), "[]"),
+ "text/html; charset=utf-8"
+ );
+ assert_eq!(infer_content_type(Some("txt"), "{\"a\": 1}"), TEXT);
+ // No extension, or one the table does not name: the body decides.
+ assert_eq!(infer_content_type(None, "{\"a\": 1}"), JSON);
+ assert_eq!(infer_content_type(None, " \n\t[1, 2]"), JSON);
+ assert_eq!(infer_content_type(None, "ok"), TEXT);
+ assert_eq!(infer_content_type(None, ""), TEXT);
+ assert_eq!(infer_content_type(Some("csv"), "[\"a\", \"b\"]"), JSON);
+ assert_eq!(infer_content_type(Some("csv"), "a,b"), TEXT);
+ // The helper takes the extension in lower case; `parse` lowers it first (below).
+ assert_eq!(infer_content_type(Some("JSON"), "ok"), TEXT);
+ }
+
+ #[test]
+ fn an_upper_case_extension_is_read_like_a_lower_case_one() {
+ let repo = std::env::temp_dir().join(format!("ra-effects-ext-{}", std::process::id()));
+ std::fs::create_dir_all(&repo).unwrap();
+ std::fs::write(repo.join("DATA.JSON"), "ok").unwrap();
+ std::fs::write(repo.join("PAGE.HTM"), "{}").unwrap();
+ // Not UTF-8, so it goes through the binary branch and its own table.
+ std::fs::write(repo.join("FEED.XML"), [0xff, 0xfe, b'<']).unwrap();
+ std::fs::write(repo.join("BLOB.BIN"), [0xff]).unwrap();
+ let content_type = |spec: &str| {
+ HttpResponseRule::parse(spec, &repo)
+ .unwrap()
+ .content_type
+ .unwrap()
+ };
+ assert_eq!(content_type("GET /a=200 @DATA.JSON"), "application/json");
+ assert_eq!(
+ content_type("GET /a=200 @PAGE.HTM"),
+ "text/html; charset=utf-8"
+ );
+ assert_eq!(content_type("GET /a=200 @FEED.XML"), "application/xml");
+ assert_eq!(
+ content_type("GET /a=200 @BLOB.BIN"),
+ "application/octet-stream"
+ );
+ std::fs::remove_dir_all(&repo).ok();
+ }
+
+ #[test]
+ fn status_reason_names_common_statuses_and_falls_back() {
+ assert_eq!(status_reason(200), "OK");
+ assert_eq!(status_reason(204), "No Content");
+ assert_eq!(status_reason(404), "Not Found");
+ assert_eq!(status_reason(429), "Too Many Requests");
+ assert_eq!(status_reason(503), "Service Unavailable");
+ // A status the table does not name reads `HTTP/1.1 418 Status`; change this on purpose.
+ assert_eq!(status_reason(418), "Status");
+ assert_eq!(status_reason(207), "Status");
+ }
}