Skip to content

Commit daacc92

Browse files
committed
deduplicated get and post functions
1 parent 27d3353 commit daacc92

File tree

3 files changed

+38
-127
lines changed

3 files changed

+38
-127
lines changed

lib/helper.js

Lines changed: 6 additions & 6 deletions
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,10 +156,10 @@ function scriptToString(data) {
155156
}
156157

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

161-
if (params && params.custom) {
162+
if (config.method == "POST") {
162163
return {
163164
url: url.format({
164165
protocol: config.protocol,
@@ -167,9 +168,8 @@ function dryRun(config, path, params) {
167168
config.port : undefined),
168169
pathname: path.pathname,
169170
}),
170-
form: params
171+
form: qs.stringify(form)
171172
};
172-
173173
} else {
174174
return {
175175
url: url.format({
@@ -178,7 +178,7 @@ function dryRun(config, path, params) {
178178
port: (config.port !== 80 && config.port !== 443 ?
179179
config.port : undefined),
180180
pathname: path.pathname,
181-
query: params
181+
query: path.query
182182
})
183183
};
184184
}

lib/webpagetest.js

Lines changed: 29 additions & 119 deletions
Original file line numberDiff line numberDiff line change
@@ -58,15 +58,10 @@ var filenames = {
5858
cached: "_Cached",
5959
};
6060

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

65-
pathname = url.format({
66-
pathname: pathname,
67-
query: query,
68-
});
69-
7065
if (proxy) {
7166
var proxyUrl = url.parse(proxy);
7267
var pathForProxy = config.protocol + "//";
@@ -85,6 +80,8 @@ function get(config, pathname, query, proxy, agent, callback, encoding) {
8580
headers: {
8681
Host: config.hostname,
8782
},
83+
method: config.method
84+
8885
};
8986
} else {
9087
protocol = config.protocol === "https:" ? https : http;
@@ -93,117 +90,13 @@ function get(config, pathname, query, proxy, agent, callback, encoding) {
9390
host: config.hostname,
9491
auth: config.auth,
9592
port: config.port,
93+
method: config.method,
9694
headers: {},
9795
};
9896
}
9997

100-
if (encoding !== "binary") {
101-
options.headers["X-WPT-API-KEY"] = this.config.key;
102-
options.headers["accept-encoding"] = "gzip,deflate";
103-
options.headers["User-Agent"] = "WebpagetestNodeWrapper/v0.6.0";
104-
}
105-
106-
if (agent) {
107-
options.agent = agent;
108-
}
109-
110-
return protocol
111-
.get(options, function getResponse(res) {
112-
var data,
113-
length,
114-
statusCode = res.statusCode;
115-
116-
if (statusCode !== 200) {
117-
callback(
118-
new helper.WPTAPIError(statusCode, http.STATUS_CODES[statusCode])
119-
);
120-
} else {
121-
data = [];
122-
length = 0;
123-
124-
encoding = res.headers["content-encoding"] || encoding || "uft8";
125-
126-
res.on("data", function onData(chunk) {
127-
data.push(chunk);
128-
length += chunk.length;
129-
});
130-
131-
res.on("end", function onEnd() {
132-
var i,
133-
len,
134-
pos,
135-
buffer = new Buffer.alloc(length),
136-
type = (res.headers["content-type"] || "").split(";")[0];
137-
138-
for (i = 0, len = data.length, pos = 0; i < len; i += 1) {
139-
data[i].copy(buffer, pos);
140-
pos += data[i].length;
141-
}
142-
143-
if (encoding === "gzip" || encoding === "deflate") {
144-
// compressed response (gzip,deflate)
145-
zlib.unzip(buffer, function unzip(err, buffer) {
146-
if (err) {
147-
callback(err);
148-
} else {
149-
callback(undefined, buffer.toString(), {
150-
type: type,
151-
encoding: encoding,
152-
});
153-
}
154-
});
155-
} else {
156-
// uncompressed response
157-
callback(undefined, buffer, {
158-
type: type,
159-
encoding: encoding,
160-
});
161-
}
162-
});
163-
}
164-
})
165-
.on("error", function onError(err) {
166-
callback(err);
167-
});
168-
}
169-
170-
// execute runTest using POST request
171-
function post(config, pathname, query, proxy, agent, callback, encoding) {
172-
var protocol, options;
173-
174-
if (proxy) {
175-
var proxyUrl = url.parse(proxy);
176-
var pathForProxy = config.protocol + "//";
177-
178-
if (config.auth) {
179-
pathForProxy += config.auth + "@";
180-
}
181-
182-
pathForProxy += config.hostname + ":" + config.port + pathname;
183-
protocol = proxyUrl.protocol === "https:" ? https : http;
184-
185-
options = {
186-
host: proxyUrl.hostname,
187-
port: proxyUrl.port,
188-
path: pathForProxy,
189-
method: "POST",
190-
headers: {
191-
Host: config.hostname,
192-
},
193-
194-
};
195-
} else {
196-
protocol = config.protocol === "https:" ? https : http;
197-
options = {
198-
path: pathname,
199-
host: config.hostname,
200-
auth: config.auth,
201-
port: config.port,
202-
method: "POST",
203-
headers: {
204-
'Content-Type': 'application/x-www-form-urlencoded',
205-
},
206-
};
98+
if (options.method == "POST") {
99+
options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
207100
}
208101

209102
if (encoding !== "binary") {
@@ -216,9 +109,7 @@ function post(config, pathname, query, proxy, agent, callback, encoding) {
216109
options.agent = agent;
217110
}
218111

219-
postData = qs.stringify(query)
220-
221-
return protocol
112+
var request = protocol
222113
.request(options, function getResponse(res) {
223114
var data,
224115
length,
@@ -276,7 +167,13 @@ function post(config, pathname, query, proxy, agent, callback, encoding) {
276167
.on("error", function onError(err) {
277168
callback(err);
278169
})
279-
.end(postData);
170+
171+
if (options.method == "POST") {
172+
return request.end(qs.stringify(data));
173+
} else {
174+
return request.end();
175+
}
176+
280177
}
281178

282179
// execute callback properly normalizing optional args
@@ -306,14 +203,27 @@ function api(pathname, callback, query, options) {
306203

307204
pathname = url.resolve(config.pathname, pathname);
308205

206+
config.method = url.format({
207+
pathname: pathname,
208+
query: query,
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+
}
218+
309219
if (options.dryRun) {
310220
// dry run: return the API url (string) only
311221
if (typeof callback === "function") {
312222
callback.apply(callback, [undefined, helper.dryRun(config, pathname, query)]);
313223
}
314224
} else {
315225
// make the real API call
316-
(options.custom !== undefined ? post : get).call(
226+
get.call(
317227
this,
318228
config,
319229
pathname,

test/edge-cases-test.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,11 +88,12 @@ describe('Edge Cases of', function() {
8888
it('gets a test with custom metrics then returns API url and payload with custom metrics data present', function (done) {
8989
wpt.runTest('http://foobar.com', {
9090
dryRun: true,
91-
custom: '[example]\nreturn 1;'
91+
mobile: 1,
92+
custom: '[example]\n\\\\' + 'X'.repeat(6 * 1024) + '\nreturn 1;'
9293
}, function (err, data) {
9394
if (err) return done(err);
9495
assert.equal(data.url, wptServer + 'runtest.php');
95-
assert.equal(data.form.custom, '[example]\nreturn 1;');
96+
assert.equal(data.form.length, 6233);
9697
done();
9798
});
9899
});

0 commit comments

Comments
 (0)