Skip to content

Commit

Permalink
test: improve tests for test-http-url.parse
Browse files Browse the repository at this point in the history
PR-URL: #18523
Reviewed-By: Jon Moss <[email protected]>
Reviewed-By: Ruben Bridgewater <[email protected]>
Reviewed-By: James M Snell <[email protected]>
  • Loading branch information
starkwang authored and MylesBorins committed Feb 21, 2018
1 parent 232ea3e commit 730a1ce
Show file tree
Hide file tree
Showing 8 changed files with 25 additions and 68 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ function check(request) {

const server = http.createServer(function(request, response) {
// run the check function
check.call(this, request, response);
check(request);
response.writeHead(200, {});
response.end('ok');
server.close();
Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-http-url.parse-auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ function check(request) {

const server = http.createServer(function(request, response) {
// run the check function
check.call(this, request, response);
check(request);
response.writeHead(200, {});
response.end('ok');
server.close();
Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-http-url.parse-basic.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ function check(request) {

const server = http.createServer(function(request, response) {
// run the check function
check.call(this, request, response);
check(request);
response.writeHead(200, {});
response.end('ok');
server.close();
Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-http-url.parse-https.request.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ function check(request) {

const server = https.createServer(httpsOptions, function(request, response) {
// run the check function
check.call(this, request, response);
check(request);
response.writeHead(200, {});
response.end('ok');
server.close();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,68 +20,25 @@
// USE OR OTHER DEALINGS IN THE SOFTWARE.

'use strict';
require('../common');
const assert = require('assert');
const common = require('../common');
const http = require('http');
const url = require('url');


assert.throws(function() {
http.request(url.parse('file:///whatever'));
}, function(err) {
if (err instanceof Error) {
assert.strictEqual(
err.message, 'Protocol "file:" not supported. Expected "http:"');
return true;
}
});

assert.throws(function() {
http.request(url.parse('mailto:[email protected]'));
}, function(err) {
if (err instanceof Error) {
assert.strictEqual(
err.message, 'Protocol "mailto:" not supported. Expected "http:"');
return true;
}
});

assert.throws(function() {
http.request(url.parse('ftp://www.example.com'));
}, function(err) {
if (err instanceof Error) {
assert.strictEqual(
err.message, 'Protocol "ftp:" not supported. Expected "http:"');
return true;
}
});

assert.throws(function() {
http.request(url.parse('javascript:alert(\'hello\');'));
}, function(err) {
if (err instanceof Error) {
assert.strictEqual(
err.message, 'Protocol "javascript:" not supported. Expected "http:"');
return true;
}
});

assert.throws(function() {
http.request(url.parse('xmpp:[email protected]'));
}, function(err) {
if (err instanceof Error) {
assert.strictEqual(
err.message, 'Protocol "xmpp:" not supported. Expected "http:"');
return true;
}
});

assert.throws(function() {
http.request(url.parse('f://some.host/path'));
}, function(err) {
if (err instanceof Error) {
assert.strictEqual(
err.message, 'Protocol "f:" not supported. Expected "http:"');
return true;
}
const invalidUrls = [
'file:///whatever',
'mailto:[email protected]',
'ftp://www.example.com',
'javascript:alert(\'hello\');',
'xmpp:[email protected]',
'f://some.host/path'
];

invalidUrls.forEach((invalid) => {
common.expectsError(
() => { http.request(url.parse(invalid)); },
{
code: 'ERR_INVALID_PROTOCOL',
type: Error
}
);
});
2 changes: 1 addition & 1 deletion test/parallel/test-http-url.parse-path.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ function check(request) {

const server = http.createServer(function(request, response) {
// run the check function
check.call(this, request, response);
check(request);
response.writeHead(200, {});
response.end('ok');
server.close();
Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-http-url.parse-post.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ function check(request) {

const server = http.createServer(function(request, response) {
// run the check function
check.call(this, request, response);
check(request);
response.writeHead(200, {});
response.end('ok');
server.close();
Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-http-url.parse-search.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ function check(request) {

const server = http.createServer(function(request, response) {
// run the check function
check.call(this, request, response);
check(request);
response.writeHead(200, {});
response.end('ok');
server.close();
Expand Down

0 comments on commit 730a1ce

Please sign in to comment.