From a76559325fb43adfad26c7c63d8a7c5900b618ef Mon Sep 17 00:00:00 2001 From: James Wright Date: Tue, 18 Apr 2017 00:40:39 +0100 Subject: [PATCH] Add convenience method for HTTP OPTIONS (#2541) * Add convenience method for HTTP OPTIONS. Tests * Suggested version no * Applying code review suggestions --- index.js | 1 + tests/test-options-convenience-method.js | 52 ++++++++++++++++++++++++ 2 files changed, 53 insertions(+) create mode 100644 tests/test-options-convenience-method.js diff --git a/index.js b/index.js index 979260db8..f9b480a1d 100755 --- a/index.js +++ b/index.js @@ -65,6 +65,7 @@ function verbFunc (verb) { // define like this to please codeintel/intellisense IDEs request.get = verbFunc('get') request.head = verbFunc('head') +request.options = verbFunc('options') request.post = verbFunc('post') request.put = verbFunc('put') request.patch = verbFunc('patch') diff --git a/tests/test-options-convenience-method.js b/tests/test-options-convenience-method.js new file mode 100644 index 000000000..a9063b6ef --- /dev/null +++ b/tests/test-options-convenience-method.js @@ -0,0 +1,52 @@ +'use strict' + +var server = require('./server') + , request = require('../index') + , tape = require('tape') + , destroyable = require('server-destroy') + +var s = server.createServer() + +destroyable(s) + +tape('setup', function (t) { + s.listen(0, function () { + s.on('/options', function (req, res) { + res.writeHead(200, { + 'x-original-method': req.method, + 'allow': 'OPTIONS, GET, HEAD' + }) + + res.end() + }) + + t.end() + }) +}) + +tape('options(string, function)', function (t) { + request.options(s.url + '/options', function (err, res) { + t.equal(err, null) + t.equal(res.statusCode, 200) + t.equal(res.headers['x-original-method'], 'OPTIONS') + t.end() + }) +}) + +tape('options(object, function)', function (t) { + request.options({ + url: s.url + '/options', + headers: { foo: 'bar' } + }, function (err, res) { + t.equal(err, null) + t.equal(res.statusCode, 200) + t.equal(res.headers['x-original-method'], 'OPTIONS') + t.end() + }) +}) + +tape('cleanup', function(t) { + s.destroy(function () { + t.end() + }) +}) \ No newline at end of file