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 using the sdk-upload using Hapi's server.inject #37

Open
jay-meister opened this issue Dec 19, 2016 · 0 comments
Open

Testing using the sdk-upload using Hapi's server.inject #37

jay-meister opened this issue Dec 19, 2016 · 0 comments
Labels
enhancement New feature or enhancement of existing functionality help wanted If you can help make progress with this issue, please comment! question A question needs to be answered before progress can be made on this issue

Comments

@jay-meister
Copy link
Member

jay-meister commented Dec 19, 2016

We are not using nightwatch in our project at the moment, but the tests in the sdk-upload example are only done using nightwatch. It would be really helpful to see another way to test this upload method.

We are using Hapi's server.inject method and after a lot of playing around, eventually managed to upload a file; we only managed this with a .txt file. There are a couple of working examples linked at the bottom which may hold the solution.

Here is a link to the PR on my project that contains the image upload with a test that successfully uploads a file to our S3 bucket using server.inject.

I thought it would be useful to copy our method into an issue here, and we can develop it as and when we work out a better/more comprehensive way of testing

// our form has the inputs: name, file_name, mission_statement, logo
// logo represents the file, and file_name is the name of the file being uploaded
// name and mission statement are additional fields that our form needs to track
// at the moment, we are struggling to upload an image, so the logo is actually a .txt file
var payloadString = 
  '--AaB03x\r\n' +
  'content-disposition: form-data; name="name"\r\n' + // input on our form called `name`
  '\r\n' +
  'Apple\r\n' + // The value of the input `name` is "Apple"
  '--AaB03x\r\n' +
  'content-disposition: form-data; name="file_name"\r\n' + // input on our form called `file_name`
  '\r\n' +
  'foxy.txt\r\n' +  // The value of the input `file_name` is "foxy.txt"
  '--AaB03x\r\n' +
  'content-disposition: form-data; name="mission_statement"\r\n' + // input on our form called `mission_statement`
  '\r\n' +
  'Change the economy!\r\n' + // The value of the input `mission_statement` is "Change the economy!"
  '--AaB03x\r\n' +
  'content-disposition: form-data; name="logo"; filename="foxy.txt"\r\n' + // input on our form called `logo`
  'Content-Type: text/plain\r\n' + // content type of the file being sent in the form
  '\r\n' +
  'foxxxxyy\r\r\n' +  // contents of the file being sent in the form
  '--AaB03x--\r\n'


var addLogoOptions = {
    method: 'POST',
    url: '/add-image',
    headers: {
      'Content-Type': 'multipart/form-data; boundary=AaB03x' // notice this boundary repeated before/after each input in the payload string
    },
    payload: payloadString
  };
}


// the tape test
tape('/add-image route successfully adds an image to S3 --> ' + __filename, function (t) {
  server.inject(addLogoOptions, function (res) {
    t.equal(res.statusCode, 200, 'status code is 200 we can upload a file to S3');
    t.end();
  });
});

This test passes, the payload string was taken and adapted from this issue on the hapi repo

It looks like there might be a slightly simpler way of testing the file upload in this issue

Finally this is a working example of the potentially more simple way of solving this problem

@nelsonic nelsonic added enhancement New feature or enhancement of existing functionality help wanted If you can help make progress with this issue, please comment! question A question needs to be answered before progress can be made on this issue labels Dec 19, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or enhancement of existing functionality help wanted If you can help make progress with this issue, please comment! question A question needs to be answered before progress can be made on this issue
Projects
None yet
Development

No branches or pull requests

2 participants