1 | 'use strict'
|
2 |
|
3 | var request = require('request'),
|
4 | check = require('../check'),
|
5 | async = require('async'),
|
6 | stringify = require('../stringify'),
|
7 | normalizePath = require('path').posix.normalize,
|
8 | parse = require('url').parse,
|
9 | format = require('url').format
|
10 |
|
11 |
|
12 |
|
13 |
|
14 | function Case() {
|
15 |
|
16 | this.name = ''
|
17 |
|
18 |
|
19 | this.skip = false
|
20 |
|
21 |
|
22 | this.post = null
|
23 |
|
24 |
|
25 | this.postUrl = ''
|
26 |
|
27 |
|
28 | this.out = null
|
29 |
|
30 |
|
31 | this.statusCode = 0
|
32 |
|
33 |
|
34 | this.finds = []
|
35 | }
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 |
|
50 |
|
51 |
|
52 | Case.prototype.execute = function (options, testName) {
|
53 | var that = this,
|
54 | it = this.skip ? options.it.skip : options.it
|
55 |
|
56 | it(this.name, function (done) {
|
57 |
|
58 | options.context.prev = {
|
59 | post: options.context.post,
|
60 | out: options.context.out
|
61 | }
|
62 | delete options.context.post
|
63 | delete options.context.out
|
64 |
|
65 | var post = that.post.execute(options.context, '<post>')
|
66 | options.context.post = post
|
67 | if (options.onPost) {
|
68 | options.onPost(that, post)
|
69 | }
|
70 |
|
71 | request({
|
72 | url: normalizeUrl(options.baseUrl + (that.postUrl || testName)),
|
73 | method: 'POST',
|
74 | json: post,
|
75 | agentOptions: {
|
76 | ca: options.ca
|
77 | }
|
78 | }, function (err, res, out) {
|
79 | var expected
|
80 | if (err) {
|
81 | return done(err)
|
82 | }
|
83 | options.context.out = out
|
84 | expected = that.out.execute(options.context, '<out>')
|
85 |
|
86 | try {
|
87 | res.statusCode.should.be.equal(that.statusCode)
|
88 | check(out, expected, options.strict)
|
89 | if (options.onOut) {
|
90 | options.onOut(that, out)
|
91 | }
|
92 | } catch (e) {
|
93 | console.log('\n-----\n' +
|
94 | 'Request details:\n' +
|
95 | '\x1b[1;32mInput:\x1b[0m\n' +
|
96 | stringify(post, true) + '\n' +
|
97 | '\x1b[1;32mOutput:\x1b[0m\n' +
|
98 | stringify(out, true, e.path) + '\n' +
|
99 | '\x1b[1;32mExpected:\x1b[0m\n' +
|
100 | stringify(expected, true, e.path) + '\n' +
|
101 | '-----\n')
|
102 | throw e
|
103 | }
|
104 | async.each(that.finds, function (find, done) {
|
105 | if (options.onFind) {
|
106 | options.onFind(that, find)
|
107 | }
|
108 | find.execute(options, done)
|
109 | }, done)
|
110 | })
|
111 | })
|
112 | }
|
113 |
|
114 | module.exports = Case
|
115 |
|
116 | function normalizeUrl(url) {
|
117 | var parsed = parse(url)
|
118 | parsed.pathname = normalizePath(parsed.pathname)
|
119 | return format(parsed)
|
120 | } |
\ | No newline at end of file |