forked from tmeasday/meteor-router
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrouter_server_tests.js
101 lines (80 loc) · 3.21 KB
/
router_server_tests.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
// XXX: is it OK to assume localhost:3000 here?
//
// it seems that stream does in it's tests.
Tinytest.add("Simple Router.serve", function(test) {
Meteor.Router.add('/server/foo', function() {
return 'data';
});
Meteor.Router.add(/server\/page\/(\d+)/, function(number) {
return [number, 'page'];
});
var resp = Meteor.http.get('http://localhost:3000/server/foo')
test.equal(resp.content, 'data');
var resp = Meteor.http.get('http://localhost:3000/server/page/7')
test.equal(resp.statusCode, 7);
test.equal(resp.content, 'page');
});
Tinytest.add("Router.serve with params", function(test) {
Meteor.Router.add('/server/bar/:id.xml', function(id) {
return id;
})
var resp = Meteor.http.get('http://localhost:3000/server/bar/content.xml')
test.equal(resp.content, 'content');
});
Tinytest.add("Router.serve various response types", function(test) {
Meteor.Router.add({
'/server/baz-1': [404, {'x-my-header': 'Baz'}, 'data'],
'/server/baz-2': [405, 'data'],
'/server/baz-3': ['data'],
'/server/baz-4': 406,
'/server/baz-5': 'data'
});
var resp = Meteor.http.get('http://localhost:3000/server/baz-1')
test.equal(resp.statusCode, 404);
test.equal(resp.content, 'data');
test.equal(resp.headers['x-my-header'], 'Baz');
// grab it again to make sure we aren't messing with it
var resp = Meteor.http.get('http://localhost:3000/server/baz-1')
test.equal(resp.statusCode, 404);
test.equal(resp.content, 'data');
test.equal(resp.headers['x-my-header'], 'Baz');
var resp = Meteor.http.get('http://localhost:3000/server/baz-2')
test.equal(resp.statusCode, 405);
test.equal(resp.content, 'data');
var resp = Meteor.http.get('http://localhost:3000/server/baz-3')
test.equal(resp.content, 'data');
var resp = Meteor.http.get('http://localhost:3000/server/baz-4')
test.equal(resp.statusCode, 406);
var resp = Meteor.http.get('http://localhost:3000/server/baz-5')
test.equal(resp.content, 'data');
});
Tinytest.add("Router.serve with futures", function(test) {
Meteor.Router.add('/server/delayed', function() {
var Future = __meteor_bootstrap__.require('fibers/future');
var fut = new Future();
setTimeout(function() {
fut.ret('foo-in-timeout');
}, 1);
return fut.wait();
});
var resp = Meteor.http.get('http://localhost:3000/server/delayed')
test.equal(resp.content, 'foo-in-timeout');
});
Tinytest.add("Router.serve without http method restriction", function(test) {
Meteor.Router.add('/bat-1', 'data');
var resp = Meteor.http.get('http://localhost:3000/bat-1');
test.equal(resp.content, 'data');
var resp = Meteor.http.post('http://localhost:3000/bat-1');
test.equal(resp.content, 'data');
});
Tinytest.add("Router.serve with http method restriction", function(test) {
Meteor.Router.add('/bat-2', 'GET', 'data');
Meteor.Router.add('/bat-2', 'POST', 'postdata');
var resp = Meteor.http.get('http://localhost:3000/bat-2');
test.equal(resp.content, 'data');
var resp = Meteor.http.post('http://localhost:3000/bat-2');
test.equal(resp.content, 'postdata');
var resp = Meteor.http.put('http://localhost:3000/bat-2');
test.notEqual(resp.content, 'postdata');
test.notEqual(resp.content, 'data');
});