-
Notifications
You must be signed in to change notification settings - Fork 15
Description
I'm running into an issue where I want to override a GET request's response xml with a little customization, but I don't seem to be able to.
From script/console, here's what I'm trying to do:
require 'dupe'
class Quote < ActiveResource::Base
self.prefix = '/my_namespace/'
end
Dupe.define :quote
Get %r{^/my_namespace/quotes/(\d+)\.xml$} do |quote_id|
raise "hi!"
end
dupe_quote = Dupe.create(:quote)
And finally:
>> Quote.find(dupe_quote.id)
=> #<Quote:0x211680c @attributes={"id"=>1}, @prefix_options={}>
I would have expected it to raise my error. My personal usage for this is somewhat more complicated, but I've made this simple example to demonstrate the problem.
Looking at the log...
>> puts Dupe.network.log.pretty_print
Logged Requests:
Request: GET /my_namespace/quotes/1.xml
Response:
1
And inspecting the defined mocks:
>> Dupe.network.mocks
=> {:get=>[#<Dupe::Network::GetMock:0x213679c @response=#<Proc:0x02137430@(eval):5>, @url_pattern=/^\/my_namespace\/quotes\.xml$/>, #<Dupe::Network::GetMock:0x21366fc @response=#<Proc:0x02137160@(eval):10>, @url_pattern=/^\/my_namespace\/quotes\/(\d+)\.xml$/>, #<Dupe::Network::GetMock:0x2131508 @response=#<Proc:0x02131594@(irb):7>, @url_pattern=/^\/my_namespace\/quotes\/(\d+)\.xml$/>], :post=>[#<Dupe::Network::PostMock:0x21366d4 @response=#<Proc:0x02136f1c@(eval):15>, @url_pattern=/^\/my_namespace\/quotes\.xml$/>], :put=>[#<Dupe::Network::PutMock:0x21366ac @response=#<Proc:0x02136b98@(eval):20>, @url_pattern=/^\/my_namespace\/quotes\/(\d+)\.xml$/>], :delete=>[#<Dupe::Network::DeleteMock:0x2136684 @response=#<Proc:0x021368b4@(eval):25>, @url_pattern=/^\/my_namespace\/quotes\/(\d+)\.xml$/>]}
Of specific note is the url patterns:
>> Dupe.network.mocks[:get].map{|mock| mock.url_pattern}
=> [/^\/my_namespace\/quotes\.xml$/, /^\/my_namespace\/quotes\/(\d+)\.xml$/, /^\/my_namespace\/quotes\/(\d+)\.xml$/]
Notice how there are two identical url patterns there (originally I was doing this with a slightly different url pattern - didn't have the ^$ symbols and was using the character class [0-9] instead of \d, but tried duplicating the pattern to see if it would overwrite -- it didn't, as you can see). It seems as though the dupe definition is creating it's normal default GET mock, but no matter what I do I can't override it with my own. Is there a way to force certain calls to not create their auto-mocks? Or is there some other way to do this?
How does it decide which url pattern/response to follow? It should just be the last one defined shouldn't it? I figure that the Dupe.define :quote would make a url pattern, but since I define mine after that one it should catch on my definition and not fall through to the automatically-generated one.
Edit: Added the call to Dupe.network.log.pretty_print