1 | var createRouter = require("..");
|
2 | var expect = require("chai").expect;
|
3 | var http = require("./http");
|
4 | var jquery = require('jquery');
|
5 | _debug = require('debug');
|
6 |
|
7 | describe("router", function() {
|
8 | it("can get JSON", function() {
|
9 | var router = createRouter();
|
10 |
|
11 | router.get("/path/:name/:id", function(req) {
|
12 | return {
|
13 | body: {
|
14 | route: "one",
|
15 | params: req.params
|
16 | }
|
17 | };
|
18 | });
|
19 |
|
20 | return http.get("/path/1/2?c=3").then(function(response) {
|
21 | expect(response.body).to.eql({
|
22 | route: "one",
|
23 | params: {
|
24 | name: "1",
|
25 | id: "2",
|
26 | c: "3"
|
27 | }
|
28 | });
|
29 | });
|
30 | });
|
31 |
|
32 | it("can respond with 400", function() {
|
33 | var router = createRouter();
|
34 |
|
35 | router.get("/path/:name/:id", function(req) {
|
36 | return {
|
37 | statusCode: 400,
|
38 | body: {
|
39 | error: 'bad luck!'
|
40 | }
|
41 | };
|
42 | });
|
43 |
|
44 | return http.get("/path/1/2?c=3").then(function () {
|
45 | throw new Error('expected to fail');
|
46 | }, function (errorResponse) {
|
47 | expect(errorResponse.statusCode).to.equal(400);
|
48 | expect(errorResponse.body).to.eql({error: 'bad luck!'});
|
49 | });
|
50 | });
|
51 |
|
52 | it("can respond with 201", function() {
|
53 | var router = createRouter();
|
54 |
|
55 | router.get("/path/:name/:id", function(req) {
|
56 | return {
|
57 | statusCode: 201,
|
58 | body: {
|
59 | message: 'created!'
|
60 | }
|
61 | };
|
62 | });
|
63 |
|
64 | return http.get("/path/1/2?c=3").then(function (response) {
|
65 | expect(response.statusCode).to.equal(201);
|
66 | expect(response.body).to.eql({message: 'created!'});
|
67 | });
|
68 | });
|
69 |
|
70 | it("can get a string", function() {
|
71 | var router = createRouter();
|
72 |
|
73 | router.get("/path/:name/:id", function(req) {
|
74 | return {
|
75 | body: 'a string'
|
76 | };
|
77 | });
|
78 |
|
79 | return http.get("/path/1/2?c=3").then(function(response) {
|
80 | expect(response.body).to.equal('a string');
|
81 | });
|
82 | });
|
83 |
|
84 | it("can post JSON", function() {
|
85 | var router = createRouter();
|
86 |
|
87 | router.post("/one/:name/:id", function(req) {
|
88 | return {
|
89 | body: {
|
90 | route: "one",
|
91 | body: req.body,
|
92 | params: req.params
|
93 | }
|
94 | };
|
95 | });
|
96 |
|
97 | return http.post("/one/1/2?c=3", { data: "blah" }).then(function(response) {
|
98 | expect(response.body).to.eql({
|
99 | route: "one",
|
100 | body: {
|
101 | data: "blah"
|
102 | },
|
103 | params: {
|
104 | name: "1",
|
105 | id: "2",
|
106 | c: "3"
|
107 | }
|
108 | });
|
109 | });
|
110 | });
|
111 |
|
112 | describe('404', function () {
|
113 | context('when there are routes', function () {
|
114 | beforeEach(function () {
|
115 | var router = createRouter();
|
116 | router.get('/', function (req) {
|
117 | return {
|
118 | body: 'hi'
|
119 | };
|
120 | });
|
121 | });
|
122 |
|
123 | it('responds with 404 if route not found', function () {
|
124 | return http.get('/notfound').then(function () {
|
125 | throw new Error('expected to get 404');
|
126 | }, function (error) {
|
127 | expect(error.statusCode).to.equal(404);
|
128 | expect(error.body).to.equal('route not found: GET /notfound');
|
129 | });
|
130 | });
|
131 | });
|
132 |
|
133 | context('when there are no routes', function () {
|
134 | beforeEach(function () {
|
135 | var router = createRouter();
|
136 | });
|
137 |
|
138 | it('responds with 404 if route not found', function () {
|
139 | try {
|
140 | return http.get('/notfound').then(function () {
|
141 | throw new Error('expected to get 404');
|
142 | }, function (error) {
|
143 | expect(error.statusCode).to.equal(404);
|
144 | expect(error.body).to.equal('route not found: GET /notfound');
|
145 | });
|
146 | } catch (e) {
|
147 | }
|
148 | });
|
149 | });
|
150 | });
|
151 | });
|