From abc34eeb7e8052ca36bf6bc93ad297d968f30c3a Mon Sep 17 00:00:00 2001 From: Jonas Platte Date: Wed, 12 Aug 2020 20:29:24 +0200 Subject: [PATCH] Move manual endpoint impl test into its own file --- ruma-api/src/lib.rs | 144 ------------------------- ruma-api/tests/manual_endpoint_impl.rs | 132 +++++++++++++++++++++++ 2 files changed, 132 insertions(+), 144 deletions(-) create mode 100644 ruma-api/tests/manual_endpoint_impl.rs diff --git a/ruma-api/src/lib.rs b/ruma-api/src/lib.rs index 6754dd4e..d5ec67c4 100644 --- a/ruma-api/src/lib.rs +++ b/ruma-api/src/lib.rs @@ -326,147 +326,3 @@ macro_rules! try_deserialize { } }; } - -#[cfg(test)] -mod tests { - /// PUT /_matrix/client/r0/directory/room/:room_alias - pub mod create { - use std::{convert::TryFrom, ops::Deref}; - - use http::{header::CONTENT_TYPE, method::Method}; - use ruma_identifiers::{RoomAliasId, RoomId}; - use serde::{Deserialize, Serialize}; - - use crate::{ - error::{ - FromHttpRequestError, FromHttpResponseError, IntoHttpError, - RequestDeserializationError, ServerError, Void, - }, - Endpoint, Metadata, Outgoing, - }; - - /// A request to create a new room alias. - #[derive(Debug)] - pub struct Request { - pub room_id: RoomId, // body - pub room_alias: RoomAliasId, // path - } - - impl Outgoing for Request { - type Incoming = Self; - } - - impl Endpoint for Request { - type Response = Response; - type ResponseError = Void; - type IncomingRequest = Self; - type IncomingResponse = Response; - - const METADATA: Metadata = Metadata { - description: "Add an alias to a room.", - method: Method::PUT, - name: "create_alias", - path: "/_matrix/client/r0/directory/room/:room_alias", - rate_limited: false, - requires_authentication: false, - }; - - fn try_into_http_request( - self, - base_url: &str, - _access_token: Option<&str>, - ) -> Result>, IntoHttpError> { - let metadata = Request::METADATA; - - let url = (base_url.to_owned() + metadata.path) - .replace(":room_alias", &self.room_alias.to_string()); - - let request_body = RequestBody { room_id: self.room_id }; - - let http_request = http::Request::builder() - .method(metadata.method) - .uri(url) - .body(serde_json::to_vec(&request_body)?) - // this cannot fail because we don't give user-supplied data to any of the - // builder methods - .unwrap(); - - Ok(http_request) - } - } - - impl TryFrom>> for Request { - type Error = FromHttpRequestError; - - fn try_from(request: http::Request>) -> Result { - let request_body: RequestBody = - match serde_json::from_slice(request.body().as_slice()) { - Ok(body) => body, - Err(err) => { - return Err(RequestDeserializationError::new(err, request).into()); - } - }; - let path_segments: Vec<&str> = request.uri().path()[1..].split('/').collect(); - Ok(Request { - room_id: request_body.room_id, - room_alias: { - let segment = path_segments.get(5).unwrap().as_bytes(); - let decoded = match percent_encoding::percent_decode(segment).decode_utf8() - { - Ok(x) => x, - Err(err) => { - return Err(RequestDeserializationError::new(err, request).into()) - } - }; - match serde_json::from_str(decoded.deref()) { - Ok(id) => id, - Err(err) => { - return Err(RequestDeserializationError::new(err, request).into()) - } - } - }, - }) - } - } - - #[derive(Debug, Serialize, Deserialize)] - struct RequestBody { - room_id: RoomId, - } - - /// The response to a request to create a new room alias. - #[derive(Clone, Copy, Debug)] - pub struct Response; - - impl Outgoing for Response { - type Incoming = Self; - } - - impl TryFrom>> for Response { - type Error = FromHttpResponseError; - - fn try_from(http_response: http::Response>) -> Result { - if http_response.status().as_u16() < 400 { - Ok(Response) - } else { - Err(FromHttpResponseError::Http(ServerError::Unknown( - crate::error::ResponseDeserializationError::from_response(http_response), - ))) - } - } - } - - impl TryFrom for http::Response> { - type Error = IntoHttpError; - - fn try_from(_: Response) -> Result>, Self::Error> { - let response = http::Response::builder() - .header(CONTENT_TYPE, "application/json") - .body(b"{}".to_vec()) - .unwrap(); - - Ok(response) - } - } - } -} diff --git a/ruma-api/tests/manual_endpoint_impl.rs b/ruma-api/tests/manual_endpoint_impl.rs new file mode 100644 index 00000000..2f1aaf0a --- /dev/null +++ b/ruma-api/tests/manual_endpoint_impl.rs @@ -0,0 +1,132 @@ +//! PUT /_matrix/client/r0/directory/room/:room_alias +use std::{convert::TryFrom, ops::Deref}; + +use http::{header::CONTENT_TYPE, method::Method}; +use ruma_identifiers::{RoomAliasId, RoomId}; +use serde::{Deserialize, Serialize}; + +use ruma_api::{ + error::{ + FromHttpRequestError, FromHttpResponseError, IntoHttpError, RequestDeserializationError, + ResponseDeserializationError, ServerError, Void, + }, + Endpoint, Metadata, Outgoing, +}; + +/// A request to create a new room alias. +#[derive(Debug)] +pub struct Request { + pub room_id: RoomId, // body + pub room_alias: RoomAliasId, // path +} + +impl Outgoing for Request { + type Incoming = Self; +} + +impl Endpoint for Request { + type Response = Response; + type ResponseError = Void; + type IncomingRequest = Self; + type IncomingResponse = Response; + + const METADATA: Metadata = Metadata { + description: "Add an alias to a room.", + method: Method::PUT, + name: "create_alias", + path: "/_matrix/client/r0/directory/room/:room_alias", + rate_limited: false, + requires_authentication: false, + }; + + fn try_into_http_request( + self, + base_url: &str, + _access_token: Option<&str>, + ) -> Result>, IntoHttpError> { + let metadata = Request::METADATA; + + let url = (base_url.to_owned() + metadata.path) + .replace(":room_alias", &self.room_alias.to_string()); + + let request_body = RequestBody { room_id: self.room_id }; + + let http_request = http::Request::builder() + .method(metadata.method) + .uri(url) + .body(serde_json::to_vec(&request_body)?) + // this cannot fail because we don't give user-supplied data to any of the + // builder methods + .unwrap(); + + Ok(http_request) + } +} + +impl TryFrom>> for Request { + type Error = FromHttpRequestError; + + fn try_from(request: http::Request>) -> Result { + let request_body: RequestBody = match serde_json::from_slice(request.body().as_slice()) { + Ok(body) => body, + Err(err) => { + return Err(RequestDeserializationError::new(err, request).into()); + } + }; + let path_segments: Vec<&str> = request.uri().path()[1..].split('/').collect(); + Ok(Request { + room_id: request_body.room_id, + room_alias: { + let segment = path_segments.get(5).unwrap().as_bytes(); + let decoded = match percent_encoding::percent_decode(segment).decode_utf8() { + Ok(x) => x, + Err(err) => return Err(RequestDeserializationError::new(err, request).into()), + }; + match serde_json::from_str(decoded.deref()) { + Ok(id) => id, + Err(err) => return Err(RequestDeserializationError::new(err, request).into()), + } + }, + }) + } +} + +#[derive(Debug, Serialize, Deserialize)] +struct RequestBody { + room_id: RoomId, +} + +/// The response to a request to create a new room alias. +#[derive(Clone, Copy, Debug)] +pub struct Response; + +impl Outgoing for Response { + type Incoming = Self; +} + +impl TryFrom>> for Response { + type Error = FromHttpResponseError; + + fn try_from(http_response: http::Response>) -> Result { + if http_response.status().as_u16() < 400 { + Ok(Response) + } else { + Err(FromHttpResponseError::Http(ServerError::Unknown( + ResponseDeserializationError::from_response(http_response), + ))) + } + } +} + +impl TryFrom for http::Response> { + type Error = IntoHttpError; + + fn try_from(_: Response) -> Result>, Self::Error> { + let response = http::Response::builder() + .header(CONTENT_TYPE, "application/json") + .body(b"{}".to_vec()) + .unwrap(); + + Ok(response) + } +}