@@ -13,12 +13,14 @@ var http = require("http"),
13
13
specs = require ( "./specs" ) ,
14
14
helper = require ( "./helper" ) ,
15
15
server = require ( "./server" ) ,
16
- mapping = require ( "./mapping" ) ;
16
+ mapping = require ( "./mapping" ) ,
17
+ qs = require ( 'querystring' ) ;
17
18
18
19
var reSpace = / \s / ,
19
20
reConnectivity =
20
21
/ ^ (?: C a b l e | D S L | 3 G S l o w | 3 G | 3 G F a s t | 4 G | L T E | E d g e | 2 G | D i a l | F I O S | N a t i v e | c u s t o m ) $ / ,
21
- reHTMLOutput = / < h \d [ ^ < ] * > ( [ ^ < ] + ) < \/ h \d > / ; // for H3 on cancelTest.php
22
+ reHTMLOutput = / < h \d [ ^ < ] * > ( [ ^ < ] + ) < \/ h \d > / , // for H3 on cancelTest.php
23
+ reHTTPmethods = / ^ (?: G E T | P O S T ) $ / ;
22
24
23
25
var paths = {
24
26
testStatus : "testStatus.php" ,
@@ -57,8 +59,8 @@ var filenames = {
57
59
cached : "_Cached" ,
58
60
} ;
59
61
60
- // GET helper function
61
- function get ( config , pathname , proxy , agent , callback , encoding ) {
62
+ // GET/POST helper function
63
+ function get ( config , pathname , data , proxy , agent , callback , encoding ) {
62
64
var protocol , options ;
63
65
64
66
if ( proxy ) {
@@ -79,6 +81,7 @@ function get(config, pathname, proxy, agent, callback, encoding) {
79
81
headers : {
80
82
Host : config . hostname ,
81
83
} ,
84
+ method : config . method
82
85
} ;
83
86
} else {
84
87
protocol = config . protocol === "https:" ? https : http ;
@@ -87,10 +90,15 @@ function get(config, pathname, proxy, agent, callback, encoding) {
87
90
host : config . hostname ,
88
91
auth : config . auth ,
89
92
port : config . port ,
93
+ method : config . method ,
90
94
headers : { } ,
91
95
} ;
92
96
}
93
97
98
+ if ( options . method == "POST" ) {
99
+ options . headers [ 'Content-Type' ] = 'application/x-www-form-urlencoded' ;
100
+ }
101
+
94
102
// api key always required
95
103
options . headers [ "X-WPT-API-KEY" ] = this . config . key ;
96
104
options . headers [ "accept-encoding" ] = "gzip,deflate" ;
@@ -100,8 +108,8 @@ function get(config, pathname, proxy, agent, callback, encoding) {
100
108
options . agent = agent ;
101
109
}
102
110
103
- return protocol
104
- . get ( options , function getResponse ( res ) {
111
+ var request = protocol
112
+ . request ( options , function getResponse ( res ) {
105
113
var data ,
106
114
length ,
107
115
statusCode = res . statusCode ;
@@ -158,6 +166,13 @@ function get(config, pathname, proxy, agent, callback, encoding) {
158
166
. on ( "error" , function onError ( err ) {
159
167
callback ( err ) ;
160
168
} ) ;
169
+
170
+ if ( options . method == "POST" ) {
171
+ return request . end ( qs . stringify ( data ) ) ;
172
+ }
173
+
174
+ return request . end ( ) ;
175
+
161
176
}
162
177
163
178
// execute callback properly normalizing optional args
@@ -185,22 +200,35 @@ function api(pathname, callback, query, options) {
185
200
config = this . config ;
186
201
}
187
202
188
- pathname = url . format ( {
189
- pathname : url . resolve ( config . pathname , pathname ) ,
190
- query : query ,
191
- } ) ;
203
+ pathname = url . resolve ( config . pathname , pathname ) ;
204
+
205
+ if ( reHTTPmethods . test ( options . http_method ) ) {
206
+ config . method = options . http_method ;
207
+ } else {
208
+ config . method = WebPageTest . defaultHTTPMethod ;
209
+ }
210
+
211
+
212
+ if ( config . method == "GET" ) {
213
+ pathname = url . format ( {
214
+ pathname : pathname ,
215
+ query : query ,
216
+ } ) ;
217
+ query = undefined ;
218
+ }
192
219
193
220
if ( options . dryRun ) {
194
221
// dry run: return the API url (string) only
195
222
if ( typeof callback === "function" ) {
196
- callback . apply ( callback , [ undefined , helper . dryRun ( config , pathname ) ] ) ;
223
+ callback . apply ( callback , [ undefined , helper . dryRun ( config , pathname , query ) ] ) ;
197
224
}
198
225
} else {
199
226
// make the real API call
200
227
get . call (
201
228
this ,
202
229
config ,
203
230
pathname ,
231
+ query ,
204
232
options . proxy ,
205
233
options . agent ,
206
234
function apiCallback ( err , data , info ) {
@@ -931,6 +959,7 @@ WebPageTest.filenames = filenames;
931
959
WebPageTest . defaultServer = "https://www.webpagetest.org" ;
932
960
WebPageTest . defaultListenPort = 7791 ;
933
961
WebPageTest . defaultWaitResultsPort = 8000 ;
962
+ WebPageTest . defaultHTTPMethod = "GET" ;
934
963
935
964
// Version
936
965
Object . defineProperty ( WebPageTest , "version" , {
0 commit comments