Skip to content

Conversation

garypen
Copy link
Contributor

@garypen garypen commented Dec 8, 2021

Provide a health check endpoint which behaves in the same way as the
health check on the gateway.

https://github.com/apollographql/apollo-server/blob/main/packages/apollo-server-express/src/ApolloServer.ts#L85

At this point in time it always returns pass because we don't have a
different health status to set.

resolves: #54

Provide a health check endpoint which behaves in the same way as the
health check on the gateway.

At this point in time it always returns pass because we don't have a
different health status to set.
Copy link
Contributor

@o0Ignition0o o0Ignition0o left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Do we need to set headers such as content type or something?

Add test for content-type and ensure it is application/json.
@garypen
Copy link
Contributor Author

garypen commented Dec 9, 2021

The json() method adds the content-type header for you. I've added to the test to make sure this is happening. See: cc097df

@garypen garypen requested a review from o0Ignition0o December 9, 2021 09:52
Comment on lines +240 to +243
let mut result = HashMap::new();
result.insert("status", "pass");
let reply = Box::new(warp::reply::json(&result)) as Box<dyn Reply>;
Ok::<_, Rejection>(reply)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nit] You can move this allocation to a static Lazy

Add snapshots for expected responses and separate tests into header
check and body check.
@garypen garypen merged commit 7afbd7d into main Dec 9, 2021
@garypen garypen deleted the health-check branch December 9, 2021 11:17
Comment on lines +715 to +723
let res = warp::test::request()
.path("/.well-known/apollo/server-health")
.reply(&filter)
.await;

let hdrs = res.headers();

assert_eq!(res.status(), 200);
assert_snapshot!(hdrs["content-type"].to_str().unwrap());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nit] Try using assert_debug_snapshot on the entire res object. Much easier!!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I only want to verify the content-type header is in place. If there is a different (new) header in the response, I don't want my test to start to fail.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's using warp::test::request() so I suspect it won't have too many changing headers. I have not tested though.

Even if something change in the future that make the response look different, the test will fail and display the difference and someone will just valid the new snapshot. No biggy.

}

#[test(tokio::test)]
async fn it_provides_health_status_header() -> Result<(), FederatedServerError> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nit] Never return errors in test! That's actually a common mistake.

Test in rust are processes that either succeed with an exit status 0 or fails with an exit status non-zero. Therefore all the standard assertion macros are based on panicking: assert_eq!() panics when the test fails.

Pancking in test == test failing, this is fine and it is even better to panic because you can get a stacktrace of where it
went wrong.

So my rule of thumb is:

  • use unwrap() a lot in test
  • never use ? and return a result (otherwise you lose the stacktrace)
  • use expect() if it the test doesnt give a useful information to the developer (in custom macros for example)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with this. I don't return errors from tests usually. I copied this from the existing tests and assumed it was the preferred approach. Your rules of thumb are the same as mine! :)

Comment on lines +705 to +706
assert_eq!(res.status(), 200);
assert_json_snapshot!(String::from_utf8_lossy(res.body()));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nit] Same here. You can probably assert the entire response using assert_debug_snapshot

Copy link
Member

@abernix abernix left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! Can we add some relic of this functionality to the documentation? Even just a placeholder will make it easier to write longer instructions later on.

@abernix
Copy link
Member

abernix commented Dec 10, 2021

I opened #260 to track the addition the documentation.

@cecton cecton mentioned this pull request Dec 13, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

HealthCheck Support
4 participants