Skip to content
This repository has been archived by the owner on Nov 8, 2024. It is now read-only.

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 subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants