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

Request stash in hooks: Using data from previous responses #108

Closed
netmilk opened this issue Oct 11, 2014 · 8 comments
Closed

Request stash in hooks: Using data from previous responses #108

netmilk opened this issue Oct 11, 2014 · 8 comments

Comments

@netmilk
Copy link
Contributor

netmilk commented Oct 11, 2014

I’d like to present concept of stashing responses. It’s support for full entity life-cycle without use of fixtures accessing directly the database. You can transform a request in hooks with data from some previous response before its execution.

This solves situation where you create an entity, server returns unique ID and you want use this ID as a URI parameter in next action to retrieve the entity.

{before, after} = require 'hooks'

responseStash = {}

after "Machines > Machines collection > Create a Machine", (transaction) ->
  responseStash[transaction.name] = transaction.real

before "Machines > Machine > Retrieve a Machine", (transaction) ->
  stash = responseStash['Machines > Machines collection > Create a Machine']
  bodyObject = JSON.parse stash.body
  newId = bodyObject['_id']
  transaction.request.uri = transaction.request.uri.replace '52341870ed55224b15ff07ef', newId
  transaction.fullPath = transaction.fullPath.replace '52341870ed55224b15ff07ef', newId

I've created an example application in CI and a proposal for API in hooks.

@dbryand
Copy link

dbryand commented Nov 26, 2014

This is a great idea--thanks!

@rickke
Copy link

rickke commented Jan 15, 2015

Request stash is a great idea. But I do not think it could fix the issue in #96.

@netmilk
Copy link
Contributor Author

netmilk commented Jan 16, 2015

@rickke Why do you think it's not a usable workaround?

@rickke
Copy link

rickke commented Jan 16, 2015

@netmilk For my understanding, stashing response use after event to get server response from one transaction and modify the request info in another transaction use before event. Is it right? The id mentioned in #96 is from server side and nothing to do with client request. So there is no way to fix it with stashng response. Am I wrong? Thanks

@netmilk
Copy link
Contributor Author

netmilk commented Jan 16, 2015

@rickke You can modify both, the request and the expectation as well in the before hook.

But you are right that in blueprint example form #96 there is no previous request, where expected id of the entity can be retrieved from the server for later modification of the expected response. (for example in when creating entity in the collection). So this does not solves #96. Thanks for noticing that!

@pocketmax
Copy link

Sorry for my confusion but was this ever fixed?

@netmilk
Copy link
Contributor Author

netmilk commented Feb 17, 2015

@pocketmax Do you mean this issue or #96?

@pocketmax
Copy link

ug, sorry, yeah I mean #96

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

4 participants