How to run Capybara against a Rails server? - ruby

I have a test that uses OAuth2 gem to make make an HTTP request:
token = client.auth_code.get_token( code, redirect_uri:redirect_uri )
Capybara does not seem to be running the Rails app as a server:
Faraday::ConnectionFailed:
Connection refused - connect(2) for "localhost" port 3000
Here is the config:
Capybara.app_host = "http://localhost:3000"
Capybara.run_server = true
Capybara.server_port = 3000
Capybara.register_driver :rack_test do |app|
Capybara::RackTest::Driver.new app,
follow_redirects:false
end
Various docs seem to suggest that the above config will run the app as a server, but it does not seem to be the case.
How to run Capybara against a server so that the server responds to HTTP requests?
EDIT: After digging in the Capybara code, it seems that different drivers have different servers. Working with Poltergeist. So perhaps Poltergeist doesn't support running a server.

As usual, a small detail was overlooked.
The spec must be tagged as :js. Otherwise it uses Rack::Test and the server is not started.

Related

Simple Ruby Sinatra application runs, does not receive requests, times out

Required Sinatra, got '/', did 'hello, world.', requested application root in browser and received nothing.
Literally the barest of bones Sinatra web application will not run as intended on my Mac, after previously working just fine. Page requests do not show up in the console.
I feel this may be due to force quitting an instance of a running application but after searching for a solution on the web I am at a loss and do not know how to rectify the issue.
edit:
require 'sinatra'
get '/' do
'Hello, World.'
end
Problem does persist after a restart
Definitely the correct URL (exactly as I've been working with all day). localhost:4567 as stated in the output when the application is run:
== Sinatra (v1.4.7) has taken the stage on 4567 for development with backup from Thin
Thin web server (v1.6.4 codename Gob Bluth)
Maximum connections set to 1024
Listening on localhost:4567, CTRL+C to stop
edit 2: scorched earth strategy; full reinstall with rvm fixed this issue.

How to restart browser with Capybara on every scenario?

I run tests using Cucumber in conjunction with Capybara and Selenium-Webdriver. I want restart browser after each scenario. Here is my env.rb. I can add in After section something like this:
After do |scenario|
onError scenario if scenario.failed?
page.driver.browser.close
end
but this kills browser after first scenario passed and all other scenarios failed with reasonable error:
Errno::ECONNREFUSED: Connection refused - connect(2) for "127.0.0.1" port 7055
Is there way to refactor my env.rb to use Before hook to start browser on every scenario?
Simple:
Before do
Capybara::Selenium::Driver.new(app, :browser => :firefox, :profile => profile)
end

Mechanize Problems Connecting to HTTP Proxy...Ruby

I have a proxy set up and running completely fine on my local host. I can connect to the proxy completely fine running this code.
Net::HTTP::Proxy('http://localhost', 1234).start #do whatever I want after this point
I can connect to it through a browser completely fine, however when I go to run it on mechanize it completely fails. Here's the code.
require 'mechanize'
agent=Mechanize.new
agent.set_proxy('localhost', 1234)
agent.get('http://google.com') #or any website for that matter
Here's the error I get back
Net::HTTP::Persistent::Error: too many connection resets (due to end of file reached - EOFError) after 0 requests on 22249020, last used 1376802493.5352573 seconds ago
I've read that the versions after 1.0.0 have difficulties connecting to http proxies, but I need to and I'm currently running version 2.7.2. Is there anything I can do to connect to a proxy.
Some proxies can't handle persistent connections (keep-alive). You need to make sure to use a HTTP/1.1-aware proxy.
What version of Ruby are you running? I have this issue with Ruby 2.1.5 and Mechanize, but not with Ruby 1.9.3.
Presumably you've resolved your problem, but for anyone else who might end up here, downgrading to Ruby 1.9.3 might be a workaround.

How to use Rack map to run two Thin (or other) servers on different ports?

My aim is to do some automated testing over HTTP and HTTPS/SSL, via Rack, without recourse to a proxy server setup or anything like that. I have a gem that I want to test and I'd like for others to be able to run tests on too, so I'd like it to be as self contained as possible.
The code for App runs fine when run on it's own, so it's not included here, the problem is with the Rack part.
I'd like to do something like this:
app = Rack::Builder.app do
map "/" do
Rack::Handler::WEBrick.run App, Port: 3000
end
map "/ssl" do
Rack::Handler::WEBrick.run App, Port: 3001 # more options for SSL here...
end
end
run app
I've tried several combinations of the code above, like:
http = Rack::Builder.app do
map "/" do
run App
end
end
https = Rack::Builder.app do
map "/ssl" do
run App
end
end
Rack::Handler::WEBrick.run http, Port: 3000
Rack::Handler::WEBrick.run https, Port: 3001 # more options for SSL here...
With the two server setup up I tend to get one server run on the first port listed, then on interrupt it will run the second server on the next port listed - and then, on the next interrupt, either another server on 9292 or it shuts down.
I'm obviously doing something not quite right.
This is quite close, but ends up running the two servers via two different command line commands:
Starting thin server on different ports
Any help is much appreciated.
Current Thin does not support this -- I checked the source code.
Thin v2 is still pre-release, but the config code looks like it supports this via declaring multiple listeners in the config file.
But Thin v2 is still alpha software.
You can also switch to another server like Unicorn that does explicitly support binding to multiple ports or addresses.

Connect to Tor network with ruby

how can I configure Ruby web client to request web pages using Tor ?
I had to use this Gem http://socksify.rubyforge.org/ then I was able to do something like this
TCPSocket::socks_server = "127.0.0.1"
TCPSocket::socks_port = 9050
reply = Net::HTTP.get URI.parse("www.google.com")
Obviously with the Tor proxy running.
I think it's like specifying proxy server for your HTTP connection. I don't know how it works in Ruby. But it will not be different from configuring browsers. Just set proxy server setting to 127.0.0.1:8118.
Created a Gem, maybe can help others: https://github.com/brunogh/tor_requests
You just need to work with the Proxy class. As Ivan says above, get Tor running then point Net::HTTP.Proxy at the correct localhost address and you're golden.

Resources