Permalink
Browse files

Fix list-style send() API in show functions when using provides()

Also fix ignoring the return value when the send() API isnused.

Patch by Alexander Shorin.

Closes COUCHDB-1272

git-svn-id: https://svn.apache.org/repos/asf/couchdb/trunk@1166623 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information...
1 parent 66e2af4 commit 92f70219ce5ba487e4eb65dea7d16a9168a8547f @janl janl committed Sep 8, 2011
Showing with 34 additions and 3 deletions.
  1. +1 −0 THANKS
  2. +7 −3 share/server/render.js
  3. +26 −0 share/www/script/test/show_documents.js
View
1 THANKS
@@ -86,5 +86,6 @@ suggesting improvements or submitting changes. Some of these people are:
* Chris Coulson <chrisccoulson.googlemail.com>
* Dave Cottlehuber <dave@muse.net.nz>
* Trond Norbye <trond.norbye@gmail.com>
+ * Alexander Shorin <kxepal@gmail.com>
For a list of authors see the `AUTHORS` file.
View
@@ -221,10 +221,10 @@ var Render = (function() {
resetList();
Mime.resetProvides();
var resp = fun.apply(ddoc, args) || {};
+ resp = maybeWrapResponse(resp);
// handle list() style API
if (chunks.length && chunks.length > 0) {
- resp = maybeWrapResponse(resp);
resp.headers = resp.headers || {};
for(var header in startResp) {
resp.headers[header] = startResp[header];
@@ -234,8 +234,12 @@ var Render = (function() {
}
if (Mime.providesUsed) {
- resp = Mime.runProvides(args[1], ddoc);
- resp = applyContentType(maybeWrapResponse(resp), Mime.responseContentType);
+ var provided_resp = Mime.runProvides(args[1], ddoc) || {};
+ provided_resp = maybeWrapResponse(provided_resp);
+ resp.body = (resp.body || "") + chunks.join("");
+ resp.body += provided_resp.body || "";
+ resp = applyContentType(resp, Mime.responseContentType);
+ resetList();
}
var type = typeOf(resp);
@@ -90,6 +90,24 @@ couchTests.show_documents = function(debug) {
start({"X-Couch-Test-Header": "Yeah"});
send("Hey");
}),
+ "list-api-provides" : stringFun(function(doc, req) {
+ provides("text", function(){
+ send("foo, ");
+ send("bar, ");
+ send("baz!");
+ })
+ }),
+ "list-api-provides-and-return" : stringFun(function(doc, req) {
+ provides("text", function(){
+ send("4, ");
+ send("5, ");
+ send("6, ");
+ return "7!";
+ })
+ send("1, ");
+ send("2, ");
+ return "3, ";
+ }),
"list-api-mix" : stringFun(function(doc, req) {
start({"X-Couch-Test-Header": "Yeah"});
send("Hey ");
@@ -395,6 +413,14 @@ couchTests.show_documents = function(debug) {
T(xhr.responseText == "Hey");
TEquals("Yeah", xhr.getResponseHeader("X-Couch-Test-Header"), "header should be cool");
+ // test list() compatible API with provides function
+ xhr = CouchDB.request("GET", "/test_suite_db/_design/template/_show/list-api-provides/foo?format=text");
+ TEquals(xhr.responseText, "foo, bar, baz!", "should join chunks to response body");
+
+ // should keep next result order: chunks + return value + provided chunks + provided return value
+ xhr = CouchDB.request("GET", "/test_suite_db/_design/template/_show/list-api-provides-and-return/foo?format=text");
+ TEquals(xhr.responseText, "1, 2, 3, 4, 5, 6, 7!", "should not break 1..7 range");
+
xhr = CouchDB.request("GET", "/test_suite_db/_design/template/_show/list-api-mix/foo");
T(xhr.responseText == "Hey Dude");
TEquals("Yeah", xhr.getResponseHeader("X-Couch-Test-Header"), "header should be cool");

0 comments on commit 92f7021

Please sign in to comment.