Skip to content

Commit c8b2065

Browse files
committed
Initial work on supporting binary bodies in both directions (request and response)
1 parent de47d18 commit c8b2065

36 files changed

+407
-209
lines changed

.eslintrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@
8989
"no-throw-literal": 2,
9090
"no-trailing-spaces": 2,
9191
"no-undef-init": 2,
92-
"no-underscore-dangle": [2, { "allow": ["_links", "_behaviors", "_mode", "_proxyResponseTime"] }],
92+
"no-underscore-dangle": [2, { "allow": ["_links", "_behaviors", "_proxyResponseTime"] }],
9393
"no-unneeded-ternary": 2,
9494
"no-unused-expressions": 2,
9595
"no-useless-call": 2,

mbTest/api/http/httpImposterTest.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -140,11 +140,11 @@ const assert = require('assert'),
140140
assert.strictEqual(response.statusCode, 200);
141141
assert.deepEqual(response.body.stubs, [
142142
{
143-
responses: [{ is: { body: '1' } }],
143+
responses: [{ is: { bodyEncoding: 'utf8', body: '1' } }],
144144
_links: { self: { href: `${api.url}/imposters/${port}/stubs/0` } }
145145
},
146146
{
147-
responses: [{ is: { body: '2' } }],
147+
responses: [{ is: { bodyEncoding: 'utf8', body: '2' } }],
148148
_links: { self: { href: `${api.url}/imposters/${port}/stubs/1` } }
149149
}
150150
]);
@@ -214,7 +214,7 @@ const assert = require('assert'),
214214
port: port + 1,
215215
name: imposter.name,
216216
recordRequests: false,
217-
stubs: [{ responses: [{ is: { body: 'Hello, World!' } }] }]
217+
stubs: [{ responses: [{ is: { bodyEncoding: 'utf8', body: 'Hello, World!' } }] }]
218218
});
219219
});
220220
});

mbTest/api/http/httpProxyStubTest.js

Lines changed: 48 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ describe('http proxy stubs', function () {
172172
});
173173
}
174174

