File tree Expand file tree Collapse file tree 2 files changed +26
-4
lines changed Expand file tree Collapse file tree 2 files changed +26
-4
lines changed Original file line number Diff line number Diff line change @@ -2,6 +2,17 @@ const ReadableStream = require('stream').Readable;
22const accepts = require ( 'accepts' ) ;
33const typeis = require ( 'type-is' ) ;
44
5+ function byteLength ( str ) {
6+ // returns the byte length of an utf8 string
7+ let s = str . length
8+ for ( let i = str . length - 1 ; i >= 0 ; i -- ) {
9+ const code = str . charCodeAt ( i )
10+ if ( code > 0x7f && code <= 0x7ff ) s ++
11+ else if ( code > 0x7ff && code <= 0xffff ) s += 2
12+ }
13+ return s
14+ }
15+
516/**
617 *
718 */
@@ -43,7 +54,7 @@ class Request extends ReadableStream {
4354 this . params = event . pathParameters ;
4455
4556 if ( ! this . get ( 'Content-Length' ) && event . body ) {
46- this . headers [ 'content-length' ] = event . body . length ;
57+ this . headers [ 'content-length' ] = byteLength ( event . body ) ;
4758 }
4859
4960 this . protocol = this . get ( 'X-Forwarded-Proto' )
Original file line number Diff line number Diff line change @@ -141,11 +141,22 @@ describe('Request object', () => {
141141 } ) ;
142142
143143 it ( 'should handle content-length header if its not provided' , ( ) => {
144- delete event . headers [ 'content-length' ] ;
145- delete event . multiValueHeaders [ 'content-length' ] ;
144+ delete event . headers [ 'Content-Length' ] ;
145+ delete event . multiValueHeaders [ 'Content-Length' ] ;
146+ const body = JSON . stringify ( requestObject ) ;
147+ event . body = body
146148
147149 const request = new Request ( event ) ;
148- const body = JSON . stringify ( requestObject ) ;
149150 expect ( request . get ( 'content-length' ) ) . toBe ( body . length ) ;
150151 } )
152+
153+ it ( 'should handle non-ascii content-length if header is not provided' , ( ) => {
154+ delete event . headers [ 'Content-Length' ] ;
155+ delete event . multiValueHeaders [ 'Content-Length' ] ;
156+ const body = JSON . stringify ( { text :"árvíztűrőtükörfúrógép" } ) ;
157+ event . body = body
158+
159+ const request = new Request ( event ) ;
160+ expect ( request . get ( 'content-length' ) ) . toBe ( 41 ) ;
161+ } )
151162} ) ;
You can’t perform that action at this time.
0 commit comments