diff options
Diffstat (limited to 'test/polyjuice/client_test.exs')
-rw-r--r-- | test/polyjuice/client_test.exs | 79 |
1 files changed, 79 insertions, 0 deletions
diff --git a/test/polyjuice/client_test.exs b/test/polyjuice/client_test.exs index ecb539a..8b5b415 100644 --- a/test/polyjuice/client_test.exs +++ b/test/polyjuice/client_test.exs @@ -70,4 +70,83 @@ defmodule Polyjuice.ClientTest do ) end end + + defmodule Httpd do + def foo(session_id, _env, {_, input}) do + if input == 'foobar' do + :mod_esi.deliver( + session_id, + 'Content-Type: application/json\r\n\r\n{"foo":"bar"}' + ) + else + :mod_esi.deliver( + session_id, + 'Status: 400 Bad Request\r\nContent-Type: application/json\r\n\r\n{"errcode":"M_UNKNOWN","error":"Wrong contents"}' + ) + end + end + end + + test "call" do + {:ok, tmpdir} = TestUtil.mktmpdir("sync-") + + try do + tmpdir_charlist = to_charlist(tmpdir) + + :inets.start() + + {:ok, httpd_pid} = + :inets.start( + :httpd, + port: 0, + server_name: 'sync.test', + server_root: tmpdir_charlist, + document_root: tmpdir_charlist, + bind_address: {127, 0, 0, 1}, + modules: [:mod_esi], + erl_script_alias: {'', [Polyjuice.ClientTest.Httpd]} + ) + + port = :httpd.info(httpd_pid) |> Keyword.fetch!(:port) + + client = %Polyjuice.Client{ + base_url: "http://127.0.0.1:#{port}/Elixir.Polyjuice.ClientTest.Httpd/", + access_token: "an_access_token", + user_id: "@alice:example.org", + test: true + } + + # binary body + assert Polyjuice.Client.API.call( + client, + %DummyEndpoint{ + http_spec: %Polyjuice.Client.Endpoint.HttpSpec{ + method: :put, + url: "foo", + headers: [], + body: "foobar", + auth_required: false + } + } + ) == {:ok, %{"foo" => "bar"}} + + # iolist body + assert Polyjuice.Client.API.call( + client, + %DummyEndpoint{ + http_spec: %Polyjuice.Client.Endpoint.HttpSpec{ + method: :put, + url: "foo", + headers: [], + body: [?f, ["oo"], ["b", [?a | "r"]]], + auth_required: false + } + } + ) == {:ok, %{"foo" => "bar"}} + + :inets.stop(:httpd, httpd_pid) + after + File.rm_rf(tmpdir) + end + end end |