Skip to content

Commit

Permalink
rename _form to _show and move the funcs in the design doc to design.…
Browse files Browse the repository at this point in the history
…show.docs

git-svn-id: https://svn.apache.org/repos/asf/couchdb/trunk@733576 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information
jchris committed Jan 12, 2009
1 parent 2417e37 commit f5e38ac
Show file tree
Hide file tree
Showing 6 changed files with 147 additions and 132 deletions.
2 changes: 1 addition & 1 deletion etc/couchdb/default.ini.tpl.in
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ _restart = {couch_httpd_misc_handlers, handle_restart_req}
[httpd_db_handlers]
_view = {couch_httpd_view, handle_view_req}
_slow_view = {couch_httpd_view, handle_slow_view_req}
_form = {couch_httpd_form, handle_form_req}
_show = {couch_httpd_show, handle_doc_show_req}

; The external module takes an optional argument allowing you to narrow it to a
; single script. Otherwise the script name is inferred from the first path section
Expand Down
4 changes: 2 additions & 2 deletions share/server/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ while (cmd = eval(readline())) {
print(toJSON(error));
}
break;
case "form":
case "show_doc":
var funSrc = cmd[1];
var doc = cmd[2];
var req = cmd[3];
Expand All @@ -346,7 +346,7 @@ while (cmd = eval(readline())) {
} catch (error) {
// Available error fields:
// message, fileName, lineNumber, stack, name
log("form function raised error: "+error.toString());
log("doc show function raised error: "+error.toString());
log("stacktrace: "+error.stack);
try {
print(toJSON(error));
Expand Down
212 changes: 110 additions & 102 deletions share/www/script/couch_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -558,6 +558,11 @@ var tests = {
for(var i=0; i<numDocsToCreate; i++) {
T(results.rows[numDocsToCreate-1-i].key==i);
}

// Check _all_docs with descending=true again (now that there are many docs)
// this fails, see COUCHDB-109
// var desc = db.allDocs({descending:true});
// T(desc.total_rows == desc.rows.length);
},

reduce: function(debug) {
Expand Down Expand Up @@ -2181,7 +2186,7 @@ var tests = {
T(xhr.status == 200)
},

forms: function(debug) {
show_documents: function(debug) {
var db = new CouchDB("test_suite_db");
db.deleteDb();
db.createDb();
Expand All @@ -2195,91 +2200,94 @@ var tests = {
var designDoc = {
_id:"_design/template",
language: "javascript",
forms: {
"hello" : stringFun(function() {
return {
body : "Hello World"
};
}),
"just-name" : stringFun(function(doc, req) {
return {
body : "Just " + doc.name
};
}),
"req-info" : stringFun(function(doc, req) {
return {
json : req
}
}),
"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/)) {
show: {
docs: {
"hello" : stringFun(function() {
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
}
body : "Hello World"
};
} else {
}),
"just-name" : stringFun(function(doc, req) {
return {
"body" : "accepting text requests",
headers : {
"Content-Type" : "text/html",
"Vary" : "Accept"
}
body : "Just " + doc.name
};
}
}),
"respondWith" : stringFun(function(doc, req) {
registerType("foo", "application/foo","application/x-foo");
return respondWith(req, {
html : function() {
return {
body:"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.
this.eval('xml.node.@foo = doc.word');
}),
"req-info" : stringFun(function(doc, req) {
return {
json : req
}
}),
"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 {
body: xml
// 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
}
};
},
foo : function() {
} else {
return {
body: "foofoo"
"body" : "accepting text requests",
headers : {
"Content-Type" : "text/html",
"Vary" : "Accept"
}
};
},
fallback : "html"
});
})
}
}),
"respondWith" : stringFun(function(doc, req) {
registerType("foo", "application/foo","application/x-foo");
return respondWith(req, {
html : function() {
return {
body:"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.
this.eval('xml.node.@foo = doc.word');
return {
body: xml
};
},
foo : function() {
return {
body: "foofoo"
};
},
fallback : "html"
});
})
}
}
};
T(db.save(designDoc).ok);
Expand All @@ -2289,35 +2297,35 @@ var tests = {
T(resp.ok);
var docid = resp.id;

// form error
var xhr = CouchDB.request("GET", "/test_suite_db/_form/");
// show error
var xhr = CouchDB.request("GET", "/test_suite_db/_show/");
T(xhr.status == 404);
T(JSON.parse(xhr.responseText).reason == "Invalid path.");

// hello template world
xhr = CouchDB.request("GET", "/test_suite_db/_form/template/hello/"+docid);
xhr = CouchDB.request("GET", "/test_suite_db/_show/template/hello/"+docid);
T(xhr.responseText == "Hello World");

// form with doc
xhr = CouchDB.request("GET", "/test_suite_db/_form/template/just-name/"+docid);
// show with doc
xhr = CouchDB.request("GET", "/test_suite_db/_show/template/just-name/"+docid);
T(xhr.responseText == "Just Rusty");

// form with missing doc
xhr = CouchDB.request("GET", "/test_suite_db/_form/template/just-name/missingdoc");
// show with missing doc
xhr = CouchDB.request("GET", "/test_suite_db/_show/template/just-name/missingdoc");
T(xhr.status == 404);
var resp = JSON.parse(xhr.responseText);
T(resp.error == "not_found");
T(resp.reason == "missing");

// missing design doc
xhr = CouchDB.request("GET", "/test_suite_db/_form/missingdoc/just-name/"+docid);
xhr = CouchDB.request("GET", "/test_suite_db/_show/missingdoc/just-name/"+docid);
T(xhr.status == 404);
var resp = JSON.parse(xhr.responseText);
T(resp.error == "not_found");
T(resp.reason == "missing_design_doc");

// query parameters
xhr = CouchDB.request("GET", "/test_suite_db/_form/template/req-info/"+docid+"?foo=bar", {
xhr = CouchDB.request("GET", "/test_suite_db/_show/template/req-info/"+docid+"?foo=bar", {
headers: {
"Accept": "text/html;text/plain;*/*",
"X-Foo" : "bar"
Expand All @@ -2331,21 +2339,21 @@ var tests = {
T(equals(resp.info.db_name, "test_suite_db"));

// returning a content-type
xhr = CouchDB.request("GET", "/test_suite_db/_form/template/xml-type/"+docid);
xhr = CouchDB.request("GET", "/test_suite_db/_show/template/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/_form/template/accept-switch/"+docid, {
xhr = CouchDB.request("GET", "/test_suite_db/_show/template/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/_form/template/accept-switch/"+docid, {
xhr = CouchDB.request("GET", "/test_suite_db/_show/template/accept-switch/"+docid, {
headers: {"Accept": "image/png;*/*"}
});
T(xhr.responseText.match(/PNG/))
Expand All @@ -2354,12 +2362,12 @@ var tests = {
T(etag2 != etag);

// proper etags
// form with doc
xhr = CouchDB.request("GET", "/test_suite_db/_form/template/just-name/"+docid);
// show with doc
xhr = CouchDB.request("GET", "/test_suite_db/_show/template/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/_form/template/just-name/"+docid, {
xhr = CouchDB.request("GET", "/test_suite_db/_show/template/just-name/"+docid, {
headers: {"if-none-match": etag}
});
// should be 304
Expand All @@ -2370,15 +2378,15 @@ var tests = {
resp = db.save(doc);
T(resp.ok);
// req with same etag
xhr = CouchDB.request("GET", "/test_suite_db/_form/template/just-name/"+docid, {
xhr = CouchDB.request("GET", "/test_suite_db/_show/template/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/_form/template/just-name/"+docid, {
xhr = CouchDB.request("GET", "/test_suite_db/_show/template/just-name/"+docid, {
headers: {"if-none-match": etag}
});
// should be 304
Expand All @@ -2388,35 +2396,35 @@ var tests = {
designDoc.isChanged = true;
T(db.save(designDoc).ok);

xhr = CouchDB.request("GET", "/test_suite_db/_form/template/just-name/"+docid, {
xhr = CouchDB.request("GET", "/test_suite_db/_show/template/just-name/"+docid, {
headers: {"if-none-match": etag}
});
// should be 304
T(xhr.status == 304);

// update design doc function
designDoc.forms["just-name"] = (function(doc, req) {
designDoc.show.docs["just-name"] = (function(doc, req) {
return {
body : "Just old " + doc.name
};
}).toString();
T(db.save(designDoc).ok);

xhr = CouchDB.request("GET", "/test_suite_db/_form/template/just-name/"+docid, {
xhr = CouchDB.request("GET", "/test_suite_db/_show/template/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/_form/template/no-set-etag/"+docid);
xhr = CouchDB.request("GET", "/test_suite_db/_show/template/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/_form/template/respondWith/"+docid, {
xhr = CouchDB.request("GET", "/test_suite_db/_show/template/respondWith/"+docid, {
headers: {
"Accept": 'text/html,application/atom+xml; q=0.9'
}
Expand All @@ -2425,7 +2433,7 @@ var tests = {
T(xhr.responseText == "Ha ha, you said \"plankton\".");

// now with xml
xhr = CouchDB.request("GET", "/test_suite_db/_form/template/respondWith/"+docid, {
xhr = CouchDB.request("GET", "/test_suite_db/_show/template/respondWith/"+docid, {
headers: {
"Accept": 'application/xml'
}
Expand All @@ -2435,7 +2443,7 @@ var tests = {
T(xhr.responseText.match(/plankton/));

// registering types works
xhr = CouchDB.request("GET", "/test_suite_db/_form/template/respondWith/"+docid, {
xhr = CouchDB.request("GET", "/test_suite_db/_show/template/respondWith/"+docid, {
headers: {
"Accept": "application/x-foo"
}
Expand Down
Loading

0 comments on commit f5e38ac

Please sign in to comment.