@@ -958,57 +958,57 @@ describe('options.baseUrl', () => {
958
958
959
959
it ( 'uses baseUrl option without trailing slash and uri is prefixed with a slash' , async ( ) => {
960
960
961
- const promise = Wreck . request ( 'get' , '/foo' , { baseUrl : 'http://localhost' } ) ;
961
+ const promise = Wreck . request ( 'get' , '/foo' , { baseUrl : 'http://localhost:0 ' } ) ;
962
962
await expect ( promise ) . to . reject ( ) ;
963
- expect ( promise . req . _headers . host ) . to . equal ( 'localhost' ) ;
963
+ expect ( promise . req . _headers . host ) . to . equal ( 'localhost:0 ' ) ;
964
964
expect ( promise . req . path ) . to . equal ( '/foo' ) ;
965
965
} ) ;
966
966
967
967
it ( 'uses baseUrl option with trailing slash and uri is prefixed without a slash' , async ( ) => {
968
968
969
- const promise = Wreck . request ( 'get' , 'foo' , { baseUrl : 'http://localhost/' } ) ;
969
+ const promise = Wreck . request ( 'get' , 'foo' , { baseUrl : 'http://localhost:0 /' } ) ;
970
970
await expect ( promise ) . to . reject ( ) ;
971
- expect ( promise . req . _headers . host ) . to . equal ( 'localhost' ) ;
971
+ expect ( promise . req . _headers . host ) . to . equal ( 'localhost:0 ' ) ;
972
972
expect ( promise . req . path ) . to . equal ( '/foo' ) ;
973
973
} ) ;
974
974
975
975
it ( 'uses baseUrl option without trailing slash and uri is prefixed without a slash' , async ( ) => {
976
976
977
- const promise = Wreck . request ( 'get' , 'foo' , { baseUrl : 'http://localhost' } ) ;
977
+ const promise = Wreck . request ( 'get' , 'foo' , { baseUrl : 'http://localhost:0 ' } ) ;
978
978
await expect ( promise ) . to . reject ( ) ;
979
- expect ( promise . req . _headers . host ) . to . equal ( 'localhost' ) ;
979
+ expect ( promise . req . _headers . host ) . to . equal ( 'localhost:0 ' ) ;
980
980
expect ( promise . req . path ) . to . equal ( '/foo' ) ;
981
981
} ) ;
982
982
983
983
it ( 'uses baseUrl option when uri is an empty string' , async ( ) => {
984
984
985
- const promise = Wreck . request ( 'get' , '' , { baseUrl : 'http://localhost' } ) ;
985
+ const promise = Wreck . request ( 'get' , '' , { baseUrl : 'http://localhost:0 ' } ) ;
986
986
await expect ( promise ) . to . reject ( ) ;
987
- expect ( promise . req . _headers . host ) . to . equal ( 'localhost' ) ;
987
+ expect ( promise . req . _headers . host ) . to . equal ( 'localhost:0 ' ) ;
988
988
expect ( promise . req . path ) . to . equal ( '/' ) ;
989
989
} ) ;
990
990
991
991
it ( 'uses baseUrl option with a path' , async ( ) => {
992
992
993
- const promise = Wreck . request ( 'get' , '/bar' , { baseUrl : 'http://localhost/foo' } ) ;
993
+ const promise = Wreck . request ( 'get' , '/bar' , { baseUrl : 'http://localhost:0 /foo' } ) ;
994
994
await expect ( promise ) . to . reject ( ) ;
995
- expect ( promise . req . _headers . host ) . to . equal ( 'localhost' ) ;
995
+ expect ( promise . req . _headers . host ) . to . equal ( 'localhost:0 ' ) ;
996
996
expect ( promise . req . path ) . to . equal ( '/foo/bar' ) ;
997
997
} ) ;
998
998
999
999
it ( 'uses baseUrl option with a path and removes extra slashes' , async ( ) => {
1000
1000
1001
- const promise = Wreck . request ( 'get' , '/bar' , { baseUrl : 'http://localhost/foo/' } ) ;
1001
+ const promise = Wreck . request ( 'get' , '/bar' , { baseUrl : 'http://localhost:0 /foo/' } ) ;
1002
1002
await expect ( promise ) . to . reject ( ) ;
1003
- expect ( promise . req . _headers . host ) . to . equal ( 'localhost' ) ;
1003
+ expect ( promise . req . _headers . host ) . to . equal ( 'localhost:0 ' ) ;
1004
1004
expect ( promise . req . path ) . to . equal ( '/foo/bar' ) ;
1005
1005
} ) ;
1006
1006
1007
1007
it ( 'uses baseUrl option with a url that has a querystring' , async ( ) => {
1008
1008
1009
- const promise = Wreck . request ( 'get' , '/bar?test=hello' , { baseUrl : 'http://localhost/foo' } ) ;
1009
+ const promise = Wreck . request ( 'get' , '/bar?test=hello' , { baseUrl : 'http://localhost:0 /foo' } ) ;
1010
1010
await expect ( promise ) . to . reject ( ) ;
1011
- expect ( promise . req . _headers . host ) . to . equal ( 'localhost' ) ;
1011
+ expect ( promise . req . _headers . host ) . to . equal ( 'localhost:0 ' ) ;
1012
1012
expect ( promise . req . path ) . to . equal ( '/foo/bar?test=hello' ) ;
1013
1013
} ) ;
1014
1014
} ) ;
@@ -1121,6 +1121,47 @@ describe('read()', () => {
1121
1121
expect ( err . isBoom ) . to . equal ( true ) ;
1122
1122
} ) ;
1123
1123
1124
+ it ( 'will not pipe the stream if no socket can be established' , async ( ) => {
1125
+
1126
+ const agent = new internals . SlowAgent ( ) ;
1127
+ const stream = new Stream . Readable ( {
1128
+ read ( ) {
1129
+
1130
+ piped = true ;
1131
+ this . push ( null ) ;
1132
+ }
1133
+ } ) ;
1134
+ const onPiped = ( ) => {
1135
+
1136
+ piped = true ;
1137
+ } ;
1138
+ let piped = false ;
1139
+
1140
+ stream . on ( 'pipe' , onPiped ) ;
1141
+
1142
+ const promiseA = Wreck . request ( 'post' , 'http://localhost:0' , {
1143
+ agent,
1144
+ payload : stream
1145
+ } ) ;
1146
+
1147
+ await expect ( promiseA ) . to . reject ( Error , / U n a b l e t o o b t a i n s o c k e t / ) ;
1148
+ expect ( piped ) . to . equal ( false ) ;
1149
+
1150
+ const handler = ( req , res ) => {
1151
+
1152
+ res . writeHead ( 200 ) ;
1153
+ res . end ( internals . payload ) ;
1154
+ } ;
1155
+
1156
+ const server = await internals . server ( handler ) ;
1157
+ const res = await Wreck . request ( 'post' , 'http://localhost:' + server . address ( ) . port , {
1158
+ payload : stream
1159
+ } ) ;
1160
+ expect ( res . statusCode ) . to . equal ( 200 ) ;
1161
+ expect ( piped ) . to . equal ( true ) ;
1162
+ server . close ( ) ;
1163
+ } ) ;
1164
+
1124
1165
it ( 'times out when stream read takes too long' , async ( ) => {
1125
1166
1126
1167
const TestStream = function ( ) {
@@ -1759,7 +1800,7 @@ describe('Events', () => {
1759
1800
once = true ;
1760
1801
} ) ;
1761
1802
1762
- await expect ( wreck . get ( 'http://127.0.0.1 ' , { timeout : 10 } ) ) . to . reject ( ) ;
1803
+ await expect ( wreck . get ( 'http://localhost:0 ' , { timeout : 10 } ) ) . to . reject ( ) ;
1763
1804
expect ( once ) . to . be . true ( ) ;
1764
1805
} ) ;
1765
1806
@@ -1777,8 +1818,8 @@ describe('Events', () => {
1777
1818
const wreck = Wreck . defaults ( { events : true } ) ;
1778
1819
wreck . events . on ( 'response' , handler ) ;
1779
1820
1780
- await expect ( wreck . get ( 'http://127.0.0.1 ' , { timeout : 10 } ) ) . to . reject ( ) ;
1781
- await expect ( wreck . get ( 'http://127.0.0.1 ' , { timeout : 10 } ) ) . to . reject ( ) ;
1821
+ await expect ( wreck . get ( 'http://localhost:0 ' , { timeout : 10 } ) ) . to . reject ( ) ;
1822
+ await expect ( wreck . get ( 'http://localhost:0 ' , { timeout : 10 } ) ) . to . reject ( ) ;
1782
1823
expect ( count ) . to . equal ( 2 ) ;
1783
1824
} ) ;
1784
1825
@@ -1973,6 +2014,12 @@ internals.server = function (handler, socket) {
1973
2014
req . pipe ( res ) ;
1974
2015
} ;
1975
2016
}
2017
+ else if ( handler === 'fail' ) {
2018
+ handler = ( req , res ) => {
2019
+
2020
+ res . socket . destroy ( ) ;
2021
+ } ;
2022
+ }
1976
2023
else if ( handler === 'ok' ) {
1977
2024
handler = ( req , res ) => {
1978
2025
@@ -2020,6 +2067,14 @@ internals.https = function (handler) {
2020
2067
} ;
2021
2068
2022
2069
2070
+ internals . SlowAgent = class SlowAgent extends Http . Agent {
2071
+ createConnection ( options , cb ) {
2072
+
2073
+ setTimeout ( cb , 200 , new Error ( 'Unable to obtain socket' ) ) ;
2074
+ }
2075
+ } ;
2076
+
2077
+
2023
2078
internals . wait = function ( timeout ) {
2024
2079
2025
2080
return new Promise ( ( resolve ) => setTimeout ( resolve , timeout ) ) ;
0 commit comments