From 730a1ceff374a7305683ad86321bcdfe665c3594 Mon Sep 17 00:00:00 2001 From: Weijia Wang <381152119@qq.com> Date: Fri, 2 Feb 2018 17:37:25 +0800 Subject: [PATCH] test: improve tests for test-http-url.parse PR-URL: https://github.com/nodejs/node/pull/18523 Reviewed-By: Jon Moss Reviewed-By: Ruben Bridgewater Reviewed-By: James M Snell --- ...p-url.parse-auth-with-header-in-request.js | 2 +- test/parallel/test-http-url.parse-auth.js | 2 +- test/parallel/test-http-url.parse-basic.js | 2 +- .../test-http-url.parse-https.request.js | 2 +- ....parse-only-support-http-https-protocol.js | 79 +++++-------------- test/parallel/test-http-url.parse-path.js | 2 +- test/parallel/test-http-url.parse-post.js | 2 +- test/parallel/test-http-url.parse-search.js | 2 +- 8 files changed, 25 insertions(+), 68 deletions(-) diff --git a/test/parallel/test-http-url.parse-auth-with-header-in-request.js b/test/parallel/test-http-url.parse-auth-with-header-in-request.js index b91dc143831086..c10f5ed7b0d31b 100644 --- a/test/parallel/test-http-url.parse-auth-with-header-in-request.js +++ b/test/parallel/test-http-url.parse-auth-with-header-in-request.js @@ -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(); diff --git a/test/parallel/test-http-url.parse-auth.js b/test/parallel/test-http-url.parse-auth.js index d166120ac2930c..ab597e132c8012 100644 --- a/test/parallel/test-http-url.parse-auth.js +++ b/test/parallel/test-http-url.parse-auth.js @@ -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(); diff --git a/test/parallel/test-http-url.parse-basic.js b/test/parallel/test-http-url.parse-basic.js index 0aa7616d05b205..a6ecd3918f1803 100644 --- a/test/parallel/test-http-url.parse-basic.js +++ b/test/parallel/test-http-url.parse-basic.js @@ -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(); diff --git a/test/parallel/test-http-url.parse-https.request.js b/test/parallel/test-http-url.parse-https.request.js index 87b7ac02e90573..f58be0257c1fc5 100644 --- a/test/parallel/test-http-url.parse-https.request.js +++ b/test/parallel/test-http-url.parse-https.request.js @@ -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(); diff --git a/test/parallel/test-http-url.parse-only-support-http-https-protocol.js b/test/parallel/test-http-url.parse-only-support-http-https-protocol.js index 903b4ba598babb..986f75d3076434 100644 --- a/test/parallel/test-http-url.parse-only-support-http-https-protocol.js +++ b/test/parallel/test-http-url.parse-only-support-http-https-protocol.js @@ -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:asdf@asdf.com')); -}, 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:isaacschlueter@jabber.org')); -}, 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:asdf@asdf.com', + 'ftp://www.example.com', + 'javascript:alert(\'hello\');', + 'xmpp:foo@bar.com', + 'f://some.host/path' +]; + +invalidUrls.forEach((invalid) => { + common.expectsError( + () => { http.request(url.parse(invalid)); }, + { + code: 'ERR_INVALID_PROTOCOL', + type: Error + } + ); }); diff --git a/test/parallel/test-http-url.parse-path.js b/test/parallel/test-http-url.parse-path.js index ab2e550f558cea..4cc5fbf48e015d 100644 --- a/test/parallel/test-http-url.parse-path.js +++ b/test/parallel/test-http-url.parse-path.js @@ -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(); diff --git a/test/parallel/test-http-url.parse-post.js b/test/parallel/test-http-url.parse-post.js index dab2e8f97ddac8..d8803cbfd267db 100644 --- a/test/parallel/test-http-url.parse-post.js +++ b/test/parallel/test-http-url.parse-post.js @@ -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(); diff --git a/test/parallel/test-http-url.parse-search.js b/test/parallel/test-http-url.parse-search.js index 53d06556c318f5..111cf97874b4d3 100644 --- a/test/parallel/test-http-url.parse-search.js +++ b/test/parallel/test-http-url.parse-search.js @@ -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();