couchdb-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From kxe...@apache.org
Subject [2/2] couch-mrview commit: updated refs/heads/master to c3bed46
Date Mon, 09 Nov 2015 23:40:29 GMT
Merge default update response headers with custom ones

COUCHDB-1447


Project: http://git-wip-us.apache.org/repos/asf/couchdb-couch-mrview/repo
Commit: http://git-wip-us.apache.org/repos/asf/couchdb-couch-mrview/commit/0334f690
Tree: http://git-wip-us.apache.org/repos/asf/couchdb-couch-mrview/tree/0334f690
Diff: http://git-wip-us.apache.org/repos/asf/couchdb-couch-mrview/diff/0334f690

Branch: refs/heads/master
Commit: 0334f690c5ccc16ea0284d9c2f2b58c77f1fc2b9
Parents: 1c8a302
Author: Alexander Shorin <kxepal@apache.org>
Authored: Thu Oct 22 23:21:17 2015 +0300
Committer: Alexander Shorin <kxepal@apache.org>
Committed: Tue Nov 10 02:38:24 2015 +0300

----------------------------------------------------------------------
 src/couch_mrview_show.erl | 78 ++++++++++++++++++++++++++++++++++++++----
 1 file changed, 71 insertions(+), 7 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/couchdb-couch-mrview/blob/0334f690/src/couch_mrview_show.erl
----------------------------------------------------------------------
diff --git a/src/couch_mrview_show.erl b/src/couch_mrview_show.erl
index dcfe22f..3c38c71 100644
--- a/src/couch_mrview_show.erl
+++ b/src/couch_mrview_show.erl
@@ -136,13 +136,11 @@ send_doc_update_response(Req, Db, DDoc, UpdateName, Doc, DocId) ->
             couch_doc:validate_docid(NewDoc#doc.id),
             {ok, NewRev} = couch_db:update_doc(Db, NewDoc, Options),
             NewRevStr = couch_doc:rev_to_str(NewRev),
-            {[
-                {<<"code">>, 201},
-                {<<"headers">>, {[
-                    {<<"X-Couch-Update-NewRev">>, NewRevStr},
-                    {<<"X-Couch-Id">>, NewDoc#doc.id}
-                ]}}
-                | JsonResp0]};
+            {JsonResp1} = apply_headers(JsonResp0, [
+                {<<"X-Couch-Update-NewRev">>, NewRevStr},
+                {<<"X-Couch-Id">>, NewDoc#doc.id}
+            ]),
+            {[{<<"code">>, 201} | JsonResp1]};
         [<<"up">>, _Other, {JsonResp0}] ->
             {[{<<"code">>, 200} | JsonResp0]}
     end,
@@ -340,6 +338,23 @@ apply_etag({ExternalResponse}, CurrentEtag) ->
         end || Field <- ExternalResponse]}
     end.
 
+apply_headers(JsonResp, []) ->
+    JsonResp;
+apply_headers(JsonResp, NewHeaders) ->
+    case couch_util:get_value(<<"headers">>, JsonResp) of
+        undefined ->
+            {[{<<"headers">>, {NewHeaders}}| JsonResp]};
+        JsonHeaders ->
+            Headers = apply_headers1(JsonHeaders, NewHeaders),
+            NewKV = {<<"headers">>, Headers},
+            {lists:keyreplace(<<"headers">>, 1, JsonResp, NewKV)}
+    end.
+apply_headers1(JsonHeaders, [{Key, Value} | Rest]) ->
+    NewJsonHeaders = json_apply_field({Key, Value}, JsonHeaders),
+    apply_headers1(NewJsonHeaders, Rest);
+apply_headers1(JsonHeaders, []) ->
+    JsonHeaders.
+
 
 % Maybe this is in the proplists API
 % todo move to couch_util
@@ -370,3 +385,52 @@ json_req_obj(Req, Db) ->
 
 last_chunk(Resp) ->
     chttpd:send_chunk(Resp, []).
+
+
+-ifdef(TEST).
+-include_lib("eunit/include/eunit.hrl").
+
+apply_headers_test_() ->
+    [
+        should_apply_headers(),
+        should_apply_headers_with_merge(),
+        should_apply_headers_with_merge_overwrite()
+    ].
+
+should_apply_headers() ->
+    ?_test(begin
+        JsonResp = [{<<"code">>, 201}],
+        Headers  = [{<<"foo">>, <<"bar">>}],
+        {Props} = apply_headers(JsonResp, Headers),
+        JsonHeaders = couch_util:get_value(<<"headers">>, Props),
+        ?assertEqual({Headers}, JsonHeaders)
+    end).
+
+should_apply_headers_with_merge() ->
+    ?_test(begin
+        BaseHeaders = [{<<"bar">>, <<"baz">>}],
+        NewHeaders  = [{<<"foo">>, <<"bar">>}],
+        JsonResp = [
+            {<<"code">>, 201},
+            {<<"headers">>, {BaseHeaders}}
+        ],
+        {Props} = apply_headers(JsonResp, NewHeaders),
+        JsonHeaders = couch_util:get_value(<<"headers">>, Props),
+        ExpectedHeaders = {NewHeaders ++ BaseHeaders},
+        ?assertEqual(ExpectedHeaders, JsonHeaders)
+    end).
+
+should_apply_headers_with_merge_overwrite() ->
+    ?_test(begin
+        BaseHeaders = [{<<"foo">>, <<"bar">>}],
+        NewHeaders  = [{<<"foo">>, <<"baz">>}],
+        JsonResp = [
+            {<<"code">>, 201},
+            {<<"headers">>, {BaseHeaders}}
+        ],
+        {Props} = apply_headers(JsonResp, NewHeaders),
+        JsonHeaders = couch_util:get_value(<<"headers">>, Props),
+        ?assertEqual({NewHeaders}, JsonHeaders)
+    end).
+
+-endif.


Mime
View raw message