Skip to content

Commit b55f6b7

Browse files
authored
Merge pull request #1 from max-ostapenko/post-request-support-for-runTest
Post request support for run test
2 parents 51e87f2 + daacc92 commit b55f6b7

File tree

3 files changed

+95
-42
lines changed

3 files changed

+95
-42
lines changed

lib/helper.js

Lines changed: 46 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,25 @@
55
* Released under the MIT License
66
*/
77

8-
var xml2js = require('xml2js'),
9-
url = require('url'),
10-
os = require('os'),
11-
csv = require('csv'),
12-
entities = require('entities');
8+
var xml2js = require('xml2js'),
9+
url = require('url'),
10+
os = require('os'),
11+
csv = require('csv'),
12+
entities = require('entities'),
13+
qs = require('querystring');
1314

14-
var parser = new xml2js.Parser({explicitArray: false, mergeAttrs: true});
15+
var parser = new xml2js.Parser({ explicitArray: false, mergeAttrs: true });
1516

1617
var reNumber = /^[\.\+\-]?[\d\.]+$/,
17-
reInvalidDec = /(?:\.\d*){2,}/,
18-
reDec = /\./,
19-
reLineBreak = /[\n\r]+/g,
20-
reLastBreak = /\n$/,
21-
reProtocol = /^https?:\/\//i,
22-
reIp = /\d+\.\d+\.\d+\.\d+/, // 127.0.0.1
18+
reInvalidDec = /(?:\.\d*){2,}/,
19+
reDec = /\./,
20+
reLineBreak = /[\n\r]+/g,
21+
reLastBreak = /\n$/,
22+
reProtocol = /^https?:\/\//i,
23+
reIp = /\d+\.\d+\.\d+\.\d+/, // 127.0.0.1
2324

24-
TAB = '\t',
25-
NEWLINE = '\n';
25+
TAB = '\t',
26+
NEWLINE = '\n';
2627

2728
function parseNumber(s) {
2829
if (typeof s !== 'string' || !reNumber.test(s) || reInvalidDec.test(s)) {
@@ -34,7 +35,7 @@ function parseNumber(s) {
3435

3536
function normalizeObj(root) {
3637
if (typeof root === 'object') {
37-
Object.keys(root).forEach(function(key) {
38+
Object.keys(root).forEach(function (key) {
3839
var value = root[key];
3940
if (typeof value === 'string') {
4041
if (value.length === 0 || value === '\n') {
@@ -62,8 +63,8 @@ function xmlToObj(xml, callback) {
6263

6364
function svToObj(delimiter, headers, sv) {
6465
var data,
65-
start = 0,
66-
obj = {};
66+
start = 0,
67+
obj = {};
6768

6869
delimiter = delimiter || ',';
6970

@@ -101,11 +102,11 @@ function svToObj(delimiter, headers, sv) {
101102
}
102103

103104
function csvParser(data, callback) {
104-
csv.parse(data.toString(), { columns: true }, function(err, data) {
105+
csv.parse(data.toString(), { columns: true }, function (err, data) {
105106
if (err) {
106107
callback.bind(this, err);
107108
}
108-
csv.transform(data, function(row) {
109+
csv.transform(data, function (row) {
109110
var key, value;
110111
for (key in row) {
111112
value = row[key].replace(/<b>|<\/b>/g, '');
@@ -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
@@ -221,8 +235,8 @@ function setQuery(map, options, query) {
221235
map.options.forEach(function eachOpts(opt) {
222236
Object.keys(opt).forEach(function eachOpt(key) {
223237
var param = opt[key],
224-
name = param.name,
225-
value = options[name] || options[key];
238+
name = param.name,
239+
value = options[name] || options[key];
226240

227241
if (value !== undefined && param.api) {
228242
if (param.array) {

lib/webpagetest.js

Lines changed: 36 additions & 10 deletions
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,8 @@ function get(config, pathname, proxy, agent, callback, encoding) {
7980
headers: {
8081
Host: config.hostname,
8182
},
83+
method: config.method
84+
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
if (encoding !== "binary") {
95103
options.headers["X-WPT-API-KEY"] = this.config.key;
96104
options.headers["accept-encoding"] = "gzip,deflate";
@@ -101,8 +109,8 @@ function get(config, pathname, proxy, agent, callback, encoding) {
101109
options.agent = agent;
102110
}
103111

104-
return protocol
105-
.get(options, function getResponse(res) {
112+
var request = protocol
113+
.request(options, function getResponse(res) {
106114
var data,
107115
length,
108116
statusCode = res.statusCode;
@@ -158,7 +166,14 @@ function get(config, pathname, proxy, agent, callback, encoding) {
158166
})
159167
.on("error", function onError(err) {
160168
callback(err);
161-
});
169+
})
170+
171+
if (options.method == "POST") {
172+
return request.end(qs.stringify(data));
173+
} else {
174+
return request.end();
175+
}
176+
162177
}
163178

164179
// execute callback properly normalizing optional args
@@ -186,22 +201,33 @@ function api(pathname, callback, query, options) {
186201
config = this.config;
187202
}
188203

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

194219
if (options.dryRun) {
195220
// dry run: return the API url (string) only
196221
if (typeof callback === "function") {
197-
callback.apply(callback, [undefined, helper.dryRun(config, pathname)]);
222+
callback.apply(callback, [undefined, helper.dryRun(config, pathname, query)]);
198223
}
199224
} else {
200225
// make the real API call
201226
get.call(
202227
this,
203228
config,
204229
pathname,
230+
query,
205231
options.proxy,
206232
options.agent,
207233
function apiCallback(err, data, info) {

test/edge-cases-test.js

Lines changed: 13 additions & 0 deletions
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)