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

Synchronised head methods #13

Open
julescmay opened this issue Mar 17, 2022 · 4 comments
Open

Synchronised head methods #13

julescmay opened this issue Mar 17, 2022 · 4 comments

Comments

@julescmay
Copy link

I'm working on a module which checks the heads of uris before deciding which to get. I've been able to create both the get and head interceptors in Nock, as follows:

nock.head ('hello.txt')..reply (200, null, headers {...});
nock.get ('hello.txt).. reply (200, "Hello world!", headers {...});

But I'm constantly forgetting to add the heads!

It would be great if I could either set a flag to auto-generate the heads (so I didn't need to specify them) or write a single rule to translate every head interceptor into the corresponding get.

Any ideas? Thanks.

@cah4a
Copy link
Owner

cah4a commented Mar 20, 2022

Hey @julescmay!

Thanks for the interesting question.

You are absolutely right that nock should support basic HTTP protocol features.
I'm sure that it shouldn't be done under the hood. Tests should always fully describe app behavior.

It could be done by adding Interceptor.withHead() method like so:

nock.get('/path')
    ..reply(200, "body", headers: {})
    ..withHead();

Currently, I don't have much time to do it by myself (I'm Ukrainian, don't have much free time for now).
If you have some time, I would really appreciate your PR.

@julescmay
Copy link
Author

I'm Ukrainian, don't have much free time for now

I understand. My heartfelt sympathies. Stay safe.

It could be done by adding Interceptor.withHead() method ...

IIUC http spec says that head should respond identically to get except for the lack of body. May I suggest: head response should be automatic unless:

  • there's a noHead() method on the interceptor;
  • there's another head interceptor on the same path.

If you have some time, I would really appreciate your PR...

Absolutely. Let me know your thoughts, and I'll get on it.

@cah4a
Copy link
Owner

cah4a commented Mar 22, 2022

My heartfelt sympathies. Stay safe.

Huge thanks for the kind words!

noHead() method on the interceptor;

It's clear that it will be more intuitive to use.
I still have a feeling that .withHead() is more controlled and like it more, but I'm ok with reverse ideology too.

there's another head interceptor on the same path.

Because there is no "priority" for the Interceptors, I have an idea to patch Registry.match method: If no interceptor is found and the requested method is HEAD try to find corresponding GET request and create a new HEAD Interceptor.

@julescmay
Copy link
Author

I have an idea to patch Registry.match method:

That was exactly my thinking.

I'm on it!

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

No branches or pull requests

2 participants