fix: set route's method to GET
This commit is contained in:
parent
8045eb979e
commit
e1c27ca308
4 changed files with 23 additions and 33 deletions
|
|
@ -40,7 +40,10 @@ pub async fn send_confirmation_email(
|
||||||
new_subscriber: NewSubscriber,
|
new_subscriber: NewSubscriber,
|
||||||
base_url: &str,
|
base_url: &str,
|
||||||
) -> Result<(), reqwest::Error> {
|
) -> Result<(), reqwest::Error> {
|
||||||
let confirmation_link = format!("{}/subscriptions/confirm?subscription_token=mytoken", base_url);
|
let confirmation_link = format!(
|
||||||
|
"{}/subscriptions/confirm?subscription_token=mytoken",
|
||||||
|
base_url
|
||||||
|
);
|
||||||
let plain_body = format!(
|
let plain_body = format!(
|
||||||
"Welcome to our newsletter! Visit {} to confirm your subscription.",
|
"Welcome to our newsletter! Visit {} to confirm your subscription.",
|
||||||
confirmation_link
|
confirmation_link
|
||||||
|
|
@ -118,8 +121,6 @@ pub async fn insert_subscriber(
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn routes_subscriptions(state: AppState) -> Router {
|
pub fn routes_subscriptions() -> Router<AppState> {
|
||||||
Router::new()
|
Router::new().route("/subscriptions", post(subscribe))
|
||||||
.route("/subscriptions", post(subscribe))
|
|
||||||
.with_state(state)
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
use crate::startup::AppState;
|
use crate::startup::AppState;
|
||||||
use axum::routing::post;
|
use axum::routing::get;
|
||||||
use axum::{
|
use axum::{
|
||||||
extract::Query,
|
extract::Query,
|
||||||
http::StatusCode,
|
http::StatusCode,
|
||||||
|
|
@ -12,14 +12,12 @@ pub struct Parameters {
|
||||||
subscription_token: String,
|
subscription_token: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[tracing::instrument(name = "Confirm a pending subscriber", skip(_parameters))]
|
// #[tracing::instrument(name = "Confirm a pending subscriber", skip(_parameters))]
|
||||||
pub async fn confirm(Query(_parameters): Query<Parameters>) -> impl IntoResponse {
|
pub async fn confirm(Query(_parameters): Query<Parameters>) -> Response {
|
||||||
println!("subscription_token: {}", _parameters.subscription_token);
|
println!("subscription_token: {}", _parameters.subscription_token);
|
||||||
return (StatusCode::OK,).into_response();
|
(StatusCode::OK,).into_response()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn routes_subscriptions_confirm(state: AppState) -> Router {
|
pub fn routes_subscriptions_confirm() -> Router<AppState> {
|
||||||
Router::new()
|
Router::new().route("/subscriptions/confirm", get(confirm))
|
||||||
.route("/subscriptions/confirm", post(confirm))
|
|
||||||
.with_state(state)
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -67,19 +67,15 @@ impl Application {
|
||||||
let state = AppState {
|
let state = AppState {
|
||||||
db_pool: connection_pool.clone(),
|
db_pool: connection_pool.clone(),
|
||||||
email_client: email_client.clone(),
|
email_client: email_client.clone(),
|
||||||
base_url: ApplicationBaseUrl (configuration.application.base_url),
|
base_url: ApplicationBaseUrl(configuration.application.base_url),
|
||||||
};
|
};
|
||||||
|
|
||||||
// NOTE: [her] There might be a problem with the state handling, the given version
|
|
||||||
// seems to me as if it has no "outer state" - but I might obviously be wrong.
|
|
||||||
//
|
|
||||||
// Check this: https://docs.rs/axum/latest/axum/routing/struct.Router.html#merging-routers-with-state
|
|
||||||
let app = Router::new()
|
let app = Router::new()
|
||||||
.merge(crate::routes::routes_health_check())
|
|
||||||
// TODO: check whether state cloning is what we want
|
// TODO: check whether state cloning is what we want
|
||||||
.merge(crate::routes::routes_subscriptions(state.clone()))
|
.merge(crate::routes::routes_subscriptions())
|
||||||
.merge(crate::routes::routes_subscriptions_confirm(state))
|
.merge(crate::routes::routes_subscriptions_confirm())
|
||||||
// .with_state(state)
|
.with_state(state)
|
||||||
|
.merge(crate::routes::routes_health_check())
|
||||||
.layer(
|
.layer(
|
||||||
// from https://docs.rs/tower-http/0.2.5/tower_http/request_id/index.html#using-trace
|
// from https://docs.rs/tower-http/0.2.5/tower_http/request_id/index.html#using-trace
|
||||||
ServiceBuilder::new()
|
ServiceBuilder::new()
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,7 @@
|
||||||
use crate::helpers::spawn_app;
|
use crate::helpers::spawn_app;
|
||||||
use reqwest::Url;
|
use reqwest::Url;
|
||||||
use wiremock::{ResponseTemplate, Mock};
|
use wiremock::matchers::{method, path};
|
||||||
use wiremock::matchers::{path, method};
|
use wiremock::{Mock, ResponseTemplate};
|
||||||
|
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
async fn the_link_returned_by_subscribe_returns_a_200_if_called() {
|
async fn the_link_returned_by_subscribe_returns_a_200_if_called() {
|
||||||
|
|
@ -17,28 +17,24 @@ async fn the_link_returned_by_subscribe_returns_a_200_if_called() {
|
||||||
|
|
||||||
app.post_subscriptions(body.into()).await;
|
app.post_subscriptions(body.into()).await;
|
||||||
let email_request = &app.email_server.received_requests().await.unwrap()[0];
|
let email_request = &app.email_server.received_requests().await.unwrap()[0];
|
||||||
let body: serde_json::Value = serde_json::from_slice(&email_request.body)
|
let body: serde_json::Value = serde_json::from_slice(&email_request.body).unwrap();
|
||||||
.unwrap();
|
|
||||||
// Extract the link from one of the request fields.
|
// Extract the link from one of the request fields.
|
||||||
let get_link = |s: &str| {
|
let get_link = |s: &str| {
|
||||||
let links: Vec<_> = linkify::LinkFinder::new()
|
let links: Vec<_> = linkify::LinkFinder::new()
|
||||||
.links(s)
|
.links(s)
|
||||||
.filter(|l| *l.kind() == linkify::LinkKind::Url)
|
.filter(|l| *l.kind() == linkify::LinkKind::Url)
|
||||||
.collect();
|
.collect();
|
||||||
assert_eq!(links.len(), 1);
|
assert_eq!(links.len(), 1);
|
||||||
links[0].as_str().to_owned()
|
links[0].as_str().to_owned()
|
||||||
};
|
};
|
||||||
let raw_confirmation_link = &get_link(body["HtmlBody"].as_str().unwrap());
|
let raw_confirmation_link = &get_link(body["HtmlBody"].as_str().unwrap());
|
||||||
let mut confirmation_link = Url::parse(raw_confirmation_link).unwrap();
|
let mut confirmation_link = Url::parse(raw_confirmation_link).unwrap();
|
||||||
|
|
||||||
assert_eq!(confirmation_link.host_str().unwrap(), "127.0.0.1");
|
assert_eq!(confirmation_link.host_str().unwrap(), "127.0.0.1");
|
||||||
confirmation_link.set_port(Some(app.port)).unwrap();
|
confirmation_link.set_port(Some(app.port)).unwrap();
|
||||||
|
|
||||||
println!("\n################################\n{}\n##########################################", confirmation_link);
|
|
||||||
|
|
||||||
// Act
|
// Act
|
||||||
let response = reqwest::get(confirmation_link)
|
let response = reqwest::get(confirmation_link).await.unwrap();
|
||||||
.await
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
assert_eq!(response.status().as_u16(), 200);
|
assert_eq!(response.status().as_u16(), 200);
|
||||||
|
|
@ -57,4 +53,3 @@ async fn confirmations_without_token_are_rejected_with_a_400() {
|
||||||
// Assert
|
// Assert
|
||||||
assert_eq!(response.status().as_u16(), 400);
|
assert_eq!(response.status().as_u16(), 400);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue