1 | 'use strict';
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | const methods = require('methods');
|
7 | const http = require('http');
|
8 | let http2;
|
9 | try {
|
10 | http2 = require('http2');
|
11 | } catch (_) {
|
12 |
|
13 | }
|
14 | const Test = require('./lib/test.js');
|
15 | const agent = require('./lib/agent.js');
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 | module.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);
|
36 | } else {
|
37 | app = http.createServer(app);
|
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 |
|
52 | obj.del = obj.delete;
|
53 |
|
54 | return obj;
|
55 | };
|
56 |
|
57 |
|
58 |
|
59 |
|
60 | module.exports.Test = Test;
|
61 |
|
62 |
|
63 |
|
64 |
|
65 | module.exports.agent = agent;
|