You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@couchdb.apache.org by va...@apache.org on 2020/02/22 02:12:01 UTC

[couchdb] 02/02: Support setting base_url in Couch test helper (take 2)

This is an automated email from the ASF dual-hosted git repository.

vatamane pushed a commit to branch prototype/fdb-layer
in repository https://gitbox.apache.org/repos/asf/couchdb.git

commit 8c75367cdfec1dca62d2dd1ee82f908fdb7b25f0
Author: ILYA Khlopotov <ii...@apache.org>
AuthorDate: Fri Feb 21 09:25:49 2020 -0800

    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.
---
 test/elixir/lib/couch.ex | 28 ++++++++++++++++++++--------
 1 file changed, 20 insertions(+), 8 deletions(-)

diff --git a/test/elixir/lib/couch.ex b/test/elixir/lib/couch.ex
index 8f47ad8..94958ea 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