-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Question- How to write tests for file upload? #1543
Comments
It's not a problem with
As far as testing POSTs with binary data, I don't think |
@pity your inject call is missing the headers needed to know what file is being uploaded and how to process it. Make your request with curl -v and add the missing headers to the test. @papajuans the payload defaults are fine, no need to add that. |
@pity - Did you get this working? If you have an example I would love to see it. I'm having some trouble getting the headers correct with Server.inject |
@PeteOtto look at the tests in tests/payload.js for some examples. |
here is example:
My request was more complicated, I simplified during write of the post. I took original string from chrome from network tab (from headers of API request part Request Payload). But still I am not sure how the real content of file should be sent. Now I got my tests running, the file will be created where it should be. There is just one thing - the content of it is broken. I am not sure how to send it. |
I've gotten the multi-part form created with the boundaries and I can send a text file across. However, when I create try to send a PDF using the following logic the file that ends up in the handler is corrupted. Am I doing something wrong with how I add the file to the payload?
|
HTTP is limited character set and all binary content has to be encoded. |
I already played a lot with encodings etc. I know that I sent the data binary from my frontend. But not sure how I can make the tests work: var filePath = __dirname + '/fixtures/sir.png';
var file = fs.readFileSync(filePath);
var payload = [
'------WebKitFormBoundaryS4AeNzAzUP7OArMi',
'Content-Disposition: form-data; name="file"; filename="CIMG3456.png"',
'Content-Type: image/png',
'',
file,
'------WebKitFormBoundaryS4AeNzAzUP7OArMi--'
];
payload = payload.join('\r\n');
var binaryPayload = new Buffer(payload, 'binary'); I know that my "file" is already a Buffer, but not sure in which encoding. So not sure what happens if I join it with my other strings. I also tried to convert it to utf8 and then back to binary. None of these approaches creates a valid image in my testcase. How do you guys decode your upload content? @petermilan : you skipped the actual content of the file, how did u encode/decode in your testcase? |
@KeKs0r working example: https://gist.github.com/Couto/127ca8a6bd28ecc4a084 |
I got it to work this way: `it('should return results in bounds', function(done) {
|
This thread has been automatically locked due to inactivity. Please open a new issue for related bugs or questions following the new issue template instructions. |
Hi,
I am trying to create a test for API call with mandatory file parameter. When I try to do this using the real API call using following form:
It is working as expected, but if I try to write test for that, it is not working.
I am using following route in (routes.attachHandlers(server)):
And here is the test I trying to be succesfull:
In the response is filed following result:
It seems to be problem with serve.inject, I am not sure how to pass testing file into the inject method. If I send there buffer, inject method will somehow alter that. What is the proper format of file, which should be passed to inject method?
thanks
The text was updated successfully, but these errors were encountered: