File tree Expand file tree Collapse file tree 3 files changed +30
-1
lines changed Expand file tree Collapse file tree 3 files changed +30
-1
lines changed Original file line number Diff line number Diff line change @@ -23,6 +23,7 @@ const createAuthScheme = require('./createAuthScheme');
23
23
const functionHelper = require ( './functionHelper' ) ;
24
24
const Endpoint = require ( './Endpoint' ) ;
25
25
const parseResources = require ( './parseResources' ) ;
26
+ const utils = require ( './utils' ) ;
26
27
27
28
/*
28
29
I'm against monolithic code like this file, but splitting it induces unneeded complexity.
@@ -406,7 +407,9 @@ class Offline {
406
407
config : routeConfig ,
407
408
handler : ( request , reply ) => { // Here we go
408
409
// Payload processing
409
- request . payload = request . payload && request . payload . toString ( ) ;
410
+ const encoding = utils . detectEncoding ( request ) ;
411
+
412
+ request . payload = request . payload && request . payload . toString ( encoding ) ;
410
413
request . rawPayload = request . payload ;
411
414
412
415
// Headers processing
Original file line number Diff line number Diff line change @@ -14,4 +14,7 @@ module.exports = {
14
14
15
15
return capitalized ;
16
16
} ,
17
+ // Detect the toString encoding from the request headers content-type
18
+ // enhance if further content types need to be non utf8 encoded.
19
+ detectEncoding : request => _ . includes ( request . headers [ 'content-type' ] , 'multipart/form-data' ) ? 'binary' : 'utf8' ,
17
20
} ;
Original file line number Diff line number Diff line change @@ -40,4 +40,27 @@ describe('utils', () => {
40
40
} ) ;
41
41
} ) ;
42
42
} ) ;
43
+
44
+ describe ( '#detectEncoding' , ( ) => {
45
+ context ( 'with application/json content-type' , ( ) => {
46
+ it ( 'should return utf8' , ( ) => {
47
+ const request = {
48
+ headers : {
49
+ 'content-type' : 'application/json' ,
50
+ } ,
51
+ } ;
52
+ expect ( utils . detectEncoding ( request ) ) . to . eq ( 'utf8' ) ;
53
+ } ) ;
54
+ } ) ;
55
+ context ( 'with multipart/form-data content-type' , ( ) => {
56
+ it ( 'should return binary' , ( ) => {
57
+ const request = {
58
+ headers : {
59
+ 'content-type' : 'multipart/form-data' ,
60
+ } ,
61
+ } ;
62
+ expect ( utils . detectEncoding ( request ) ) . to . eq ( 'binary' ) ;
63
+ } ) ;
64
+ } ) ;
65
+ } ) ;
43
66
} ) ;
You can’t perform that action at this time.
0 commit comments