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

Commit

Permalink
fix(converter): fix mismatching content-type header with data type (#17)
Browse files Browse the repository at this point in the history
closes #15
  • Loading branch information
derevnjuk authored Nov 19, 2019
1 parent 5caf667 commit cde8d54
Show file tree
Hide file tree
Showing 10 changed files with 218 additions and 210 deletions.
8 changes: 4 additions & 4 deletions __tests__/application_json.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
const { encodeSample } = require('../src');
const { encodePayload } = require('../src');

test('Test encodeSample for application/json', function() {
test('Test encodePayload for application/json', function() {
const jsonSample = {
name: 'Tom',
surname: 'Trailer',
age: 22
};

const jsonEncoded = encodeSample(jsonSample, 'application/json', {});
expect(jsonEncoded).toEqual(JSON.stringify(jsonSample));
const jsonEncoded = encodePayload(jsonSample, 'application/json', {});
expect(jsonEncoded.text).toEqual(JSON.stringify(jsonSample));
});
8 changes: 4 additions & 4 deletions __tests__/application_x_www_form_urlencoded.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
const { encodeSample } = require('../src');
const { encodePayload } = require('../src');
const querystring = require('querystring');

test('Test encodeSample for application/x-www-form-urlencoded', function() {
test('Test encodePayload 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));
const queryStringEncoded = encodePayload(querystringSample, 'application/x-www-form-urlencoded', {});
expect(queryStringEncoded.text).toEqual(querystring.stringify(querystringSample));
});
8 changes: 4 additions & 4 deletions __tests__/application_xml.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const { encodeSample } = require('../src');
const { encodePayload } = require('../src');
const { toXML } = require('jstoxml');

test('Test encodeSample for application/xml', function() {
test('Test encodePayload for application/xml', function() {
const xmlSample = {
name: 'Tom',
surname: 'Trailer',
Expand All @@ -13,6 +13,6 @@ test('Test encodeSample for application/xml', function() {
indent: ' '
};

const xmlEncoded = encodeSample(xmlSample, 'application/xml', {});
expect(xmlEncoded).toEqual(toXML(xmlSample, xmlOptions));
const xmlEncoded = encodePayload(xmlSample, 'application/xml', {});
expect(xmlEncoded.text).toEqual(toXML(xmlSample, xmlOptions));
});
12 changes: 6 additions & 6 deletions __tests__/image_sample.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
const { encodeSample } = require('../src');
const { encodePayload } = require('../src');

test('Test encodeSample for image/*', function() {
const pngSample = encodeSample({}, 'image/png', {});
const jpgSample = encodeSample({}, 'image/jpg', {});
test('Test encodePayload for image/*', function() {
const pngSample = encodePayload({}, 'image/png', {});
const jpgSample = encodePayload({}, 'image/jpg', {});

expect(atob(pngSample).includes('PNG')).toEqual(true);
expect(atob(jpgSample).includes('ÿØÿÛ')).toEqual(true);
expect(atob(pngSample.text).includes('PNG')).toEqual(true);
expect(atob(jpgSample.text).includes('ÿØÿÛ')).toEqual(true);
});
38 changes: 16 additions & 22 deletions __tests__/multipart_form_data.js
Original file line number Diff line number Diff line change
@@ -1,46 +1,39 @@
const {encodeSample} = require('../src');
const {encodePayload} = require('../src');

test('Test encodeSample for multipart/form-data', function () {
test('Test encodePayload for multipart/form-data', function () {
const multiPartFormData = {
name: 'Tom',
surname: 'Trailer',
image: 'image'
image: Buffer.from('image').toString('base64')
};

const content = {
encoding: {
image: {
contentType: 'image/png'
}
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 += 'Content-Disposition: form-data; name="name"\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 += 'Content-Disposition: form-data; name="surname"\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--';
'Content-Type: image/png\r\n' +
'Content-Transfer-Encoding: base64\r\n\r\niVBORw0KGgo=\r\n--956888039105887155673143--';

const multipartFormDataEncoded = encodeSample(multiPartFormData, 'multipart/form-data', {});
const multipartFormDataComplexEncoded = encodeSample(multiPartFormData, 'multipart/form-data', content);
const multipartFormDataComplexEncoded = encodePayload(multiPartFormData, 'multipart/form-data', content);

expect(multipartFormDataEncoded).toEqual(encodedMultipartExpected);
expect(multipartFormDataComplexEncoded).toEqual(encodedMultipartComplexExpected);
expect(multipartFormDataComplexEncoded.mimeType).toEqual('multipart/form-data; boundary=956888039105887155673143')
expect(multipartFormDataComplexEncoded.text).toEqual(encodedMultipartComplexExpected);
});


test('Test encodeSample for multipart/form-data', function () {
test('Test encodePayload for multipart/form-data', function () {
const multiPartFormData = {
person: {
name: 'John',
Expand All @@ -52,7 +45,8 @@ test('Test encodeSample for multipart/form-data', function () {
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);
const multipartFormDataEncoded = encodePayload(multiPartFormData, 'multipart/form-data');
expect(multipartFormDataEncoded.mimeType).toEqual('multipart/form-data; boundary=956888039105887155673143')
expect(multipartFormDataEncoded.text).toEqual(encodedMultipartExpected);

});
67 changes: 31 additions & 36 deletions __tests__/multipart_mixin.js
Original file line number Diff line number Diff line change
@@ -1,49 +1,44 @@
const {encodeSample} = require('../src');
const { encodePayload } = require('../src')

test('Test encodeSample for multipart/form-data', function () {
test('Test encodePayload for multipart/form-data', function() {
const multipartMixin = {
user: {
username: 'john',
password: 'password'
},
token: 'user_token',
amount: 100,
buffer: 'base65'
};
buffer: Buffer.from('base65').toString('base64')
}

const content = {
encoding: {
user: {
contentType: 'application/json'
},
token: {
contentType: 'text/plain'
},
buffer: {
contentType: 'application/octet-stream'
}
user: {
contentType: 'application/json'
},
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);
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\r\n' +
'user_token\r\n' +
'--956888039105887155673143\r\n' +
'Content-Disposition: form-data; name="amount"\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' +
'Content-Transfer-Encoding: base64\r\n\r\n' +
'YmFzZTY1\r\n' +
'--956888039105887155673143--'
const multipartMixinEncoded = encodePayload(multipartMixin, 'multipart/mixin', content)

});
expect(multipartMixinEncoded.text).toEqual(multipartMixinExpected)
expect(multipartMixinEncoded.mimeType).toEqual('multipart/mixin; boundary=956888039105887155673143')
})
8 changes: 4 additions & 4 deletions __tests__/text_plain.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const { encodeSample } = require('../src');
const { encodePayload } = require('../src');

test('Test encodeSample for text/plain', function() {
test('Test encodePayload for text/plain', function() {
const primitiveSample = 'primitive';
const primitiveEncoded = encodeSample(primitiveSample, '*/*', {});
expect(primitiveEncoded).toEqual(Buffer.from(primitiveSample).toString('base64'));
const primitiveEncoded = encodePayload(primitiveSample, '*/*');
expect(primitiveEncoded.text).toEqual(primitiveSample);
});
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
],
"main": "./src/index.js",
"dependencies": {
"@neuralegion/openapi-sampler": "^0.7.1",
"@neuralegion/openapi-sampler": "^0.7.2",
"@types/har-format": "^1.2.4",
"@types/swagger-schema-official": "^2.0.18",
"js-yaml": "^3.13.1",
Expand Down
Loading

0 comments on commit cde8d54

Please sign in to comment.