1
1
import { Socket } from 'net' ;
2
2
import { ClientRequest , IncomingMessage } from 'http' ;
3
3
import * as querystring from 'querystring' ;
4
-
5
4
import { fixRequestBody , BodyParserLikeRequest } from '../../src/handlers/fix-request-body' ;
6
5
7
6
const fakeProxyRequest = ( ) : ClientRequest => {
@@ -17,6 +16,15 @@ const createRequestWithBody = (body: unknown): BodyParserLikeRequest => {
17
16
return req ;
18
17
} ;
19
18
19
+ const handlerFormDataBodyData = ( contentType : string , data : { [ key : string ] : any } ) => {
20
+ const boundary = contentType . replace ( / ^ .* b o u n d a r y = ( .* ) $ / , '$1' ) ;
21
+ let str = '' ;
22
+ for ( const [ key , value ] of Object . entries ( data ) ) {
23
+ str += `--${ boundary } \r\nContent-Disposition: form-data; name="${ key } "\r\n\r\n${ value } \r\n` ;
24
+ }
25
+ return str ;
26
+ } ;
27
+
20
28
describe ( 'fixRequestBody' , ( ) => {
21
29
it ( 'should not write when body is undefined' , ( ) => {
22
30
const proxyRequest = fakeProxyRequest ( ) ;
@@ -57,6 +65,55 @@ describe('fixRequestBody', () => {
57
65
expect ( proxyRequest . write ) . toHaveBeenCalledWith ( expectedBody ) ;
58
66
} ) ;
59
67
68
+ it ( 'should write when body is not empty and Content-Type is multipart/form-data' , ( ) => {
69
+ const proxyRequest = fakeProxyRequest ( ) ;
70
+ proxyRequest . setHeader ( 'content-type' , 'multipart/form-data' ) ;
71
+
72
+ jest . spyOn ( proxyRequest , 'setHeader' ) ;
73
+ jest . spyOn ( proxyRequest , 'write' ) ;
74
+
75
+ fixRequestBody ( proxyRequest , createRequestWithBody ( { someField : 'some value' } ) ) ;
76
+
77
+ const expectedBody = handlerFormDataBodyData ( 'multipart/form-data' , {
78
+ someField : 'some value' ,
79
+ } ) ;
80
+
81
+ expect ( expectedBody ) . toMatchInlineSnapshot ( `
82
+ "--multipart/form-data
83
+ Content-Disposition: form-data; name="someField"
84
+
85
+ some value
86
+ "
87
+ ` ) ;
88
+ expect ( proxyRequest . setHeader ) . toHaveBeenCalledWith ( 'Content-Length' , expectedBody . length ) ;
89
+ expect ( proxyRequest . write ) . toHaveBeenCalledWith ( expectedBody ) ;
90
+ } ) ;
91
+
92
+ it ( 'should write when body is not empty and Content-Type includes multipart/form-data' , ( ) => {
93
+ const proxyRequest = fakeProxyRequest ( ) ;
94
+ proxyRequest . setHeader ( 'content-type' , 'multipart/form-data' ) ;
95
+
96
+ jest . spyOn ( proxyRequest , 'setHeader' ) ;
97
+ jest . spyOn ( proxyRequest , 'write' ) ;
98
+
99
+ fixRequestBody ( proxyRequest , createRequestWithBody ( { someField : 'some value' } ) ) ;
100
+
101
+ const expectedBody = handlerFormDataBodyData ( 'multipart/form-data' , {
102
+ someField : 'some value' ,
103
+ } ) ;
104
+
105
+ expect ( expectedBody ) . toMatchInlineSnapshot ( `
106
+ "--multipart/form-data
107
+ Content-Disposition: form-data; name="someField"
108
+
109
+ some value
110
+ "
111
+ ` ) ;
112
+
113
+ expect ( proxyRequest . setHeader ) . toHaveBeenCalledWith ( 'Content-Length' , expectedBody . length ) ;
114
+ expect ( proxyRequest . write ) . toHaveBeenCalledWith ( expectedBody ) ;
115
+ } ) ;
116
+
60
117
it ( 'should write when body is not empty and Content-Type ends with +json' , ( ) => {
61
118
const proxyRequest = fakeProxyRequest ( ) ;
62
119
proxyRequest . setHeader ( 'content-type' , 'application/merge-patch+json; charset=utf-8' ) ;
@@ -65,7 +122,6 @@ describe('fixRequestBody', () => {
65
122
jest . spyOn ( proxyRequest , 'write' ) ;
66
123
67
124
fixRequestBody ( proxyRequest , createRequestWithBody ( { someField : 'some value' } ) ) ;
68
-
69
125
const expectedBody = JSON . stringify ( { someField : 'some value' } ) ;
70
126
expect ( proxyRequest . setHeader ) . toHaveBeenCalledWith ( 'Content-Length' , expectedBody . length ) ;
71
127
expect ( proxyRequest . write ) . toHaveBeenCalledWith ( expectedBody ) ;
0 commit comments