summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorILYA Khlopotov <iilyak@apache.org>2020-02-21 09:25:49 -0800
committerPaul J. Davis <paul.joseph.davis@gmail.com>2020-03-02 12:26:22 -0600
commit2e13cffa24042f54ed4b7cd522020c7afc0df48f (patch)
tree58354e3b8d8f4010405d1a10e963ea2413984db3
parentaa3b738537f1203ab25025a0d3c179f7d74be185 (diff)
downloadcouchdb-2e13cffa24042f54ed4b7cd522020c7afc0df48f.tar.gz
Support setting base_url in Couch test helper (take 2)
The reason why previous attempt failed is because it overrode [important logic in `process_url/2`](https://github.com/myfreeweb/httpotion/blob/v3.1.2/lib/httpotion.ex#L34:L35): ``` def process_url(url, options) do process_url(url) |> prepend_protocol |> append_query_string(options) end ``` This PR fixes the problem by adding the `prepend_protocol` and `append_query_string`. It also refactor the way base_url is passed around.
-rw-r--r--src/chttpd/test/exunit/tracing_test.exs2
-rw-r--r--test/elixir/lib/couch.ex44
-rw-r--r--test/elixir/test/auth_cache_test.exs2
3 files changed, 32 insertions, 16 deletions
diff --git a/src/chttpd/test/exunit/tracing_test.exs b/src/chttpd/test/exunit/tracing_test.exs
index b50ef936e..f66fb87a2 100644
--- a/src/chttpd/test/exunit/tracing_test.exs
+++ b/src/chttpd/test/exunit/tracing_test.exs
@@ -29,7 +29,7 @@ defmodule Couch.Test.OpenTracing do
setup context do
db_name = Utils.random_name("db")
- session = Couch.login(context.base_url, context.user, context.pass)
+ session = Couch.login(context.user, context.pass, base_url: context.base_url)
on_exit(fn ->
delete_db(session, db_name)
diff --git a/test/elixir/lib/couch.ex b/test/elixir/lib/couch.ex
index 3aef07f01..ed5862331 100644
--- a/test/elixir/lib/couch.ex
+++ b/test/elixir/lib/couch.ex
@@ -3,7 +3,7 @@ defmodule Couch.Session do
CouchDB session helpers.
"""
- defstruct [:cookie, :error]
+ defstruct [:cookie, :error, :base_url]
def new(cookie, error \\ "") do
%Couch.Session{cookie: cookie, error: error}
@@ -42,12 +42,12 @@ defmodule Couch.Session do
# if the need arises.
def go(%Couch.Session{} = sess, method, url, opts) do
- opts = Keyword.merge(opts, cookie: sess.cookie)
+ opts = Keyword.merge(opts, cookie: sess.cookie, base_url: sess.base_url)
Couch.request(method, url, opts)
end
def go!(%Couch.Session{} = sess, method, url, opts) do
- opts = Keyword.merge(opts, cookie: sess.cookie)
+ opts = Keyword.merge(opts, cookie: sess.cookie, base_url: sess.base_url)
Couch.request!(method, url, opts)
end
end
@@ -71,9 +71,10 @@ defmodule Couch do
url
end
- def process_url(url) do
- base_url = System.get_env("EX_COUCH_URL") || "http://127.0.0.1:15984"
- base_url <> url
+ def process_url(url, options) do
+ (Keyword.get(options, :base_url) <> url)
+ |> prepend_protocol
+ |> append_query_string(options)
end
def process_request_headers(headers, _body, options) do
@@ -96,10 +97,13 @@ defmodule Couch do
end
def process_options(options) do
+ base_url = System.get_env("EX_COUCH_URL") || "http://127.0.0.1:15984"
+ options = Keyword.put_new(options, :base_url, base_url)
+
options
- |> set_auth_options()
- |> set_inactivity_timeout()
- |> set_request_timeout()
+ |> set_auth_options()
+ |> set_inactivity_timeout()
+ |> set_request_timeout()
end
def process_request_body(body) do
@@ -161,17 +165,29 @@ defmodule Couch do
login(user, pass)
end
- def login(user, pass, expect \\ :success) do
- resp = Couch.post("/_session", body: %{:username => user, :password => pass})
+ def login(user, pass, options \\ []) do
+ options = options |> Enum.into(%{})
+
+ base_url =
+ Map.get_lazy(options, :base_url, fn ->
+ System.get_env("EX_COUCH_URL") || "http://127.0.0.1:15984"
+ end)
+
+ resp =
+ Couch.post(
+ "/_session",
+ body: %{:username => user, :password => pass},
+ base_url: base_url
+ )
- if expect == :success do
+ if Map.get(options, :expect, :success) == :success do
true = resp.body["ok"]
cookie = resp.headers[:"set-cookie"]
[token | _] = String.split(cookie, ";")
- %Couch.Session{cookie: token}
+ %Couch.Session{cookie: token, base_url: base_url}
else
true = Map.has_key?(resp.body, "error")
- %Couch.Session{error: resp.body["error"]}
+ %Couch.Session{error: resp.body["error"], base_url: base_url}
end
end
end
diff --git a/test/elixir/test/auth_cache_test.exs b/test/elixir/test/auth_cache_test.exs
index 2ba396de7..5582b2f96 100644
--- a/test/elixir/test/auth_cache_test.exs
+++ b/test/elixir/test/auth_cache_test.exs
@@ -56,7 +56,7 @@ defmodule AuthCacheTest do
end
defp login_fail(user, password) do
- resp = Couch.login(user, password, :fail)
+ resp = Couch.login(user, password, expect: :fail)
assert resp.error, "Login error is expected."
end