diff --git a/__tests__/application_json.js b/__tests__/application_json.js new file mode 100644 index 0000000..731a5bf --- /dev/null +++ b/__tests__/application_json.js @@ -0,0 +1,12 @@ +const { encodeSample } = require('../src'); + +test('Test encodeSample for application/json', function() { + const jsonSample = { + name: 'Tom', + surname: 'Trailer', + age: 22 + }; + + const jsonEncoded = encodeSample(jsonSample, 'application/json', {}); + expect(jsonEncoded).toEqual(JSON.stringify(jsonSample)); +}); diff --git a/__tests__/application_x_www_form_urlencoded.js b/__tests__/application_x_www_form_urlencoded.js new file mode 100644 index 0000000..f45a4a7 --- /dev/null +++ b/__tests__/application_x_www_form_urlencoded.js @@ -0,0 +1,12 @@ +const { encodeSample } = require('../src'); +const querystring = require('querystring'); + +test('Test encodeSample for application/x-www-form-urlencoded', function() { + const querystringSample = { + name: 'Tom', + surname: 'Trailer', + age: 22 + }; + const queryStringEncoded = encodeSample(querystringSample, 'application/x-www-form-urlencoded', {}); + expect(queryStringEncoded).toEqual(querystring.stringify(querystringSample)); +}); diff --git a/__tests__/application_xml.js b/__tests__/application_xml.js new file mode 100644 index 0000000..d6af9d3 --- /dev/null +++ b/__tests__/application_xml.js @@ -0,0 +1,18 @@ +const { encodeSample } = require('../src'); +const { toXML } = require('jstoxml'); + +test('Test encodeSample for application/xml', function() { + const xmlSample = { + name: 'Tom', + surname: 'Trailer', + age: 22 + }; + + const xmlOptions = { + header: true, + indent: ' ' + }; + + const xmlEncoded = encodeSample(xmlSample, 'application/xml', {}); + expect(xmlEncoded).toEqual(toXML(xmlSample, xmlOptions)); +}); diff --git a/__tests__/image_sample.js b/__tests__/image_sample.js new file mode 100644 index 0000000..031036b --- /dev/null +++ b/__tests__/image_sample.js @@ -0,0 +1,9 @@ +const { encodeSample } = require('../src'); + +test('Test encodeSample for image/*', function() { + const pngSample = encodeSample({}, 'image/png', {}); + const jpgSample = encodeSample({}, 'image/jpg', {}); + + expect(atob(pngSample).includes('PNG')).toEqual(true); + expect(atob(jpgSample).includes('ÿØÿÛ')).toEqual(true); +}); diff --git a/__tests__/multipart_form_data.js b/__tests__/multipart_form_data.js new file mode 100644 index 0000000..a2a5a90 --- /dev/null +++ b/__tests__/multipart_form_data.js @@ -0,0 +1,58 @@ +const {encodeSample} = require('../src'); + +test('Test encodeSample for multipart/form-data', function () { + const multiPartFormData = { + name: 'Tom', + surname: 'Trailer', + image: 'image' + }; + + const content = { + encoding: { + image: { + contentType: 'image/png' + } + } + }; + + let encodedMultipart = '--956888039105887155673143\r\n'; + encodedMultipart += 'Content-Disposition: form-data; name="name"\r\n'; + encodedMultipart += 'Content-Type: text/plain\r\n\r\n'; + encodedMultipart += 'Tom\r\n'; + encodedMultipart += '--956888039105887155673143\r\n'; + encodedMultipart += 'Content-Disposition: form-data; name="surname"\r\n'; + encodedMultipart += 'Content-Type: text/plain\r\n\r\n'; + encodedMultipart += 'Trailer\r\n'; + encodedMultipart += '--956888039105887155673143\r\n'; + + const encodedMultipartExpected = encodedMultipart + + 'Content-Disposition: form-data; name="image"\r\n' + + 'Content-Type: text/plain\r\n\r\nimage\r\n--956888039105887155673143--'; + const encodedMultipartComplexExpected = encodedMultipart + + 'Content-Disposition: form-data; name="image"; filename="image"\r\n' + + 'Content-Type: image/png\r\n\r\niVBORw0KGgo=\r\n--956888039105887155673143--'; + + const multipartFormDataEncoded = encodeSample(multiPartFormData, 'multipart/form-data', {}); + const multipartFormDataComplexEncoded = encodeSample(multiPartFormData, 'multipart/form-data', content); + + expect(multipartFormDataEncoded).toEqual(encodedMultipartExpected); + expect(multipartFormDataComplexEncoded).toEqual(encodedMultipartComplexExpected); +}); + + +test('Test encodeSample for multipart/form-data', function () { + const multiPartFormData = { + person: { + name: 'John', + surname: 'Doe' + } + }; + let encodedMultipartExpected = '--956888039105887155673143\r\n'; + encodedMultipartExpected += 'Content-Disposition: form-data; name="person"\r\n'; + encodedMultipartExpected += 'Content-Type: application/json\r\n\r\n'; + encodedMultipartExpected += '{"name":"John","surname":"Doe"}\r\n--956888039105887155673143--'; + + const multipartFormDataEncoded = encodeSample(multiPartFormData, 'multipart/form-data', {}); + expect(multipartFormDataEncoded).toEqual(encodedMultipartExpected); + +}); diff --git a/__tests__/multipart_mixin.js b/__tests__/multipart_mixin.js new file mode 100644 index 0000000..31564b7 --- /dev/null +++ b/__tests__/multipart_mixin.js @@ -0,0 +1,49 @@ +const {encodeSample} = require('../src'); + +test('Test encodeSample for multipart/form-data', function () { + const multipartMixin = { + user: { + username: 'john', + password: 'password' + }, + token: 'user_token', + amount: 100, + buffer: 'base65' + }; + + const content = { + encoding: { + user: { + contentType: 'application/json' + }, + token: { + contentType: 'text/plain' + }, + buffer: { + contentType: 'application/octet-stream' + } + } + }; + + const multipartMixinExpected = '--956888039105887155673143\r\n' + + 'Content-Disposition: form-data; name="user"\r\n'+ + 'Content-Type: application/json\r\n\r\n'+ + '{"username":"john","password":"password"}\r\n'+ + '--956888039105887155673143\r\n'+ + 'Content-Disposition: form-data; name="token"\r\n'+ + 'Content-Type: text/plain\r\n\r\n'+ + 'dXNlcl90b2tlbg==\r\n'+ + '--956888039105887155673143\r\n'+ + 'Content-Disposition: form-data; name="amount"\r\n'+ + 'Content-Type: text/plain\r\n\r\n'+ + '100\r\n'+ + '--956888039105887155673143\r\n'+ + 'Content-Disposition: form-data; name="buffer"; filename="buffer"\r\n'+ + 'Content-Type: application/octet-stream\r\n\r\n'+ + 'YmFzZTY1\r\n'+ + '--956888039105887155673143--'; + const multipartMixinEncoded = encodeSample(multipartMixin, 'multipart/mixin', content); + + expect(multipartMixinEncoded).toEqual(multipartMixinExpected); + +}); diff --git a/__tests__/oas2har.js b/__tests__/oas2har.js index ef0329b..29c1526 100644 --- a/__tests__/oas2har.js +++ b/__tests__/oas2har.js @@ -1,21 +1,20 @@ -const { oasToHarList } = require('../src') -const githubSwagger = require('./github_swagger') +const { oasToHarList } = require('../src'); +const githubSwagger = require('./github_swagger'); test('GitHub swagger v2 JSON to HAR', async function() { - const [firstRequest] = await oasToHarList(githubSwagger) - const { har } = firstRequest - - expect(har.method).toEqual('GET') - expect(har.url).toEqual('https://api.github.com/emojis') - expect(har.httpVersion).toEqual('HTTP/1.1') -}) + const [firstRequest] = await oasToHarList(githubSwagger); + const { har } = firstRequest; + expect(har.method).toEqual('GET'); + expect(har.url).toEqual('https://api.github.com/emojis'); + expect(har.httpVersion).toEqual('HTTP/1.1'); +}); test('Petstore OpenApi v3 YAML to JSON converts to HAR', async function() { - const [firstRequest] = await oasToHarList(process.cwd() + '/__tests__/petstore_oas.yaml') - const { har } = firstRequest + const [firstRequest] = await oasToHarList(process.cwd() + '/__tests__/petstore_oas.yaml'); + const { har } = firstRequest; - expect(har.method).toEqual('PUT') - expect(har.url).toEqual('https://petstore.swagger.io/v2/pet') - expect(har.httpVersion).toEqual('HTTP/1.1') -}) + expect(har.method).toEqual('PUT'); + expect(har.url).toEqual('https://petstore.swagger.io/v2/pet'); + expect(har.httpVersion).toEqual('HTTP/1.1'); +}); diff --git a/__tests__/text_plain.js b/__tests__/text_plain.js new file mode 100644 index 0000000..aa2645c --- /dev/null +++ b/__tests__/text_plain.js @@ -0,0 +1,7 @@ +const { encodeSample } = require('../src'); + +test('Test encodeSample for text/plain', function() { + const primitiveSample = 'primitive'; + const primitiveEncoded = encodeSample(primitiveSample, '*/*', {}); + expect(primitiveEncoded).toEqual(Buffer.from(primitiveSample).toString('base64')); +}); diff --git a/package-lock.json b/package-lock.json index 2023e2e..fe1b489 100644 --- a/package-lock.json +++ b/package-lock.json @@ -838,9 +838,9 @@ } }, "@neuralegion/openapi-sampler": { - "version": "0.6.1", - "resolved": "https://registry.npmjs.org/@neuralegion/openapi-sampler/-/openapi-sampler-0.6.1.tgz", - "integrity": "sha512-I6g0/oI+YD13uNpNaAr/iLvdR+gbZMyTZXOJyE62sqkJYC/Ja1LCnel8Nz0i8vod9bNWKPqhbYVufj7R3gU78g==", + "version": "0.7.1", + "resolved": "https://registry.npmjs.org/@neuralegion/openapi-sampler/-/openapi-sampler-0.7.1.tgz", + "integrity": "sha512-jfbpWrgIkTfrSn0KC3lv88dMRgWzC+S74UvKZc+WsGV6JuSZKbBuGCoK12aFvAhUzoToMGIezsihtac8bxT6fA==", "requires": { "faker": "^4.1.0", "json-pointer": "^0.6.0", @@ -1008,15 +1008,15 @@ } }, "conventional-commits-parser": { - "version": "3.0.7", - "resolved": "https://registry.npmjs.org/conventional-commits-parser/-/conventional-commits-parser-3.0.7.tgz", - "integrity": "sha512-4mx/FRC92z0yIiXGyRVYQFhn0jWDwvxnj2UuLaUi3hJSG4Thall6GXA8YOPHQK2qvotciJandJIVmuSvLgDLbQ==", + "version": "3.0.8", + "resolved": "https://registry.npmjs.org/conventional-commits-parser/-/conventional-commits-parser-3.0.8.tgz", + "integrity": "sha512-YcBSGkZbYp7d+Cr3NWUeXbPDFUN6g3SaSIzOybi8bjHL5IJ5225OSCxJJ4LgziyEJ7AaJtE9L2/EU6H7Nt/DDQ==", "dev": true, "requires": { "JSONStream": "^1.0.4", "is-text-path": "^1.0.1", "lodash": "^4.17.15", - "meow": "^4.0.0", + "meow": "^5.0.0", "split2": "^2.0.0", "through2": "^3.0.0", "trim-off-newlines": "^1.0.0" @@ -1030,23 +1030,6 @@ } } }, - "meow": { - "version": "4.0.1", - "resolved": "https://registry.npmjs.org/meow/-/meow-4.0.1.tgz", - "integrity": "sha512-xcSBHD5Z86zaOc+781KrupuHAzeGXSLtiAOmBsiLDiPSaYSB6hdew2ng9EBAnZ62jagG9MHAOdxpDi/lWBFJ/A==", - "dev": true, - "requires": { - "camelcase-keys": "^4.0.0", - "decamelize-keys": "^1.0.0", - "loud-rejection": "^1.0.0", - "minimist": "^1.1.3", - "minimist-options": "^3.0.1", - "normalize-package-data": "^2.3.4", - "read-pkg-up": "^3.0.0", - "redent": "^2.0.0", - "trim-newlines": "^2.0.0" - } - }, "through2": { "version": "3.0.1", "resolved": "https://registry.npmjs.org/through2/-/through2-3.0.1.tgz", @@ -1165,9 +1148,9 @@ } }, "@semantic-release/release-notes-generator": { - "version": "7.3.2", - "resolved": "https://registry.npmjs.org/@semantic-release/release-notes-generator/-/release-notes-generator-7.3.2.tgz", - "integrity": "sha512-vYGydZPoQqL4aJOsaqXTZIekRb3aa/OlxlEVUvyrWWlNGqmQ1T7NUOos9eoN5DBCEuk6PwDrxPbhzgswxcvprQ==", + "version": "7.3.3", + "resolved": "https://registry.npmjs.org/@semantic-release/release-notes-generator/-/release-notes-generator-7.3.3.tgz", + "integrity": "sha512-uF7f9+epsxGNE39i0Ssu56V4w/xd3L4/yWw7FeAFp+SNLXPWTkTErdF2wwkVpxpRpCdgXsoj6T01tMN8tPfciQ==", "dev": true, "requires": { "conventional-changelog-angular": "^5.0.0", @@ -1193,15 +1176,15 @@ } }, "conventional-commits-parser": { - "version": "3.0.7", - "resolved": "https://registry.npmjs.org/conventional-commits-parser/-/conventional-commits-parser-3.0.7.tgz", - "integrity": "sha512-4mx/FRC92z0yIiXGyRVYQFhn0jWDwvxnj2UuLaUi3hJSG4Thall6GXA8YOPHQK2qvotciJandJIVmuSvLgDLbQ==", + "version": "3.0.8", + "resolved": "https://registry.npmjs.org/conventional-commits-parser/-/conventional-commits-parser-3.0.8.tgz", + "integrity": "sha512-YcBSGkZbYp7d+Cr3NWUeXbPDFUN6g3SaSIzOybi8bjHL5IJ5225OSCxJJ4LgziyEJ7AaJtE9L2/EU6H7Nt/DDQ==", "dev": true, "requires": { "JSONStream": "^1.0.4", "is-text-path": "^1.0.1", "lodash": "^4.17.15", - "meow": "^4.0.0", + "meow": "^5.0.0", "split2": "^2.0.0", "through2": "^3.0.0", "trim-off-newlines": "^1.0.0" @@ -1215,6 +1198,16 @@ } } }, + "find-up": { + "version": "4.1.0", + "resolved": "https://registry.npmjs.org/find-up/-/find-up-4.1.0.tgz", + "integrity": "sha512-PpOwAdQ/YlXQ2vj8a3h8IipDuYRi3wceVQQGYWxNINccq40Anw7BlsEXCMbt1Zt+OLA6Fq9suIpIWD0OsnISlw==", + "dev": true, + "requires": { + "locate-path": "^5.0.0", + "path-exists": "^4.0.0" + } + }, "locate-path": { "version": "5.0.0", "resolved": "https://registry.npmjs.org/locate-path/-/locate-path-5.0.0.tgz", @@ -1224,35 +1217,6 @@ "p-locate": "^4.1.0" } }, - "meow": { - "version": "4.0.1", - "resolved": "https://registry.npmjs.org/meow/-/meow-4.0.1.tgz", - "integrity": "sha512-xcSBHD5Z86zaOc+781KrupuHAzeGXSLtiAOmBsiLDiPSaYSB6hdew2ng9EBAnZ62jagG9MHAOdxpDi/lWBFJ/A==", - "dev": true, - "requires": { - "camelcase-keys": "^4.0.0", - "decamelize-keys": "^1.0.0", - "loud-rejection": "^1.0.0", - "minimist": "^1.1.3", - "minimist-options": "^3.0.1", - "normalize-package-data": "^2.3.4", - "read-pkg-up": "^3.0.0", - "redent": "^2.0.0", - "trim-newlines": "^2.0.0" - }, - "dependencies": { - "read-pkg-up": { - "version": "3.0.0", - "resolved": "https://registry.npmjs.org/read-pkg-up/-/read-pkg-up-3.0.0.tgz", - "integrity": "sha1-PtSWaF26D4/hGNBpHcUfSh/5bwc=", - "dev": true, - "requires": { - "find-up": "^2.0.0", - "read-pkg": "^3.0.0" - } - } - } - }, "p-limit": { "version": "2.2.1", "resolved": "https://registry.npmjs.org/p-limit/-/p-limit-2.2.1.tgz", @@ -1295,6 +1259,26 @@ "integrity": "sha512-ak9Qy5Q7jYb2Wwcey5Fpvg2KoAc/ZIhLSLOSBmRmygPsGwkVVt0fZa0qrtMz+m6tJTAHfZQ8FnmB4MG4LWy7/w==", "dev": true }, + "read-pkg": { + "version": "5.2.0", + "resolved": "https://registry.npmjs.org/read-pkg/-/read-pkg-5.2.0.tgz", + "integrity": "sha512-Ug69mNOpfvKDAc2Q8DRpMjjzdtrnv9HcSMX+4VsZxD1aZ6ZzrIE7rlzXBtWTyhULSMKg076AW6WR5iZpD0JiOg==", + "dev": true, + "requires": { + "@types/normalize-package-data": "^2.4.0", + "normalize-package-data": "^2.5.0", + "parse-json": "^5.0.0", + "type-fest": "^0.6.0" + }, + "dependencies": { + "type-fest": { + "version": "0.6.0", + "resolved": "https://registry.npmjs.org/type-fest/-/type-fest-0.6.0.tgz", + "integrity": "sha512-q+MB8nYR1KDLrgr4G5yemftpMC7/QLqVndBmEEdqzmNj5dcFOO4Oo8qlwZE3ULT3+Zim1F8Kq4cBnikNhlCMlg==", + "dev": true + } + } + }, "read-pkg-up": { "version": "7.0.0", "resolved": "https://registry.npmjs.org/read-pkg-up/-/read-pkg-up-7.0.0.tgz", @@ -1304,38 +1288,6 @@ "find-up": "^4.1.0", "read-pkg": "^5.2.0", "type-fest": "^0.8.1" - }, - "dependencies": { - "find-up": { - "version": "4.1.0", - "resolved": "https://registry.npmjs.org/find-up/-/find-up-4.1.0.tgz", - "integrity": "sha512-PpOwAdQ/YlXQ2vj8a3h8IipDuYRi3wceVQQGYWxNINccq40Anw7BlsEXCMbt1Zt+OLA6Fq9suIpIWD0OsnISlw==", - "dev": true, - "requires": { - "locate-path": "^5.0.0", - "path-exists": "^4.0.0" - } - }, - "read-pkg": { - "version": "5.2.0", - "resolved": "https://registry.npmjs.org/read-pkg/-/read-pkg-5.2.0.tgz", - "integrity": "sha512-Ug69mNOpfvKDAc2Q8DRpMjjzdtrnv9HcSMX+4VsZxD1aZ6ZzrIE7rlzXBtWTyhULSMKg076AW6WR5iZpD0JiOg==", - "dev": true, - "requires": { - "@types/normalize-package-data": "^2.4.0", - "normalize-package-data": "^2.5.0", - "parse-json": "^5.0.0", - "type-fest": "^0.6.0" - }, - "dependencies": { - "type-fest": { - "version": "0.6.0", - "resolved": "https://registry.npmjs.org/type-fest/-/type-fest-0.6.0.tgz", - "integrity": "sha512-q+MB8nYR1KDLrgr4G5yemftpMC7/QLqVndBmEEdqzmNj5dcFOO4Oo8qlwZE3ULT3+Zim1F8Kq4cBnikNhlCMlg==", - "dev": true - } - } - } } }, "through2": { @@ -1346,12 +1298,6 @@ "requires": { "readable-stream": "2 || 3" } - }, - "type-fest": { - "version": "0.8.1", - "resolved": "https://registry.npmjs.org/type-fest/-/type-fest-0.8.1.tgz", - "integrity": "sha512-4dbzIzqvjtgiM5rw1k5rEHtBANKmdudhGyBEajN01fEyhaAIhsoKNy6y7+IN93IfpFtwY9iqi7kD+xwKhQsNJA==", - "dev": true } } }, @@ -1396,6 +1342,12 @@ "@babel/types": "^7.3.0" } }, + "@types/color-name": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/@types/color-name/-/color-name-1.1.1.tgz", + "integrity": "sha512-rr+OQyAjxze7GgWrSaJwydHStIhHq2lvY3BOC2Mj7KnzI7XK0Uw1TOOdI9lDoajEbSWLiYgoo4f1R51erQfhPQ==", + "dev": true + }, "@types/events": { "version": "3.0.0", "resolved": "https://registry.npmjs.org/@types/events/-/events-3.0.0.tgz", @@ -1450,9 +1402,9 @@ "dev": true }, "@types/node": { - "version": "12.12.7", - "resolved": "https://registry.npmjs.org/@types/node/-/node-12.12.7.tgz", - "integrity": "sha512-E6Zn0rffhgd130zbCbAr/JdXfXkoOUFAKNs/rF8qnafSJ8KYaA/j3oz7dcwal+lYjLA7xvdd5J4wdYpCTlP8+w==", + "version": "12.12.9", + "resolved": "https://registry.npmjs.org/@types/node/-/node-12.12.9.tgz", + "integrity": "sha512-kV3w4KeLsRBW+O2rKhktBwENNJuqAUQHS3kf4ia2wIaF/MN6U7ANgTsx7tGremcA0Pk3Yh0Hl0iKiLPuBdIgmw==", "dev": true }, "@types/normalize-package-data": { @@ -1597,12 +1549,12 @@ } }, "ansi-escapes": { - "version": "4.2.1", - "resolved": "https://registry.npmjs.org/ansi-escapes/-/ansi-escapes-4.2.1.tgz", - "integrity": "sha512-Cg3ymMAdN10wOk/VYfLV7KCQyv7EDirJ64500sU7n9UlmioEtDuU5Gd+hj73hXSU/ex7tHJSssmyftDdkMLO8Q==", + "version": "4.3.0", + "resolved": "https://registry.npmjs.org/ansi-escapes/-/ansi-escapes-4.3.0.tgz", + "integrity": "sha512-EiYhwo0v255HUL6eDyuLrXEkTi7WwVCLAw+SeOQ7M7qdun1z1pum4DEm/nuqIVbPvi9RPPc9k9LbyBv6H0DwVg==", "dev": true, "requires": { - "type-fest": "^0.5.2" + "type-fest": "^0.8.1" } }, "ansi-regex": { @@ -2504,9 +2456,9 @@ } }, "conventional-changelog-writer": { - "version": "4.0.10", - "resolved": "https://registry.npmjs.org/conventional-changelog-writer/-/conventional-changelog-writer-4.0.10.tgz", - "integrity": "sha512-vtO9vBAVh7XnSpGLTB1BOGgsGTz1MdvFjzbSXLrtapWCHWwuVOZFgwdLhlS0MaXwlF1dksWdEb6tnr42Ie2INw==", + "version": "4.0.11", + "resolved": "https://registry.npmjs.org/conventional-changelog-writer/-/conventional-changelog-writer-4.0.11.tgz", + "integrity": "sha512-g81GQOR392I+57Cw3IyP1f+f42ME6aEkbR+L7v1FBBWolB0xkjKTeCWVguzRrp6UiT1O6gBpJbEy2eq7AnV1rw==", "dev": true, "requires": { "compare-func": "^1.3.1", @@ -2515,7 +2467,7 @@ "handlebars": "^4.4.0", "json-stringify-safe": "^5.0.1", "lodash": "^4.17.15", - "meow": "^4.0.0", + "meow": "^5.0.0", "semver": "^6.0.0", "split": "^1.0.0", "through2": "^3.0.0" @@ -2527,23 +2479,6 @@ "integrity": "sha512-8xOcRHvCjnocdS5cpwXQXVzmmh5e5+saE2QGoeQmbKmRS6J3VQppPOIt0MnmE+4xlZoumy0GPG0D0MVIQbNA1A==", "dev": true }, - "meow": { - "version": "4.0.1", - "resolved": "https://registry.npmjs.org/meow/-/meow-4.0.1.tgz", - "integrity": "sha512-xcSBHD5Z86zaOc+781KrupuHAzeGXSLtiAOmBsiLDiPSaYSB6hdew2ng9EBAnZ62jagG9MHAOdxpDi/lWBFJ/A==", - "dev": true, - "requires": { - "camelcase-keys": "^4.0.0", - "decamelize-keys": "^1.0.0", - "loud-rejection": "^1.0.0", - "minimist": "^1.1.3", - "minimist-options": "^3.0.1", - "normalize-package-data": "^2.3.4", - "read-pkg-up": "^3.0.0", - "redent": "^2.0.0", - "trim-newlines": "^2.0.0" - } - }, "through2": { "version": "3.0.1", "resolved": "https://registry.npmjs.org/through2/-/through2-3.0.1.tgz", @@ -4384,9 +4319,9 @@ "dev": true }, "handlebars": { - "version": "4.5.1", - "resolved": "https://registry.npmjs.org/handlebars/-/handlebars-4.5.1.tgz", - "integrity": "sha512-C29UoFzHe9yM61lOsIlCE5/mQVGrnIOrOq7maQl76L7tYPCgC1og0Ajt6uWnX4ZTxBPnjw+CUvawphwCfJgUnA==", + "version": "4.5.3", + "resolved": "https://registry.npmjs.org/handlebars/-/handlebars-4.5.3.tgz", + "integrity": "sha512-3yPecJoJHK/4c6aZhSvxOyG4vJKDshV36VHp0iVCDVh7o9w2vwi3NSnL2MMPj3YdduqaBcu7cGbggJQM0br9xA==", "dev": true, "requires": { "neo-async": "^2.6.0", @@ -4444,9 +4379,9 @@ "dev": true }, "has-symbols": { - "version": "1.0.0", - "resolved": "https://registry.npmjs.org/has-symbols/-/has-symbols-1.0.0.tgz", - "integrity": "sha1-uhqPGvKg/DllD1yFA2dwQSIGO0Q=", + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/has-symbols/-/has-symbols-1.0.1.tgz", + "integrity": "sha512-PLcsoqu++dmEIZB+6totNFKq/7Do+Z0u4oT0zKOJNl3lYK6vGwwu2hjHs+68OEZbTjiUE9bgOABXbP/GvrS0Kg==", "dev": true }, "has-value": { @@ -4588,9 +4523,9 @@ "dev": true }, "husky": { - "version": "3.0.9", - "resolved": "https://registry.npmjs.org/husky/-/husky-3.0.9.tgz", - "integrity": "sha512-Yolhupm7le2/MqC1VYLk/cNmYxsSsqKkTyBhzQHhPK1jFnC89mmmNVuGtLNabjDI6Aj8UNIr0KpRNuBkiC4+sg==", + "version": "3.1.0", + "resolved": "https://registry.npmjs.org/husky/-/husky-3.1.0.tgz", + "integrity": "sha512-FJkPoHHB+6s4a+jwPqBudBDvYZsoQW5/HBuMSehC8qDiCe50kpcxeqFoDSlow+9I6wg47YxBoT3WxaURlrDIIQ==", "dev": true, "requires": { "chalk": "^2.4.2", @@ -4748,9 +4683,9 @@ "dev": true }, "import-fresh": { - "version": "3.2.0", - "resolved": "https://registry.npmjs.org/import-fresh/-/import-fresh-3.2.0.tgz", - "integrity": "sha512-w1waegcRoBrRQ6kfX2+KWc3lIaVUvHiib7F1ZpggbL5+GyZZrpbOnOVNNuylJonNHO6aCJubjfDUzH6J8HmwSA==", + "version": "3.2.1", + "resolved": "https://registry.npmjs.org/import-fresh/-/import-fresh-3.2.1.tgz", + "integrity": "sha512-6e1q1cnWP2RXD9/keSkxHScg508CdXqXWgWBaETNhyuBFz+kUZlKboh+ISK+bU++DmbHimVBrOz/zzPe0sZ3sQ==", "dev": true, "requires": { "parent-module": "^1.0.0", @@ -6280,6 +6215,11 @@ "verror": "1.10.0" } }, + "jstoxml": { + "version": "1.6.0", + "resolved": "https://registry.npmjs.org/jstoxml/-/jstoxml-1.6.0.tgz", + "integrity": "sha512-vIq/Q1ats3TpMaqR6IhqFDRpMGLypCWsekHvsPGQhnkcicJZdpnPRLMFqXoefqajNsOQIv8RvW9U8WRj6M7bHQ==" + }, "kind-of": { "version": "6.0.2", "resolved": "https://registry.npmjs.org/kind-of/-/kind-of-6.0.2.tgz", @@ -6321,9 +6261,9 @@ "dev": true }, "lint-staged": { - "version": "9.4.2", - "resolved": "https://registry.npmjs.org/lint-staged/-/lint-staged-9.4.2.tgz", - "integrity": "sha512-OFyGokJSWTn2M6vngnlLXjaHhi8n83VIZZ5/1Z26SULRUWgR3ITWpAEQC9Pnm3MC/EpCxlwts/mQWDHNji2+zA==", + "version": "9.4.3", + "resolved": "https://registry.npmjs.org/lint-staged/-/lint-staged-9.4.3.tgz", + "integrity": "sha512-PejnI+rwOAmKAIO+5UuAZU9gxdej/ovSEOAY34yMfC3OS4Ac82vCBPzAWLReR9zCPOMqeVwQRaZ3bUBpAsaL2Q==", "dev": true, "requires": { "chalk": "^2.4.2", @@ -7175,9 +7115,9 @@ "dev": true }, "npm": { - "version": "6.13.0", - "resolved": "https://registry.npmjs.org/npm/-/npm-6.13.0.tgz", - "integrity": "sha512-zjSJ8zjk0cDBZXqTWbQ6+qOdm1m2k489YDFP60RQRUhOxT5LOBhl+cDtFlEXEIblcNjofmsZ/qQ/wzmn5frimQ==", + "version": "6.13.1", + "resolved": "https://registry.npmjs.org/npm/-/npm-6.13.1.tgz", + "integrity": "sha512-2awiDZ9JuV/UoF4oXGhekCURC2X+eLLRz9/e58AGrPDlpzyn7e4oCaZmkzyEaisxM7jSoFKNnZhzB4xbmbM0Yw==", "dev": true, "requires": { "JSONStream": "^1.3.5", @@ -8501,7 +8441,7 @@ } }, "https-proxy-agent": { - "version": "2.2.2", + "version": "2.2.4", "bundled": true, "dev": true, "requires": { @@ -9088,7 +9028,7 @@ } }, "make-fetch-happen": { - "version": "5.0.0", + "version": "5.0.2", "bundled": true, "dev": true, "requires": { @@ -9096,7 +9036,7 @@ "cacache": "^12.0.0", "http-cache-semantics": "^3.8.1", "http-proxy-agent": "^2.1.0", - "https-proxy-agent": "^2.2.1", + "https-proxy-agent": "^2.2.3", "lru-cache": "^5.1.1", "mississippi": "^3.0.0", "node-fetch-npm": "^2.0.2", @@ -10022,17 +9962,17 @@ "dev": true }, "smart-buffer": { - "version": "4.0.2", + "version": "4.1.0", "bundled": true, "dev": true }, "socks": { - "version": "2.3.2", + "version": "2.3.3", "bundled": true, "dev": true, "requires": { - "ip": "^1.1.5", - "smart-buffer": "4.0.2" + "ip": "1.1.5", + "smart-buffer": "^4.1.0" } }, "socks-proxy-agent": { @@ -11242,9 +11182,9 @@ } }, "react-is": { - "version": "16.11.0", - "resolved": "https://registry.npmjs.org/react-is/-/react-is-16.11.0.tgz", - "integrity": "sha512-gbBVYR2p8mnriqAwWx9LbuUrShnAuSCNnuPGyc7GJrMVQtPDAh8iLpv7FRuMPFb56KkaVZIYSz1PrjI9q0QPCw==", + "version": "16.12.0", + "resolved": "https://registry.npmjs.org/react-is/-/react-is-16.12.0.tgz", + "integrity": "sha512-rPCkf/mWBtKc97aLL9/txD8DZdemK0vkA3JMLShjlJB3Pj3s+lpf1KaBzMfQrAmhMQB0n1cU/SUGgKKBCe837Q==", "dev": true }, "read-pkg": { @@ -11818,9 +11758,9 @@ "dev": true }, "semantic-release": { - "version": "15.13.30", - "resolved": "https://registry.npmjs.org/semantic-release/-/semantic-release-15.13.30.tgz", - "integrity": "sha512-QetSFNr2hO6dZ/NDBh49neESF6OGinJfh4NcVhzMRg+rZpeqXq9sCbpZr0uDu+5HSkJYnX25MpMTF0QyyFhpxg==", + "version": "15.13.31", + "resolved": "https://registry.npmjs.org/semantic-release/-/semantic-release-15.13.31.tgz", + "integrity": "sha512-mrtYkH4p0FvXIRFCsr2r5il/A+Uj7oeeq+dgyojAbr4Tzywv9AlCYHeE3A8U3eE4bMJPiBV4YnQRsk1QS8yDDw==", "dev": true, "requires": { "@semantic-release/commit-analyzer": "^6.1.0", @@ -11848,15 +11788,51 @@ "resolve-from": "^5.0.0", "semver": "^6.0.0", "signale": "^1.2.1", - "yargs": "^14.0.0" + "yargs": "^15.0.1" }, "dependencies": { + "ansi-styles": { + "version": "4.2.0", + "resolved": "https://registry.npmjs.org/ansi-styles/-/ansi-styles-4.2.0.tgz", + "integrity": "sha512-7kFQgnEaMdRtwf6uSfUnVr9gSGC7faurn+J/Mv90/W+iTtN0405/nLdopfMWwchyxhbGYl6TC4Sccn9TUkGAgg==", + "dev": true, + "requires": { + "@types/color-name": "^1.1.1", + "color-convert": "^2.0.1" + } + }, "camelcase": { "version": "5.3.1", "resolved": "https://registry.npmjs.org/camelcase/-/camelcase-5.3.1.tgz", "integrity": "sha512-L28STB170nwWS63UjtlEOE3dldQApaJXZkOI1uMFfzf3rRuPegHaHesyee+YxQ+W6SvRDQV6UrdOdRiR153wJg==", "dev": true }, + "cliui": { + "version": "6.0.0", + "resolved": "https://registry.npmjs.org/cliui/-/cliui-6.0.0.tgz", + "integrity": "sha512-t6wbgtoCXvAzst7QgXxJYqPt0usEfbgQdftEPbLL/cvv6HPE5VgvqCuAIDR0NgU52ds6rFwqrgakNLrHEjCbrQ==", + "dev": true, + "requires": { + "string-width": "^4.2.0", + "strip-ansi": "^6.0.0", + "wrap-ansi": "^6.2.0" + } + }, + "color-convert": { + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/color-convert/-/color-convert-2.0.1.tgz", + "integrity": "sha512-RRECPsj7iu/xb5oKYcsFHSppFNnsj/52OVTRKb4zP5onXwVF3zVmmToNcOfGC+CRDpfK/U584fMg38ZHCaElKQ==", + "dev": true, + "requires": { + "color-name": "~1.1.4" + } + }, + "color-name": { + "version": "1.1.4", + "resolved": "https://registry.npmjs.org/color-name/-/color-name-1.1.4.tgz", + "integrity": "sha512-dOy+3AuW3a2wNbZHIuMZpTcgjGuLU/uBL/ubcZF9OXbDo8ff4O8yVp5Bf0efS8uEoYo5q4Fx7dY9OgQGXgAsQA==", + "dev": true + }, "cosmiconfig": { "version": "6.0.0", "resolved": "https://registry.npmjs.org/cosmiconfig/-/cosmiconfig-6.0.0.tgz", @@ -11870,12 +11846,6 @@ "yaml": "^1.7.2" } }, - "emoji-regex": { - "version": "7.0.3", - "resolved": "https://registry.npmjs.org/emoji-regex/-/emoji-regex-7.0.3.tgz", - "integrity": "sha512-CwBLREIQ7LvYFB0WyRvwhq5N5qPhc6PMjD6bYggFlI5YyDgl+0vxq5VHbMOFqLg7hfWzmu8T5Z1QofhmTIhItA==", - "dev": true - }, "find-up": { "version": "4.1.0", "resolved": "https://registry.npmjs.org/find-up/-/find-up-4.1.0.tgz", @@ -11895,12 +11865,6 @@ "lru-cache": "^5.1.1" } }, - "is-fullwidth-code-point": { - "version": "2.0.0", - "resolved": "https://registry.npmjs.org/is-fullwidth-code-point/-/is-fullwidth-code-point-2.0.0.tgz", - "integrity": "sha1-o7MKXE8ZkYMWeqq5O+764937ZU8=", - "dev": true - }, "locate-path": { "version": "5.0.0", "resolved": "https://registry.npmjs.org/locate-path/-/locate-path-5.0.0.tgz", @@ -11995,82 +11959,49 @@ "type-fest": "^0.8.1" } }, - "string-width": { - "version": "3.1.0", - "resolved": "https://registry.npmjs.org/string-width/-/string-width-3.1.0.tgz", - "integrity": "sha512-vafcv6KjVZKSgz06oM/H6GDBrAtz8vdhQakGjFIvNrHA6y3HCF1CInLy+QLq8dTJPQ1b+KDUqDFctkdRW44e1w==", + "strip-ansi": { + "version": "6.0.0", + "resolved": "https://registry.npmjs.org/strip-ansi/-/strip-ansi-6.0.0.tgz", + "integrity": "sha512-AuvKTrTfQNYNIctbR1K/YGTR1756GycPsg7b9bdV9Duqur4gv6aKqHXah67Z8ImS7WEz5QVcOtlfW2rZEugt6w==", "dev": true, "requires": { - "emoji-regex": "^7.0.1", - "is-fullwidth-code-point": "^2.0.0", - "strip-ansi": "^5.1.0" + "ansi-regex": "^5.0.0" } }, - "type-fest": { - "version": "0.8.1", - "resolved": "https://registry.npmjs.org/type-fest/-/type-fest-0.8.1.tgz", - "integrity": "sha512-4dbzIzqvjtgiM5rw1k5rEHtBANKmdudhGyBEajN01fEyhaAIhsoKNy6y7+IN93IfpFtwY9iqi7kD+xwKhQsNJA==", - "dev": true + "wrap-ansi": { + "version": "6.2.0", + "resolved": "https://registry.npmjs.org/wrap-ansi/-/wrap-ansi-6.2.0.tgz", + "integrity": "sha512-r6lPcBGxZXlIcymEu7InxDMhdW0KDxpLgoFLcguasxCaJ/SOIZwINatK9KY/tf+ZrlywOKU0UDj3ATXUBfxJXA==", + "dev": true, + "requires": { + "ansi-styles": "^4.0.0", + "string-width": "^4.1.0", + "strip-ansi": "^6.0.0" + } }, "yargs": { - "version": "14.2.0", - "resolved": "https://registry.npmjs.org/yargs/-/yargs-14.2.0.tgz", - "integrity": "sha512-/is78VKbKs70bVZH7w4YaZea6xcJWOAwkhbR0CFuZBmYtfTYF0xjGJF43AYd8g2Uii1yJwmS5GR2vBmrc32sbg==", + "version": "15.0.2", + "resolved": "https://registry.npmjs.org/yargs/-/yargs-15.0.2.tgz", + "integrity": "sha512-GH/X/hYt+x5hOat4LMnCqMd8r5Cv78heOMIJn1hr7QPPBqfeC6p89Y78+WB9yGDvfpCvgasfmWLzNzEioOUD9Q==", "dev": true, "requires": { - "cliui": "^5.0.0", + "cliui": "^6.0.0", "decamelize": "^1.2.0", - "find-up": "^3.0.0", + "find-up": "^4.1.0", "get-caller-file": "^2.0.1", "require-directory": "^2.1.1", "require-main-filename": "^2.0.0", "set-blocking": "^2.0.0", - "string-width": "^3.0.0", + "string-width": "^4.2.0", "which-module": "^2.0.0", "y18n": "^4.0.0", - "yargs-parser": "^15.0.0" - }, - "dependencies": { - "find-up": { - "version": "3.0.0", - "resolved": "https://registry.npmjs.org/find-up/-/find-up-3.0.0.tgz", - "integrity": "sha512-1yD6RmLI1XBfxugvORwlck6f75tYL+iR0jqwsOrOxMZyGYqUuDhJ0l4AXdO1iX/FTs9cBAMEk1gWSEx1kSbylg==", - "dev": true, - "requires": { - "locate-path": "^3.0.0" - } - }, - "locate-path": { - "version": "3.0.0", - "resolved": "https://registry.npmjs.org/locate-path/-/locate-path-3.0.0.tgz", - "integrity": "sha512-7AO748wWnIhNqAuaty2ZWHkQHRSNfPVIsPIfwEOWO22AmaoVrWavlOcMR5nzTLNYvp36X220/maaRsrec1G65A==", - "dev": true, - "requires": { - "p-locate": "^3.0.0", - "path-exists": "^3.0.0" - } - }, - "p-locate": { - "version": "3.0.0", - "resolved": "https://registry.npmjs.org/p-locate/-/p-locate-3.0.0.tgz", - "integrity": "sha512-x+12w/To+4GFfgJhBEpiDcLozRJGegY+Ei7/z0tSLkMmxGZNybVMSfWj9aJn8Z5Fc7dBUNJOOVgPv2H7IwulSQ==", - "dev": true, - "requires": { - "p-limit": "^2.0.0" - } - }, - "path-exists": { - "version": "3.0.0", - "resolved": "https://registry.npmjs.org/path-exists/-/path-exists-3.0.0.tgz", - "integrity": "sha1-zg6+ql94yxiSXqfYENe1mwEP1RU=", - "dev": true - } + "yargs-parser": "^16.1.0" } }, "yargs-parser": { - "version": "15.0.0", - "resolved": "https://registry.npmjs.org/yargs-parser/-/yargs-parser-15.0.0.tgz", - "integrity": "sha512-xLTUnCMc4JhxrPEPUYD5IBR1mWCK/aT6+RJ/K29JY2y1vD+FhtgKK0AXRWvI262q3QSffAQuTouFIKUuHX89wQ==", + "version": "16.1.0", + "resolved": "https://registry.npmjs.org/yargs-parser/-/yargs-parser-16.1.0.tgz", + "integrity": "sha512-H/V41UNZQPkUMIT5h5hiwg4QKIY1RPvoBV4XcjUbRM8Bk2oKqqyZ0DIEbTFZB0XjbtSPG8SAa/0DxCQmiRgzKg==", "dev": true, "requires": { "camelcase": "^5.0.0", @@ -12973,9 +12904,9 @@ } }, "type-fest": { - "version": "0.5.2", - "resolved": "https://registry.npmjs.org/type-fest/-/type-fest-0.5.2.tgz", - "integrity": "sha512-DWkS49EQKVX//Tbupb9TFa19c7+MK1XmzkrZUR8TAktmE/DizXoaoJV6TZ/tSIPXipqNiRI6CyAe7x69Jb6RSw==", + "version": "0.8.1", + "resolved": "https://registry.npmjs.org/type-fest/-/type-fest-0.8.1.tgz", + "integrity": "sha512-4dbzIzqvjtgiM5rw1k5rEHtBANKmdudhGyBEajN01fEyhaAIhsoKNy6y7+IN93IfpFtwY9iqi7kD+xwKhQsNJA==", "dev": true }, "uglify-js": { @@ -13197,9 +13128,9 @@ } }, "which": { - "version": "2.0.1", - "resolved": "https://registry.npmjs.org/which/-/which-2.0.1.tgz", - "integrity": "sha512-N7GBZOTswtB9lkQBZA4+zAXrjEIWAUOB93AvzUiudRzRxhUdLURQ7D/gAIMY1gatT/LTbmbcv8SiYazy3eYB7w==", + "version": "2.0.2", + "resolved": "https://registry.npmjs.org/which/-/which-2.0.2.tgz", + "integrity": "sha512-BLI3Tl1TW3Pvl70l3yq3Y64i+awpwXqsGBYWkkqMtnbXgrMD+yj7rhW0kuEDxzJaYXGjEW5ogapKNMEKNMjibA==", "dev": true, "requires": { "isexe": "^2.0.0" diff --git a/package.json b/package.json index b8b50c6..88cbaf8 100644 --- a/package.json +++ b/package.json @@ -7,11 +7,12 @@ ], "main": "./src/index.js", "dependencies": { - "@neuralegion/openapi-sampler": "^0.6.1", + "@neuralegion/openapi-sampler": "^0.7.1", "@types/har-format": "^1.2.4", "@types/swagger-schema-official": "^2.0.18", "js-yaml": "^3.13.1", - "url-template": "^2.0.8" + "url-template": "^2.0.8", + "jstoxml": "^1.6.0" }, "typings": "./src/index.d.ts", "repository": { @@ -36,11 +37,11 @@ "@semantic-release/git": "^7.0.17", "eslint": "^6.6.0", "esprima": "^4.0.1", - "husky": "^3.0.7", + "husky": "^3.1.0", "jest": "^24.8.0", "lint-staged": "^9.4.0", "prettier": "^1.11.1", - "semantic-release": "^15.13.27" + "semantic-release": "^15.13.31" }, "scripts": { "lint": "eslint", diff --git a/src/converter.js b/src/converter.js index 8e45351..853b75c 100644 --- a/src/converter.js +++ b/src/converter.js @@ -24,13 +24,15 @@ var OpenAPISampler = require('@neuralegion/openapi-sampler'); var load = require('./loader'); var urlTemplate = require('url-template'); - +const { toXML } = require('jstoxml'); +const querystring = require('querystring'); /** * Create HAR Request object for path and method pair described in given swagger. * * @param {Object} swagger Swagger document - * @param {string} path Key of the path + * @param {string} path Key clear + * of the path * @param {string} method Key of the method * @param {Object} queryParamValues Optional: Values for the query parameters if present * @return {Object} HAR Request object @@ -73,31 +75,57 @@ var createHar = function(swagger, path, method, queryParamValues) { * @return {object} */ var getPayload = function(swagger, path, method) { - if (typeof swagger.paths[path][method].parameters !== 'undefined') { - for (var i in swagger.paths[path][method].parameters) { - var param = swagger.paths[path][method].parameters[i] + const pathObj = swagger.paths[path][method]; + + if (typeof pathObj.parameters !== 'undefined') { + for (var i in pathObj.parameters) { + var param = pathObj.parameters[i] if (typeof param.in !== 'undefined' && param.in.toLowerCase() === 'body' && typeof param.schema !== 'undefined') { try { - const sample = OpenAPISampler.sample(param.schema, { skipReadOnly: true }, swagger) + const sample = OpenAPISampler.sample(param.schema, { skipReadOnly: true }, swagger); + let consumes; + if (pathObj.consumes && pathObj.consumes.length) { + consumes = pathObj.consumes; + } else if (swagger.consumes && swagger.consumes.length) { + consumes = swagger.consumes; + } + const paramContentType = OpenAPISampler.sample({ + type: 'array', + examples: consumes ? consumes : ['application/json'] + }); + return { - mimeType: 'application/json', - text: JSON.stringify(sample) + mimeType: paramContentType, + text: encodeSample(sample, paramContentType, param.schema) } } catch (err) { - console.log(err) return null } } } } - if (swagger.paths[path][method].requestBody && swagger.paths[path][method].requestBody.content && - swagger.paths[path][method].requestBody.content['application/json'] && - swagger.paths[path][method].requestBody.content['application/json'].schema) { - const sample = OpenAPISampler.sample(swagger.paths[path][method].requestBody.content['application/json'].schema, { skipReadOnly: true }, swagger) + + let content = swagger.paths[path][method].requestBody ? + swagger.paths[path][method].requestBody.content + : null ; + + const keys = Object.keys(content || {}); + if (!keys.length) { + return null; + } + + const contentType = OpenAPISampler.sample({ + type: 'array', + examples: keys + }); + + if (content[contentType] && content[contentType].schema) { + let sampleContent = content[contentType]; + const sample = OpenAPISampler.sample(content[contentType].schema, { skipReadOnly: true }, swagger); return { - mimeType: 'application/json', - text: JSON.stringify(sample) + mimeType: contentType, + text: encodeSample(sample, contentType, sampleContent) } } return null @@ -379,6 +407,52 @@ var resolveRef = function(oai, ref) { return recursive(oai, 1) } + +/** + * Iterate over all defined keys under encoding and apply encoding for them + * + * @param {string[]} keys Array of keys referencing properties and how to encode them + * @param {any} sample The sample whose properties are encoded + * @param {object} content The content options + * @return {object} + */ +var encodeProperties = function (keys, sample, content) { + const encodedSample = keys.reduce((encodedSample, encodingKey) => { + encodedSample[encodingKey] = encodeValue(sample[encodingKey], content.encoding[encodingKey].contentType, content); + return encodedSample; + }, {}); + return Object.assign({}, sample, encodedSample); +}; + +/** + * Infer which content type is used from type of object. If encoding is defined use the encoding type. + * + * @param {any} value Value for which the content type os determined + * @param {string} paramKey Key of the param, that is the param name + * @param {object} content The content options + * @return {string} + */ +var getMultipartContentType = function (value, paramKey, content) { + + if (content.encoding && content.encoding[paramKey] && content.encoding[paramKey].contentType) { + return content.encoding[paramKey].contentType; + } + + switch (typeof value) { + case 'object': + return 'application/json'; + + case 'string': + case 'number': + case 'boolean': + return 'text/plain'; + + default: + return 'application/octet-stream'; + } +}; + + /** * * @param {Object} swagger Swagger document @@ -395,13 +469,93 @@ var serializePath = function (swagger, path, method) { var param = swagger.paths[path][method].parameters[i]; if (typeof param.in !== 'undefined' && param.in.toLowerCase() === 'path') { const sample = OpenAPISampler.sample(param.schema || param, {}, swagger); - Object.assign(params, { [param.name]: sample }); + Object.assign(params, {[param.name]: sample}); } } } return templateUrl.expand(params); } +/* + * Returns the encoded value for defined content + * + * @param {any} value The sampled value to encode + * @param {string} contentType The content-type of the value + * @param {object} content The content options + * @return {any} + */ +var encodeValue = function (value, contentType, content) { + switch (contentType) { + case 'application/json': + return JSON.stringify(value); + + case 'application/x-www-form-urlencoded': + return querystring.stringify(value); + + case 'application/xml': + const xmlOptions = { + header: true, + indent: ' ' + }; + return toXML(value, xmlOptions); + + case 'multipart/form-data': + case 'multipart/mixin': + let inputNames = Object.keys(value); + let rawData = inputNames.reduce((params, key) => { + const multipartContentType = getMultipartContentType(value[key], key, content); + let param = '--956888039105887155673143\r\n'; + switch (multipartContentType) { + case 'text/plain': + case 'application/json': + param += `Content-Disposition: form-data; name="${key}"\r\n`; + break; + default: + param += `Content-Disposition: form-data; name="${key}"; filename="${key}"\r\n`; + } + + param += `Content-Type: ${multipartContentType}\r\n\r\n`; + param += typeof value[key] === 'object' ? JSON.stringify(value[key]) : value[key]; + params.push(param); + return params; + }, []).join('\r\n'); + rawData += '\r\n--956888039105887155673143--'; + return rawData; + + case 'image/jpg': + case 'image/jpeg': + return Buffer.from([0xFF, 0xD8, 0xFF, 0xDB], 'hex').toString('base64'); + + case 'image/png': + case 'image/*': + return Buffer.from([0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A], 'hex').toString('base64'); + + default: + if (typeof value === 'object') { + value = JSON.stringify(value); + } + return Buffer.from(value).toString('base64'); + + } +}; + +/** + * Encode the sample + * + * @param {any} sample Sample object to encode + * @param {string} contentType The content-type of the value + * @param {object} content The content options + * @return {any} + */ +var encodeSample = function (sample, contentType, content) { + let encodedSample = sample; + if (content.encoding) { + encodedSample = encodeProperties(Object.keys(content.encoding), sample, content); + } + return encodeValue(encodedSample, contentType, content); +}; + module.exports = { - oasToHarList: oasToHarList + oasToHarList: oasToHarList, + encodeSample: encodeSample }