znsio / specmatic

Turn your contracts into executable specifications. Contract Driven Development - Collaboratively Design & Independently Deploy MicroServices & MicroFrontends.
https://specmatic.in
MIT License
250 stars 51 forks source link

Content substitution for passthrough responses #148

Open julianharty opened 3 years ago

julianharty commented 3 years ago

Is your feature request related to a problem? Please describe. I would appreciate being able to substitute parameters in responses from the server before forwarding the revised response to the caller. This would improve the potency of qontract for testing client software in particular, for instance to be able to modify the Time-To-Live (TTL) value a server returns which would then cause the client to send a request to refresh (extend) the TTL far sooner than waiting for the period set in the server's response.

As an example (based on https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cache-Control), if a server responds with Cache-Control: public, max-age=604800, immutable I would love to be able to specify a new value for the max-age parameter that would be applied in-situ.

Describe the solution you'd like There are various ways this could be specified, for instance a simplistic approach could be something along the lines of: var response = str.replace("max-age=604800", "max-age=120"); (bastardised JavaScript syntax).

This could be scoped, e.g.:

We could also consider a syntax where a formula is used to describe the substitution. For my immediate wishes this isn't necessary.

Describe alternatives you've considered In a magical world, qontract could include a plug-in architecture where we could create and use plugins to provide more flexibility and/or reduce the burden and complexity of qontract's core codebase.

We could alternately implement this function by modifying the server and/or the client however doing so materially changes the system we're testing, and sometimes the source code and/or access to configuration files may not be available. The approach proposed here would enable black-box protocol testing and evaluations.

Additional context Where substitution changes the content length and/or the response includes or is processed using a hash, then some additional processing may be needed for these circumstances.

julianharty commented 3 years ago

Perhaps https://qontract.run/documentation/suggestions.html would partly support some simple use cases, from reading it I can't work out whether it would enable these substitutions to be applied to responses.

Also, I realise I forgot to mention that I may want/need to be able to use relative substitutions, particularly for time/date values e.g. "valid-until="NOW() + MINUTES(5).

julianharty commented 3 years ago

Another clarification - I'm thinking of this for the passThrough mode of operation.

joelrosario commented 3 years ago

Noted. Thanks for the suggestion @julianharty !

To clarify, Suggestions is a way of externalising Gherkin examples. Qontract generates and http request for each of the provided examples in the Qontract Gherkin spec by combining it with the request definition in that scenario, then sends it to the service under test, and verifies the format of the response received in exchange.

This feature request is different. It would allow one to modify a proxied response in flight, using an expression, script or plugin.

Looks like we may need to work on the documentation for suggestions, so thanks for letting us know that you looked at it.