@@ -2,8 +2,9 @@ import request from 'supertest';
2
2
import * as http from 'http' ;
3
3
import processRequest from '../../src/lib/process-request' ;
4
4
import { LogOptions } from 'src/lib/construct-payload' ;
5
+ import FormData from 'form-data' ;
5
6
6
- function createApp ( reqOptions ?: LogOptions , shouldPreParse : boolean = false ) {
7
+ function createApp ( reqOptions ?: LogOptions , shouldPreParse : boolean = false , bodyOverride ? ) {
7
8
const requestListener = function ( req : http . IncomingMessage , res : http . ServerResponse ) {
8
9
let body = "" ;
9
10
@@ -20,7 +21,7 @@ function createApp(reqOptions?: LogOptions, shouldPreParse: boolean = false) {
20
21
body = JSON . parse ( body ) ;
21
22
}
22
23
23
- res . end ( JSON . stringify ( processRequest ( req , body , reqOptions ) ) ) ;
24
+ res . end ( JSON . stringify ( processRequest ( req , bodyOverride ? bodyOverride : body , reqOptions ) ) ) ;
24
25
} ) ;
25
26
} ;
26
27
@@ -96,6 +97,43 @@ it('should work with *+json', () => {
96
97
} ) ;
97
98
} ) ;
98
99
100
+ it ( 'should work with multipart/form-data' , ( ) => {
101
+ const app = createApp ( { denylist : [ 'password' ] } ) ;
102
+
103
+ const form = new FormData ( ) ;
104
+ form . append ( 'password' , '123456' ) ;
105
+ form . append ( 'apiKey' , 'abc' ) ;
106
+ form . append ( 'another' , 'Hello world' ) ;
107
+
108
+ const formHeaders = form . getHeaders ( ) ;
109
+
110
+ return request ( app )
111
+ . post ( '/' )
112
+ . set ( formHeaders )
113
+ . send ( form . getBuffer ( ) . toString ( ) )
114
+ . expect ( ( { body } ) => {
115
+ // If the request body for multipart form comes in as a string, we record it as is.
116
+ expect ( body . postData . text ) . toBe ( form . getBuffer ( ) . toString ( ) ) ;
117
+ } ) ;
118
+ } ) ;
119
+
120
+ it ( 'should fail gracefully with circular json objects' , ( ) => {
121
+ const obj = { foo : null } ;
122
+ obj . foo = obj ;
123
+
124
+ const app = createApp ( { denylist : [ 'password' ] } , false , obj ) ;
125
+
126
+ return request ( app )
127
+ . post ( '/' )
128
+ . set ( 'content-type' , 'text/plain' )
129
+ . send ( 'this isn\'t used' )
130
+ . expect ( ( { body } ) => {
131
+ console . log ( body ) ;
132
+ // If the request body for multipart form comes in as a string, we record it as is.
133
+ expect ( body . postData . text ) . toBe ( '[ReadMe is unable to handle circular JSON. Please contact support if you have any questions.]' ) ;
134
+ } ) ;
135
+ } ) ;
136
+
99
137
describe ( 'options' , ( ) => {
100
138
describe ( 'denylist/allowlist' , ( ) => {
101
139
it ( 'should strip denylisted json properties' , ( ) => {
0 commit comments