use flow_abi_testing::catalog as common;
use common::*;
use flow_abi::{Limits, Mode, decode_bytes, encode_bytes, schema};
use flow_types::Type;
fn url() -> Type {
named(URL)
}
fn media() -> Type {
named(MEDIA_TYPE)
}
fn bytes_of(ty: &Type, value: &flow_value::Value) -> Vec<u8> {
encode_bytes(&forms_catalog(), ty, value).expect("encodes")
}
fn binary_read(ty: &Type, spelled: &str) -> Result<String, (String, String)> {
let bytes = bytes_of(&Type::Text, &text(spelled));
decode_bytes(&forms_catalog(), ty, &bytes, Mode::Reply, &Limits::LANGUAGE)
.map(|value| text_of(&value).to_owned())
.map_err(|error| (error.path.to_string(), error.problem))
}
#[test]
fn a_url_is_encoded_as_its_normal_text_exactly_as_its_base() {
let catalog = forms_catalog();
assert_eq!(
enc(&catalog, &url(), &text("http://example.com/")),
"\"http://example.com/\""
);
assert_eq!(
bytes_of(&url(), &text("http://example.com/")),
bytes_of(&Type::Text, &text("http://example.com/"))
);
}
#[test]
fn json_decoding_reads_any_spelling_of_a_url_as_the_one_url_it_denotes() {
let catalog = forms_catalog();
for (spelled, normal) in [
("HTTP://Example.COM", "http://example.com/"),
("http://h/a/../b?%7e", "http://h/b?~"),
("mailto:ada@example.com", "mailto:ada@example.com"),
] {
assert_eq!(
reread(&catalog, &url(), &format!("\"{spelled}\"")),
format!("\"{normal}\""),
"{spelled:?}"
);
assert_eq!(flow_abi::normal_url(spelled).as_deref(), Some(normal));
}
}
#[test]
fn a_string_that_is_not_a_url_does_not_decode_as_a_url() {
let catalog = forms_catalog();
for spelled in ["/relative", "http://a b/", "", "http://bücher.example/"] {
assert_eq!(
refusal(&catalog, &url(), &format!("\"{spelled}\"")),
(
String::new(),
format!("is \"{spelled}\", which is not a URL")
)
);
}
}
#[test]
fn the_binary_encoding_of_a_url_is_its_normal_text_and_nothing_else() {
assert_eq!(
binary_read(&url(), "http://example.com/"),
Ok("http://example.com/".to_owned())
);
assert_eq!(
binary_read(&url(), "HTTP://Example.COM"),
Err((
String::new(),
"is a URL written \"HTTP://Example.COM\", which is not its normal form \"http://example.com/\""
.to_owned()
))
);
assert_eq!(
binary_read(&url(), "/relative"),
Err((
String::new(),
"is \"/relative\", which is not a URL".to_owned()
))
);
}
#[test]
fn only_std_s_url_has_the_form_not_a_type_another_repository_names_url() {
let catalog = forms_catalog();
assert_eq!(
reread(&catalog, &named(OTHER_URL), "\"HTTP://A B\""),
"\"HTTP://A B\""
);
}
#[test]
fn a_url_s_schema_is_a_string_with_the_format_uri() {
let catalog = forms_catalog();
let written = schema(&catalog, &url()).expect("a schema").canonical();
assert!(
written.contains(r#"{"format":"uri","type":"string"}"#),
"{written}"
);
}
#[test]
fn json_decoding_reads_any_spelling_of_a_media_type_as_the_one_it_denotes() {
let catalog = forms_catalog();
assert_eq!(
reread(&catalog, &media(), r#""Text/HTML ; Charset=\"utf-8\"""#),
r#""text/html;charset=utf-8""#
);
assert_eq!(flow_abi::normal_media_type("A/B;;").as_deref(), Some("a/b"));
}
#[test]
fn a_string_that_is_not_a_media_type_does_not_decode_as_a_media_type() {
let catalog = forms_catalog();
assert_eq!(
refusal(&catalog, &media(), r#""text""#),
(
String::new(),
r#"is "text", which is not a media type"#.to_owned()
)
);
}
#[test]
fn the_binary_encoding_of_a_media_type_is_its_normal_text_and_nothing_else() {
assert_eq!(
binary_read(&media(), "text/html"),
Ok("text/html".to_owned())
);
assert_eq!(
binary_read(&media(), "Text/HTML"),
Err((
String::new(),
r#"is a media type written "Text/HTML", which is not its normal form "text/html""#
.to_owned()
))
);
}
#[test]
fn a_media_type_s_schema_is_its_base_s_with_no_format() {
let catalog = forms_catalog();
let written = schema(&catalog, &media()).expect("a schema").canonical();
assert!(written.contains(r#"{"type":"string"}"#), "{written}");
assert!(!written.contains("format"), "{written}");
}
#[test]
fn a_value_out_of_its_form_anywhere_in_a_reply_does_not_decode_and_names_its_place() {
let catalog = forms_catalog();
assert_eq!(
reread(
&catalog,
&named(LINK),
r#"{"href": "HTTP://A", "kind": "A/B"}"#
),
r#"{"href":"http://a/","kind":"a/b"}"#
);
assert_eq!(
refusal(
&catalog,
&named(LINK),
r#"{"href": "http://a/", "kind": "a"}"#
)
.0,
"kind"
);
let bytes = [
bytes_of(&Type::Text, &text("HTTP://A")),
bytes_of(&Type::Text, &text("a/b")),
]
.concat();
let refused = decode_bytes(
&catalog,
&named(LINK),
&bytes,
Mode::Reply,
&Limits::LANGUAGE,
)
.expect_err("a URL out of its form");
assert_eq!(refused.path, "href");
}
#[test]
fn a_value_out_of_its_form_is_not_encoded_in_either_form() {
let catalog = forms_catalog();
let link = rec(&catalog, LINK, vec![text("HTTP://A"), text("a/b")]);
let refused =
|error: flow_abi::EncodeError| (error.path.to_string(), error.kind, error.problem);
let out_of_form = (
"href".to_owned(),
flow_abi::EncodeErrorKind::Mismatch,
"is a URL written \"HTTP://A\", which is not its normal form \"http://a/\"".to_owned(),
);
assert_eq!(
encode_bytes(&catalog, &named(LINK), &link).map_err(refused),
Err(out_of_form.clone())
);
assert_eq!(
flow_abi::encode(&catalog, &named(LINK), &link).map_err(refused),
Err(out_of_form)
);
}