Api calls getting redirected (Moved Temporarily) using Ember-cli, ember test --server, and http-mock

I have some http-mock request/responses I’d like to use only in a testing environment. Our API is completely built out, so I’d only like to use the mocks when running ‘ember test --server’ or ‘ember test’ (CI mode). I’m running into a few issues setting this up and was hoping someone had a solution.

When I run 'ember test --server ', tests run at an url that’s like http://localhost:7357/7150/tests/index.html

However, any time a request to the backend is made I get a 302 Moved Temporarily response. For example, when the client does a GET to what I would expect would be my data endpoint url in this testing mode: http://localhost:7357/brands/1, I get the 302 response and the url is redirected to http://localhost:7357/brands/1/test/index.html (which does not exists)

the mock works fine when I run tests in a browser using port 4200, ie

I think I’m missing some magic configuration somewhere. Anyone know how I can get my ‘ember test --server’ to utilize my mocked out backend using ember-cli’s http-mocks?

I’m getting the same, did you have any luck working this out?

This SO post had the answer: javascript - How to get test to access ember-cli server when run in CI mode? - Stack Overflow

And this commit demonstrates it: enabling CORS in the test server to allow the CI tests to hit it · voluspa/vilive@948962e · GitHub

You need to tell ember test to hit a running Ember server (which you have made accept CORS), which proxies the request to your test server (e.g. Rails).

Or you could get your test server to accept CORS directly instead.

1 Like

thanks. we ended up using Pretender to always stub out api calls in our tests instead

Have a look at https://github.com/samselikoff/ember-cli-mirage/.

We were also using http-mock + Pretender combo earlier. Recently moved to ember-cli-mirage, which works flawlessly and gives you best of both.