summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorncshaw <ncshaw@ibm.com>2021-07-14 15:37:26 -0400
committerNick Vatamaniuc <nickva@users.noreply.github.com>2021-07-17 12:36:33 -0400
commit647aea29ce8431fa5c2049cc6da47a9305ad3e6b (patch)
treeb05bfcbd8bd40e0cb558a47f0a79d416afdbf7b5
parent284546167a0240e272229c9c8f9beecb69681cd9 (diff)
downloadcouchdb-647aea29ce8431fa5c2049cc6da47a9305ad3e6b.tar.gz
Fix response code for nonexistent attachment
-rw-r--r--src/chttpd/src/chttpd_db.erl36
-rw-r--r--test/elixir/test/attachments_test.exs5
2 files changed, 30 insertions, 11 deletions
diff --git a/src/chttpd/src/chttpd_db.erl b/src/chttpd/src/chttpd_db.erl
index 0c9d0ede8..41bb74135 100644
--- a/src/chttpd/src/chttpd_db.erl
+++ b/src/chttpd/src/chttpd_db.erl
@@ -1664,6 +1664,22 @@ couch_doc_open(Db, DocId, Rev, Options) ->
end
end.
+get_attachment(Req, Db, DocId, FileName) ->
+ % Check if attachment exists
+ #doc_query_args{
+ rev=Rev,
+ options=Options
+ } = parse_doc_query(Req),
+ #doc{
+ atts=Atts
+ } = Doc = couch_doc_open(Db, DocId, Rev, Options),
+ case [A || A <- Atts, couch_att:fetch(name, A) == FileName] of
+ [] ->
+ {error, missing};
+ [Att] ->
+ {ok, Doc, Att}
+ end.
+
% Attachment request handlers
db_attachment_req(#httpd{method = 'GET', mochi_req = MochiReq} = Req, Db, DocId, FileNameParts) ->
@@ -1676,17 +1692,10 @@ db_attachment_req(#httpd{method = 'GET', mochi_req = MochiReq} = Req, Db, DocId,
"/"
)
),
- #doc_query_args{
- rev = Rev,
- options = Options
- } = parse_doc_query(Req),
- #doc{
- atts = Atts
- } = Doc = couch_doc_open(Db, DocId, Rev, Options),
- case [A || A <- Atts, couch_att:fetch(name, A) == FileName] of
- [] ->
+ case get_attachment(Req, Db, DocId, FileName) of
+ {error, missing} ->
throw({not_found, "Document is missing attachment"});
- [Att] ->
+ {ok, Doc, Att} ->
[Type, Enc, DiskLen, AttLen, Md5] = couch_att:fetch(
[type, encoding, disk_len, att_len, md5], Att
),
@@ -1822,7 +1831,12 @@ db_attachment_req(#httpd{method = Method} = Req, Db, DocId, FileNameParts) when
NewAtt =
case Method of
'DELETE' ->
- [];
+ case get_attachment(Req, Db, DocId, FileName) of
+ {error, missing} ->
+ throw({not_found, "Document is missing attachment"});
+ {ok, _, _} ->
+ []
+ end;
_ ->
MimeType =
case chttpd:header_value(Req, "Content-Type") of
diff --git a/test/elixir/test/attachments_test.exs b/test/elixir/test/attachments_test.exs
index 8e7f7d352..1f7c27ada 100644
--- a/test/elixir/test/attachments_test.exs
+++ b/test/elixir/test/attachments_test.exs
@@ -109,6 +109,11 @@ defmodule AttachmentsTest do
resp = Couch.delete("/#{db_name}/bin_doc/foo.txt", query: %{w: 3})
assert resp.status_code == 409
+
+ resp = Couch.delete("/#{db_name}/bin_doc/notexisting.txt", query: %{w: 3, rev: rev})
+ assert resp.status_code == 404
+ assert resp.body["error"] == "not_found"
+ assert resp.body["reason"] == "Document is missing attachment"
resp = Couch.delete("/#{db_name}/bin_doc/foo.txt", query: %{w: 3, rev: rev})
assert resp.status_code == 200