about summary refs log tree commit diff stats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/err.rs16
-rw-r--r--src/response.rs91
2 files changed, 104 insertions, 3 deletions
diff --git a/src/err.rs b/src/err.rs
index 6c178bd..8b4dffc 100644
--- a/src/err.rs
+++ b/src/err.rs
@@ -49,3 +49,19 @@ impl std::fmt::Display for Supernova {
         write!(f, "{}", &self.message)
     }
 }
+
+#[cfg(test)]
+mod tests {
+    use super::*;
+
+    #[test]
+    fn test_supernova() {
+        let mut sn = Supernova::boom("test");
+        assert_eq!(sn.code(), response::Code::Unknown);
+
+        sn.with_code(response::Code::BadRequest);
+        assert_eq!(sn.code(), response::Code::BadRequest);
+
+        assert_eq!(format!("{}", sn), String::from("test"));
+    }
+}
diff --git a/src/response.rs b/src/response.rs
index f11723b..3356998 100644
--- a/src/response.rs
+++ b/src/response.rs
@@ -22,7 +22,7 @@ pub const GEMINI_MIME: &str = "text/gemini";
 #[allow(dead_code)]
 #[derive(Clone, Copy, Debug, PartialEq)]
 pub enum Code {
-    Unknown = 00,
+    Unknown = 99,
     Input = 10,
     SensitiveInput = 11,
     Success = 20,
@@ -44,9 +44,9 @@ pub enum Code {
 }
 
 impl Code {
-    pub fn get_header(&self, mime: &str) -> Vec<u8> {
+    pub fn get_header(&self, meta: &str) -> Vec<u8> {
         let msg = if *self == Code::Success {
-            format!("{} {}\r\n", self, mime)
+            format!("{} {}\r\n", self, meta)
         } else {
             format!("{}\r\n", self)
         };
@@ -88,3 +88,88 @@ impl fmt::Display for Code {
 pub fn footer_bytes<'a>() -> &'a [u8] {
     "\n\n~~~~ served by laika ~~~~~~~~~\nhttps://sr.ht/~gbmor/laika\n\n".as_bytes()
 }
+
+#[cfg(test)]
+mod tests {
+    use super::*;
+
+    #[test]
+    fn header_check() {
+        // we don't care about the metadata, just the code and the line ending
+        let unknown = Code::Unknown.get_header("");
+        assert!(unknown.ends_with("\r\n".as_bytes()));
+        assert_eq!(&unknown[0..2], "99".as_bytes());
+
+        let input = Code::Input.get_header("");
+        assert!(input.ends_with("\r\n".as_bytes()));
+        assert_eq!(&input[0..2], "10".as_bytes());
+
+        let sensitive_input = Code::SensitiveInput.get_header("");
+        assert!(sensitive_input.ends_with("\r\n".as_bytes()));
+        assert_eq!(&sensitive_input[0..2], "11".as_bytes());
+
+        let success = Code::Success.get_header("");
+        assert!(success.ends_with("\r\n".as_bytes()));
+        assert_eq!(&success[0..2], "20".as_bytes());
+
+        let rdr_temp = Code::RedirectTemporary.get_header("");
+        assert!(rdr_temp.ends_with("\r\n".as_bytes()));
+        assert_eq!(&rdr_temp[0..2], "30".as_bytes());
+
+        let rdr_perm = Code::RedirectPermanent.get_header("");
+        assert!(rdr_perm.ends_with("\r\n".as_bytes()));
+        assert_eq!(&rdr_perm[0..2], "31".as_bytes());
+
+        let temp_fail = Code::TemporaryFailure.get_header("");
+        assert!(temp_fail.ends_with("\r\n".as_bytes()));
+        assert_eq!(&temp_fail[0..2], "40".as_bytes());
+
+        let server_unavailable = Code::ServerUnavailable.get_header("");
+        assert!(server_unavailable.ends_with("\r\n".as_bytes()));
+        assert_eq!(&server_unavailable[0..2], "41".as_bytes());
+
+        let cgi_err = Code::CgiError.get_header("");
+        assert!(cgi_err.ends_with("\r\n".as_bytes()));
+        assert_eq!(&cgi_err[0..2], "42".as_bytes());
+
+        let proxy_err = Code::ProxyError.get_header("");
+        assert!(proxy_err.ends_with("\r\n".as_bytes()));
+        assert_eq!(&proxy_err[0..2], "43".as_bytes());
+
+        let slow_down = Code::SlowDown.get_header("");
+        assert!(slow_down.ends_with("\r\n".as_bytes()));
+        assert_eq!(&slow_down[0..2], "44".as_bytes());
+
+        let perm_fail = Code::PermanentFailure.get_header("");
+        assert!(perm_fail.ends_with("\r\n".as_bytes()));
+        assert_eq!(&perm_fail[0..2], "50".as_bytes());
+
+        let not_found = Code::NotFound.get_header("");
+        assert!(not_found.ends_with("\r\n".as_bytes()));
+        assert_eq!(&not_found[0..2], "51".as_bytes());
+
+        let gone = Code::Gone.get_header("");
+        assert!(gone.ends_with("\r\n".as_bytes()));
+        assert_eq!(&gone[0..2], "52".as_bytes());
+
+        let proxy_req_refused = Code::ProxyRequestRefused.get_header("");
+        assert!(proxy_req_refused.ends_with("\r\n".as_bytes()));
+        assert_eq!(&proxy_req_refused[0..2], "53".as_bytes());
+
+        let bad_req = Code::BadRequest.get_header("");
+        assert!(bad_req.ends_with("\r\n".as_bytes()));
+        assert_eq!(&bad_req[0..2], "59".as_bytes());
+
+        let client_cert_required = Code::ClientCertificateRequired.get_header("");
+        assert!(client_cert_required.ends_with("\r\n".as_bytes()));
+        assert_eq!(&client_cert_required[0..2], "60".as_bytes());
+
+        let cert_not_auth = Code::CertificateNotAuthorised.get_header("");
+        assert!(cert_not_auth.ends_with("\r\n".as_bytes()));
+        assert_eq!(&cert_not_auth[0..2], "61".as_bytes());
+
+        let cert_not_valid = Code::CertificateNotValid.get_header("");
+        assert!(cert_not_valid.ends_with("\r\n".as_bytes()));
+        assert_eq!(&cert_not_valid[0..2], "62".as_bytes());
+    }
+}