Repository: postmanlabs/postman-code-generators
Pull request #37 (2019-07-03 07:54:22)

Fixed failing snippet generation for file body type
Codegens changed :

- nodejs-request
- powershell-restmethod



Patch:
diff --git a/codegens/nodejs-request/lib/parseRequest.js b/codegens/nodejs-request/lib/parseRequest.js
index 8654dcf..733c016 100644
--- a/codegens/nodejs-request/lib/parseRequest.js
+++ b/codegens/nodejs-request/lib/parseRequest.js
@@ -3,10 +3,10 @@ var _ = require('./lodash'),
     sanitize = require('./util').sanitize;
 
 /**
- * parses body of request when type of the request body is formdata or urlencoded and 
+ * parses body of request when type of the request body is formdata or urlencoded and
  * returns code snippet for nodejs to add body
  *
- * @param {Array<Object>} dataArray - array containing body elements of request 
+ * @param {Array<Object>} dataArray - array containing body elements of request
  * @param {String} indentString - string required for indentation
  * @param {Boolean} trimBody - indicates whether to trim body or not
  */
@@ -21,7 +21,7 @@ function extractFormData (dataArray, indentString, trimBody) {
         /* istanbul ignore next */
         if (item.type === 'file') {
             /**
-             * creating snippet to send file in nodejs request 
+             * creating snippet to send file in nodejs request
              * for example:
              *  'fieldname': {
              *      'value': fs.createStream('filename.ext'),
@@ -29,7 +29,7 @@ function extractFormData (dataArray, indentString, trimBody) {
              *          'filename': 'filename.ext',
              *          'contentType: null
              *          }
-             *      }   
+             *      }
              *  }
              */
             accumalator.push([
@@ -55,7 +55,7 @@ function extractFormData (dataArray, indentString, trimBody) {
 
 /**
  * Parses body object based on mode of body and returns code snippet
- * 
+ *
  * @param {Object} requestbody - json object for body of request
  * @param {String} indentString - string for indentation
  * @param {Boolean} trimBody - indicates whether to trim body fields or not
@@ -74,7 +74,7 @@ function parseBody (requestbody, indentString, trimBody) {
             /* istanbul ignore next */
             case 'file':
                 return 'formData: {\n' +
-                        extractFormData([{type: 'file', key: 'file', src: requestbody[requestbody.mode].src}]) +
+                        extractFormData(requestbody[requestbody.mode], indentString, trimBody) +
                         indentString + '}';
             default:
                 return '';
@@ -84,11 +84,11 @@ function parseBody (requestbody, indentString, trimBody) {
 }
 
 /**
- * parses header of request object and returns code snippet of nodejs request to add header 
- * 
+ * parses header of request object and returns code snippet of nodejs request to add header
+ *
  * @param {Object} request - Postman SDK request object
  * @param {String} indentString - indentation required in code snippet
- * @returns {String} - code snippet of nodejs request to add header 
+ * @returns {String} - code snippet of nodejs request to add header
  */
 function parseHeader (request, indentString) {
     var headerObject = request.getHeaders({enabled: true}),
diff --git a/codegens/powershell-restmethod/lib/index.js b/codegens/powershell-restmethod/lib/index.js
index d105c4d..effd002 100644
--- a/codegens/powershell-restmethod/lib/index.js
+++ b/codegens/powershell-restmethod/lib/index.js
@@ -71,14 +71,15 @@ function parseFileData (body, trim) {
     var bodySnippet = '$multipartContent = [System.Net.Http.MultipartFormDataContent]::new()\n';
     _.forEach(body, function (data) {
         if (!data.disabled) {
-            bodySnippet += '$multipartFile = "Path of your file here"\n' +
-            '$FileStream = [System.IO.FileStream]::new($multipartFile, [System.IO.FileMode]::Open)\n';
-            '$fileHeader = [System.Net.Http.Headers.ContentDispositionHeaderValue]::new("form-data")\n';
-            '$fileHeader.Name = "File header name"\n'`$fileHeader.FileName = "${sanitize(data.key, trim)}"\n`;
-            '$fileContent = [System.Net.Http.StreamContent]::new($FileStream)\n';
-            '$fileContent.Headers.ContentDisposition = $fileHeader\n';
+            bodySnippet += `$multipartFile = "${data.src}"\n` +
+            '$FileStream = [System.IO.FileStream]::new($multipartFile, [System.IO.FileMode]::Open)\n' +
+            '$fileHeader = [System.Net.Http.Headers.ContentDispositionHeaderValue]::new("form-data")\n' +
+            '$fileHeader.Name = "Form data field name"\n' +
+            `$fileHeader.FileName = "${sanitize(data.key, trim)}"\n` +
+            '$fileContent = [System.Net.Http.StreamContent]::new($FileStream)\n' +
+            '$fileContent.Headers.ContentDisposition = $fileHeader\n' +
             '$fileContent.Headers.ContentType = ' +
-            '[System.Net.Http.Headers.MediaTypeHeaderValue]::Parse("Content type of your file")\n';
+            '[System.Net.Http.Headers.MediaTypeHeaderValue]::Parse("Content-Type Header")\n' +
             '$multipartContent.Add($fileContent)\n\n';
         }
     });


Test patch:
diff --git a/codegens/nodejs-request/test/unit/snippet.test.js b/codegens/nodejs-request/test/unit/snippet.test.js
index a47643e..8fa0666 100644
--- a/codegens/nodejs-request/test/unit/snippet.test.js
+++ b/codegens/nodejs-request/test/unit/snippet.test.js
@@ -13,7 +13,7 @@ var expect = require('chai').expect,
 
 /**
  * compiles and runs codesnippet then compare it with newman output
- * 
+ *
  * @param {String} codeSnippet - code snippet that needed to run using java
  * @param {Object} collection - collection which will be run using newman
  * @param {Function} done - callback for async call from mocha
@@ -238,6 +238,31 @@ describe('nodejs-request convert function', function () {
             });
         });
 
+        it('should generate snippet for file body mode', function () {
+            request = new sdk.Request({
+                'url': 'https://echo.getpostman.com/post',
+                'method': 'POST',
+                'body': {
+                    'mode': 'file',
+                    'file': [
+                        {
+                            'key': 'fileName',
+                            'src': 'file',
+                            'type': 'file'
+                        }
+                    ]
+                }
+            });
+            options = { indentType: 'space', indentCount: 2 };
+            convert(request, options, function (error, snippet) {
+                if (error) {
+                    expect.fail(null, null, error);
+                }
+                expect(snippet).to.be.a('string');
+                expect(snippet).to.not.equal('');
+            });
+        });
+
         describe('getOptions function', function () {
 
             it('should return an array of specific options', function () {
diff --git a/codegens/powershell-restmethod/test/unit/convert.test.js b/codegens/powershell-restmethod/test/unit/convert.test.js
index 12a32b2..a320792 100644
--- a/codegens/powershell-restmethod/test/unit/convert.test.js
+++ b/codegens/powershell-restmethod/test/unit/convert.test.js
@@ -267,6 +267,31 @@ describe('Powershell-restmethod converter', function () {
                 expect(snippet).to.be.a('string');
             });
         });
+
+        it('should generate snippet for file body mode', function () {
+            request = new sdk.Request({
+                'url': 'https://echo.getpostman.com/post',
+                'method': 'POST',
+                'body': {
+                    'mode': 'file',
+                    'file': [
+                        {
+                            'key': 'fileName',
+                            'src': 'file',
+                            'type': 'file'
+                        }
+                    ]
+                }
+            });
+            options = { indentType: 'space', indentCount: 2 };
+            convert(request, options, function (error, snippet) {
+                if (error) {
+                    expect.fail(null, null, error);
+                }
+                expect(snippet).to.be.a('string');
+                expect(snippet).to.not.equal('');
+            });
+        });
     });
 
     describe('getOptions function', function () {