Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Router should respond with subscription-protocol header for callback #3939

Merged
merged 6 commits into from
Oct 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changesets/maint_bnjjj_fix_3929.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
### Router should respond with subscription-protocol header for callback ([Issue #3929](https://github.com/apollographql/router/issues/3929))

Callback protocol documentation specifies that router responds with `subscription-protocol: callback/1.0` header to the initialization (check) message. Currently router does not set this header on the response.

By [@bnjjj](https://github.com/bnjjj) in https://github.com/apollographql/router/pull/3939
18 changes: 18 additions & 0 deletions apollo-router/src/plugins/subscription.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ use bytes::Buf;
use futures::future::BoxFuture;
use hmac::Hmac;
use hmac::Mac;
use http::HeaderName;
use http::HeaderValue;
use http::Method;
use http::StatusCode;
use multimap::MultiMap;
Expand Down Expand Up @@ -46,6 +48,8 @@ pub(crate) const APOLLO_SUBSCRIPTION_PLUGIN_NAME: &str = "subscription";
pub(crate) static SUBSCRIPTION_CALLBACK_HMAC_KEY: OnceCell<String> = OnceCell::new();
pub(crate) const SUBSCRIPTION_WS_CUSTOM_CONNECTION_PARAMS: &str =
"apollo.subscription.custom_connection_params";
const CALLBACK_SUBSCRIPTION_HEADER_NAME: &str = "subscription-protocol";
const CALLBACK_SUBSCRIPTION_HEADER_VALUE: &str = "callback/1.0";

#[derive(Debug, Clone)]
pub(crate) struct Subscription {
Expand Down Expand Up @@ -464,6 +468,7 @@ impl Service<router::Request> for CallbackService {
Ok(router::Response {
response: http::Response::builder()
.status(StatusCode::NO_CONTENT)
.header(HeaderName::from_static(CALLBACK_SUBSCRIPTION_HEADER_NAME), HeaderValue::from_static(CALLBACK_SUBSCRIPTION_HEADER_VALUE))
.body::<hyper::Body>("".into())
.map_err(BoxError::from)?,
context: req.context,
Expand All @@ -472,6 +477,7 @@ impl Service<router::Request> for CallbackService {
Ok(router::Response {
response: http::Response::builder()
.status(StatusCode::NOT_FOUND)
.header(HeaderName::from_static(CALLBACK_SUBSCRIPTION_HEADER_NAME), HeaderValue::from_static(CALLBACK_SUBSCRIPTION_HEADER_VALUE))
.body("suscription doesn't exist".into())
.map_err(BoxError::from)?,
context: req.context,
Expand Down Expand Up @@ -694,6 +700,12 @@ mod tests {
.unwrap();
let resp = web_endpoint.clone().oneshot(http_req).await.unwrap();
assert_eq!(resp.status(), http::StatusCode::NO_CONTENT);
assert_eq!(
resp.headers()
.get(HeaderName::from_static(CALLBACK_SUBSCRIPTION_HEADER_NAME))
.unwrap(),
HeaderValue::from_static(CALLBACK_SUBSCRIPTION_HEADER_VALUE)
);

let http_req = http::Request::post(format!(
"http://localhost:4000/subscription/callback/{new_sub_id}"
Expand Down Expand Up @@ -912,6 +924,12 @@ mod tests {
.unwrap();
let resp = web_endpoint.clone().oneshot(http_req).await.unwrap();
assert_eq!(resp.status(), http::StatusCode::NO_CONTENT);
assert_eq!(
resp.headers()
.get(HeaderName::from_static(CALLBACK_SUBSCRIPTION_HEADER_NAME))
.unwrap(),
HeaderValue::from_static(CALLBACK_SUBSCRIPTION_HEADER_VALUE)
);

let http_req = http::Request::post(format!(
"http://localhost:4000/subscription/callback/{new_sub_id}"
Expand Down