Dredd - How to modify test result based on http status code?

380 Views Asked by At

I am developing an API, using apiary.io and Swagger. I want to test the API using dredd, and for normal API responses (HTTP status code 200) this is fine. However, when the API returns different status code based on parameters (e.g. 403), dredd reports the test as failed. I would like to mark the test as successful, with an optional logging message.

I've tried this:

hooks.afterEach(function(transaction){
    if (transaction.real){
        switch (transaction.real.statusCode){
            case "400":
            case "403":
            case "406":
                transaction.fail=false;
                hooks.log('Failed, but expected by the API');
        }
    }
});

However this doesn't work, the test is still marked as failed. What am I missing?

1

There are 1 best solutions below

0
On

Is your API description document in OAS3? You might be experiencing this issue. Dredd should be marking the non-2xx cases as skipped automatically in case of OAS2, but it is not doing this yet for OAS3, support for that format is still experimental.

A workaround would be to set the transaction as skipped yourself in a before hook:

hooks.beforeEach(function(transaction){
    if (transaction.real){
        switch (transaction.real.statusCode){
            case "400":
            case "403":
            case "406":
                transaction.skip = true;
        }
    }
});