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

Add call to sitespeed #3

Merged
merged 5 commits into from
Mar 13, 2020
Merged

Add call to sitespeed #3

merged 5 commits into from
Mar 13, 2020

Conversation

rmparr
Copy link
Contributor

@rmparr rmparr commented Mar 11, 2020

This is a PR for issue #1 .

I have written a function to call sitespeed.io and return either the results or the errors.

I have also updated the index.js route handler to confirm a URL was provided, and to asynchronously return either the errors or a success message from the call to sitespeed.

There is also a small (and long running) test on the callSitespeed function, but this can probably be improved upon.

Currently, this is writing the responses of the sitespeed call to the tmp directory. This can be changed, or we can write something to ship the data to another location.

This directory will hold the results of the sitespeed call.
@rmparr rmparr requested a review from mrchrisadams March 11, 2020 14:15
@rmparr rmparr force-pushed the add-call-to-sitespeed branch from f937cdb to 5d7f310 Compare March 11, 2020 14:33
Copy link

@mamhoff mamhoff left a comment

Choose a reason for hiding this comment

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

Hm, the last commit shows that this will be a very slow test. Can we mock out the call to sitespeed.io and only test the logic we add?

@@ -0,0 +1,7 @@
const callSitespeed = require('../src/callSitespeed');

test('the response is good', async () => {
Copy link

Choose a reason for hiding this comment

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

In the Ruby world, we have libraries for mocking out expensive external API calls. I believe this should be done here as well, otherwise this test suite will quickly become unmanageable.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's not a very pleasant test for sure. There is not much to test aside from the actual API call, though. Mocking it would basically involve setting errors = [], and then testing if errors.length = 0.

What I think would be a reasonable solution would be to move the test out of the actual function and into an expected long-running integration test. The integration test would still take 20-30 seconds.

Copy link
Member

@mrchrisadams mrchrisadams left a comment

Choose a reason for hiding this comment

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

Ryan, this is looking good, and just a couple changes are needed before I'd merge it in - thanks!

There's a comment from Martin about testing, and mocking the call out to sitespeed.

While it's useful for now as a starting point , jest supports mocking quite well, and I'm happy to chat about in more detail.

👍

https://jestjs.io/docs/en/manual-mocks

src/index.js Outdated
const msg = 'no Pub/Sub message received';
console.error(`error: ${msg}`);
return h.response(`Bad Request: ${msg}`).code(400)
if (!url) {
Copy link
Member

Choose a reason for hiding this comment

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

We're not doing any checking about whether this is a valid url at the mo - would you please have a go using Joi?

I think there's an example in the code showing how to use Joi, the validation library. Can you please look over this and make this reject a request if we don't have a valid url passed in the POST body/payload?

This tutorial should help.

https://hapi.dev/tutorials/validation/?lang=en_US

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 opted for a small function with a regex. Aiming to limit the dependencies as much as possible, this was 4 lines vs 1.7mb.

This function is writing to the tmp directory, which will have to be rsynced to a remote directory.
@rmparr rmparr force-pushed the add-call-to-sitespeed branch from 5d7f310 to 41ccbc3 Compare March 13, 2020 10:46
rmparr added 3 commits March 13, 2020 11:49
The handler now sends an async call to the callSitespeed function, returning either a list of errors or a success message.

The handler now also validates a correctly formed URL is passed in.

The entire server is exported so it can be tested.
The tests confirm bad requests return failures, and that a good request should return success.
@rmparr rmparr force-pushed the add-call-to-sitespeed branch from 41ccbc3 to 6ff9532 Compare March 13, 2020 10:49
@rmparr rmparr requested a review from mrchrisadams March 13, 2020 10:51
@mrchrisadams
Copy link
Member

Hi Ryan, this is looking good. I'm happy to merge this in, as further feedback about coding styles and so on after likely best done when we have some explicit guidelines to refer to, to stop them being too subjective :)

Nice work 👍

@mrchrisadams mrchrisadams merged commit 20bbcf8 into master Mar 13, 2020
@rmparr rmparr deleted the add-call-to-sitespeed branch March 13, 2020 11:24
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.

3 participants