sway icon indicating copy to clipboard operation
sway copied to clipboard

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

Open osher opened this issue 9 years ago • 4 comments
trafficstars

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?

osher avatar Oct 20 '16 09:10 osher

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.

whitlockjc avatar Oct 20 '16 13:10 whitlockjc

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...

osher avatar Oct 23 '16 09:10 osher

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

whitlockjc avatar Oct 31 '16 15:10 whitlockjc

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.

whitlockjc avatar Jun 08 '18 14:06 whitlockjc