Skip to content
This repository has been archived by the owner on Feb 22, 2019. It is now read-only.

Does not react to different request parameters #1

Open
lukesargeant opened this issue Jan 27, 2015 · 6 comments
Open

Does not react to different request parameters #1

lukesargeant opened this issue Jan 27, 2015 · 6 comments

Comments

@lukesargeant
Copy link

I've tried using this library but my authentication tests fail since they test the same end point with valid/invalid passwords yet this addon caches the first call to api/session. It would be great if there was a way to configure it to also respect the request parameters.

@andremalan
Copy link
Contributor

Hey, sorry I missed this earlier. I'm not sure how you would make things work to have different responses to the same request. The way that I have overcome it in my apps, has been to append an extra parameter to the different requests in test so that I can work with different responses (authentication/create/?username=foo&password=bar&error_test=true) or something like that.

@lukesargeant
Copy link
Author

I should have made it clear I'm doing a POST. Betamax will cache the login fail test and then when supplied correct login details it repeats the failure it has cached for the url.

@andremalan
Copy link
Contributor

Yeah, unfortunately the way things work, it expects all ajax calls to have the same response, depending on what is posted. If you post with different values (different username or password) does it not cache those as different requests?

@jamesarosen
Copy link

Sinon's fake server allows the response to be a function instead of an object. See the server.respondWith(response); section of the docs. I don't know how you might get Betamax to use that, though.

@andremalan
Copy link
Contributor

Yeah, part of the issue is that maybe there should be an option to map calls from a specific test to results. Right now it caches requests that are identical, but this messes with any case where your test might expect two identical requests to return two different results.

@jamesarosen
Copy link

For now, I'm using the Sinon server that Betamax exposes:

import { getServer } from '../../helpers/fake-server';

const passwordIsPassword = /^(.*&)?password=password(&.*)?$/;

getServer().respondWith('POST', '/sign-in', function(request) {
  if (passwordIsPassword.test(request.requestBody)) {
    request.respond(200, {}, JSON.stringify({ user: { id: 8 } }));
  } else {
    request.respond(403, {}, '');
  }
});

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

3 participants