Skip to content

Commit

Permalink
add unit tests checking requestInfo parsing method
Browse files Browse the repository at this point in the history
  • Loading branch information
biryukovmaxim committed Jun 18, 2023
1 parent da5ed62 commit 4189d81
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 18 deletions.
33 changes: 15 additions & 18 deletions engineioxide/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@ pub enum Error {
/// Otherwise, return a 500
impl<B> From<Error> for Response<ResponseBody<B>> {
fn from(err: Error) -> Self {
let conn_err_resp = |message: &'static str| {
Response::builder()
.status(400)
.body(ResponseBody::custom_response(message.into()))
.unwrap()
};
match err {
Error::HttpErrorResponse(code) => Response::builder()
.status(code)
Expand All @@ -59,24 +65,15 @@ impl<B> From<Error> for Response<ResponseBody<B>> {
.status(400)
.body(ResponseBody::empty_response())
.unwrap(),
Error::UnknownTransport => Response::builder()
.status(400)
.body(ResponseBody::custom_response(
"{\"code\":\"0\",\"message\":\"Transport unknown\"}".into(),
))
.unwrap(),
Error::BadHandshakeMethod => Response::builder()
.status(400)
.body(ResponseBody::custom_response(
"{\"code\":\"2\",\"message\":\"Bad handshake method\"}".into(),
))
.unwrap(),
Error::UnsupportedProtocolVersion => Response::builder()
.status(400)
.body(ResponseBody::custom_response(
"{\"code\":\"5\",\"message\":\"Unsupported protocol version\"}".into(),
))
.unwrap(),
Error::UnknownTransport => {
conn_err_resp("{\"code\":\"0\",\"message\":\"Transport unknown\"}")
}
Error::BadHandshakeMethod => {
conn_err_resp("{\"code\":\"2\",\"message\":\"Bad handshake method\"}")
}
Error::UnsupportedProtocolVersion => {
conn_err_resp("{\"code\":\"5\",\"message\":\"Unsupported protocol version\"}")
}
e => {
debug!("uncaught error {e:?}");
Response::builder()
Expand Down
21 changes: 21 additions & 0 deletions engineioxide/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,7 @@ impl FromStr for TransportType {
}

/// The request information extracted from the request URI.
#[derive(Debug)]
struct RequestInfo {
/// The socket id if present in the request.
sid: Option<Sid>,
Expand Down Expand Up @@ -316,4 +317,24 @@ mod tests {
assert_eq!(info.transport, TransportType::Websocket);
assert_eq!(info.method, Method::GET);
}
#[test]
fn transport_unknown_err() {
let req = build_request("http://localhost:3000/socket.io/?EIO=4&transport=grpc");
let err = RequestInfo::parse(&req).unwrap_err();
assert!(matches!(err, Error::UnknownTransport));
}
#[test]
fn unsupported_protocol_version() {
let req = build_request("http://localhost:3000/socket.io/?EIO=2&transport=polling");
let err = RequestInfo::parse(&req).unwrap_err();
assert!(matches!(err, Error::UnsupportedProtocolVersion));
}
#[test]
fn bad_handshake_method() {
let req = Request::post("http://localhost:3000/socket.io/?EIO=4&transport=polling")
.body(())
.unwrap();
let err = RequestInfo::parse(&req).unwrap_err();
assert!(matches!(err, Error::BadHandshakeMethod));
}
}

0 comments on commit 4189d81

Please sign in to comment.