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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
|
// Licensed under the Apache License, Version 2.0 (the "License"); you may not
// use this file except in compliance with the License. You may obtain a copy
// of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
// License for the specific language governing permissions and limitations under
// the License.
couchTests.show_documents = function(debug) {
var db = new CouchDB("test_suite_db");
db.deleteDb();
db.createDb();
if (debug) debugger;
var designDoc = {
_id:"_design/template",
language: "javascript",
shows: {
"hello" : stringFun(function(doc, req) {
if (doc) {
return "Hello World";
} else {
if(req.docId) {
return "New World";
} else {
return "Empty World";
}
}
}),
"just-name" : stringFun(function(doc, req) {
if (doc) {
return {
body : "Just " + doc.name
};
} else {
return {
body : "No such doc",
code : 404
};
}
}),
"json" : stringFun(function(doc, req) {
return {
json : doc
}
}),
"req-info" : stringFun(function(doc, req) {
return {
json : req
}
}),
"render-error" : stringFun(function(doc, req) {
return noSuchVariable;
}),
"xml-type" : stringFun(function(doc, req) {
return {
"headers" : {
"Content-Type" : "application/xml"
},
"body" : new XML('<xml><node foo="bar"/></xml>')
}
}),
"no-set-etag" : stringFun(function(doc, req) {
return {
headers : {
"Etag" : "skipped"
},
"body" : "something"
}
}),
"accept-switch" : stringFun(function(doc, req) {
if (req.headers["Accept"].match(/image/)) {
return {
// a 16x16 px version of the CouchDB logo
"base64" :
["iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAMAAAAoLQ9TAAAAsV",
"BMVEUAAAD////////////////////////5ur3rEBn////////////////wDBL/",
"AADuBAe9EB3IEBz/7+//X1/qBQn2AgP/f3/ilpzsDxfpChDtDhXeCA76AQH/v7",
"/84eLyWV/uc3bJPEf/Dw/uw8bRWmP1h4zxSlD6YGHuQ0f6g4XyQkXvCA36MDH6",
"wMH/z8/yAwX64ODeh47BHiv/Ly/20dLQLTj98PDXWmP/Pz//39/wGyJ7Iy9JAA",
"AADHRSTlMAbw8vf08/bz+Pv19jK/W3AAAAg0lEQVR4Xp3LRQ4DQRBD0QqTm4Y5",
"zMxw/4OleiJlHeUtv2X6RbNO1Uqj9g0RMCuQO0vBIg4vMFeOpCWIWmDOw82fZx",
"vaND1c8OG4vrdOqD8YwgpDYDxRgkSm5rwu0nQVBJuMg++pLXZyr5jnc1BaH4GT",
"LvEliY253nA3pVhQqdPt0f/erJkMGMB8xucAAAAASUVORK5CYII="].join(''),
headers : {
"Content-Type" : "image/png",
"Vary" : "Accept" // we set this for proxy caches
}
};
} else {
return {
"body" : "accepting text requests",
headers : {
"Content-Type" : "text/html",
"Vary" : "Accept"
}
};
}
}),
"respondWith" : stringFun(function(doc, req) {
registerType("foo", "application/foo","application/x-foo");
return respondWith(req, {
html : function() {
return "Ha ha, you said \"" + doc.word + "\".";
},
xml : function() {
var xml = new XML('<xml><node/></xml>');
// Becase Safari can't stand to see that dastardly
// E4X outside of a string. Outside of tests you
// can just use E4X literals.
this.eval('xml.node.@foo = doc.word');
return {
body: xml
};
},
foo : function() {
return {
body: "foofoo"
};
},
fallback : "html"
});
})
}
};
T(db.save(designDoc).ok);
var doc = {"word":"plankton", "name":"Rusty"}
var resp = db.save(doc);
T(resp.ok);
var docid = resp.id;
// show error
var xhr = CouchDB.request("GET", "/test_suite_db/_design/template/_show/");
T(xhr.status == 404, 'Should be missing');
T(JSON.parse(xhr.responseText).reason == "Invalid path.");
// hello template world
xhr = CouchDB.request("GET", "/test_suite_db/_design/template/_show/hello/"+docid);
T(xhr.responseText == "Hello World");
//
// };
//
// function foo() {
// // error stacktraces
// xhr = CouchDB.request("GET", "/test_suite_db/_design/template/_show/render-error/"+docid);
// T(JSON.parse(xhr.responseText).error == "render_error");
// hello template world (no docid)
xhr = CouchDB.request("GET", "/test_suite_db/_design/template/_show/hello");
T(xhr.responseText == "Empty World");
// // hello template world (non-existing docid)
xhr = CouchDB.request("GET", "/test_suite_db/_design/template/_show/hello/nonExistingDoc");
T(xhr.responseText == "New World");
// show with doc
xhr = CouchDB.request("GET", "/test_suite_db/_design/template/_show/just-name/"+docid);
T(xhr.responseText == "Just Rusty");
// show with missing doc
xhr = CouchDB.request("GET", "/test_suite_db/_design/template/_show/just-name/missingdoc");
T(xhr.status == 404, 'Doc should be missing');
T(xhr.responseText == "No such doc");
// show with missing func
xhr = CouchDB.request("GET", "/test_suite_db/_design/template/_show/missing/"+docid);
T(xhr.status == 404, "function is missing");
// missing design doc
xhr = CouchDB.request("GET", "/test_suite_db/_design/missingddoc/_show/just-name/"+docid);
T(xhr.status == 404);
var resp = JSON.parse(xhr.responseText);
T(resp.error == "not_found");
// query parameters
xhr = CouchDB.request("GET", "/test_suite_db/_design/template/_show/req-info/"+docid+"?foo=bar", {
headers: {
"Accept": "text/html;text/plain;*/*",
"X-Foo" : "bar"
}
});
var resp = JSON.parse(xhr.responseText);
T(equals(resp.headers["X-Foo"], "bar"));
T(equals(resp.query, {foo:"bar"}));
T(equals(resp.verb, "GET"));
T(equals(resp.path[5], docid));
T(equals(resp.info.db_name, "test_suite_db"));
// returning a content-type
xhr = CouchDB.request("GET", "/test_suite_db/_design/template/_show/xml-type/"+docid);
T("application/xml" == xhr.getResponseHeader("Content-Type"));
T("Accept" == xhr.getResponseHeader("Vary"));
// accept header switching
// different mime has different etag
xhr = CouchDB.request("GET", "/test_suite_db/_design/template/_show/accept-switch/"+docid, {
headers: {"Accept": "text/html;text/plain;*/*"}
});
T("text/html" == xhr.getResponseHeader("Content-Type"));
T("Accept" == xhr.getResponseHeader("Vary"));
var etag = xhr.getResponseHeader("etag");
xhr = CouchDB.request("GET", "/test_suite_db/_design/template/_show/accept-switch/"+docid, {
headers: {"Accept": "image/png;*/*"}
});
T(xhr.responseText.match(/PNG/))
T("image/png" == xhr.getResponseHeader("Content-Type"));
var etag2 = xhr.getResponseHeader("etag");
T(etag2 != etag);
// proper etags
// show with doc
xhr = CouchDB.request("GET", "/test_suite_db/_design/template/_show/just-name/"+docid);
// extract the ETag header values
etag = xhr.getResponseHeader("etag");
// get again with etag in request
xhr = CouchDB.request("GET", "/test_suite_db/_design/template/_show/just-name/"+docid, {
headers: {"if-none-match": etag}
});
// should be 304
T(xhr.status == 304);
// update the doc
doc.name = "Crusty";
resp = db.save(doc);
T(resp.ok);
// req with same etag
xhr = CouchDB.request("GET", "/test_suite_db/_design/template/_show/just-name/"+docid, {
headers: {"if-none-match": etag}
});
// status is 200
T(xhr.status == 200);
// get new etag and request again
etag = xhr.getResponseHeader("etag");
xhr = CouchDB.request("GET", "/test_suite_db/_design/template/_show/just-name/"+docid, {
headers: {"if-none-match": etag}
});
// should be 304
T(xhr.status == 304);
// update design doc (but not function)
designDoc.isChanged = true;
T(db.save(designDoc).ok);
xhr = CouchDB.request("GET", "/test_suite_db/_design/template/_show/just-name/"+docid, {
headers: {"if-none-match": etag}
});
// should be 304
T(xhr.status == 304);
// update design doc function
designDoc.shows["just-name"] = (function(doc, req) {
return {
body : "Just old " + doc.name
};
}).toString();
T(db.save(designDoc).ok);
xhr = CouchDB.request("GET", "/test_suite_db/_design/template/_show/just-name/"+docid, {
headers: {"if-none-match": etag}
});
// status is 200
T(xhr.status == 200);
// JS can't set etag
xhr = CouchDB.request("GET", "/test_suite_db/_design/template/_show/no-set-etag/"+docid);
// extract the ETag header values
etag = xhr.getResponseHeader("etag");
T(etag != "skipped")
// test the respondWith mime matcher
xhr = CouchDB.request("GET", "/test_suite_db/_design/template/_show/respondWith/"+docid, {
headers: {
"Accept": 'text/html,application/atom+xml; q=0.9'
}
});
T(xhr.getResponseHeader("Content-Type") == "text/html");
T(xhr.responseText == "Ha ha, you said \"plankton\".");
// now with xml
xhr = CouchDB.request("GET", "/test_suite_db/_design/template/_show/respondWith/"+docid, {
headers: {
"Accept": 'application/xml'
}
});
T(xhr.getResponseHeader("Content-Type") == "application/xml");
T(xhr.responseText.match(/node/));
T(xhr.responseText.match(/plankton/));
// registering types works
xhr = CouchDB.request("GET", "/test_suite_db/_design/template/_show/respondWith/"+docid, {
headers: {
"Accept": "application/x-foo"
}
});
T(xhr.getResponseHeader("Content-Type") == "application/x-foo");
T(xhr.responseText.match(/foofoo/));
// test the respondWith mime matcher without
xhr = CouchDB.request("GET", "/test_suite_db/_design/template/_show/respondWith/"+docid, {
headers: {
"Accept": 'text/html,application/atom+xml; q=0.9'
}
});
T(xhr.getResponseHeader("Content-Type") == "text/html");
T(xhr.responseText == "Ha ha, you said \"plankton\".");
// test inclusion of conflict state
var doc1 = {_id:"foo", a:1};
var doc2 = {_id:"foo", a:2};
db.save(doc1);
//create the conflict with a all_or_nothing bulk docs request
var docs = [doc2];
db.bulkSave(docs, {all_or_nothing:true});
xhr = CouchDB.request("GET", "/test_suite_db/_design/template/_show/json/foo");
TEquals(1, JSON.parse(xhr.responseText)._conflicts.length);
};
|