UNPKG

1.3 kBJavaScriptView Raw
1'use strict';
2
3/**
4 * Module dependencies.
5 */
6const methods = require('methods');
7const http = require('http');
8let http2;
9try {
10 http2 = require('http2'); // eslint-disable-line global-require
11} catch (_) {
12 // eslint-disable-line no-empty
13}
14const Test = require('./lib/test.js');
15const agent = require('./lib/agent.js');
16
17/**
18 * Test against the given `app`,
19 * returning a new `Test`.
20 *
21 * @param {Function|Server} app
22 * @return {Test}
23 * @api public
24 */
25module.exports = function(app, options = {}) {
26 const obj = {};
27
28 if (typeof app === 'function') {
29 if (options.http2) {
30 if (!http2) {
31 throw new Error(
32 'supertest: this version of Node.js does not support http2'
33 );
34 }
35 app = http2.createServer(app); // eslint-disable-line no-param-reassign
36 } else {
37 app = http.createServer(app); // eslint-disable-line no-param-reassign
38 }
39 }
40
41 methods.forEach(function(method) {
42 obj[method] = function(url) {
43 var test = new Test(app, method, url);
44 if (options.http2) {
45 test.http2();
46 }
47 return test;
48 };
49 });
50
51 // Support previous use of del
52 obj.del = obj.delete;
53
54 return obj;
55};
56
57/**
58 * Expose `Test`
59 */
60module.exports.Test = Test;
61
62/**
63 * Expose the agent function
64 */
65module.exports.agent = agent;