Skip to content

Commit 4e87ffb

Browse files
committed
Fixed failing snippet generation in nodejs-request and powershell for file body type
1 parent 6388bdf commit 4e87ffb

File tree

4 files changed

+69
-18
lines changed

4 files changed

+69
-18
lines changed

codegens/nodejs-request/lib/parseRequest.js

+9-9
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ var _ = require('./lodash'),
33
sanitize = require('./util').sanitize;
44

55
/**
6-
* parses body of request when type of the request body is formdata or urlencoded and
6+
* parses body of request when type of the request body is formdata or urlencoded and
77
* returns code snippet for nodejs to add body
88
*
9-
* @param {Array<Object>} dataArray - array containing body elements of request
9+
* @param {Array<Object>} dataArray - array containing body elements of request
1010
* @param {String} indentString - string required for indentation
1111
* @param {Boolean} trimBody - indicates whether to trim body or not
1212
*/
@@ -21,15 +21,15 @@ function extractFormData (dataArray, indentString, trimBody) {
2121
/* istanbul ignore next */
2222
if (item.type === 'file') {
2323
/**
24-
* creating snippet to send file in nodejs request
24+
* creating snippet to send file in nodejs request
2525
* for example:
2626
* 'fieldname': {
2727
* 'value': fs.createStream('filename.ext'),
2828
* 'options': {
2929
* 'filename': 'filename.ext',
3030
* 'contentType: null
3131
* }
32-
* }
32+
* }
3333
* }
3434
*/
3535
accumalator.push([
@@ -55,7 +55,7 @@ function extractFormData (dataArray, indentString, trimBody) {
5555

5656
/**
5757
* Parses body object based on mode of body and returns code snippet
58-
*
58+
*
5959
* @param {Object} requestbody - json object for body of request
6060
* @param {String} indentString - string for indentation
6161
* @param {Boolean} trimBody - indicates whether to trim body fields or not
@@ -74,7 +74,7 @@ function parseBody (requestbody, indentString, trimBody) {
7474
/* istanbul ignore next */
7575
case 'file':
7676
return 'formData: {\n' +
77-
extractFormData([{type: 'file', key: 'file', src: requestbody[requestbody.mode].src}]) +
77+
extractFormData(requestbody[requestbody.mode], indentString, trimBody) +
7878
indentString + '}';
7979
default:
8080
return '';
@@ -84,11 +84,11 @@ function parseBody (requestbody, indentString, trimBody) {
8484
}
8585

8686
/**
87-
* parses header of request object and returns code snippet of nodejs request to add header
88-
*
87+
* parses header of request object and returns code snippet of nodejs request to add header
88+
*
8989
* @param {Object} request - Postman SDK request object
9090
* @param {String} indentString - indentation required in code snippet
91-
* @returns {String} - code snippet of nodejs request to add header
91+
* @returns {String} - code snippet of nodejs request to add header
9292
*/
9393
function parseHeader (request, indentString) {
9494
var headerObject = request.getHeaders({enabled: true}),

codegens/nodejs-request/test/unit/snippet.test.js

+26-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ var expect = require('chai').expect,
1313

1414
/**
1515
* compiles and runs codesnippet then compare it with newman output
16-
*
16+
*
1717
* @param {String} codeSnippet - code snippet that needed to run using java
1818
* @param {Object} collection - collection which will be run using newman
1919
* @param {Function} done - callback for async call from mocha
@@ -238,6 +238,31 @@ describe('nodejs-request convert function', function () {
238238
});
239239
});
240240

241+
it('should generate snippet for file body mode', function () {
242+
request = new sdk.Request({
243+
'url': 'https://echo.getpostman.com/post',
244+
'method': 'POST',
245+
'body': {
246+
'mode': 'file',
247+
'file': [
248+
{
249+
'key': 'fileName',
250+
'src': 'file',
251+
'type': 'file'
252+
}
253+
]
254+
}
255+
});
256+
options = { indentType: 'space', indentCount: 2 };
257+
convert(request, options, function (error, snippet) {
258+
if (error) {
259+
expect.fail(null, null, error);
260+
}
261+
expect(snippet).to.be.a('string');
262+
expect(snippet).to.not.equal('');
263+
});
264+
});
265+
241266
describe('getOptions function', function () {
242267

243268
it('should return an array of specific options', function () {

codegens/powershell-restmethod/lib/index.js

+8-7
Original file line numberDiff line numberDiff line change
@@ -71,14 +71,15 @@ function parseFileData (body, trim) {
7171
var bodySnippet = '$multipartContent = [System.Net.Http.MultipartFormDataContent]::new()\n';
7272
_.forEach(body, function (data) {
7373
if (!data.disabled) {
74-
bodySnippet += '$multipartFile = "Path of your file here"\n' +
75-
'$FileStream = [System.IO.FileStream]::new($multipartFile, [System.IO.FileMode]::Open)\n';
76-
'$fileHeader = [System.Net.Http.Headers.ContentDispositionHeaderValue]::new("form-data")\n';
77-
'$fileHeader.Name = "File header name"\n'`$fileHeader.FileName = "${sanitize(data.key, trim)}"\n`;
78-
'$fileContent = [System.Net.Http.StreamContent]::new($FileStream)\n';
79-
'$fileContent.Headers.ContentDisposition = $fileHeader\n';
74+
bodySnippet += `$multipartFile = "${data.src}"\n` +
75+
'$FileStream = [System.IO.FileStream]::new($multipartFile, [System.IO.FileMode]::Open)\n' +
76+
'$fileHeader = [System.Net.Http.Headers.ContentDispositionHeaderValue]::new("form-data")\n' +
77+
'$fileHeader.Name = "File header name"\n' +
78+
`$fileHeader.FileName = "${sanitize(data.key, trim)}"\n` +
79+
'$fileContent = [System.Net.Http.StreamContent]::new($FileStream)\n' +
80+
'$fileContent.Headers.ContentDisposition = $fileHeader\n' +
8081
'$fileContent.Headers.ContentType = ' +
81-
'[System.Net.Http.Headers.MediaTypeHeaderValue]::Parse("Content type of your file")\n';
82+
'[System.Net.Http.Headers.MediaTypeHeaderValue]::Parse("Content type of your file")\n' +
8283
'$multipartContent.Add($fileContent)\n\n';
8384
}
8485
});

codegens/powershell-restmethod/test/unit/convert.test.js

+26-1
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ describe('Powershell-restmethod converter', function () {
115115
describe('convert for different request types', function () {
116116
mainCollection.item.forEach(function (item) {
117117
// Skipping tests for Travis CI, till powershell dependency issue is sorted on travis
118-
it.skip(item.name, function (done) {
118+
it(item.name, function (done) {
119119
var request = new sdk.Request(item.request),
120120
collection = {
121121
item: [
@@ -267,6 +267,31 @@ describe('Powershell-restmethod converter', function () {
267267
expect(snippet).to.be.a('string');
268268
});
269269
});
270+
271+
it('should generate snippet for file body mode', function () {
272+
request = new sdk.Request({
273+
'url': 'https://echo.getpostman.com/post',
274+
'method': 'POST',
275+
'body': {
276+
'mode': 'file',
277+
'file': [
278+
{
279+
'key': 'fileName',
280+
'src': 'file',
281+
'type': 'file'
282+
}
283+
]
284+
}
285+
});
286+
options = { indentType: 'space', indentCount: 2 };
287+
convert(request, options, function (error, snippet) {
288+
if (error) {
289+
expect.fail(null, null, error);
290+
}
291+
expect(snippet).to.be.a('string');
292+
expect(snippet).to.not.equal('');
293+
});
294+
});
270295
});
271296

272297
describe('getOptions function', function () {

0 commit comments

Comments
 (0)