javascriptrestjasminejasmine-nodefrisby.js

How to verify if either 200 or 201 status code in Frisby Test


I'm using frisby v0.8.5 and jasimine-node. With frisby there's the following to assert for expected return http status code:

 f = frisby.create(/*stuff....*/);
 ...
 ... 
 f.expectStatus(200);
 f.toss();

This works fine if the endpoint has a single expected status, but in some cases (like a POST endpoint) you might get back a 201 for created or a 200 if the resource is already present.

Is there any way in frisby to check for on of several acceptable response status codes?


Solution

  • Ok, found an easy way to do this using the after() and native jasmine matching functionaly.

    The general idea is since Frisby is asynchronous, you need to check the result after it's been tossed and caught. Inside the raw result, you can manually verify the status code against a regular expression, which in this case is either 200 or 201:

     f = frisby.create(/*stuff....*/);
     // ...
     // ... 
    
     f.after( function(err, res, body) {
    
         //Look for either a 200 or 201
         expect(res.statusCode).toMatch(//20[0|1]/);
    
     });
    
     f.toss();