@@ -13,12 +13,14 @@ var http = require("http"),
1313 specs = require ( "./specs" ) ,
1414 helper = require ( "./helper" ) ,
1515 server = require ( "./server" ) ,
16- mapping = require ( "./mapping" ) ;
16+ mapping = require ( "./mapping" ) ,
17+ qs = require ( 'querystring' ) ;
1718
1819var reSpace = / \s / ,
1920 reConnectivity =
2021 / ^ (?: 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 ) $ / ;
2224
2325var paths = {
2426 testStatus : "testStatus.php" ,
@@ -57,8 +59,8 @@ var filenames = {
5759 cached : "_Cached" ,
5860} ;
5961
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 ) {
6264 var protocol , options ;
6365
6466 if ( proxy ) {
@@ -79,6 +81,7 @@ function get(config, pathname, proxy, agent, callback, encoding) {
7981 headers : {
8082 Host : config . hostname ,
8183 } ,
84+ method : config . method
8285 } ;
8386 } else {
8487 protocol = config . protocol === "https:" ? https : http ;
@@ -87,10 +90,15 @@ function get(config, pathname, proxy, agent, callback, encoding) {
8790 host : config . hostname ,
8891 auth : config . auth ,
8992 port : config . port ,
93+ method : config . method ,
9094 headers : { } ,
9195 } ;
9296 }
9397
98+ if ( options . method == "POST" ) {
99+ options . headers [ 'Content-Type' ] = 'application/x-www-form-urlencoded' ;
100+ }
101+
94102 // api key always required
95103 options . headers [ "X-WPT-API-KEY" ] = this . config . key ;
96104 options . headers [ "accept-encoding" ] = "gzip,deflate" ;
@@ -100,8 +108,8 @@ function get(config, pathname, proxy, agent, callback, encoding) {
100108 options . agent = agent ;
101109 }
102110
103- return protocol
104- . get ( options , function getResponse ( res ) {
111+ var request = protocol
112+ . request ( options , function getResponse ( res ) {
105113 var data ,
106114 length ,
107115 statusCode = res . statusCode ;
@@ -158,6 +166,13 @@ function get(config, pathname, proxy, agent, callback, encoding) {
158166 . on ( "error" , function onError ( err ) {
159167 callback ( err ) ;
160168 } ) ;
169+
170+ if ( options . method == "POST" ) {
171+ return request . end ( qs . stringify ( data ) ) ;
172+ }
173+
174+ return request . end ( ) ;
175+
161176}
162177
163178// execute callback properly normalizing optional args
@@ -185,22 +200,35 @@ function api(pathname, callback, query, options) {
185200 config = this . config ;
186201 }
187202
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+ }
192219
193220 if ( options . dryRun ) {
194221 // dry run: return the API url (string) only
195222 if ( typeof callback === "function" ) {
196- callback . apply ( callback , [ undefined , helper . dryRun ( config , pathname ) ] ) ;
223+ callback . apply ( callback , [ undefined , helper . dryRun ( config , pathname , query ) ] ) ;
197224 }
198225 } else {
199226 // make the real API call
200227 get . call (
201228 this ,
202229 config ,
203230 pathname ,
231+ query ,
204232 options . proxy ,
205233 options . agent ,
206234 function apiCallback ( err , data , info ) {
@@ -931,6 +959,7 @@ WebPageTest.filenames = filenames;
931959WebPageTest . defaultServer = "https://www.webpagetest.org" ;
932960WebPageTest . defaultListenPort = 7791 ;
933961WebPageTest . defaultWaitResultsPort = 8000 ;
962+ WebPageTest . defaultHTTPMethod = "GET" ;
934963
935964// Version
936965Object . defineProperty ( WebPageTest , "version" , {
0 commit comments