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
Add request assertion feature to server #882
Conversation
addon/request-assertion.js
Outdated
@@ -0,0 +1,90 @@ | |||
/* * RequestAssertion implements a simple DSL for asserting that specific requests were made. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure if RequestAssertion
is the best name since 'assertion' is a loaded term. Maybe something like RequestChecker
?
A few other things I'd like to do/consider/I missed:
|
The server tracks all handled requests and provides an API to allow users to check whether a request matching the given parameters has been made since the server was started. ```javascript server.received.get.to('/contacts', { name: 'Darth Vader' }) server.didNotReceive.post.to('any'); server.received.get.to(/\/secrets.*/); server.didNotReceive.request.to('/robot/5'); ``` There is also a getter for the last request made to the server: ```javascript server.lastRequest ```
4779275
to
1f0c920
Compare
Sorry about delay, hoping to get to this soon. Just glancing over, will folks want to assert against content type or other things? Should we
Don't have to change right now, just a quick thought. |
I like the idea. I'm on vacation until the 4th, so I won't be able to update the PR until then. Glad to hear any other feedback you may have when you get to it. Thanks! |
Few suggestions:
|
Great feedback -- I should have time to make those changes tomorrow evening. |
* server.received... returns the number of matching requests server.didNotReceive returns a boolean * Arbitrary FakeXHR attributes can be passed * Removed 'with' alias for 'to' * Make server.requests a public attribute * Removed special behavior when 'any' is passed to 'to'
6a1e8f9
to
1a279e3
Compare
All right, this is ready for another peek.
// Two GETs are made to '/contacts', then...
assert.equal(server.received.get.to('/contacts'), 2);
assert.ok(server.received.get.to('/contacts'));
assert.ok(server.didNotReceive.post.to('/contacts')); You can assert against any FakeXHR attributes: assert.ok(server.received.get.to('/contacts', { requestHeaders: { 'Foo': 'Bar' } })); Also made
I don't see how we could make that syntax work while still allowing further chaining with 'to'. That aside, I did have a mechanism for allowing any URLs but I removed it because I didn't like the API ( |
@samselikoff -- any further changes you'd like to see on this? I can write up some documentation once you give it the 👍 |
Closing this as stale, but keeping a reference to it in miragejs/miragejs#217 in case we want to reference these APIs. |
Implements #845
The server tracks all handled requests and provides an API to allow
users to check whether a request matching the given parameters has
been made since the server was started.
I ended up adding RegExp support mostly because it was simpler than adding support for paths like "/posts/:id".
There is also a getter for the last request made to the server:
Comments and suggestions are welcome!