-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathtest.coffee
280 lines (223 loc) · 9.91 KB
/
test.coffee
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
# Tests for the REST-ful interface
assert = require 'assert'
http = require 'http'
rest = require './rest.js'
# A bit of variety
otSimple = require('ot-simple').type
otText = require('ot-text').type
express = require 'express'
livedb = require 'livedb'
# Async fetch. Aggregates whole response and sends to callback.
# Callback should be function(response, data) {...}
fetch = (method, port, path, postData, extraHeaders, callback) ->
if typeof extraHeaders == 'function'
callback = extraHeaders
extraHeaders = null
headers = extraHeaders || {'x-testing': 'booyah'}
request = http.request {method, path, host: 'localhost', port, headers}, (response) ->
data = ''
response.on 'data', (chunk) -> data += chunk
response.on 'end', ->
data = data.trim()
if response.headers['content-type'] == 'application/json'
data = JSON.parse(data)
callback response, data, response.headers
if postData?
postData = JSON.stringify(postData) if typeof(postData) == 'object'
request.write postData
request.end()
writeOps = (db, cName, docName, ops, index = 0, callback) ->
if typeof index is 'function'
[index, callback] = [0, index]
if index >= ops.length
return callback()
op = ops[index]
db.writeOp cName, docName, op, (err) ->
return callback(err) if err
# Recurse.
writeOps db, cName, docName, ops, index+1, callback
# Frontend tests
describe 'rest', ->
beforeEach (done) ->
@collection = '__c'
@doc = '__doc'
# Tests fill this in to provide expected backend functionality
@db = livedb.memory()
@backend = livedb.client @db
@middleware = require('livedb-middleware') @backend
app = express()
app.use '/doc', rest @middleware
# Used by the connect middleware below.
app.use (err, req, res, next) ->
if err.message is 'Forbidden'
res.send 403, 'Forbidden'
else
next err
@port = 4321
@server = http.createServer app
@server.listen @port, done
afterEach (done) ->
@server.on 'close', done
@server.close()
describe 'GET & HEAD', ->
it 'returns 404 for nonexistant documents', (done) ->
fetch 'GET', @port, "/doc/#{@collection}/#{@doc}", null, (res, data, headers) ->
assert.strictEqual res.statusCode, 404
assert.strictEqual headers['x-ot-version'], '0'
assert.equal headers['x-ot-type'], null
done()
it 'return 404 and empty body when on HEAD on a nonexistant document', (done) ->
fetch 'HEAD', @port, "/doc/#{@collection}/#{@doc}", null, (res, data, headers) ->
assert.strictEqual res.statusCode, 404
assert.strictEqual data, ''
assert.strictEqual headers['x-ot-version'], '0'
assert.equal headers['x-ot-type'], null
done()
it 'returns 200, empty body, version and type when on HEAD on a document', (done) ->
@db.writeSnapshot 'c', 'd', {v:1, type:otText.uri, data:'hi there'}, =>
fetch 'HEAD', @port, "/doc/c/d", null, (res, data, headers) ->
assert.strictEqual res.statusCode, 200
assert.strictEqual headers['x-ot-version'], '1'
assert.strictEqual headers['x-ot-type'], otText.uri
assert.ok headers['etag']
assert.strictEqual data, ''
done()
it 'document returns the document snapshot', (done) ->
@db.writeSnapshot 'c', 'd', {v:1, type:otSimple.uri, data:{str:'Hi'}}, =>
fetch 'GET', @port, "/doc/c/d", null, (res, data, headers) ->
assert.strictEqual res.statusCode, 200
assert.strictEqual headers['x-ot-version'], '1'
assert.strictEqual headers['x-ot-type'], otSimple.uri
assert.ok headers['etag']
assert.strictEqual headers['content-type'], 'application/json'
assert.deepEqual data, {str:'Hi'}
done()
it 'document returns the entire document structure when envelope=true', (done) ->
@db.writeSnapshot 'c', 'd', {v:1, type:otSimple.uri, data:{str:'Hi'}}, =>
fetch 'GET', @port, "/doc/c/d?envelope=true", null, (res, data, headers) ->
assert.strictEqual res.statusCode, 200
assert.strictEqual headers['x-ot-version'], '1'
assert.strictEqual headers['x-ot-type'], otSimple.uri
assert.strictEqual headers['content-type'], 'application/json'
assert.deepEqual data, {v:1, type:otSimple.uri, data:{str:'Hi'}}
done()
it 'a plaintext document is returned as a string', (done) ->
@db.writeSnapshot 'c', 'd', {v:1, type:otText.uri, data:'hi'}, =>
fetch 'GET', @port, "/doc/c/d", null, (res, data, headers) ->
assert.strictEqual res.statusCode, 200
assert.strictEqual headers['x-ot-version'], '1'
assert.strictEqual headers['x-ot-type'], otText.uri
assert.ok headers['etag']
assert.strictEqual headers['content-type'], 'text/plain'
assert.deepEqual data, 'hi'
done()
it 'ETag is the same between responses', (done) ->
@db.writeSnapshot 'c', 'd', {v:1, type:otText.uri, data:'hi'}, =>
fetch 'GET', @port, "/doc/c/d", null, (res, data, headers) =>
tag = headers['etag']
# I don't care what the etag is, but if I fetch it again it should be the same.
fetch 'GET', @port, "/doc/c/d", null, (res, data, headers) ->
assert.strictEqual headers['etag'], tag
done()
it 'ETag changes when version changes', (done) ->
@db.writeSnapshot 'c', 'd', {v:1, type:otText.uri, data:'hi'}, =>
fetch 'GET', @port, "/doc/c/d", null, (res, data, headers) =>
tag = headers['etag']
@backend.submit 'c', 'd', {v:1, op:['x']}, =>
fetch 'GET', @port, "/doc/c/d", null, (res, data, headers) =>
assert.notStrictEqual headers['etag'], tag
done()
describe 'GET /ops', ->
it 'returns ops', (done) ->
ops = [{v:0, create:{type:otText.uri}}, {v:1, op:[]}, {v:2, op:[]}]
writeOps @db, 'c', 'd', ops, =>
fetch 'GET', @port, '/doc/c/d/ops', null, (res, data, headers) ->
assert.strictEqual res.statusCode, 200
assert.deepEqual data, ops
done()
it 'limits FROM based on query parameter', (done) ->
ops = [{v:0, create:{type:otText.uri}}, {v:1, op:[]}, {v:2, op:[]}]
writeOps @db, 'c', 'd', ops, =>
fetch 'GET', @port, '/doc/c/d/ops?to=2', null, (res, data, headers) ->
assert.strictEqual res.statusCode, 200
assert.deepEqual data, [ops[0], ops[1]]
done()
it 'limits TO based on query parameter', (done) ->
ops = [{v:0, create:{type:otText.uri}}, {v:1, op:[]}, {v:2, op:[]}]
writeOps @db, 'c', 'd', ops, =>
fetch 'GET', @port, '/doc/c/d/ops?from=1', null, (res, data, headers) ->
assert.strictEqual res.statusCode, 200
assert.deepEqual data, [ops[1], ops[2]]
done()
it 'returns empty list for nonexistant document', (done) ->
fetch 'GET', @port, '/doc/c/d/ops', null, (res, data, headers) ->
assert.strictEqual res.statusCode, 200
assert.deepEqual data, []
done()
describe 'POST', ->
it 'lets you submit', (done) ->
called = false
@backend.submit = (cName, docName, opData, options, callback) ->
assert.strictEqual cName, 'c'
assert.strictEqual docName, 'd'
assert.deepEqual opData, {v:5, op:[1,2,3]}
called = true
callback null, 5, []
fetch 'POST', @port, "/doc/c/d", {v:5, op:[1,2,3]}, (res, ops) =>
assert.strictEqual res.statusCode, 200
assert.deepEqual ops, []
assert called
done()
it 'POST a document with invalid JSON returns 400', (done) ->
fetch 'POST', @port, "/doc/c/d", 'invalid>{json', (res, data) ->
assert.strictEqual res.statusCode, 400
done()
describe 'PUT', ->
it 'PUT a document creates it', (done) ->
called = false
@backend.submit = (cName, docName, opData, options, callback) ->
assert.strictEqual cName, 'c'
assert.strictEqual docName, 'd'
assert.deepEqual opData, {create:{type:'simple'}}
called = true
callback null, 5, []
fetch 'PUT', @port, "/doc/c/d", {type:'simple'}, (res, data, headers) =>
assert.strictEqual res.statusCode, 200
assert.strictEqual headers['x-ot-version'], '5'
assert called
done()
describe 'DELETE', ->
it 'deletes a document', (done) ->
called = false
@backend.submit = (cName, docName, opData, options, callback) ->
assert.strictEqual cName, 'c'
assert.strictEqual docName, 'd'
assert.deepEqual opData, {del:true}
called = true
callback null, 5, []
fetch 'DELETE', @port, "/doc/c/d", null, (res, data, headers) =>
assert.strictEqual res.statusCode, 200
assert.strictEqual headers['x-ot-version'], '5'
assert called
done()
describe 'with middleware', ->
describe 'disallowing connections', ->
beforeEach ->
@middleware.use 'connect', (action, callback) ->
assert.equal action.action, 'connect'
assert action.req.socket.remoteAddress in ['localhost', '127.0.0.1'] # Is there a nicer way to do this?
# This is added in fetch() above
assert.strictEqual action.req.headers['x-testing'], 'booyah'
callback 'Forbidden'
checkResponse = (done) -> (res, data) ->
#console.log res.statusCode, res.headers, res.body
assert.strictEqual(res.statusCode, 403)
assert.deepEqual data, 'Forbidden'
done()
it "can't get", (done) ->
fetch 'GET', @port, "/doc/c/d", null, checkResponse(done)
it "can't create", (done) ->
fetch 'PUT', @port, "/doc/c/d", {create:{type:'simple'}, v:0}, checkResponse(done)
it "can't submit an op", (done) ->
# Submit an op to a nonexistant doc
fetch 'POST', @port, "/doc/c/d", {op:{position: 0, text: 'Hi'}, v:0}, checkResponse(done)