-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into jwt-add-role-support
- Loading branch information
Showing
9 changed files
with
267 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
defmodule HttpTest do | ||
use CouchTestCase | ||
|
||
@moduletag :http | ||
|
||
@tag :with_db | ||
test "location header", context do | ||
db_name = context[:db_name] | ||
resp = Couch.put("/#{db_name}/test", body: %{}) | ||
db_url = Couch.process_url("/" <> db_name) | ||
assert resp.headers.hdrs["location"] == db_url <> "/test" | ||
end | ||
|
||
@tag :with_db | ||
test "location header should include X-Forwarded-Host", context do | ||
db_name = context[:db_name] | ||
|
||
resp = | ||
Couch.put("/#{db_name}/test2", | ||
body: %{}, | ||
headers: ["X-Forwarded-Host": "mysite.com"] | ||
) | ||
|
||
assert resp.headers.hdrs["location"] == "http:https://mysite.com/#{db_name}/test2" | ||
end | ||
|
||
@tag :with_db | ||
test "location header should include custom header", context do | ||
db_name = context[:db_name] | ||
|
||
server_config = [ | ||
%{ | ||
:section => "httpd", | ||
:key => "x_forwarded_host", | ||
:value => "X-Host" | ||
} | ||
] | ||
|
||
run_on_modified_server(server_config, fn -> | ||
resp = | ||
Couch.put("/#{db_name}/test3", | ||
body: %{}, | ||
headers: ["X-Host": "mysite2.com"] | ||
) | ||
|
||
assert resp.headers.hdrs["location"] == "http:https://mysite2.com/#{db_name}/test3" | ||
end) | ||
end | ||
|
||
@tag :with_db | ||
test "COUCHDB-708: newlines document names", context do | ||
db_name = context[:db_name] | ||
|
||
resp = | ||
Couch.put("/#{db_name}/docid%0A/attachment.txt", | ||
body: %{}, | ||
headers: ["Content-Type": "text/plain;charset=utf-8"] | ||
) | ||
|
||
db_url = Couch.process_url("/" <> db_name) | ||
assert resp.headers.hdrs["location"] == db_url <> "/docid%0A/attachment.txt" | ||
|
||
resp = | ||
Couch.put("/#{db_name}/docidtest%0A", | ||
body: %{}, | ||
headers: ["Content-Type": "text/plain;charset=utf-8"] | ||
) | ||
|
||
db_url = Couch.process_url("/" <> db_name) | ||
assert resp.headers.hdrs["location"] == db_url <> "/docidtest%0A" | ||
|
||
resp = | ||
Couch.post("/#{db_name}/", | ||
body: %{_id: "docidtestpost%0A"}, | ||
headers: ["Content-Type": "application/json"] | ||
) | ||
|
||
db_url = Couch.process_url("/" <> db_name) | ||
assert resp.headers.hdrs["location"] == db_url <> "/docidtestpost%250A" | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
defmodule JsonpTest do | ||
use CouchTestCase | ||
|
||
@moduletag :jsonp | ||
|
||
@tag :with_db | ||
test "jsonp not configured callbacks", context do | ||
db_name = context[:db_name] | ||
{:ok, _} = create_doc(db_name, %{_id: "0", a: 0, b: 0}) | ||
|
||
resp = Couch.get("/#{db_name}/0?callback=jsonp_no_chunk") | ||
assert resp.status_code == 200 | ||
assert resp.headers.hdrs["content-type"] == "application/json" | ||
end | ||
|
||
@tag :with_db | ||
test "jsonp unchunked callbacks", context do | ||
db_name = context[:db_name] | ||
|
||
server_config = [ | ||
%{ | ||
:section => "httpd", | ||
:key => "allow_jsonp", | ||
:value => "true" | ||
} | ||
] | ||
|
||
{:ok, create_resp} = create_doc(db_name, %{_id: "0", a: 0, b: 0}) | ||
|
||
run_on_modified_server(server_config, fn -> | ||
resp = Couch.get("/#{db_name}/0?callback=jsonp_no_chunk") | ||
|
||
assert resp.status_code == 200 | ||
assert resp.headers.hdrs["content-type"] == "application/javascript" | ||
|
||
{callback_fun, callback_param} = parse_callback(resp.body) | ||
|
||
assert callback_fun == "jsonp_no_chunk" | ||
assert create_resp.body["id"] == callback_param["_id"] | ||
assert create_resp.body["rev"] == callback_param["_rev"] | ||
|
||
resp = Couch.get("/#{db_name}/0?callback=jsonp_no_chunk\"") | ||
assert resp.status_code == 400 | ||
end) | ||
end | ||
|
||
@tag :with_db | ||
test "jsonp chunked callbacks", context do | ||
db_name = context[:db_name] | ||
|
||
server_config = [ | ||
%{ | ||
:section => "httpd", | ||
:key => "allow_jsonp", | ||
:value => "true" | ||
} | ||
] | ||
|
||
design_doc = %{ | ||
_id: "_design/test", | ||
language: "javascript", | ||
views: %{ | ||
all_docs: %{map: "function(doc) {if(doc.a) emit(null, doc.a);}"} | ||
} | ||
} | ||
|
||
{:ok, _} = create_doc(db_name, design_doc) | ||
{:ok, _} = create_doc(db_name, %{_id: "0", a: 0, b: 0}) | ||
{:ok, _} = create_doc(db_name, %{_id: "1", a: 1, b: 1}) | ||
|
||
run_on_modified_server(server_config, fn -> | ||
resp = Couch.get("/#{db_name}/_design/test/_view/all_docs?callback=jsonp_chunk") | ||
assert resp.status_code == 200 | ||
assert resp.headers.hdrs["content-type"] == "application/javascript" | ||
|
||
{callback_fun, callback_param} = parse_callback(resp.body) | ||
|
||
assert callback_fun == "jsonp_chunk" | ||
assert callback_param["total_rows"] == 1 | ||
|
||
resp = Couch.get("/#{db_name}/_design/test/_view/all_docs?callback=jsonp_chunk'") | ||
assert resp.status_code == 400 | ||
|
||
resp = Couch.get("/#{db_name}/_changes?callback=jsonp_chunk") | ||
assert resp.status_code == 200 | ||
assert resp.headers.hdrs["content-type"] == "application/javascript" | ||
|
||
{callback_fun, callback_param} = parse_callback(resp.body) | ||
assert callback_fun == "jsonp_chunk" | ||
assert length(callback_param["results"]) == 3 | ||
|
||
end) | ||
end | ||
|
||
defp parse_callback(msg) do | ||
captures = Regex.scan(~r/\/\* CouchDB \*\/(\w+)\((.*)\)/s, msg) | ||
|
||
callback_fun = | ||
captures | ||
|> Enum.map(fn p -> Enum.at(p, 1) end) | ||
|> Enum.at(0) | ||
|
||
param = | ||
captures | ||
|> Enum.map(fn p -> Enum.at(p, 2) end) | ||
|> Enum.filter(fn p -> String.trim(p) != "" end) | ||
|> Enum.map(fn p -> | ||
p | ||
|> IO.iodata_to_binary() | ||
|> :jiffy.decode([:return_maps]) | ||
end) | ||
|> Enum.at(0) | ||
|
||
{callback_fun, param} | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
defmodule MethodOverrideTest do | ||
use CouchTestCase | ||
|
||
@moduletag :http | ||
|
||
@moduledoc """ | ||
Allow broken HTTP clients to fake a full method vocabulary with an | ||
X-HTTP-METHOD-OVERRIDE header | ||
""" | ||
|
||
@tag :with_db | ||
test "method override PUT", context do | ||
db_name = context[:db_name] | ||
|
||
resp = | ||
Couch.post("/#{db_name}/fnord", | ||
body: %{bob: "connie"}, | ||
headers: ["X-HTTP-Method-Override": "PUT"] | ||
) | ||
|
||
assert resp.status_code == 201 | ||
|
||
resp = Couch.get("/#{db_name}/fnord") | ||
assert resp.body["bob"] == "connie" | ||
end | ||
|
||
@tag :with_db | ||
test "method override DELETE", context do | ||
db_name = context[:db_name] | ||
{:ok, resp} = create_doc(db_name, %{_id: "fnord", bob: "connie"}) | ||
|
||
resp = | ||
Couch.post("/#{db_name}/fnord?rev=#{resp.body["rev"]}", | ||
headers: ["X-HTTP-Method-Override": "DELETE"] | ||
) | ||
|
||
assert resp.status_code == 200 | ||
|
||
resp = Couch.get("/#{db_name}/fnord") | ||
assert resp.status_code == 404 | ||
end | ||
|
||
@tag :with_db | ||
test "Method Override is ignored when original Method isn't POST", context do | ||
db_name = context[:db_name] | ||
|
||
resp = | ||
Couch.get("/#{db_name}/fnord2", | ||
body: %{bob: "connie"}, | ||
headers: ["X-HTTP-Method-Override": "PUT"] | ||
) | ||
|
||
assert resp.status_code == 404 | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters