aboutsummaryrefslogtreecommitdiffstats
path: root/activeresource/test/cases/http_mock_test.rb
blob: d2fd911314082d2ae6fc2e52ec0fea266ed7dab5 (plain) (blame)
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
require 'abstract_unit'
require 'active_support/core_ext/object/inclusion'

class HttpMockTest < ActiveSupport::TestCase
  setup do
    @http = ActiveResource::HttpMock.new("http://example.com")
  end

  FORMAT_HEADER = ActiveResource::Connection::HTTP_FORMAT_HEADER_NAMES

  [:post, :put, :get, :delete, :head].each do |method|
    test "responds to simple #{method} request" do
      ActiveResource::HttpMock.respond_to do |mock|
        mock.send(method, "/people/1", { FORMAT_HEADER[method] => "application/json" }, "Response")
      end

      assert_equal "Response", request(method, "/people/1", FORMAT_HEADER[method] => "application/json").body
    end

    test "adds format header by default to #{method} request" do
      ActiveResource::HttpMock.respond_to do |mock|
        mock.send(method, "/people/1", {}, "Response")
      end

      assert_equal "Response", request(method, "/people/1", FORMAT_HEADER[method] => "application/json").body
    end

    test "respond only when headers match header by default to #{method} request" do
      ActiveResource::HttpMock.respond_to do |mock|
        mock.send(method, "/people/1", {"X-Header" => "X"}, "Response")
      end

      assert_equal "Response", request(method, "/people/1", "X-Header" => "X").body
      assert_raise(ActiveResource::InvalidRequestError) { request(method, "/people/1") }
    end

    test "does not overwrite format header to #{method} request" do
      ActiveResource::HttpMock.respond_to do |mock|
        mock.send(method, "/people/1", {FORMAT_HEADER[method] => "application/json"}, "Response")
      end

      assert_equal "Response", request(method, "/people/1", FORMAT_HEADER[method] => "application/json").body
    end

    test "ignores format header when there is only one response to same url in a #{method} request" do
      ActiveResource::HttpMock.respond_to do |mock|
        mock.send(method, "/people/1", {}, "Response")
      end

      assert_equal "Response", request(method, "/people/1", FORMAT_HEADER[method] => "application/json").body
      assert_equal "Response", request(method, "/people/1", FORMAT_HEADER[method] => "application/xml").body
    end

    test "responds correctly when format header is given to #{method} request" do
      ActiveResource::HttpMock.respond_to do |mock|
        mock.send(method, "/people/1", { FORMAT_HEADER[method] => "application/xml" }, "XML")
        mock.send(method, "/people/1", { FORMAT_HEADER[method] => "application/json" }, "Json")
      end

      assert_equal "XML", request(method, "/people/1", FORMAT_HEADER[method] => "application/xml").body
      assert_equal "Json", request(method, "/people/1", FORMAT_HEADER[method] => "application/json").body
    end

    test "raises InvalidRequestError if no response found for the #{method} request" do
      ActiveResource::HttpMock.respond_to do |mock|
        mock.send(method, "/people/1", { FORMAT_HEADER[method] => "application/json" }, "json")
      end

      assert_raise(::ActiveResource::InvalidRequestError) do
        request(method, "/people/1", FORMAT_HEADER[method] => "application/xml")
      end
    end

  end

  test "allows you to send in pairs directly to the respond_to method" do
    matz  = { :person => { :id => 1, :name => "Matz" } }.to_json

    create_matz = ActiveResource::Request.new(:post, '/people.json', matz, {})
    created_response = ActiveResource::Response.new("", 201, { "Location" => "/people/1.json" })
    get_matz = ActiveResource::Request.new(:get, '/people/1.json', nil)
    ok_response = ActiveResource::Response.new(matz, 200, {})

    pairs = {create_matz => created_response, get_matz => ok_response}

    ActiveResource::HttpMock.respond_to(pairs)
    assert_equal 2, ActiveResource::HttpMock.responses.length
    assert_equal "", ActiveResource::HttpMock.responses.assoc(create_matz)[1].body
    assert_equal matz, ActiveResource::HttpMock.responses.assoc(get_matz)[1].body
  end

  test "resets all mocked responses on each call to respond_to with a block by default" do
    ActiveResource::HttpMock.respond_to do |mock|
      mock.send(:get, "/people/1", {}, "JSON1")
    end
    assert_equal 1, ActiveResource::HttpMock.responses.length

    ActiveResource::HttpMock.respond_to do |mock|
      mock.send(:get, "/people/2", {}, "JSON2")
    end
    assert_equal 1, ActiveResource::HttpMock.responses.length
  end

  test "resets all mocked responses on each call to respond_to by passing pairs by default" do
    ActiveResource::HttpMock.respond_to do |mock|
      mock.send(:get, "/people/1", {}, "JSON1")
    end
    assert_equal 1, ActiveResource::HttpMock.responses.length

    matz  = { :person => { :id => 1, :name => "Matz" } }.to_json
    get_matz = ActiveResource::Request.new(:get, '/people/1.json', nil)
    ok_response = ActiveResource::Response.new(matz, 200, {})
    ActiveResource::HttpMock.respond_to({get_matz => ok_response})

    assert_equal 1, ActiveResource::HttpMock.responses.length
  end

  test "allows you to add new responses to the existing responses by calling a block" do
    ActiveResource::HttpMock.respond_to do |mock|
      mock.send(:get, "/people/1", {}, "JSON1")
    end
    assert_equal 1, ActiveResource::HttpMock.responses.length

    ActiveResource::HttpMock.respond_to(false) do |mock|
      mock.send(:get, "/people/2", {}, "JSON2")
    end
    assert_equal 2, ActiveResource::HttpMock.responses.length
  end

  test "allows you to add new responses to the existing responses by passing pairs" do
    ActiveResource::HttpMock.respond_to do |mock|
      mock.send(:get, "/people/1", {}, "JSON1")
    end
    assert_equal 1, ActiveResource::HttpMock.responses.length

    matz  = { :person => { :id => 1, :name => "Matz" } }.to_json
    get_matz = ActiveResource::Request.new(:get, '/people/1.json', nil)
    ok_response = ActiveResource::Response.new(matz, 200, {})
    ActiveResource::HttpMock.respond_to({get_matz => ok_response}, false)

    assert_equal 2, ActiveResource::HttpMock.responses.length
  end

  test "allows you to replace the existing reponse with the same request by calling a block" do
    ActiveResource::HttpMock.respond_to do |mock|
      mock.send(:get, "/people/1", {}, "JSON1")
    end
    assert_equal 1, ActiveResource::HttpMock.responses.length

    ActiveResource::HttpMock.respond_to(false) do |mock|
      mock.send(:get, "/people/1", {}, "JSON2")
    end
    assert_equal 1, ActiveResource::HttpMock.responses.length
  end

  test "allows you to replace the existing reponse with the same request by passing pairs" do
    ActiveResource::HttpMock.respond_to do |mock|
      mock.send(:get, "/people/1", {}, "JSON1")
    end
    assert_equal 1, ActiveResource::HttpMock.responses.length

    matz  = { :person => { :id => 1, :name => "Matz" } }.to_json
    get_matz = ActiveResource::Request.new(:get, '/people/1', nil)
    ok_response = ActiveResource::Response.new(matz, 200, {})

    ActiveResource::HttpMock.respond_to({get_matz => ok_response}, false)
    assert_equal 1, ActiveResource::HttpMock.responses.length
  end

  test "do not replace the response with the same path but different method by calling a block" do
    ActiveResource::HttpMock.respond_to do |mock|
      mock.send(:get, "/people/1", {}, "JSON1")
    end
    assert_equal 1, ActiveResource::HttpMock.responses.length

    ActiveResource::HttpMock.respond_to(false) do |mock|
      mock.send(:put, "/people/1", {}, "JSON2")
    end
    assert_equal 2, ActiveResource::HttpMock.responses.length
  end

  test "do not replace the response with the same path but different method by passing pairs" do
    ActiveResource::HttpMock.respond_to do |mock|
      mock.send(:get, "/people/1", {}, "JSON1")
    end
    assert_equal 1, ActiveResource::HttpMock.responses.length

    put_matz = ActiveResource::Request.new(:put, '/people/1', nil)
    ok_response = ActiveResource::Response.new("", 200, {})

    ActiveResource::HttpMock.respond_to({put_matz => ok_response}, false)
    assert_equal 2, ActiveResource::HttpMock.responses.length
  end

  def request(method, path, headers = {}, body = nil)
    if method.in?([:put, :post])
      @http.send(method, path, body, headers)
    else
      @http.send(method, path, headers)
    end
  end
end