diff options
author | ILYA Khlopotov <iilyak@apache.org> | 2020-02-21 09:25:49 -0800 |
---|---|---|
committer | Nick Vatamaniuc <nickva@users.noreply.github.com> | 2020-02-21 21:11:47 -0500 |
commit | 8c75367cdfec1dca62d2dd1ee82f908fdb7b25f0 (patch) | |
tree | efd52dadf84b151a567f10ab1ca0209e1ee3f05f | |
parent | ded6d64781fa7e8b6adfd1bf5aa548503ba3606f (diff) | |
download | couchdb-8c75367cdfec1dca62d2dd1ee82f908fdb7b25f0.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-- | test/elixir/lib/couch.ex | 28 |
1 files changed, 20 insertions, 8 deletions
diff --git a/test/elixir/lib/couch.ex b/test/elixir/lib/couch.ex index 8f47ad85c..94958ea12 100644 --- a/test/elixir/lib/couch.ex +++ b/test/elixir/lib/couch.ex @@ -4,7 +4,7 @@ defmodule Couch.Session do """ @enforce_keys [:cookie] - defstruct [:cookie] + defstruct [:cookie, :base_url] def new(cookie) do %Couch.Session{cookie: cookie} @@ -33,12 +33,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 @@ -54,9 +54,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 @@ -79,6 +80,8 @@ 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) if Keyword.get(options, :cookie) == nil do headers = Keyword.get(options, :headers, []) @@ -118,11 +121,20 @@ defmodule Couch do end def login(user, pass) do - resp = Couch.post("/_session", body: %{:username => user, :password => pass}) + base_url = System.get_env("EX_COUCH_URL") || "http://127.0.0.1:15984" + login(base_url, user, pass) + end + + def login(base_url, user, pass) do + resp = Couch.post( + "/_session", + body: %{:username => user, :password => pass}, + base_url: base_url + ) 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} end end |