Skip to content

Commit 0817f40

Browse files
committed
method in configs
1 parent dee4ed0 commit 0817f40

File tree

4 files changed

+18
-7
lines changed

4 files changed

+18
-7
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ webpagetest --help
9898
- **-s, --server** _\<server\>_: the WPT server URL [https://www.webpagetest.org]
9999
- **-d, --dryrun**: just return the RESTful API URL
100100
- **-o, --out** _\<file\>_: place the output into \<file\>. Defaults to stdout
101+
- **--http_method** _\<method\>_: the HTTP method to use (GET, POST) [GET]
101102

102103
_The default WPT server can also be specified via environment variable `WEBPAGETEST_SERVER`_
103104

@@ -522,6 +523,7 @@ wpt.runTest(script, (err, data) => {
522523

523524
- **dryRun**: _Boolean_, if `true`, method does not make an actual request to the API Server but rather returns an object with `url` which contains the actual URL to make the GET request to WebPageTest API Server
524525
- **server**: _String_, if specified, overrides the WebPageTest server informed in the constructor only for that method call
526+
- **http_method**: _String_, if specified, overrides the HTTP method in the constructor only for that method call (GET, POST) [GET]
525527

526528
#### Test (works with `runTest` and `runTestAndWait`)
527529

lib/mapping.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ var options = {
2121
bool: true,
2222
info: "just return the RESTful API URL",
2323
},
24+
http_method: {
25+
name: "http_method",
26+
info: "HTTP method to use for submitting the test (GET or POST) [GET]",
27+
}
2428
},
2529
test: {
2630
location: {

lib/webpagetest.js

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ var http = require("http"),
1919
var reSpace = /\s/,
2020
reConnectivity =
2121
/^(?:Cable|DSL|3GSlow|3G|3GFast|4G|LTE|Edge|2G|Dial|FIOS|Native|custom)$/,
22-
reHTMLOutput = /<h\d[^<]*>([^<]+)<\/h\d>/; // for H3 on cancelTest.php
22+
reHTMLOutput = /<h\d[^<]*>([^<]+)<\/h\d>/, // for H3 on cancelTest.php
23+
reHTTPmethods = /^(?:GET|POST)$/;
2324

2425
var paths = {
2526
testStatus: "testStatus.php",
@@ -169,10 +170,10 @@ function get(config, pathname, data, proxy, agent, callback, encoding) {
169170

170171
if (options.method == "POST") {
171172
return request.end(qs.stringify(data));
172-
} else {
173-
return request.end();
174173
}
175174

175+
return request.end();
176+
176177
}
177178

178179
// execute callback properly normalizing optional args
@@ -202,10 +203,12 @@ function api(pathname, callback, query, options) {
202203

203204
pathname = url.resolve(config.pathname, pathname);
204205

205-
config.method = url.format({
206-
pathname: pathname,
207-
query: query,
208-
}).toString().length > 6 * 1024 ? "POST" : "GET";
206+
if (reHTTPmethods.test(options.http_method)) {
207+
config.method = options.http_method;
208+
} else {
209+
config.method = WebPageTest.defaultHTTPMethod;
210+
}
211+
209212

210213
if (config.method == "GET") {
211214
pathname = url.format({
@@ -960,6 +963,7 @@ WebPageTest.filenames = filenames;
960963
WebPageTest.defaultServer = "https://www.webpagetest.org";
961964
WebPageTest.defaultListenPort = 7791;
962965
WebPageTest.defaultWaitResultsPort = 8000;
966+
WebPageTest.defaultHTTPMethod = "GET";
963967

964968
// Version
965969
Object.defineProperty(WebPageTest, "version", {

test/edge-cases-test.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ describe('Edge Cases of', function() {
8989
wpt.runTest('http://foobar.com', {
9090
dryRun: true,
9191
mobile: 1,
92+
http_method: 'POST',
9293
custom: '[example]\n\\\\' + 'X'.repeat(6 * 1024) + '\nreturn 1;'
9394
}, function (err, data) {
9495
if (err) return done(err);

0 commit comments

Comments
 (0)