|
7 | 7 | subject(:relation) { described_class.new context, pattern } |
8 | 8 |
|
9 | 9 | let(:context) { Restify::Context.new('http://test.host/') } |
10 | | - let(:pattern) { '/resource/{id}' } |
| 10 | + let(:pattern) { '/resource/{id}{?q*}' } |
11 | 11 |
|
12 | 12 | describe '#==' do |
13 | 13 | it 'equals pattern' do |
@@ -120,4 +120,115 @@ def to_param |
120 | 120 | it { expect { expanded }.to raise_exception TypeError, /Can't convert Hash into String./ } |
121 | 121 | end |
122 | 122 | end |
| 123 | + |
| 124 | + shared_examples 'non-data-request' do |method| |
| 125 | + it "issues a #{method.upcase} request" do |
| 126 | + expect(context).to receive(:request) do |meth, uri, opts| |
| 127 | + expect(meth).to eq method |
| 128 | + expect(uri.to_s).to eq 'http://test.host/resource/' |
| 129 | + expect(opts).to be_nil |
| 130 | + end |
| 131 | + relation.send(method) |
| 132 | + end |
| 133 | + |
| 134 | + it 'accepts params as keyword argument' do |
| 135 | + expect(context).to receive(:request) do |meth, uri, opts| |
| 136 | + expect(meth).to eq method |
| 137 | + expect(uri.to_s).to eq 'http://test.host/resource/1' |
| 138 | + expect(opts).to be_nil |
| 139 | + end |
| 140 | + relation.send(method, params: {id: 1}) |
| 141 | + end |
| 142 | + |
| 143 | + it 'accepts params as positional argument' do |
| 144 | + expect(context).to receive(:request) do |meth, uri, opts| |
| 145 | + expect(meth).to eq method |
| 146 | + expect(uri.to_s).to eq 'http://test.host/resource/1' |
| 147 | + expect(opts).to be_nil |
| 148 | + end |
| 149 | + relation.send(method, {id: 1}) |
| 150 | + end |
| 151 | + |
| 152 | + it 'merges keyword params into positional params' do |
| 153 | + expect(context).to receive(:request) do |meth, uri, opts| |
| 154 | + expect(meth).to eq method |
| 155 | + expect(uri.to_s).to eq 'http://test.host/resource/1?a=2&b=3' |
| 156 | + expect(opts).to be_nil |
| 157 | + end |
| 158 | + relation.send(method, {id: 1, q: {a: 1, c: 1}}, params: {q: {a: 2, b: 3}}) |
| 159 | + end |
| 160 | + end |
| 161 | + |
| 162 | + describe '#get' do |
| 163 | + include_examples 'non-data-request', :get |
| 164 | + end |
| 165 | + |
| 166 | + describe '#head' do |
| 167 | + include_examples 'non-data-request', :head |
| 168 | + end |
| 169 | + |
| 170 | + describe '#delete' do |
| 171 | + include_examples 'non-data-request', :delete |
| 172 | + end |
| 173 | + |
| 174 | + shared_examples 'data-request' do |method| |
| 175 | + let(:data) { Object.new } |
| 176 | + |
| 177 | + it "issues a #{method.upcase} request" do |
| 178 | + expect(context).to receive(:request) do |meth, uri, opts| |
| 179 | + expect(meth).to eq method |
| 180 | + expect(uri.to_s).to eq 'http://test.host/resource/' |
| 181 | + expect(opts).to eq({data: nil}) |
| 182 | + end |
| 183 | + relation.send(method) |
| 184 | + end |
| 185 | + |
| 186 | + it 'accepts params as keyword argument' do |
| 187 | + expect(context).to receive(:request) do |meth, uri, opts| |
| 188 | + expect(meth).to eq method |
| 189 | + expect(uri.to_s).to eq 'http://test.host/resource/1' |
| 190 | + expect(opts).to eq({data: nil}) |
| 191 | + end |
| 192 | + relation.send(method, params: {id: 1}) |
| 193 | + end |
| 194 | + |
| 195 | + it 'accepts data as keyword argument' do |
| 196 | + expect(context).to receive(:request) do |meth, uri, opts| |
| 197 | + expect(meth).to eq method |
| 198 | + expect(uri.to_s).to eq 'http://test.host/resource/1' |
| 199 | + expect(opts).to eq({data:}) |
| 200 | + end |
| 201 | + relation.send(method, data:, params: {id: 1}) |
| 202 | + end |
| 203 | + |
| 204 | + it 'accepts data as position argument' do |
| 205 | + expect(context).to receive(:request) do |meth, uri, opts| |
| 206 | + expect(meth).to eq method |
| 207 | + expect(uri.to_s).to eq 'http://test.host/resource/1' |
| 208 | + expect(opts).to eq({data:}) |
| 209 | + end |
| 210 | + relation.send(method, data, params: {id: 1}) |
| 211 | + end |
| 212 | + |
| 213 | + it 'prefers data from keyword argument' do |
| 214 | + expect(context).to receive(:request) do |meth, uri, opts| |
| 215 | + expect(meth).to eq method |
| 216 | + expect(uri.to_s).to eq 'http://test.host/resource/1' |
| 217 | + expect(opts).to eq({data:}) |
| 218 | + end |
| 219 | + relation.send(method, 'DATA', data:, params: {id: 1}) |
| 220 | + end |
| 221 | + end |
| 222 | + |
| 223 | + describe '#post' do |
| 224 | + include_examples 'data-request', :post |
| 225 | + end |
| 226 | + |
| 227 | + describe '#put' do |
| 228 | + include_examples 'data-request', :put |
| 229 | + end |
| 230 | + |
| 231 | + describe '#patch' do |
| 232 | + include_examples 'data-request', :patch |
| 233 | + end |
123 | 234 | end |
0 commit comments