Skip to content

Commit b28c65b

Browse files
committed
Post request support for run test
1 parent 51e87f2 commit b28c65b

File tree

3 files changed

+73
-21
lines changed

3 files changed

+73
-21
lines changed

lib/helper.js

+26-12
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ var xml2js = require('xml2js'),
99
url = require('url'),
1010
os = require('os'),
1111
csv = require('csv'),
12-
entities = require('entities');
12+
entities = require('entities')
13+
qs = require('querystring');
1314

1415
var parser = new xml2js.Parser({explicitArray: false, mergeAttrs: true});
1516

@@ -155,19 +156,32 @@ function scriptToString(data) {
155156
}
156157

157158
// Build the RESTful API url call only
158-
function dryRun(config, path) {
159+
function dryRun(config, path, form) {
159160
path = url.parse(path, true);
160161

161-
return {
162-
url: url.format({
163-
protocol: config.protocol,
164-
hostname: config.hostname,
165-
port: (config.port !== 80 && config.port !== 443 ?
166-
config.port : undefined),
167-
pathname: path.pathname,
168-
query: path.query
169-
})
170-
};
162+
if (config.method == "POST") {
163+
return {
164+
url: url.format({
165+
protocol: config.protocol,
166+
hostname: config.hostname,
167+
port: (config.port !== 80 && config.port !== 443 ?
168+
config.port : undefined),
169+
pathname: path.pathname,
170+
}),
171+
form: qs.stringify(form)
172+
};
173+
} else {
174+
return {
175+
url: url.format({
176+
protocol: config.protocol,
177+
hostname: config.hostname,
178+
port: (config.port !== 80 && config.port !== 443 ?
179+
config.port : undefined),
180+
pathname: path.pathname,
181+
query: path.query
182+
})
183+
};
184+
}
171185
}
172186

173187
// Normalize server config

lib/webpagetest.js

+34-9
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ 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

1819
var reSpace = /\s/,
1920
reConnectivity =
@@ -57,8 +58,8 @@ var filenames = {
5758
cached: "_Cached",
5859
};
5960

60-
// GET helper function
61-
function get(config, pathname, proxy, agent, callback, encoding) {
61+
// GET/POST helper function
62+
function get(config, pathname, data, proxy, agent, callback, encoding) {
6263
var protocol, options;
6364

6465
if (proxy) {
@@ -79,6 +80,7 @@ function get(config, pathname, proxy, agent, callback, encoding) {
7980
headers: {
8081
Host: config.hostname,
8182
},
83+
method: config.method
8284
};
8385
} else {
8486
protocol = config.protocol === "https:" ? https : http;
@@ -87,10 +89,15 @@ function get(config, pathname, proxy, agent, callback, encoding) {
8789
host: config.hostname,
8890
auth: config.auth,
8991
port: config.port,
92+
method: config.method,
9093
headers: {},
9194
};
9295
}
9396

97+
if (options.method == "POST") {
98+
options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
99+
}
100+
94101
if (encoding !== "binary") {
95102
options.headers["X-WPT-API-KEY"] = this.config.key;
96103
options.headers["accept-encoding"] = "gzip,deflate";
@@ -101,8 +108,8 @@ function get(config, pathname, proxy, agent, callback, encoding) {
101108
options.agent = agent;
102109
}
103110

104-
return protocol
105-
.get(options, function getResponse(res) {
111+
var request = protocol
112+
.request(options, function getResponse(res) {
106113
var data,
107114
length,
108115
statusCode = res.statusCode;
@@ -159,6 +166,13 @@ function get(config, pathname, proxy, agent, callback, encoding) {
159166
.on("error", function onError(err) {
160167
callback(err);
161168
});
169+
170+
if (options.method == "POST") {
171+
return request.end(qs.stringify(data));
172+
} else {
173+
return request.end();
174+
}
175+
162176
}
163177

164178
// execute callback properly normalizing optional args
@@ -186,22 +200,33 @@ function api(pathname, callback, query, options) {
186200
config = this.config;
187201
}
188202

189-
pathname = url.format({
190-
pathname: url.resolve(config.pathname, pathname),
203+
pathname = url.resolve(config.pathname, pathname);
204+
205+
config.method = url.format({
206+
pathname: pathname,
191207
query: query,
192-
});
208+
}).toString().length > 6 * 1024 ? "POST" : "GET";
209+
210+
if (config.method == "GET") {
211+
pathname = url.format({
212+
pathname: pathname,
213+
query: query,
214+
});
215+
query = undefined;
216+
}
193217

194218
if (options.dryRun) {
195219
// dry run: return the API url (string) only
196220
if (typeof callback === "function") {
197-
callback.apply(callback, [undefined, helper.dryRun(config, pathname)]);
221+
callback.apply(callback, [undefined, helper.dryRun(config, pathname, query)]);
198222
}
199223
} else {
200224
// make the real API call
201225
get.call(
202226
this,
203227
config,
204228
pathname,
229+
query,
205230
options.proxy,
206231
options.agent,
207232
function apiCallback(err, data, info) {

test/edge-cases-test.js

+13
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,19 @@ describe('Edge Cases of', function() {
8585
});
8686
});
8787

88+
it('gets a test with custom metrics then returns API url and payload with custom metrics data present', function (done) {
89+
wpt.runTest('http://foobar.com', {
90+
dryRun: true,
91+
mobile: 1,
92+
custom: '[example]\n\\\\' + 'X'.repeat(6 * 1024) + '\nreturn 1;'
93+
}, function (err, data) {
94+
if (err) return done(err);
95+
assert.equal(data.url, wptServer + 'runtest.php');
96+
assert.equal(data.form.length, 6233);
97+
done();
98+
});
99+
});
100+
88101
});
89102

90103
describe('WebPageTest localhost helper', function() {

0 commit comments

Comments
 (0)