[wip] unit tests for sharry module
- doc + testing for `uri` module
This commit is contained in:
parent
abf76c9df7
commit
f4e0bc5be4
2 changed files with 83 additions and 22 deletions
|
|
@ -30,7 +30,7 @@ impl From<String> for AliasID {
|
||||||
/// - impl `serde` for cachefile handling
|
/// - impl `serde` for cachefile handling
|
||||||
/// - impl `Display` for formatting compatibility
|
/// - impl `Display` for formatting compatibility
|
||||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||||
pub struct ShareID(String);
|
pub struct ShareID(pub(super) String);
|
||||||
|
|
||||||
impl fmt::Display for ShareID {
|
impl fmt::Display for ShareID {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
|
|
@ -51,7 +51,7 @@ impl From<String> for ShareID {
|
||||||
/// - impl `Display` for formatting compatibility
|
/// - impl `Display` for formatting compatibility
|
||||||
/// - impl `TryFrom<String>` for extracting from matching a "PATCH" uri
|
/// - impl `TryFrom<String>` for extracting from matching a "PATCH" uri
|
||||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||||
pub struct FileID(String);
|
pub struct FileID(pub(super) String);
|
||||||
|
|
||||||
impl fmt::Display for FileID {
|
impl fmt::Display for FileID {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
|
|
@ -102,7 +102,7 @@ impl TryFrom<String> for FileID {
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
use crate::check_trait;
|
use crate::check_trait;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn basic_traits_working() {
|
fn basic_traits_working() {
|
||||||
|
|
|
||||||
|
|
@ -25,28 +25,34 @@ impl AsRef<[u8]> for Uri {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Uri {
|
impl Uri {
|
||||||
pub fn new(protocol: impl fmt::Display, base_url: impl fmt::Display) -> Self {
|
/// create a new Sharry URI
|
||||||
Self(format!("{protocol}://{base_url}"))
|
pub fn new(scheme: impl fmt::Display, host: impl fmt::Display) -> Self {
|
||||||
|
Self(format!("{scheme}://{host}"))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// arbitrary endpoint in the Sharry API v2
|
||||||
fn endpoint(&self, path: fmt::Arguments) -> String {
|
fn endpoint(&self, path: fmt::Arguments) -> String {
|
||||||
let uri = format!("{}/api/v2/{path}", self.0);
|
let uri = format!("{}/api/v2/{path}", self.0);
|
||||||
trace!("endpoint: {uri:?}");
|
trace!("endpoint: {uri:?}");
|
||||||
uri
|
uri
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Sharry API endpoint to create a new share
|
||||||
pub fn share_create(&self) -> String {
|
pub fn share_create(&self) -> String {
|
||||||
self.endpoint(format_args!("alias/upload/new"))
|
self.endpoint(format_args!("alias/upload/new"))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Sharry API endpoint to ping a share's notification hook
|
||||||
pub fn share_notify(&self, share_id: &super::ShareID) -> String {
|
pub fn share_notify(&self, share_id: &super::ShareID) -> String {
|
||||||
self.endpoint(format_args!("alias/mail/notify/{share_id}"))
|
self.endpoint(format_args!("alias/mail/notify/{share_id}"))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Sharry API endpoint to create a new file inside a share
|
||||||
pub fn file_create(&self, share_id: &super::ShareID) -> String {
|
pub fn file_create(&self, share_id: &super::ShareID) -> String {
|
||||||
self.endpoint(format_args!("alias/upload/{share_id}/files/tus"))
|
self.endpoint(format_args!("alias/upload/{share_id}/files/tus"))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Sharry API endpoint to push data into a file inside a share
|
||||||
pub fn file_patch(&self, share_id: &super::ShareID, file_id: &super::FileID) -> String {
|
pub fn file_patch(&self, share_id: &super::ShareID, file_id: &super::FileID) -> String {
|
||||||
self.endpoint(format_args!("alias/upload/{share_id}/files/tus/{file_id}"))
|
self.endpoint(format_args!("alias/upload/{share_id}/files/tus/{file_id}"))
|
||||||
}
|
}
|
||||||
|
|
@ -55,31 +61,86 @@ impl Uri {
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
use crate::{
|
||||||
|
check_trait,
|
||||||
|
sharry::{FileID, ShareID},
|
||||||
|
};
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn basic_tests() {
|
fn basic_traits_working() {
|
||||||
let cases = vec![
|
let cases = vec![
|
||||||
|
// simple http host
|
||||||
|
"http://example.com",
|
||||||
|
// https host with port
|
||||||
|
"https://my-host:8080",
|
||||||
|
// custom scheme
|
||||||
|
"custom+scheme://host",
|
||||||
|
];
|
||||||
|
|
||||||
|
for uri_data in cases {
|
||||||
|
let uri = Uri(uri_data.to_owned());
|
||||||
|
check_trait(&uri_data, &uri.to_string(), "Display", "Uri");
|
||||||
|
check_trait(&uri_data.as_bytes(), &uri.as_ref(), "AsRef<[u8]>", "Uri");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_new() {
|
||||||
|
let cases = vec![
|
||||||
|
// simple http host
|
||||||
("http", "example.com", "http://example.com"),
|
("http", "example.com", "http://example.com"),
|
||||||
|
// https host with port
|
||||||
("https", "my-host:8080", "https://my-host:8080"),
|
("https", "my-host:8080", "https://my-host:8080"),
|
||||||
|
// custom scheme
|
||||||
("custom+scheme", "host", "custom+scheme://host"),
|
("custom+scheme", "host", "custom+scheme://host"),
|
||||||
];
|
];
|
||||||
|
|
||||||
for (protocol, base_url, display) in cases {
|
for (scheme, host, expected) in cases {
|
||||||
let uri = Uri::new(protocol, base_url);
|
let uri = Uri::new(scheme, host);
|
||||||
assert_eq!(
|
assert_eq!(&expected, &uri.to_string());
|
||||||
uri.to_string(),
|
|
||||||
display,
|
|
||||||
"`impl Display for Uri` expected: {:?}, got {:?}",
|
|
||||||
display,
|
|
||||||
uri.to_string(),
|
|
||||||
);
|
|
||||||
assert_eq!(
|
|
||||||
uri.as_ref(),
|
|
||||||
display.as_bytes(),
|
|
||||||
"`impl AsRef<[u8]> for Uri` expected: {:?}, got {:?}",
|
|
||||||
display.as_bytes(),
|
|
||||||
uri.as_ref(),
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_endpoint() {
|
||||||
|
let cases = vec![
|
||||||
|
// simple path
|
||||||
|
("path/to/something", "/api/v2/path/to/something"),
|
||||||
|
// underscores, hyphens, dots
|
||||||
|
("bob_smith-son.eve", "/api/v2/bob_smith-son.eve"),
|
||||||
|
// unicode
|
||||||
|
("漢字ユーザー", "/api/v2/漢字ユーザー"),
|
||||||
|
// empty path
|
||||||
|
("", "/api/v2/"),
|
||||||
|
// leading/trailing spaces
|
||||||
|
(" frank ", "/api/v2/ frank "),
|
||||||
|
// uppercase
|
||||||
|
("GUEST", "/api/v2/GUEST"),
|
||||||
|
// numeric
|
||||||
|
("12345", "/api/v2/12345"),
|
||||||
|
];
|
||||||
|
|
||||||
|
let uri = Uri("".to_owned());
|
||||||
|
for (path, expected) in cases {
|
||||||
|
assert_eq!(&expected, &uri.endpoint(format_args!("{path}")));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_pub_endpoints() {
|
||||||
|
let uri = Uri("".to_owned());
|
||||||
|
let share_id = ShareID("sid".to_owned());
|
||||||
|
let file_id = FileID("fid".to_owned());
|
||||||
|
|
||||||
|
assert_eq!("/api/v2/alias/upload/new", uri.share_create());
|
||||||
|
assert_eq!("/api/v2/alias/mail/notify/sid", uri.share_notify(&share_id));
|
||||||
|
assert_eq!(
|
||||||
|
"/api/v2/alias/upload/sid/files/tus",
|
||||||
|
uri.file_create(&share_id)
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
"/api/v2/alias/upload/sid/files/tus/fid",
|
||||||
|
uri.file_patch(&share_id, &file_id)
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue