Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Testing file uploads in hapijs #70

Open
heron2014 opened this issue May 23, 2016 · 1 comment
Open

Testing file uploads in hapijs #70

heron2014 opened this issue May 23, 2016 · 1 comment
Labels

Comments

@heron2014
Copy link
Member

hapijs/hapi#1543
https://gist.github.com/Couto/127ca8a6bd28ecc4a084
https://www.npmjs.com/package/form-data

full example WIP

@heron2014 heron2014 added the Hapi label May 23, 2016
@heron2014
Copy link
Member Author

Example of how to test upload file to the Google Drive

var Code = require('code');
var Lab = require('lab');
var Server = require('../lib/index.js');

var lab = exports.lab = Lab.script();
var describe = lab.experiment;
var expect = Code.expect;
var it = lab.test;

var FormData = require('form-data');
var fs = require('fs');
var streamToPromise = require('stream-to-promise');
describe('/file/upload file, function () {

  it('Upload file', function (done) {

    //mock the upload of the file
    var nock = require('nock');
    nock('https://www.googleapis.com')
      .post('/upload/drive/v3/files?uploadType=multipart')
      .reply(200, {id: 'theIdOfTheFile'}); //in our example we reply the id from google drive

      var form = new FormData();
      form.append('id', '123'); // your form data
      form.append('selectedFile', fs.createReadStream('./test/fixtures/file.txt'));

    streamToPromise(form).then(function (payload) {
      Server.init(0, function (err, server) {

        expect(err).to.not.exist();

        var options = {
          method: "POST",
          url: "/file/upload",
          headers: form.getHeaders(),
          payload: payload,
          credentials: { id: "12", "name": "Some name", valid: true}
        };

        server.inject(options, function (res) {
          //redirect to the candidate page
          expect(res.statusCode).to.equal(302);
          expect(res.headers.location).to.equal('/candidate/123');
          server.stop(done);
        });
      });


    })


  });
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant