-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
131 lines (120 loc) · 4.01 KB
/
test.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
const jsonparser = require('.');
function req(type, body){
return {
headers: {
'content-type': type || '',
'content-length' : body ? body.length : 0
},
body : body || null
};
}
describe('jsonparser', function() {
it('should exist', function() {
expect(jsonparser).toBeDefined();
});
it('should return a function', function() {
expect(jsonparser()).toBeInstanceOf(Function);
});
it('should fail if invalid json data with status code 400', function(done){
const json = { hello: 'world' };
const invalidJson = JSON.stringify(json) + '[]';
const reqObj = req('application/json', invalidJson);
const instance = jsonparser();
instance(reqObj, null, function(err){
expect(err).toBeDefined();
expect(err.status).toEqual(400);
expect(err.message).toBeDefined();
done();
});
});
it('should pass for valid json data', function(done){
const json = { hello: 'world'};
const validJson = JSON.stringify(json);
const reqObj = req('application/json', validJson);
const instance = jsonparser();
instance(reqObj, null, function(err){
if(err) {
return done(err);
}
expect(reqObj.json).toEqual(json);
done();
});
});
});
describe('jsonparser non-strict', function(){
it('should neglect content type', function(done){
const json = { hello: 'world'};
const validJson = JSON.stringify(json);
const reqObj = req('formdata', validJson);
const instance = jsonparser();
instance(reqObj, null, function(err){
if(err) {
return done(err);
}
expect(reqObj.json).toEqual(json);
done();
});
});
});
describe('jsonparser strict', function(){
it('should fail if content type is not json with 415 status code',function(done){
const json = { hello: 'world' };
const validJson = JSON.stringify(json);
const instance = jsonparser({strict: true});
instance(req('text/*', validJson), null, function(err){
expect(err.status).toEqual(415);
expect(err.message).toBeDefined();
done();
});
});
it('should pass if content type is json and is valid', function(done){
const json = {hello:'world'};
const validJson = JSON.stringify(json);
const reqObj = req('application/json', validJson);
const instance = jsonparser({strict: true});
instance(reqObj, null, function(err){
if(err) {
return done(err);
}
expect(reqObj.json).toEqual(json);
done();
});
});
});
describe('jsonparser non-bodyCheck', function(){
it('should pass if body is not there', function(done){
const instance = jsonparser();
const reqObj = req('application/json');
instance(reqObj, null, function(err){
if(err) {
return done(err);
}
expect(reqObj.json).toBeNull();
done();
});
});
});
describe('jsonparser bodyCheck', function(){
it('should pass if body is not preset', function(done){
const instance = jsonparser({bodyCheck:true});
const reqObj = req('application/json');
instance(reqObj, null, function(err){
expect(err.status).toEqual(400);
expect(err.message).toBeDefined();
done();
});
});
it('should pass if body is a validJson', function(done){
const json = { hello: 'world' };
const validJson = JSON.stringify(json);
const reqObj = req('application/json', validJson);
const instance = jsonparser({bodyCheck:true});
instance(reqObj, null, function(err){
if(err) {
return done(err);
}
expect(reqObj.json).toEqual(json);
done();
});
});
});