UNPKG

4.13 kBtext/coffeescriptView Raw
1assert = require 'assert'
2fetch = require './lib/fetch'
3copy = require './lib/copy'
4status = require('http').STATUS_CODES
5port = +process.env.CRIXALIS_PORT + 5
6Crixalis = require '../lib'
7
8html = '<html><body><h1>Test</h1></body></html>'
9json =
10 result:
11 status: 'ok'
12
13Crixalis
14 .route '/redirect/:url', ->
15 @redirect('/' + @params.url)
16 .route '/json', ->
17 @stash.json = json
18
19 @render()
20 .route '/html', ->
21 @body = html
22
23 @render()
24 .route '/null', ->
25 @render()
26 .route '/error', ->
27 @error(503)
28 .start 'http', port
29 .unref()
30
31request = (options) ->
32 options.host ?= 'localhost'
33 options.port ?= port
34 options.path ?= '/'
35 options.method ?= 'GET'
36
37 return ->
38 fetch options, @callback
39 return
40
41matches = (expected) ->
42 return (error, response) ->
43 if expected.path
44 assert.equal expected.path, response.headers['location']
45
46 if expected.type
47 assert.equal expected.type, response.headers['content-type']
48
49 if expected.size
50 assert.equal expected.size, response.headers['content-length']
51
52 if expected.data
53 assert.deepEqual expected.data, response.body
54
55 if expected.code
56 assert.equal expected.code, response.statusCode
57
58 return
59
60(require 'vows')
61 .describe('view')
62 .addBatch
63 redirect:
64 topic: request
65 path: '/redirect/json'
66
67 result: matches
68 code: 302
69 type: undefined
70 path: '/json'
71 data: status[302]
72 size: Buffer.byteLength status[302]
73
74 json:
75 topic: null
76
77 get:
78 topic: request
79 method: 'GET'
80 path: '/json'
81
82 result: matches
83 code: 200
84 type: 'application/json; charset=utf-8'
85 size: Buffer.byteLength JSON.stringify json
86 data: JSON.stringify json
87
88 head:
89 topic: request
90 method: 'HEAD'
91 path: '/json'
92
93 result: matches
94 code: 200
95 type: 'application/json; charset=utf-8'
96 size: Buffer.byteLength JSON.stringify json
97
98 html:
99 topic: null
100
101 get:
102 topic: request
103 method: 'GET'
104 path: '/html'
105
106 result: matches
107 code: 200
108 type: 'text/html; charset=utf-8'
109 size: Buffer.byteLength html
110 data: html
111
112 head:
113 topic: request
114 method: 'HEAD'
115 path: '/html'
116
117 result: matches
118 code: 200
119 type: 'text/html; charset=utf-8'
120 size: Buffer.byteLength html
121
122 none:
123 topic: null
124
125 get:
126 topic: request
127 method: 'GET'
128 path: '/null'
129
130 result: matches
131 code: 204
132 type: undefined
133 size: undefined
134 data: undefined
135
136 head:
137 topic: request
138 method: 'HEAD'
139 path: '/null'
140
141 result: matches
142 code: 204
143 type: undefined
144 size: undefined
145 data: undefined
146
147 post:
148 topic: request
149 method: 'POST'
150 path: '/null'
151
152 result: matches
153 code: 200 # should not set 204 for POST request
154 type: undefined
155 size: undefined
156 data: undefined
157
158 error:
159 topic: null
160
161 json:
162 topic: request
163 method: 'POST'
164 path: '/error'
165 headers:
166 accept: 'application/json'
167
168 result: matches
169 code: 503
170 type: 'application/json; charset=utf-8'
171 data: JSON.stringify
172 error:
173 message: status[503]
174
175 html:
176 topic: request
177 method: 'POST'
178 path: '/error'
179 headers:
180 accept: 'text/html'
181
182 result: matches
183 code: 503
184 type: 'text/html; charset=utf-8'
185 size: Buffer.byteLength status[503]
186 data: status[503]
187
188 400:
189 topic: request
190 method: 'POST'
191 path: '/redirect/json'
192 headers:
193 'content-type': 'application/json'
194 'accept': 'application/json'
195 data: '{["json...'
196
197 result: matches
198 code: 400
199 type: 'application/json; charset=utf-8'
200 data: JSON.stringify
201 error:
202 message: status[400]
203
204 404:
205 topic: request
206 method: 'POST'
207 path: '/notfound'
208
209 result: matches
210 code: 404
211 type: 'text/html; charset=utf-8'
212 size: Buffer.byteLength status[404]
213 data: status[404]
214
215 default:
216 topic: request
217 method: 'POST'
218 path: '/error'
219
220 result: matches
221 code: 503
222 type: 'text/html; charset=utf-8'
223 size: Buffer.byteLength status[503]
224 data: status[503]
225 .export(module)