175-
['application/octet-stream', 'audio/mpeg', 'audio/mp4', 'image/gif', 'image/jpeg', 'video/avi', 'video/mpeg'].forEach(mimeType => {
175+
['application/octet-stream', 'audio/mpeg', 'audio/mp4', 'image/gif', 'image/jpeg', 'video/avi', 'video/mpeg', ''].forEach(mimeType => {
176176
it(`should treat ${mimeType} as binary`, async function () {
177177
const buffer = Buffer.from([0, 1, 2, 3]),
178178
origin = {
@@ -183,7 +183,7 @@ describe('http proxy stubs', function () {
183183
is: {
184184
body: buffer.toString('base64'),
185185
headers: { 'content-type': mimeType },
186-
_mode: 'binary'
186+
bodyEncoding: null
187187
}
188188
}]
189189
}]
@@ -298,6 +298,8 @@ describe('http proxy stubs', function () {
298298
state.count = state.count || 0;
299299
state.count += 1;
300300
return {
301+
headers: { 'Content-Type': 'text/plain; charset=utf-8' }, // Tell the client (the proxy recorder) what kind of content.
302+
bodyEncoding: 'utf8', // Tell the stub that the provided body is utf8
301303
body: `${state.count}. ${request.path}`
302304
};
303305
},
@@ -335,6 +337,8 @@ describe('http proxy stubs', function () {
335337
state.count = state.count || 0;
336338
state.count += 1;
337339
return {
340+
headers: { 'Content-Type': 'text/plain; charset=utf-8' }, // Tell the client (the proxy recorder) what kind of content.
341+
bodyEncoding: 'utf8', // Tell the stub that the provided body is utf8
338342
body: `${state.count}. ${request.path}`
339343
};
340344
},
@@ -378,6 +382,8 @@ describe('http proxy stubs', function () {
378382
state.count = state.count || 0;
379383
state.count += 1;
380384
return {
385+
headers: { 'Content-Type': 'text/plain; charset=utf-8' }, // Tell the client (the proxy recorder) what kind of content.
386+
bodyEncoding: 'utf8', // Tell the stub that the provided body is utf8
381387
body: `${state.count}. ${JSON.stringify(request.query)}`
382388
};
383389
},
@@ -417,6 +423,8 @@ describe('http proxy stubs', function () {
417423
state.count = state.count || 0;
418424
state.count += 1;
419425
return {
426+
headers: { 'Content-Type': 'text/plain; charset=utf-8' }, // Tell the client (the proxy recorder) what kind of content.
427+
bodyEncoding: 'utf8', // Tell the stub that the provided body is utf8
420428
body: `${state.count}. ${JSON.stringify(request.query)}`
421429
};
422430
},
@@ -452,7 +460,12 @@ describe('http proxy stubs', function () {
452460

453461
it('should persist behaviors from origin server', async function () {
454462
const originServerPort = port + 1,
455-
originServerStub = { responses: [{ is: { body: '${SALUTATION} ${NAME}' } }] },
463+
originServerStub = { responses: [{
464+
is: {
465+
headers: { 'Content-Type': 'text/plain; charset=utf-8' }, // Tell the client (the proxy recorder) what kind of content.
466+
bodyEncoding: 'utf8', // Tell the stub that the provided body is utf8
467+
body: '${SALUTATION} ${NAME}'
468+
} }] },
456469
originServerRequest = {
457470
protocol: 'http',
458471
port: originServerPort,
@@ -528,6 +541,8 @@ describe('http proxy stubs', function () {
528541
state.count = state.count || 0;
529542
state.count += 1;
530543
return {
544+
headers: { 'Content-Type': 'text/plain; charset=utf-8' }, // Tell the client (the proxy recorder) what kind of content.
545+
bodyEncoding: 'utf8', // Tell the stub that the provided body is utf8
531546
body: `${state.count}. ${request.path}`
532547
};
533548
},
@@ -580,10 +595,11 @@ describe('http proxy stubs', function () {
580595
headers: {
581596
Connection: 'close',
582597
Date: 'NOW',
598+
'Content-Type': 'text/plain; charset=utf-8',
583599
'Transfer-Encoding': 'chunked'
584600
},
585601
body: '1. /first',
586-
_mode: 'text'
602+
bodyEncoding: 'utf8'
587603
}
588604
},
589605
{
@@ -592,10 +608,11 @@ describe('http proxy stubs', function () {
592608
headers: {
593609
Connection: 'close',
594610
Date: 'NOW',
611+
'Content-Type': 'text/plain; charset=utf-8',
595612
'Transfer-Encoding': 'chunked'
596613
},
597614
body: '3. /first',
598-
_mode: 'text'
615+
bodyEncoding: 'utf8'
599616
}
600617
}
601618
]
@@ -615,10 +632,11 @@ describe('http proxy stubs', function () {
615632
headers: {
616633
Connection: 'close',
617634
Date: 'NOW',
635+
'Content-Type': 'text/plain; charset=utf-8',
618636
'Transfer-Encoding': 'chunked'
619637
},
620638
body: '2. /second',
621-
_mode: 'text'
639+
bodyEncoding: 'utf8'
622640
}
623641
}
624642
]
@@ -636,7 +654,7 @@ describe('http proxy stubs', function () {
636654
is: {
637655
body: buffer.toString('base64'),
638656
headers: { 'content-encoding': 'gzip' },
639-
_mode: 'binary'
657+
bodyEncoding: null
640658
}
641659
},
642660
originServerStub = { responses: [originServerResponse] },
@@ -652,14 +670,19 @@ describe('http proxy stubs', function () {
652670
await api.createImposter(originServerRequest);
653671
await api.createImposter(proxyRequest);
654672

655-
const response = await client.responseFor({ method: 'GET', port, path: '/', mode: 'binary' });
673+
const response = await client.responseFor({ method: 'GET', port, path: '/', responseEncoding: null });
656674

657-
assert.deepEqual(response.body.toJSON().data, [0, 1, 2, 3]);
675+
assert.deepEqual(response.body, Buffer.from([0, 1, 2, 3]));
658676
});
659677

660678
it('should persist decorated proxy responses and only run decorator once', async function () {
661679
const originServerPort = port + 1,
662-
originServerStub = { responses: [{ is: { body: 'origin server' } }] },
680+
originServerStub = { responses: [{
681+
is: {
682+
headers: { 'Content-Type': 'text/plain; charset=utf-8' }, // Tell the client (the proxy recorder) what kind of content.
683+
bodyEncoding: 'utf8', // Tell the stub that the provided body is utf8
684+
body: 'origin server'
685+
} }] },
663686
originServerRequest = {
664687
protocol: 'http',
665688
port: originServerPort,
@@ -746,6 +769,7 @@ describe('http proxy stubs', function () {
746769
encoding = 'content-length';
747770
}
748771
return {
772+
bodyEncoding: 'utf8',
749773
body: `Encoding: ${encoding}`
750774
};
751775
},
@@ -784,7 +808,11 @@ describe('http proxy stubs', function () {
784808

785809
it('should add decorate behaviors to newly created response', async function () {
786810
const originServerPort = port + 1,
787-
originServerStub = { responses: [{ is: { body: 'origin server' } }] },
811+
originServerStub = { responses: [{
812+
is: {
813+
headers: { 'Content-Type': 'text/plain; charset=utf-8' }, // Tell the client (the proxy recorder) what kind of content.
814+
bodyEncoding: 'utf8', // Tell the stub that the provided body is utf8
815+
body: 'origin server' } }] },
788816
originServerRequest = {
789817
protocol: 'http',
790818
port: originServerPort,
@@ -818,11 +846,11 @@ describe('http proxy stubs', function () {
818846
name: 'origin'
819847
},
820848
firstStaticStub = {
821-
responses: [{ is: { body: 'first stub' } }],
822-
predicates: [{ equals: { body: 'fail match so we fall through to proxy' } }]
849+
responses: [{ is: { bodyEncoding: 'utf8', body: 'first stub' } }],
850+
predicates: [{ equals: { bodyEncoding: 'utf8', body: 'fail match so we fall through to proxy' } }]
823851
},
824852
proxyStub = { responses: [{ proxy: { to: `http://localhost:${originServerPort}`, mode: 'proxyAlways' } }] },
825-
secondStaticStub = { responses: [{ is: { body: 'second stub' } }] },
853+
secondStaticStub = { responses: [{ is: { bodyEncoding: 'utf8', body: 'second stub' } }] },
826854
proxyRequest = {
827855
protocol: 'http',
828856
port,
@@ -880,7 +908,12 @@ describe('http proxy stubs', function () {
880908

881909
it('should save JSON bodies as JSON instead of text (issue #656)', async function () {
882910
const originServerPort = port + 1,
883-
originServerStub = { responses: [{ is: { body: { json: true } } }] },
911+
originServerStub = { responses: [{
912+
is: {
913+
headers: { 'Content-Type': 'text/plain; charset=utf-8' }, // Tell the client (the proxy recorder) what kind of content.
914+
bodyEncoding: 'utf8', // Tell the stub that the provided body is utf8
915+
body: { json: true }
916+
} }] },
884917
originServerRequest = {
885918
protocol: 'http',
886919
port: originServerPort,

0 commit comments

Comments
 (0)