1 | "use strict";
|
2 |
|
3 | var prepareItem = require("./prepareItem"),
|
4 | async = require("async"),
|
5 | supertest = require("supertest"),
|
6 | prepareOptions = require("./prepareOptions");
|
7 |
|
8 | function prepareRequest(base, state, cb) {
|
9 | process.nextTick(function () {
|
10 | var item = state.item,
|
11 | r = new supertest.Test(base, item.method, item.path);
|
12 | if (item.code) {
|
13 | r.expect(item.code);
|
14 | }
|
15 | if (item.data) {
|
16 | r.send(item.data);
|
17 | }
|
18 | if (item.json) {
|
19 | r.set('Accept', 'application/json');
|
20 | r.expect('Content-Type', 'application/json; charset=UTF-8');
|
21 | }
|
22 | if (typeof item.maxRedirects === "number" && item.maxRedirects >= 0) {
|
23 | r.redirects(item.maxRedirects);
|
24 | }
|
25 | state.request = r;
|
26 | cb(null, state);
|
27 | });
|
28 | }
|
29 |
|
30 | function processItem(options, stats, item, cb) {
|
31 | var state = {
|
32 | item: prepareItem(item, options.defaults),
|
33 | request: null,
|
34 | error: null
|
35 | };
|
36 |
|
37 | options.output.endpointStart(state.item);
|
38 |
|
39 | async.series([
|
40 | prepareRequest.bind(null, options.base, state),
|
41 | options.beforeEach.bind(options, state),
|
42 | state.item.before.bind(state.item, state),
|
43 | function (cb) {
|
44 | state.request.end(function (err, res) {
|
45 | if (err) {
|
46 | state.error = err;
|
47 | cb();
|
48 | return;
|
49 | }
|
50 | if (item.json) {
|
51 | var json;
|
52 | try {
|
53 | json = JSON.parse(res.text);
|
54 | } catch(e) {
|
55 | e.message = "Error while parsing expected JSON response: " + e.message + "\n\n" + res.text;
|
56 | state.error = e;
|
57 | return cb();
|
58 | }
|
59 | item.json.validate(res.text, function (err, data) {
|
60 | if (err) {
|
61 | state.error = err;
|
62 | } else {
|
63 | stats.passed += 1;
|
64 | }
|
65 | cb();
|
66 | });
|
67 | return;
|
68 | }
|
69 | if (item.result) {
|
70 | if (typeof item.result === "function") {
|
71 | return item.result(res.text, cb);
|
72 | } else if (item.result !== res.text) {
|
73 | state.error = new Error("Response doesn't match the expected result.");
|
74 | }
|
75 | }
|
76 | cb();
|
77 | })
|
78 | },
|
79 | state.item.after.bind(state.item, state),
|
80 | options.afterEach.bind(options, state),
|
81 | function (cb) {
|
82 | process.nextTick(function () {
|
83 | options.output.endpointEnd(state);
|
84 | cb();
|
85 | });
|
86 | }
|
87 | ], cb);
|
88 | }
|
89 |
|
90 | function runTests(options, cb) {
|
91 | var stats = {
|
92 | passed: 0
|
93 | },
|
94 | isSuccess = false;
|
95 |
|
96 | options = prepareOptions(options);
|
97 |
|
98 | async.series([
|
99 | function (cb) {
|
100 | process.nextTick(function () {
|
101 | options.output.start(options.base);
|
102 | cb();
|
103 | });
|
104 | },
|
105 | options.before.bind(options),
|
106 | async.mapSeries.bind(async,
|
107 | options.tests,
|
108 | processItem.bind(null, options, stats)
|
109 | ),
|
110 | options.after.bind(options),
|
111 | function (cb) {
|
112 | process.nextTick(function () {
|
113 | isSuccess = stats.passed === options.tests.length;
|
114 | options.output.end(stats.passed, options.tests.length, cb);
|
115 | });
|
116 | }
|
117 | ], function () {
|
118 | if (cb) {
|
119 | cb(null, isSuccess);
|
120 | } else {
|
121 | process.exit(isSuccess ? 0 : 1);
|
122 | }
|
123 | });
|
124 | }
|
125 |
|
126 | module.exports = runTests; |
\ | No newline at end of file |