register_uri with regex

93 views
Skip to first unread message

millisami

unread,
Oct 21, 2009, 4:51:46 AM10/21/09
to FakeWeb
I read in the readme that fakeweb can register urls based upon the
regex.
My factory sequence is as follows:

Factory.sequence :link do |n|
"http://myapp#{n}.local"
end

So, I register this as follows:

FakeWeb.allow_net_connect = false
FakeWeb.register_uri(:get, /(^(http|https):\/\/.+(local))/ix, :body =>
"is made blank", :status => ["200", "OK"])

But its bypassing the fakeweb.
What I want to capture is that if the url has .local extension, I want
the fakeweb to catch it??
Is my regex wrong??

Chris Kampmeier

unread,
Oct 21, 2009, 10:20:56 AM10/21/09
to fakewe...@googlegroups.com
Hi millisami, sorry you're having trouble.

I tried your example in the console and it worked fine for me:

chrisk@al3:~ $ irb -rubygems
>> require 'fakeweb'
=> true
>> FakeWeb.allow_net_connect = false
=> false
>> FakeWeb.register_uri(:get, /(^(http|https):\/\/.+(local))/ix, :body => "is made blank", :status => ["200", "OK"])
=> [#<FakeWeb::Responder:0x1015c8698 @uri=/(^(http|https):\/\/.+(local))/ix, @method=:get, @times=1, @options={:status=>["200", "OK"], :body=>"is made blank"}>]
>> response = Net::HTTP.start('host.local') { |http| http.get('/') }
=> #<Net::HTTPOK 200 OK readbody=true>
>> response.body
=> "is made blank"
>> [response.code, response.message]
=> ["200", "OK"]

What version of FakeWeb are you using? Do you get a FakeWeb::NetConnectNotAllowedError, or just a time out? Can you post a console log (or failing test) like the one above?

Thanks!
Chris 

Sachin Sagar Rai

unread,
Oct 21, 2009, 11:06:15 AM10/21/09
to fakewe...@googlegroups.com
Hi Chris,
Well, I tried the same code that you'd tested and it works for me too in my irb console.
I'm using fakeweb 1.2.6
Let me clear up the scenario.
In my model, I'm using a plugin validates_uri_existence_of

class Project < ActiveRecord::Base
    validates_uri_existence_of :link, :allow_redirect => false
end

So, when I run my cucumber feature:
I get the following error:

======================================================================
    Given there exists 20 projects                 # features/step_definitions/projects_steps.rb:5
      Validation failed: Link is giving an error (HTTP code between 400 and 505) or is not responding (ActiveRecord::RecordInvalid)
      ./features/step_definitions/projects_steps.rb:8
      ./features/step_definitions/projects_steps.rb:6:in `/^there exists (\d+) projects$/'
      features/project.feature:8:in `Given there exists 20 projects'
    When I go to projects index page               # features/step_definitions/webrat_steps.rb:10
    Then I should see "Recent Projects"            # features/step_definitions/webrat_steps.rb:93

Failing Scenarios:
cucumber features/project.feature:7 # Scenario: Lists all the projects with pagination
=====================================================================

The step definition will execute the Factory(:project)

I'm confused. When I register the uri with Fakeweb for individual url manually, it works as expected but doesn't if I use the regex style?


Sachin Sagar Rai (aka - millisami)
<%= Freelance Web Designer#Developer %>
a.g.f.n. (a guy from Nepal)
http://www.tfm.com.np
http://www.welcometonewnepal.com
9851065264 / 9803857018
Reply all
Reply to author
Forward
0 new messages