This repository has been archived by the owner on Dec 3, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(converter): add support for basic media type encoding (#13)
closes #7
- Loading branch information
Showing
11 changed files
with
520 additions
and
270 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
|
||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
|
||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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'); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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')); | ||
}); |
Oops, something went wrong.