File tree 2 files changed +26
-8
lines changed
2 files changed +26
-8
lines changed Original file line number Diff line number Diff line change @@ -45,14 +45,21 @@ function requestBuilder (defaults) {
45
45
const headers = reqData . headers
46
46
const body = reqData . body
47
47
48
- let host = getPropertyCaseInsensitive ( reqData . headers , 'host' ) || reqData . host
48
+ const headersDefinedHost = getPropertyCaseInsensitive ( reqData . headers , 'host' )
49
+ let host = headersDefinedHost || reqData . host
49
50
50
51
if ( ! host ) {
51
52
const hostname = reqData . hostname
52
53
const port = reqData . port
53
54
host = hostname + ':' + port
54
55
}
55
-
56
+ const baseReq = [
57
+ `${ method } ${ path } HTTP/1.1`
58
+ ]
59
+ if ( ! headersDefinedHost ) {
60
+ baseReq . push ( `Host: ${ host } ` )
61
+ }
62
+ baseReq . push ( 'Connection: keep-alive' )
56
63
if ( reqData . auth ) {
57
64
const encodedAuth = Buffer . from ( reqData . auth ) . toString ( 'base64' )
58
65
headers . Authorization = `Basic ${ encodedAuth } `
@@ -62,12 +69,6 @@ function requestBuilder (defaults) {
62
69
throw new Error ( `${ method } HTTP method is not supported` )
63
70
}
64
71
65
- const baseReq = [
66
- `${ method } ${ path } HTTP/1.1` ,
67
- `Host: ${ host } ` ,
68
- 'Connection: keep-alive'
69
- ]
70
-
71
72
let bodyBuf
72
73
73
74
if ( typeof body === 'string' ) {
Original file line number Diff line number Diff line change @@ -99,6 +99,23 @@ test('request builder should add a Content-Length header when the body buffer ex
99
99
'request is okay' )
100
100
} )
101
101
102
+ test ( 'request builder should add only one HOST header' , ( t ) => {
103
+ t . plan ( 1 )
104
+
105
+ const opts = server . address ( )
106
+ opts . method = 'POST'
107
+ opts . headers = {
108
+ Host : 'example.com'
109
+ }
110
+
111
+ const build = RequestBuilder ( opts )
112
+
113
+ const result = build ( { body : 'body' } )
114
+ t . same ( result ,
115
+ Buffer . from ( 'POST / HTTP/1.1\r\nConnection: keep-alive\r\nHost: example.com\r\nContent-Length: 4\r\n\r\nbody' ) ,
116
+ 'request is okay' )
117
+ } )
118
+
102
119
test ( 'request builder should add a Content-Length header with correct calculated value when the body buffer exists and idReplacement is enabled as a default override' , ( t ) => {
103
120
t . plan ( 1 )
104
121
You can’t perform that action at this time.
0 commit comments