4

I'm trying to test out my API using FrisbyJS (which sits on top of Jasmine for Node). I was wondering if anyone knew how to submit/send an image using Jasmine?

http://frisbyjs.com/

My current code is...

var frisby = require('frisby');

var URL = 'http://localhost/api';

frisby.globalSetup({
  request: {
  headers: {
   'Content-Type': 'application/x-www-form-urlencoded',
   'Accept': 'application/json',
   'api': 'key'
  }
 },
 timeout: (30 * 1000)
});

frisby.create('submit an image')
  .post(URL + 'image', {images: '@test.jpg'}, {method: 'POST'})
  .expectStatus(200)
  .afterJSON(function(cart){
    console.log('made it!');
  })
}).toss();

And I get the following error:

  1) Frisby Test: submit an image
[ POST http://localhost/api ]
  Message:
   timeout: timed out after 30000 msec waiting for HTTP Request timed out before completing
   Stacktrace:
    undefined

    Finished in 31.458 seconds

And yes, the image test.jpg does exist in the same folder :) as the spec file and where I'm executing the spec itself (jasmine-node .).

Siddharth Sharma
  • 1,653
  • 2
  • 19
  • 35
user766987
  • 692
  • 1
  • 8
  • 17

1 Answers1

4

I found a solution based on the example given at https://github.com/vlucas/frisby/blob/master/examples/httpbin_multipart_spec.js

var frisby = require('frisby');
var fs = require('fs');
var path = require('path');
var FormData = require('form-data');

var contentPath = path.resolve(__dirname, './path/to/image.jpg');    
var form = new FormData();

form.append('file', fs.createReadStream(contentPath), {
  knownLength: fs.statSync(contentPath).size
});

frisby.create('Create content asset')
  .post(apiPath + '/assets', form, {
     json: false,
     headers: {
       'content-type': 'multipart/form-data; boundary=' + form.getBoundary(),
       'content-length': form.getLengthSync(),
     }
})
.expectStatus(201)
.toss()
Alex
  • 1,664
  • 1
  • 11
  • 3