chakram

Chakram multipart file upload with authentication

Chakram has a lot of examples of using JSON requests, however I needed a working solution for chakram multipart file upload and authenticate using Authorization header.

TL;DR

Source code of a working solution containing a protected API and a sample chakram test is on my GitHub.

Introduction

Chakram is very flexible REST API testing framework, since you are technically programming all tests in JavaScript manually. Tests and assertion reminds me Jasmine, a generic unit testing framework, just Chakram solely focuses on API integration testing.

One technical challenge I’ve run into recently was that Chakram seems to be designed for API endpoints that use JSON content type. While JSON is now almost de facto standard for APIs communication, but I needed to upload a multipart file to an authenticated endpoint…

Web API with authentication

A simple .NET Core Web API exposing GET and POST endpoints protected by simple authentication from previous blog post. To test authentication make a GET request with Authorization header set to auth_key. Screenshot below.

chakram multipart file upload

And to test file upload make a form-data POST with a key of file and value any file. Keep the same Authorization header as in the GEST request set. Screenshot below.

chakram multipart file upload

Chakram multipart file upload

On the official documentation page there is nothing about file upload and post function signature and underlying code are following.

exports.post = function (url, data, params) {
    return exports.request('POST', url, extendWithData(data, params));
};

And after trying to pass in my test file as data, and authorize header as params I had to face a failure, API responded with 400 response code. The reason is in the extendWithData function, code below.

var extendWithData = function (data, params) {
    return extend({body: data}, params);
};

The data object goes into JSON object’s body field, and the request goes as a JSON request. However, my API is expecting multipart file upload… The solution is to ignore data, and pass everything into params, which is then passed untouched to the underlying Request module.

And the final working solution is below.

var requestObject = {
    'formData': {
        'file': fs.createReadStream('./package.json'),
    },
    'headers': {
        'Authorization': 'auth_key'
    }
}

// ...

chakram.post('http://localhost:5000/api/values', undefined, requestObject);

// ...

Get the full source code from my GitHub, and run the test with `npm test` in the command line — this returns the following result — success! Screenshot below.

chakram multipart file upload

Tips and tricks

Using Visual Studio Code you can run Web API in one console window, and open a second (or more!) for other operations like running integration tests from command line. This way you can do everything from the same VS Code window. See screenshot above.

Summary

Chakram is very easy to use for REST API endpoints using JSON requests. However, if you need to test a more advanced scenario like multipart file upload with authentication, you need to look into how to correctly pass the parameters down to the underlying Request module layer.