aboutsummaryrefslogtreecommitdiff
path: root/lib/http_client/in_memory.ex
blob: a448afdf77ddf81f3d149788493a50e44ce75c96 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
defmodule Zendex.HttpClient.InMemory do
  @moduledoc """
  Allows testing of the Zendex project by mocking out calls to an actual Zendesk
  API.
  """

  @base_url "http://test.zendesk.com"

  def get!(@base_url <> "/api/v2/tickets.json",
           [{"Authorization", _authentication}]) do
    fake_response("ticket")
  end

  def get!(@base_url <> "/api/v2/search.json?query=requester%3AJimbob+type%3Aticket",
           [{"Authorization", _authentication}]) do
    fake_response(["Jimbob Ticket 1", "Jimbob Ticket 2"])
  end

  def get!(@base_url <> "/api/v2/search.json?query=requester%3AReginald+type%3Aticket&sort_by=created_at&sort_order=desc",
           [{"Authorization", _authentication}]) do
    fake_response(["Reginald Ticket 1", "Reginald Ticket 2"])
  end

  def get!(@base_url <> "/api/v2/users.json",
           [{"Authorization", _authentication}]) do
    fake_response("users")
  end

  def get!("#{@base_url}/api/v2/users/87.json",
           [{"Authorization", _authentication}]) do
    fake_response(%{"user": %{"id": 87, "name": "Quim Stroud"}})
  end

  def get!("#{@base_url}/api/v2/users/show_many.json?ids=6,67",
           [{"Authorization", _authentication}]) do
    fake_response(%{users: [%{id: 6, name: "Kiki Segal"},
                            %{id: 67, name: "Sarpedon Baumgartner"}]})
  end

  def post!(@base_url <> "/api/v2/tickets.json",
            "{\"ticket\":{}}",
            [{"Authorization", _authentication}, {"Content-Type", "application/json"}]) do
    fake_response("Ticket created successfully!")
  end

  def post!(@base_url <> "/api/v2/users.json",
            "{\"user\":{\"name\":\"Roger\",\"email\":\"roger@dodger.com\"}}",
            [{"Authorization", _authentication}, {"Content-Type", "application/json"}]) do
    fake_response(%{user: %{id: 1234, name: "Roger", email: "roger@dodger.com"}})
  end

  defp fake_response(body), do: %{body: Poison.encode!(body)}
end