@@ -2,14 +2,19 @@ import { inProcessResponseToLambdaResponse, errorResponse} from '../src/response
2
2
import { InProcessResponse } from 'in-process-request' ;
3
3
4
4
describe ( 'inProcessResponseToLambdaResponse' , ( ) => {
5
- const generateMockResponse = ( isUTF8 : boolean = true ) : InProcessResponse => ( {
6
- body : Buffer . from ( 'hello' ) ,
5
+ interface GenerateMockResponseOptions {
6
+ body ?: Buffer
7
+ isUTF8 ?: boolean
8
+ contentType ?: string
9
+ }
10
+ const generateMockResponse = ( opts : GenerateMockResponseOptions = { } ) : InProcessResponse => ( {
11
+ body : opts . body || Buffer . from ( 'hello' ) ,
7
12
headers : {
8
13
'set-cookie' : [ 'chocolate=10; Path=/' , 'peanut_butter=20; Path=/' ] ,
9
- 'content-type' : 'text/plain' ,
14
+ 'content-type' : opts . contentType || 'text/plain' ,
10
15
'x-custom' : '10' ,
11
16
} ,
12
- isUTF8,
17
+ isUTF8 : opts . isUTF8 === true ,
13
18
statusCode : 200 ,
14
19
statusMessage : 'OK' ,
15
20
} ) ;
@@ -81,17 +86,37 @@ describe('inProcessResponseToLambdaResponse', () => {
81
86
82
87
describe ( 'when the body is not utf8' , ( ) => {
83
88
it ( 'has base64 body' , ( ) => {
84
- const res = inProcessResponseToLambdaResponse ( generateMockResponse ( false ) , true , false ) ;
89
+ const res = inProcessResponseToLambdaResponse ( generateMockResponse ( { isUTF8 : false , contentType : 'not-text/plain' } ) , true , false ) ;
85
90
expect ( res . body ) . toEqual ( 'aGVsbG8=' ) ;
86
91
} )
87
92
88
93
it ( 'is not base64 encoded' , ( ) => {
89
- const res = inProcessResponseToLambdaResponse ( generateMockResponse ( false ) , true , false ) ;
94
+ const res = inProcessResponseToLambdaResponse ( generateMockResponse ( { isUTF8 : false , contentType : 'not-text/plain' } ) , true , false ) ;
90
95
expect ( res . isBase64Encoded ) . toEqual ( true ) ;
91
96
} )
92
97
98
+ it ( 'has basee64 body if isUTF8 is set to false and content type starts with text/ and the content is not UTF8' , ( ) => {
99
+ const res = inProcessResponseToLambdaResponse ( generateMockResponse ( { body : Buffer . from ( [ 1 , 2 , 3 , 4 , 5 , 6 ] ) , isUTF8 : false , contentType : 'binary/octet-stream' } ) , false , false ) ;
100
+ expect ( res . isBase64Encoded ) . toEqual ( true ) ;
101
+ expect ( res . body ) . toEqual ( 'AQIDBAUG' ) ;
102
+ } )
103
+
104
+ } )
93
105
106
+ describe ( 'UTF8 content' , ( ) => {
107
+ it ( 'has utf8 body if isUTF8 is set to true' , ( ) => {
108
+ const res = inProcessResponseToLambdaResponse ( generateMockResponse ( { isUTF8 : true , contentType : 'not-text/plain' } ) , false , false ) ;
109
+ expect ( res . isBase64Encoded ) . toEqual ( false ) ;
110
+ expect ( res . body ) . toEqual ( 'hello' ) ;
111
+ } )
112
+
113
+ it ( 'has utf8 body if isUTF8 is set to false but content type starts with text/ and the content is text' , ( ) => {
114
+ const res = inProcessResponseToLambdaResponse ( generateMockResponse ( { body : Buffer . from ( 'text' ) , isUTF8 : false , contentType : 'text/plain' } ) , false , false ) ;
115
+ expect ( res . isBase64Encoded ) . toEqual ( false ) ;
116
+ expect ( res . body ) . toEqual ( 'text' ) ;
117
+ } )
94
118
} )
119
+
95
120
} )
96
121
97
122
describe ( 'errorResponse' , ( ) => {
0 commit comments