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

What can I use to sample the headers I should expect from a server? #109

Open
osher opened this issue Oct 20, 2016 · 4 comments
Open

What can I use to sample the headers I should expect from a server? #109

osher opened this issue Oct 20, 2016 · 4 comments

Comments

@osher
Copy link

osher commented Oct 20, 2016

What can I use to sample the headers I should expect from a server?
The OpenApi spec allows definition of response headers.

I noted sway.Response#getSample() - which is cool - but it returns only body.
Is there an API to sample the headers object?

Full story is bellow to whoever is interested.

Any ideas?

Thanks,
-- Osher

Full Story

I'm working on a code generator for a spec-first process where
1 - specs are written by architects,
2 - generator uses specs to generate a working project with configs, controllers, unit tests, e2e test, etc - all tests should pass.
3 - developers replace mock replies with real implementation without breaking tests (and may be required to add tests of their own to the provided structure).

Concerning the controller implementation part, my target is to render a function like the example bellow.
(The tests part will need to use the same information, so it's relevant for tests too)

const SVC_HEADER = '[email protected]/' + require('os').getHostName() ;

exports.myCoolPath_GET = (req, res, next) => {
    //TODO: real implementation
    res.statusCode( 200 ); 
    res.headers({
       'content-type': 'application/json',
       'x-svc' : SVC_HEADER 
    });
    res.end( JSON.stringify({
       message: "lorem ipsulum"
    })
}
exports.myCoolPath_PUT = (req, res, next) => { ... }
exports.myCoolPath_DELETE = (req, res, next) => { ... }

So basically, I read the spec using sway on generation time and use a templating engine to get that done. Eventually architects and developers can replace values in the generated samples to make their case more concrete if they so wish.

I'm cool with the statusCode, and with the body.
What would you suggest me for handling the headers?

@whitlockjc
Copy link
Member

I thought about this before but forgot to implement it. I think it's a cool idea but it would likely be a breaking change so I'll have to schedule it accordingly. I'm going to be out of pocket until the 31st but I would love to talk about it more.

@osher
Copy link
Author

osher commented Oct 23, 2016

The change does not have to be breaking.
You can introduce new APIs:
#getSampleBody() - explicit name, alias to #getSample() with backward compatibility
#getSampleHeaders() - what I need
#getSampleResponse() - returns a single object with { statusCode, headers, body}
Then you can depricate #getSample() whenever you like and version-jump permits, or even rewire it to #getSampleResponse() if you're hot for that name.

I would also suggest
api.getOperation('/hello', 'GET').getSampleResponse(statusCode)
as shortcut for
api.getOperation('/hello', 'GET').getResponse(200).getSampleResponse()

You may want to go as far as
api.getSampleResponse('/hello', 'GET', 200)
but although this last variant looks elegant I don't see many people actually using it beside me and my code-generator...

@whitlockjc
Copy link
Member

I'm sure we can do it in a non-breaking approach too. Thanks for the ideas.

@whitlockjc
Copy link
Member

Since the next release of sway will be a breaking one, I'm going to update Request#getSample and Response#getSample to return an object whose values are keys corresponding to in locations, each of which will be populated by parameters of that in and having sample values by name.

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