Skip to content

Commit

Permalink
fix(webhooks): properly handle webhook request for root path (/)
Browse files Browse the repository at this point in the history
  • Loading branch information
azasypkin committed Oct 9, 2023
1 parent b75b835 commit a5c3dcd
Showing 1 changed file with 54 additions and 1 deletion.
55 changes: 54 additions & 1 deletion src/server/handlers/webhooks_responders.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ pub async fn webhooks_responders(
};

// Make sure path doesn't end with trailing slash as it's not allowed.
if responder_path.ends_with('/') {
if responder_path.len() > 1 && responder_path.ends_with('/') {
responder_path.pop();
}

Expand Down Expand Up @@ -336,4 +336,57 @@ mod tests {

Ok(())
}

#[actix_rt::test]
async fn can_handle_request_with_subdomain_url_type_for_root_path() -> anyhow::Result<()> {
let app_state = mock_app_state().await?;

// Insert user into the database.
let user = mock_user()?;
let users = app_state.api.users();
users.upsert(&user).await?;

// Insert auto responders data.
let responder = AutoResponder {
path: "/".to_string(),
method: AutoResponderMethod::Any,
requests_to_track: 3,
status_code: 200,
body: Some("body".to_string()),
headers: Some(vec![("key".to_string(), "value".to_string())]),
delay: None,
};
app_state
.api
.auto_responders()
.upsert_auto_responder(user.id, responder)
.await?;

let request = TestRequest::with_uri("https://dev-handle-1.webhooks.secutils.dev")
.insert_header(("x-replaced-path", "/"))
.insert_header(("x-forwarded-host", "dev-handle-1.webhooks.secutils.dev"))
.to_http_request();
let path = web::Path::<PathParams>::from_request(&request, &mut Payload::None)
.await
.unwrap();
let response = webhooks_responders(web::Data::new(app_state), request, Bytes::new(), path)
.await
.unwrap();
assert_debug_snapshot!(response, @r###"
HttpResponse {
error: None,
res:
Response HTTP/1.1 200 OK
headers:
"key": "value"
body: Sized(4)
,
}
"###);

let body = response.into_body().try_into_bytes().unwrap();
assert_eq!(body, Bytes::from_static(b"body"));

Ok(())
}
}

0 comments on commit a5c3dcd

Please sign in to comment.