fix(https): align canonical binding semantics
This commit is contained in:
@@ -4,9 +4,10 @@ use wip_https::wip_protocol::{
|
||||
InterfaceDescriptor, Object, ProtocolError, ProtocolErrorCode,
|
||||
};
|
||||
use wip_https::{
|
||||
BatchRequest, BatchRequestItem, BatchRequestKind, BatchResponse, BatchResponseItem,
|
||||
BatchSuccess, ClientResponseError, DecodedResponse, Endpoint, InvalidResponseKind, Limits,
|
||||
decode_batch_request, decode_batch_response, encode_batch_error_response, encode_batch_request,
|
||||
BatchMethod, BatchRequest, BatchRequestItem, BatchRequestKind, BatchResponse,
|
||||
BatchResponseItem, BatchSuccess, ClientResponseError, DecodedResponse, Endpoint,
|
||||
InvalidResponseKind, Limits, ResourceLimitKind, decode_batch_request, decode_batch_response,
|
||||
encode_batch_error_response, encode_batch_request, encode_batch_resource_limit_error_response,
|
||||
encode_batch_response,
|
||||
};
|
||||
|
||||
@@ -39,6 +40,7 @@ fn batch_round_trips_by_unique_id_with_item_failures_and_validators() {
|
||||
let endpoint = Endpoint::parse("https://example.test/wip").unwrap();
|
||||
let encoded_request = encode_batch_request(&endpoint, &request, limits()).unwrap();
|
||||
assert_eq!(encoded_request.uri().path(), "/wip/v1/batch");
|
||||
assert_eq!(encoded_request.headers()["accept"], "application/json");
|
||||
assert_eq!(
|
||||
decode_batch_request(encoded_request.body(), limits()).unwrap(),
|
||||
request
|
||||
@@ -115,8 +117,65 @@ fn batch_interface_validator_round_trips_in_item_body() {
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn malformed_known_method_arguments_are_item_local() {
|
||||
let body = br#"{
|
||||
"requests": [
|
||||
{"id":"valid","method":"fetch","arguments":{"path":"/item"}},
|
||||
{"id":"invalid","method":"fetch_tree","arguments":{"path":"/","depth":"deep","limit":1}}
|
||||
]
|
||||
}"#;
|
||||
let decoded = decode_batch_request(body, limits()).unwrap();
|
||||
assert!(matches!(
|
||||
decoded.requests[0].request,
|
||||
BatchRequestKind::Fetch(_)
|
||||
));
|
||||
let invalid = match &decoded.requests[1].request {
|
||||
BatchRequestKind::Invalid { method, error } => {
|
||||
assert_eq!(*method, BatchMethod::FetchTree);
|
||||
assert_eq!(error.code, ProtocolErrorCode::InvalidRequest);
|
||||
error.clone()
|
||||
}
|
||||
other => panic!("expected item-local invalid request, got {other:?}"),
|
||||
};
|
||||
|
||||
let response = BatchResponse {
|
||||
responses: vec![
|
||||
BatchResponseItem {
|
||||
id: "valid".into(),
|
||||
outcome: Ok(BatchSuccess::Fetch(Object {
|
||||
name: "item".into(),
|
||||
description: None,
|
||||
interfaces: vec![],
|
||||
r#ref: None,
|
||||
validator: None,
|
||||
})),
|
||||
},
|
||||
BatchResponseItem {
|
||||
id: "invalid".into(),
|
||||
outcome: Err(invalid),
|
||||
},
|
||||
],
|
||||
};
|
||||
let encoded = encode_batch_response(&decoded, &response, limits()).unwrap();
|
||||
assert_eq!(encoded.status(), StatusCode::OK);
|
||||
assert_eq!(
|
||||
decode_batch_response(&decoded, &encoded, limits()).unwrap(),
|
||||
DecodedResponse::Success(response)
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn invalid_batch_envelopes_are_rejected_at_top_level() {
|
||||
assert!(decode_batch_request(br#"{}"#, limits()).is_err());
|
||||
|
||||
let malformed_item = br#"{
|
||||
"requests": [
|
||||
{"id":"item","method":"fetch","arguments":{"path":"/"},"extra":true}
|
||||
]
|
||||
}"#;
|
||||
assert!(decode_batch_request(malformed_item, limits()).is_err());
|
||||
|
||||
let duplicate = br#"{
|
||||
"requests": [
|
||||
{"id":"same","method":"fetch","arguments":{"path":"/a"}},
|
||||
@@ -136,6 +195,67 @@ fn invalid_batch_envelopes_are_rejected_at_top_level() {
|
||||
assert!(decode_batch_request(duplicate, too_many).is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn batch_resource_limits_use_413_or_422_and_never_429() {
|
||||
let error = ProtocolError {
|
||||
code: ProtocolErrorCode::ResourceLimitExceeded,
|
||||
message: "bounded".into(),
|
||||
};
|
||||
for (kind, status) in [
|
||||
(
|
||||
ResourceLimitKind::RepresentationBytes,
|
||||
StatusCode::PAYLOAD_TOO_LARGE,
|
||||
),
|
||||
(ResourceLimitKind::Logical, StatusCode::UNPROCESSABLE_ENTITY),
|
||||
] {
|
||||
let encoded = encode_batch_resource_limit_error_response(&error, kind, limits()).unwrap();
|
||||
assert_eq!(encoded.status(), status);
|
||||
assert_ne!(encoded.status(), StatusCode::TOO_MANY_REQUESTS);
|
||||
assert_eq!(
|
||||
decode_batch_response(&request(), &encoded, limits()).unwrap(),
|
||||
DecodedResponse::ProtocolFailure {
|
||||
error: error.clone(),
|
||||
status_mismatch: None,
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn batch_success_body_requires_exactly_http_200() {
|
||||
let request = request();
|
||||
let response = BatchResponse {
|
||||
responses: vec![
|
||||
BatchResponseItem {
|
||||
id: "object".into(),
|
||||
outcome: Ok(BatchSuccess::Fetch(Object {
|
||||
name: "item".into(),
|
||||
description: None,
|
||||
interfaces: vec![],
|
||||
r#ref: None,
|
||||
validator: None,
|
||||
})),
|
||||
},
|
||||
BatchResponseItem {
|
||||
id: "interface".into(),
|
||||
outcome: Err(ProtocolError {
|
||||
code: ProtocolErrorCode::InterfaceNotFound,
|
||||
message: "gone".into(),
|
||||
}),
|
||||
},
|
||||
],
|
||||
};
|
||||
let encoded = encode_batch_response(&request, &response, limits()).unwrap();
|
||||
for status in [StatusCode::CREATED, StatusCode::ACCEPTED] {
|
||||
let mut noncanonical = encoded.clone();
|
||||
*noncanonical.status_mut() = status;
|
||||
assert!(matches!(
|
||||
decode_batch_response(&request, &noncanonical, limits()).unwrap_err(),
|
||||
ClientResponseError::InvalidResponse { .. }
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn top_level_batch_errors_use_the_batch_allowed_code_set() {
|
||||
let invalid = ProtocolError {
|
||||
|
||||
@@ -9,11 +9,12 @@ use wip_https::wip_protocol::{
|
||||
TypeDeclaration, TypeExpr, UnionCase, Value,
|
||||
};
|
||||
use wip_https::{
|
||||
ClientResponseError, DecodedResponse, Endpoint, InvalidResponseKind, Limits,
|
||||
decode_call_operation_metadata, decode_call_operation_request, decode_call_operation_response,
|
||||
decode_fetch_interface_request, decode_fetch_interface_response, decode_fetch_request,
|
||||
decode_fetch_response, decode_fetch_tree_request, decode_fetch_tree_response,
|
||||
encode_call_operation_request, encode_call_operation_response, encode_fetch_interface_request,
|
||||
ClientResponseError, CodecError, DecodedResponse, Endpoint, InvalidResponseKind, Limits,
|
||||
ResourceLimitKind, decode_call_operation_metadata, decode_call_operation_request,
|
||||
decode_call_operation_response, decode_fetch_interface_request,
|
||||
decode_fetch_interface_response, decode_fetch_request, decode_fetch_response,
|
||||
decode_fetch_tree_request, decode_fetch_tree_response, encode_call_operation_request,
|
||||
encode_call_operation_response, encode_fetch_interface_request,
|
||||
encode_fetch_interface_response, encode_fetch_request, encode_fetch_response,
|
||||
encode_fetch_tree_request, encode_fetch_tree_response,
|
||||
};
|
||||
@@ -193,6 +194,36 @@ fn call_request() -> CallOperationRequest {
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn every_single_interaction_request_accepts_json() {
|
||||
let fetch =
|
||||
encode_fetch_request(&endpoint(), &FetchRequest { path: "/".into() }, limits()).unwrap();
|
||||
let fetch_tree = encode_fetch_tree_request(
|
||||
&endpoint(),
|
||||
&FetchTreeRequest {
|
||||
path: "/".into(),
|
||||
depth: 1,
|
||||
limit: 1,
|
||||
},
|
||||
limits(),
|
||||
)
|
||||
.unwrap();
|
||||
let fetch_interface = encode_fetch_interface_request(
|
||||
&endpoint(),
|
||||
&FetchInterfaceRequest {
|
||||
interface: "interface-v1".into(),
|
||||
},
|
||||
limits(),
|
||||
)
|
||||
.unwrap();
|
||||
let call = encode_call_operation_request(&endpoint(), &call_request(), &descriptor(), limits())
|
||||
.unwrap();
|
||||
|
||||
for request in [fetch, fetch_tree, fetch_interface, call] {
|
||||
assert_eq!(request.headers()["accept"], "application/json");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn fetch_round_trips_direct_body_and_body_validator() {
|
||||
let request = FetchRequest {
|
||||
@@ -362,11 +393,21 @@ fn malformed_unknown_null_and_noncanonical_base64_are_rejected() {
|
||||
#[test]
|
||||
fn caller_limits_bound_bytes_and_nesting() {
|
||||
let tiny = Limits::new(8, 8, 2, 1).unwrap();
|
||||
assert!(decode_fetch_request(br#"{"path":"/long"}"#, tiny).is_err());
|
||||
let too_large = decode_fetch_request(br#"{"path":"/long"}"#, tiny).unwrap_err();
|
||||
assert!(matches!(&too_large, CodecError::BodyTooLarge { .. }));
|
||||
assert_eq!(
|
||||
too_large.resource_limit_kind(),
|
||||
Some(ResourceLimitKind::RepresentationBytes)
|
||||
);
|
||||
|
||||
let nested = br#"{"path":[[["/"]]]}"#;
|
||||
let nesting_limited = Limits::new(1024, 1024, 2, 1).unwrap();
|
||||
assert!(decode_fetch_request(nested, nesting_limited).is_err());
|
||||
let too_deep = decode_fetch_request(nested, nesting_limited).unwrap_err();
|
||||
assert!(matches!(&too_deep, CodecError::NestingTooDeep { .. }));
|
||||
assert_eq!(
|
||||
too_deep.resource_limit_kind(),
|
||||
Some(ResourceLimitKind::Logical)
|
||||
);
|
||||
|
||||
let outbound_limited = Limits::new(1_000_000, 1_000_000, 1, 10).unwrap();
|
||||
assert!(
|
||||
|
||||
@@ -3,9 +3,10 @@ use wip_https::wip_protocol::{
|
||||
FetchRequest, ProtocolError, ProtocolErrorCode, ProtocolInteraction,
|
||||
};
|
||||
use wip_https::{
|
||||
ClientResponseError, DecodedResponse, InvalidResponseKind, Limits, TransportFailureKind,
|
||||
decode_fetch_response, encode_protocol_error_response, error_code_name, is_json_content_type,
|
||||
status_for_error,
|
||||
ClientResponseError, DecodedResponse, InvalidResponseKind, Limits, ResourceLimitKind,
|
||||
TransportFailureKind, decode_fetch_response, encode_protocol_error_response,
|
||||
encode_resource_limit_error_response, error_code_name, is_json_content_type, status_for_error,
|
||||
status_for_resource_limit,
|
||||
};
|
||||
|
||||
fn limits() -> Limits {
|
||||
@@ -95,7 +96,7 @@ fn every_protocol_code_has_stable_name_and_status() {
|
||||
(
|
||||
ProtocolErrorCode::ResourceLimitExceeded,
|
||||
"resource_limit_exceeded",
|
||||
StatusCode::TOO_MANY_REQUESTS,
|
||||
StatusCode::UNPROCESSABLE_ENTITY,
|
||||
),
|
||||
(
|
||||
ProtocolErrorCode::Internal,
|
||||
@@ -115,6 +116,73 @@ fn every_protocol_code_has_stable_name_and_status() {
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn resource_limit_statuses_encode_and_decode_by_concrete_limit_class() {
|
||||
let error = ProtocolError {
|
||||
code: ProtocolErrorCode::ResourceLimitExceeded,
|
||||
message: "bounded".into(),
|
||||
};
|
||||
let cases = [
|
||||
(
|
||||
ResourceLimitKind::RepresentationBytes,
|
||||
StatusCode::PAYLOAD_TOO_LARGE,
|
||||
),
|
||||
(ResourceLimitKind::Logical, StatusCode::UNPROCESSABLE_ENTITY),
|
||||
];
|
||||
for (kind, status) in cases {
|
||||
assert_eq!(status_for_resource_limit(kind), status);
|
||||
let encoded = encode_resource_limit_error_response(
|
||||
ProtocolInteraction::Fetch,
|
||||
&error,
|
||||
kind,
|
||||
limits(),
|
||||
)
|
||||
.unwrap();
|
||||
assert_eq!(encoded.status(), status);
|
||||
assert_eq!(
|
||||
decode_fetch_response(&request(), &encoded, limits()).unwrap(),
|
||||
DecodedResponse::ProtocolFailure {
|
||||
error: error.clone(),
|
||||
status_mismatch: None,
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
let default =
|
||||
encode_protocol_error_response(ProtocolInteraction::Fetch, &error, limits()).unwrap();
|
||||
assert_eq!(default.status(), StatusCode::UNPROCESSABLE_ENTITY);
|
||||
assert_ne!(default.status(), StatusCode::TOO_MANY_REQUESTS);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rate_limit_status_without_protocol_envelope_stays_transport_failure() {
|
||||
let response = Response::builder()
|
||||
.status(StatusCode::TOO_MANY_REQUESTS)
|
||||
.body(b"rate limited".to_vec())
|
||||
.unwrap();
|
||||
assert!(matches!(
|
||||
decode_fetch_response(&request(), &response, limits()).unwrap_err(),
|
||||
ClientResponseError::TransportBinding(failure)
|
||||
if failure.status == StatusCode::TOO_MANY_REQUESTS
|
||||
&& failure.kind == TransportFailureKind::HttpStatus
|
||||
));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn valid_success_body_requires_exactly_http_200() {
|
||||
let success_body = br#"{"name":"","interfaces":[]}"#;
|
||||
for status in [StatusCode::CREATED, StatusCode::ACCEPTED] {
|
||||
assert!(matches!(
|
||||
decode_fetch_response(&request(), &response(status, success_body), limits())
|
||||
.unwrap_err(),
|
||||
ClientResponseError::InvalidResponse {
|
||||
kind: InvalidResponseKind::NonCanonicalSuccessStatus,
|
||||
..
|
||||
}
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn server_encoder_rejects_codes_disallowed_for_the_interaction() {
|
||||
let error = ProtocolError {
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
use wip_https::http::{Method, Request};
|
||||
use wip_https::{Endpoint, Route, validate_http_request};
|
||||
use wip_https::http::{Method, Request, StatusCode};
|
||||
use wip_https::{Endpoint, RequestMetadataError, Route, validate_http_request};
|
||||
|
||||
#[test]
|
||||
fn endpoint_is_https_absolute_queryless_and_canonical() {
|
||||
@@ -72,6 +72,29 @@ fn inbound_http_metadata_enforces_post_route_and_json() {
|
||||
.body(Vec::<u8>::new())
|
||||
.unwrap();
|
||||
assert!(validate_http_request(&endpoint, Route::Fetch, &wrong_route).is_err());
|
||||
|
||||
let incompatible_accept = Request::builder()
|
||||
.method(Method::POST)
|
||||
.uri("/wip/v1/fetch")
|
||||
.header("content-type", "application/json")
|
||||
.header("accept", "text/plain, */*;q=1, application/json;q=0")
|
||||
.body(Vec::<u8>::new())
|
||||
.unwrap();
|
||||
let error = validate_http_request(&endpoint, Route::Fetch, &incompatible_accept).unwrap_err();
|
||||
assert!(matches!(&error, RequestMetadataError::NotAcceptable));
|
||||
assert_eq!(error.status(), StatusCode::NOT_ACCEPTABLE);
|
||||
|
||||
let unsupported_media_type = Request::builder()
|
||||
.method(Method::POST)
|
||||
.uri("/wip/v1/fetch")
|
||||
.header("content-type", "text/plain")
|
||||
.header("accept", "application/json")
|
||||
.body(Vec::<u8>::new())
|
||||
.unwrap();
|
||||
let error =
|
||||
validate_http_request(&endpoint, Route::Fetch, &unsupported_media_type).unwrap_err();
|
||||
assert!(matches!(&error, RequestMetadataError::UnsupportedMediaType));
|
||||
assert_eq!(error.status(), StatusCode::UNSUPPORTED_MEDIA_TYPE);
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
Reference in New Issue
Block a user