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,
|
||||
base_url: &str,
|
||||
) -> 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!(
|
||||
"Welcome to our newsletter! Visit {} to confirm your subscription.",
|
||||
confirmation_link
|
||||
|
|
@ -118,8 +121,6 @@ pub async fn insert_subscriber(
|
|||
Ok(())
|
||||
}
|
||||
|
||||
pub fn routes_subscriptions(state: AppState) -> Router {
|
||||
Router::new()
|
||||
.route("/subscriptions", post(subscribe))
|
||||
.with_state(state)
|
||||
pub fn routes_subscriptions() -> Router<AppState> {
|
||||
Router::new().route("/subscriptions", post(subscribe))
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
use crate::startup::AppState;
|
||||
use axum::routing::post;
|
||||
use axum::routing::get;
|
||||
use axum::{
|
||||
extract::Query,
|
||||
http::StatusCode,
|
||||
|
|
@ -12,14 +12,12 @@ pub struct Parameters {
|
|||
subscription_token: String,
|
||||
}
|
||||
|
||||
#[tracing::instrument(name = "Confirm a pending subscriber", skip(_parameters))]
|
||||
pub async fn confirm(Query(_parameters): Query<Parameters>) -> impl IntoResponse {
|
||||
// #[tracing::instrument(name = "Confirm a pending subscriber", skip(_parameters))]
|
||||
pub async fn confirm(Query(_parameters): Query<Parameters>) -> Response {
|
||||
println!("subscription_token: {}", _parameters.subscription_token);
|
||||
return (StatusCode::OK,).into_response();
|
||||
(StatusCode::OK,).into_response()
|
||||
}
|
||||
|
||||
pub fn routes_subscriptions_confirm(state: AppState) -> Router {
|
||||
Router::new()
|
||||
.route("/subscriptions/confirm", post(confirm))
|
||||
.with_state(state)
|
||||
pub fn routes_subscriptions_confirm() -> Router<AppState> {
|
||||
Router::new().route("/subscriptions/confirm", get(confirm))
|
||||
}
|
||||
|
|
|
|||
|
|
@ -67,19 +67,15 @@ impl Application {
|
|||
let state = AppState {
|
||||
db_pool: connection_pool.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()
|
||||
.merge(crate::routes::routes_health_check())
|
||||
// TODO: check whether state cloning is what we want
|
||||
.merge(crate::routes::routes_subscriptions(state.clone()))
|
||||
.merge(crate::routes::routes_subscriptions_confirm(state))
|
||||
// .with_state(state)
|
||||
.merge(crate::routes::routes_subscriptions())
|
||||
.merge(crate::routes::routes_subscriptions_confirm())
|
||||
.with_state(state)
|
||||
.merge(crate::routes::routes_health_check())
|
||||
.layer(
|
||||
// from https://docs.rs/tower-http/0.2.5/tower_http/request_id/index.html#using-trace
|
||||
ServiceBuilder::new()
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
use crate::helpers::spawn_app;
|
||||
use reqwest::Url;
|
||||
use wiremock::{ResponseTemplate, Mock};
|
||||
use wiremock::matchers::{path, method};
|
||||
use wiremock::matchers::{method, path};
|
||||
use wiremock::{Mock, ResponseTemplate};
|
||||
|
||||
#[tokio::test]
|
||||
async fn the_link_returned_by_subscribe_returns_a_200_if_called() {
|
||||
|
|
@ -17,8 +17,7 @@ async fn the_link_returned_by_subscribe_returns_a_200_if_called() {
|
|||
|
||||
app.post_subscriptions(body.into()).await;
|
||||
let email_request = &app.email_server.received_requests().await.unwrap()[0];
|
||||
let body: serde_json::Value = serde_json::from_slice(&email_request.body)
|
||||
.unwrap();
|
||||
let body: serde_json::Value = serde_json::from_slice(&email_request.body).unwrap();
|
||||
// Extract the link from one of the request fields.
|
||||
let get_link = |s: &str| {
|
||||
let links: Vec<_> = linkify::LinkFinder::new()
|
||||
|
|
@ -30,15 +29,12 @@ async fn the_link_returned_by_subscribe_returns_a_200_if_called() {
|
|||
};
|
||||
let raw_confirmation_link = &get_link(body["HtmlBody"].as_str().unwrap());
|
||||
let mut confirmation_link = Url::parse(raw_confirmation_link).unwrap();
|
||||
|
||||
assert_eq!(confirmation_link.host_str().unwrap(), "127.0.0.1");
|
||||
confirmation_link.set_port(Some(app.port)).unwrap();
|
||||
|
||||
println!("\n################################\n{}\n##########################################", confirmation_link);
|
||||
|
||||
// Act
|
||||
let response = reqwest::get(confirmation_link)
|
||||
.await
|
||||
.unwrap();
|
||||
let response = reqwest::get(confirmation_link).await.unwrap();
|
||||
|
||||
// Assert
|
||||
assert_eq!(response.status().as_u16(), 200);
|
||||
|
|
@ -57,4 +53,3 @@ async fn confirmations_without_token_are_rejected_with_a_400() {
|
|||
// Assert
|
||||
assert_eq!(response.status().as_u16(), 400);
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue