Skip to content
This repository has been archived by the owner on Dec 3, 2021. It is now read-only.

Add support for basic media type encoding #13

Merged
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
12 changes: 12 additions & 0 deletions __tests__/application_json.js
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));
});
12 changes: 12 additions & 0 deletions __tests__/application_x_www_form_urlencoded.js
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));
});
18 changes: 18 additions & 0 deletions __tests__/application_xml.js
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));
});
9 changes: 9 additions & 0 deletions __tests__/image_sample.js
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);
});
58 changes: 58 additions & 0 deletions __tests__/multipart_form_data.js
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);

});
49 changes: 49 additions & 0 deletions __tests__/multipart_mixin.js
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);

});
29 changes: 14 additions & 15 deletions __tests__/oas2har.js
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');
});
7 changes: 7 additions & 0 deletions __tests__/text_plain.js
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'));
});
Loading