New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Prefer: return=minimal support #605
Changes from 6 commits
1014684
bfa946c
5fd5143
9f3e745
f1b6a96
f05b672
70fedd6
c603bc7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
% 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. | ||
|
||
-module(chttpd_prefer_header). | ||
|
||
|
||
-export([ | ||
maybe_return_minimal/2 | ||
]). | ||
|
||
|
||
-include_lib("couch/include/couch_db.hrl"). | ||
|
||
|
||
maybe_return_minimal(#httpd{mochi_req = MochiReq}, Headers) -> | ||
case get_prefer_header(MochiReq) of | ||
"return=minimal" -> | ||
filter_headers(Headers, get_header_list()); | ||
_ -> | ||
Headers | ||
end. | ||
|
||
|
||
get_prefer_header(Req) -> | ||
case Req:get_header_value("Prefer") of | ||
Value when is_list(Value) -> | ||
string:to_lower(Value); | ||
undefined -> | ||
undefined | ||
end. | ||
|
||
|
||
filter_headers(Headers, IncludeList) -> | ||
lists:filter(fun({HeaderName, _}) -> | ||
lists:member(HeaderName, IncludeList) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Seems like you probably want to look into mochiweb's header handling to make this comparison case insensitive. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Because users may or may not give headers in the same case as we have them specified internally. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also that'd make for a good unit test as well. |
||
end, Headers). | ||
|
||
|
||
get_header_list() -> | ||
SectionStr = config:get("chttpd", "prefer_minimal", []), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the default needs to be ""? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've changed it to |
||
split_list(SectionStr). | ||
|
||
|
||
split_list(S) -> | ||
re:split(S, "\\s*,\\s*", [trim, {return, list}]). |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
% 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. | ||
|
||
-module(chttpd_prefer_header_test). | ||
-include_lib("couch/include/couch_db.hrl"). | ||
-include_lib("eunit/include/eunit.hrl"). | ||
|
||
|
||
mock_request(ExcludeHeader) -> | ||
Headers = mochiweb_headers:make(ExcludeHeader), | ||
MochiReq = mochiweb_request:new(nil, 'GET', "/", {1, 1}, Headers), | ||
MochiReq:cleanup(), | ||
#httpd{mochi_req = MochiReq}. | ||
|
||
|
||
default_headers() -> | ||
[ | ||
{"Cache-Control","must-revalidate"}, | ||
{"Content-Type","application/json"}, | ||
{"Content-Length", "100"}, | ||
{"ETag","\"12343\""}, | ||
{"X-Couch-Request-ID","7bd1adab86"}, | ||
{"X-CouchDB-Body-Time","0"}, | ||
{"Vary", "Accept-Encoding"}, | ||
{"Server","CouchDB/2.1.0-f1a1d7f1c (Erlang OTP/19)"} | ||
]. | ||
|
||
|
||
minimal_options_headers() -> | ||
[ | ||
{"Cache-Control","must-revalidate"}, | ||
{"Content-Type","application/json"}, | ||
{"Content-Length", "100"}, | ||
{"ETag","\"12343\""}, | ||
{"Vary", "Accept-Encoding"}, | ||
{"Server","CouchDB/2.1.0-f1a1d7f1c (Erlang OTP/19)"} | ||
]. | ||
|
||
|
||
default_no_exclude_header_test() -> | ||
Headers = chttpd_prefer_header:maybe_return_minimal( | ||
mock_request([]), | ||
default_headers() | ||
), | ||
?assertEqual(default_headers(), Headers). | ||
|
||
|
||
unsupported_exclude_header_test() -> | ||
Req = mock_request([{"prefer", "Wrong"}]), | ||
Headers = chttpd_prefer_header:maybe_return_minimal(Req, default_headers()), | ||
?assertEqual(default_headers(), Headers). | ||
|
||
|
||
empty_header_test() -> | ||
Req = mock_request([{"prefer", ""}]), | ||
Headers = chttpd_prefer_header:maybe_return_minimal(Req, default_headers()), | ||
?assertEqual(default_headers(), Headers). | ||
|
||
setup() -> | ||
ok = meck:new(config), | ||
ok = meck:expect(config, get, fun("chttpd", "prefer_minimal", _) -> | ||
"Cache-Control, Content-Length, Content-Type, ETag, Server, Vary" | ||
end), | ||
ok. | ||
|
||
|
||
teardown(_) -> | ||
meck:unload(config). | ||
|
||
|
||
exclude_headers_test_() -> | ||
{ | ||
"Test Prefer headers", | ||
{ | ||
foreach, fun setup/0, fun teardown/1, | ||
[ | ||
fun minimal_options/1, | ||
fun minimal_options_check_header_case/1, | ||
fun minimal_options_check_header_value_case/1 | ||
] | ||
} | ||
}. | ||
|
||
|
||
minimal_options(_) -> | ||
Req = mock_request([{"Prefer", "return=minimal"}]), | ||
Headers = chttpd_prefer_header:maybe_return_minimal(Req, default_headers()), | ||
?_assertEqual(minimal_options_headers(), Headers). | ||
|
||
|
||
minimal_options_check_header_case(_) -> | ||
Req = mock_request([{"prefer", "return=minimal"}]), | ||
Headers = chttpd_prefer_header:maybe_return_minimal(Req, default_headers()), | ||
?_assertEqual(minimal_options_headers(), Headers). | ||
|
||
|
||
minimal_options_check_header_value_case(_) -> | ||
Req = mock_request([{"prefer", "RETURN=MINIMAL"}]), | ||
Headers = chttpd_prefer_header:maybe_return_minimal(Req, default_headers()), | ||
?_assertEqual(minimal_options_headers(), Headers). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should be commented out by default.