Testing using the sdk-upload using Hapi's server.inject
- Dominant language
- Elixir
- Stars
- 108
- Forks
- 20
- Avg merge
- 21h 46m
- Merged PRs (30d)
- 4
Description
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](https://github.com/emfoundation/ce100-app/pull/496/files) 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
```js
// 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](https://github.com/hapijs/hapi/issues/1543#issuecomment-40427657) on the hapi repo
It looks like there might be a slightly simpler way of testing the file upload in [this issue](https://github.com/hapijs/discuss/issues/77)
Finally [this is a working example](https://gist.github.com/Couto/127ca8a6bd28ecc4a084) of the potentially more simple way of solving this problem
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.