Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions codegens/postman-cli/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -217,16 +217,15 @@ function addFormDataBody (snippet, body, opts) {
}

if (data.type === 'file') {
const sanitizedSrc = sanitize(data.src, opts.trim, '"', true),
wrappedSrc = `@"${sanitizedSrc}"`,
finalSrc = sanitize(wrappedSrc, opts.trim, opts.quoteType, opts.quoteType === '"');
snippet += ` ${opts.quoteType}${sanitize(data.key, opts.trim, opts.quoteType)}=${finalSrc}`;
const sanitizedSrc = sanitize(data.src, opts.trim, opts.quoteType);
snippet += `${opts.indent}${form('-f', opts.format)} ` +
`${opts.quoteType}${sanitize(data.key, opts.trim, opts.quoteType)}=@${sanitizedSrc}`;
snippet += opts.quoteType;
}
else {
const sanitizedValue = sanitize(data.value, opts.trim, '"', true),
finalValue = sanitize(sanitizedValue, opts.trim, opts.quoteType, opts.quoteType === '"');
snippet += `${opts.indent} ${form('-f', opts.format)} ` +
snippet += `${opts.indent}${form('-f', opts.format)} ` +
`${opts.quoteType}${sanitize(data.key, opts.trim, opts.quoteType)}=${finalValue}`;
snippet += opts.quoteType;
}
Expand Down
51 changes: 48 additions & 3 deletions codegens/postman-cli/test/unit/convert.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -479,9 +479,54 @@ describe('postman-cli convert function', function () {
expect.fail(null, null, error);
}
expect(snippet).to.be.a('string');
expect(snippet).to.include('\'no file=@"/path/to/file"\'');
expect(snippet).to.include('\'no src=@"/path/to/file"\'');
expect(snippet).to.include('\'invalid src=@"/path/to/file"\'');
expect(snippet).to.include('--form \'no file=@/path/to/file\'');
expect(snippet).to.include('--form \'no src=@/path/to/file\'');
expect(snippet).to.include('--form \'invalid src=@/path/to/file\'');
});
});

it('should generate correct --form flag for each field in multipart/form-data with text and file', function () {
var request = new Request({
'method': 'POST',
'header': [],
'body': {
'mode': 'formdata',
'formdata': [
{
'key': 'textField',
'value': '123',
'type': 'text'
},
{
'key': 'fileField',
'value': '',
'type': 'file',
'src': '/path/to/document.pdf'
}
]
},
'url': {
'raw': 'https://postman-echo.com/post',
'protocol': 'https',
'host': [
'postman-echo',
'com'
],
'path': [
'post'
]
}
});
convert(request, {}, function (error, snippet) {
if (error) {
expect.fail(null, null, error);
}
expect(snippet).to.be.a('string');
// Each field should have its own --form flag
expect(snippet).to.include('--form \'textField=');
expect(snippet).to.include('--form \'fileField=@/path/to/document.pdf\'');
// File path should NOT have inner double quotes
expect(snippet).to.not.include('@"/path/to/document.pdf"');
});
});

Expand Down
Loading