-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.lib.js
168 lines (162 loc) · 5.36 KB
/
test.lib.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
var mw_f = require("../")
, mw
, order
, req = {}
, _query = {}
, _body = {}
, _params = {}
, dv = "default" + Math.random()
;
Object.defineProperties( req
, { query :
{ get:
function() {
order.push("query");
return _query;
}
}
, body :
{ get:
function() {
order.push("body");
return _body;
}
}
, params :
{ get:
function() {
order.push("params");
return _params;
}
}
}
)
module.exports =
{ "request-param" :
{ "should be a middleware factory, names 1 argument - cfg" :
function() {
mw_f.should.be.a.Function;
mw_f.length.should.eql(1);
}
, "using the factory without arguments" :
{ "should not fail" :
function() {
mw = mw_f()
}
, "should return a middleware handler" :
function() {
mw.should.be.a.Function;
mw.length.should.eql(3);
}
, "using the middelware with valid arguments" :
{ "should not fail and call the done" :
function(done) {
mw(req, null, done);
}
, "should decorate the request with a method 'param', names 3 args: field, default, order" :
function() {
req.should.have.property('param');
req.param.should.be.a.Function;
req.param.length.should.eql(3);
}
, "using req.param(..) api " :
{ afterEach:
function() {
delete _body.field;
delete _query.field;
delete _params.field;
}
, "should search by default order(first in 'params', then in 'body', then in 'query' and last - return defaultValue)" :
function() {
order = [];
req.param("field", dv).should.eql(dv);
order.should.eql( ["params","query","body"] );
}
, "should stop on the first collection that has the searched field" :
function test_stops_on_first() {
order = [];
_body.field = "body" + Math.random();
req.param("field", dv).should.eql(_body.field);
order.should.eql( ["params","query","body"] );
order = [];
_query.field = "query" + Math.random();
req.param("field", dv).should.eql(_query.field);
order.should.eql( ["params","query"] );
order = [];
_params.field = "params" + Math.random();
req.param("field", dv).should.eql(_params.field);
order.should.eql( ["params"] );
}
, "with injected order" :
{ "should search the collections by the injeted order, ending with defaultValue" :
function() {
order = [];
req.param("no-such-field", dv, ["query", "body", "params"]).should.eql(dv);
order.should.eql( ["query", "body", "params"] );
}
}
}
}
}
, "using the factory with settings object with 'order' attribute as valid array of strings" :
{ "should not fail" :
function() {
mw = mw_f( {order: ["body", "query", "params"] } )
}
, "should return a middleware handler" :
function() {
mw.should.be.a.Function;
mw.length.should.eql(3);
}
, "using the middleware with valid arguments" :
{ "should not fail and call the done" :
function(done) {
mw(req, null, done);
}
, "should decorate the request with a method 'param', names 3 args: field, default, order" :
function() {
req.should.have.property('param');
req.param.should.be.a.Function;
req.param.length.should.eql(3);
}
, "using req.param(..) api " :
{ afterEach:
function() {
delete _body.field;
delete _query.field;
delete _params.field;
}
, "should search the collections by the order they are given" :
function() {
order = [];
req.param("no-such-field", dv).should.eql(dv);
order.should.eql( ["body", "query", "params"] );
}
, "should stop on the first collection that has the searched field" :
function test_stops_on_first() {
order = [];
_params.field = "params" + Math.random();
req.param("field", dv).should.eql(_params.field);
order.should.eql( ["body", "query", "params"] );
order = [];
_query.field = "query" + Math.random();
req.param("field", dv).should.eql(_query.field);
order.should.eql( ["body","query"] );
order = [];
_body.field = "body" + Math.random();
req.param("field", dv).should.eql(_body.field);
order.should.eql( ["body"] );
}
, "with in-call injected order" :
{ "should search the collections by the injeted order, ending with defaultValue" :
function() {
order = [];
req.param("no-such-field", dv, ["query", "body", "params"]).should.eql(dv);
order.should.eql( ["query", "body", "params"] );
}
}
}
}
}
}
}