I simulated REST API by using combination of cucumberjs, phantomjs with setting proxy server to 127.0.0.1 and hooking a node.js process with http-proxy
and nock
there. CucumberJS is not the important part, you can write test scenario any way, the rest is the key to the simulation. It is able to mock simply by match-request-return-data, but you can as well filter by patterns and hook callback function to produce a reply, so you can simulate to any level of granularity that you need (in extreme ending with a full demo server, but you can do it incrementally).
It works nicely:
- Phantomjs request an URI.
- Request goes to proxy server on 127.0.0.1:port.
- Your node.js process proxies it transparently forward using
http-proxy
. So any "normal" loading (pages, images) works.
- If you choose to intercept some requests (API, mainly) you use
nock
for it.
In my scenario, I combined it with cucumber js tests in the same process, so it went as:
- Test runs.
- It sets up
nock
HTTP mocking for scenario it tests.
- It loads a page in phantomjs via Selenium protocol.
The rest is as shown earlier in this paragraph (that is, it is a bit of a cycle, I as test runner instruct phantomjs to load a page, which forward all requests back to me, and I forward them to the net; or intercept them if it is the tested API).