Pavlo
Pavlo

Reputation: 44957

How to respond with request body using sinon.fakeServer?

Sinon documentation states that it's possible to access the request object:

server.respondWith(response);

[...]

When the response is a Function, it will be passed the request object. You must manually call respond on it to complete the request.

But the naive approach doesn't seem to work:

const server = sinon.server.create();

server.respondWith(request => request.requestBody);

(Throws response as an error in my Mocha suite).

Upvotes: 2

Views: 563

Answers (1)

Srdjan Dejanovic
Srdjan Dejanovic

Reputation: 118

You need to add server.respond();. After that you will have server.requests object.
For example in qunit:

server.respond([200, { "Content-type": "application/json" }, "OK"]);
assert.ok(server.requests.length > 0, "Response received");
assert.ok(server.requests[0].status == 200, "Status is 200");

Upvotes: 1

Related Questions