How to run Rails/Capybara with Puma server in SSL mode? - ruby-on-rails

I am trying to get capybara to run test puma server over ssl. Need help configuring it, please:
Capybara.register_server :ssl_puma do |app, port, host|
require 'rack/handler/puma'
Rack::Handler::Puma.run(app, Host: host, Port: port, Threads: "0:1") do |server|
ctx = Puma::MiniSSL::Context.new
ctx.key = ENV['SSL_KEY_PATH']
ctx.cert = ENV['SSL_CERT_PATH']
ctx.verify_mode = Puma::MiniSSL::VERIFY_NONE
server.add_ssl_listener host, port, ctx # this line is wrong, but that's the gyst of what needs to happen
end
end
Any ideas?

If you're using a recent version of Capybara I believe you should be able to do something like
Capybara.server = :puma, { Host: "ssl://#{Capybara.server_host}?key=#{ENV['SSL_KEY_PATH']}&cert=#{ENV['SSL_CERT_PATH']" }

Related

Capybara, Rails, Selenium, Chromedriver + ERR:CONNECTION_REFUSED

I'm setting up Capybara with Selenium Webdriver in my Rails application, and I keep running into the following error:
Helpdesk Ticketing System assigning a ticket should mark ticket as assigned
Failure/Error: visit tickets_path
Selenium::WebDriver::Error::UnknownError:
unknown error: net::ERR_CONNECTION_REFUSED
(Session info: chrome=85.0.4183.102)
My settings are as follows:
Capybara.server = :puma
Capybara.app_host = 'http://intranet.lvh.me'
Capybara.default_driver = :selenium_chrome_headless
In the logs it shows this before the failure, choosing a different port each time it runs:
Capybara starting Puma...
* Version 4.3.6 , codename: Mysterious Traveller
* Min threads: 0, max threads: 4
* Listening on tcp://127.0.0.1:56277
Anyone help?
I solved my problem. I was missing:
Capybara.always_include_port = true
I had set route-level URL defaults inside of config/environments/test.rb and that option pulls through to all parts of the application.
When I removed the line shown below, all my tests passed again.
# config/environments/test.rb
Rails.application.routes.default_url_options = {
host: "server.localhost",
port: 3000
}

Redis keeps calling out to localhost:6379 even though deployed to Heroku

I have a Rails app deployed to Heroku and I cant for the life of me figure out why it keeps wanting to deploy to the local. I do not even have localhost:6379 anywhere in my code for the front end (react native) or the back end which is my Rails API.
This is the error I get any time I have a new broadcast:
Completed 500 Internal Server Error in 111ms (ActiveRecord: 47.1ms)
Redis::CannotConnectError (Error connecting to Redis on localhost:6379 (Errno::ECONNREFUSED)):
Application.yaml:
`gmail_username: "<email_address>"
gmail_password: "<password>"
AWS_ACCESS_KEY: "<access_key>"
AWS_SECRET_KEY: "<secret_key>"
AWS_BUCKET: "<my_s3_app_bucket>"
REDIS_URL: "<redis_url>"`
Cable.yaml
production:
adapter: redis
url: <long_url_address>
host: <host_from_url>
port: <port_from_url>
password: <password_from_url>
Production.rb:
config.action_cable.allowed_request_origins = ["https://lynx-v1.herokuapp.com/"]
config.action_cable.url = "wss://lynx-v1.herokuapp.com/cable"
config.web_socket_server_url = "wss://lynx-v1.herokuapp.com/cable"
(i set both action cable and web socket just to test which worked, no matter which i go with i still get the error)
/config/initializers/redis.rb
require "redis"
uri = URI.parse(ENV["REDIS_URL"])
$redis = Redis.new(
:url => ENV["REDIS_URL"],
)
I dont know what is going on. Is it some kind of default that makes Redis look for local host 6379? I follow the steps step by step and I keep getting this error.
It started working again. There was an add-on gem in the rails app that was not fully configured. After finishing the setup for the side gem it started working again.

Rails correctly configure host/port with Cucumber/Capybara on CircleCI

I have problems figuring out the good way to set up the host/port for test on CircleCI
EDIT 2 - Requirements :
Rails app running locally on TEST_PORT (if available in ENV variable) or on default port 3000
I have session based tests, so magically switching from localhost to 127.0.0.1 will cause test failures
On my CircleCI environment I mapped host www.example.com to 127.0.0.1 and I'd like Capybara to connect to that website instead of directly localhost/127.0.0.1
On my CircleCI environment the port 80 is reserved so the rails app HAS to run on a different port (like 3042)
Some integration tests need to connect to remote website (Sorry no VCR yet) www.example-remote.com on port 80
Previously my test suite was running fine with localhost:3042 but then I realized I had problems with tests that used session : the rails app itself started on localhost but then emails were sent to the 127.0.0.1 address which caused session-based tests to fail
I changed the following config
# feature/env.rb
Capybara.server_port = ENV['TEST_PORT'] || 3042
Rails.application.routes.default_url_options[:port] = Capybara.server_port
if ENV['CIRCLECI']
Capybara.default_host = 'http://www.example.com/'
end
# configuration/test.rb
config.action_mailer.default_url_options = {
host: (ENV['CIRCLECI'].present? ? 'www.example.com' : '127.0.0.1'),
port: ENV['TEST_PORT'] || 3042
}
# circle.yml
machine:
hosts:
www.example.com: 127.0.0.1
But now I'm getting weird email urls being generated like http://www.example.com/:3042/xxx
Did someone manage a working configuration on circleCI using custom host name ?
EDIT
Capybara 2.13
Rails 5.0
Cucumber 2.4
CircleCI 1.x
Capybara.default_host only affects tests using the rack_test driver (and only if Capybara.app_host isn't set). It shouldn't have the trailing '/' on it, and it already defaults to 'http://www.example.com' so your setting of it should be unnecessary.
If what you're trying to do is make all your tests (JS and non-JS) go to 'http://www.example.com' by default then you should be able to do either
Capybara.server_host = 'www.example.com'
or
Capybara.app_host = 'http://www.example.com'
Capybara.always_include_port = true
My new config which seems to work for session-based tests but fails for remote websites (it tries to reach the remote server with the same TEST_PORT I have defined (eg click on email with http://www.example-remote.com/some_path --> Capybara connects to http://www.example-remote.com:TEST_PORT/some_path)
# features/env.rb
# If test port specified, use it
if ENV['TEST_PORT'].present?
Capybara.server_port = ENV['TEST_PORT']
elsif ActionMailer::Base.default_url_options[:port].try do |port|
Capybara.server_port = port
end
else
Rails.logger.warn 'Capybara server port could not be inferred'
end
# Note that Capybara needs either an IP or a URL with http://
# Most TEST_HOST env variable will only include domain name
def set_capybara_host
host = [
ENV['TEST_HOST'],
ActionMailer::Base.default_url_options[:host]
].detect(&:present?)
if host.present?
# If the host is an IP, Capybara.app_host = IP will crash so do nothing
return if host =~ /^[\d\.]+/
# If hostname starts with http(s)
if host =~ %r(^(?:https?\:\/\/)|(?:\d+))
# OK
elsif Capybara.server_port == 443
host = 'https://' + host
else
host = 'http://' + host
end
puts "Attempting to set Capybara host to #{host}"
Capybara.app_host = host
else
Rails.logger.warn 'Capybara server host could not be inferred'
end
end
set_capybara_host
# config/environments/test.rb
Capybara.always_include_port = true
config.action_mailer.default_url_options = {
host: (ENV['TEST_HOST'].present? ? ENV['TEST_HOST'] : '127.0.0.1'),
port: (ENV['TEST_PORT'].present? ? ENV['TEST_PORT'] : 3042)
}

Seahorse::Client::NetworkingError: Connection refused - connect(2) for "localhost" port 8000

I am building a rails application and I use dynamodb (using dynamoid) as the database. For testing, I use dynamodb-local.
When I get into the test database from command line , I get the following error.
Seahorse::Client::NetworkingError: Connection refused - connect(2) for
"localhost" port 8000
config/initializers/dynamoid.rb
AWS_CONFIG = YAML.load_file("#{Rails.root}/config/aws.yml")[Rails.env]
Dynamoid.configure do |config|
config.adapter = 'aws_sdk_v2'
config.namespace = AWS_CONFIG['namespace']
config.warn_on_scan = false # Output a warning to the logger when you perform a scan rather than a query on a table.
config.read_capacity = 5 # Read capacity for tables, setting low
config.write_capacity = 5 # Write capacity for your tables
end
if Rails.env.test? || ENV['ASSET_PRECOMPILE'].present?
p "Comes here"
Aws.config[:ssl_verify_peer] = false
Aws.config.update({
credentials: Aws::Credentials.new('xxx','xxx'),
endpoint: 'https://localhost:8000',
region: 'us-west-2'
})
Rakefile:
task :start_test_dynamo do
FileUtils.cd('rails-root') do
sh "rake dynamodb:local:test:start"
sh "rake dynamodb:seed"
end
end
Check two things
Your dynamodb-local already load ?
Your endpoint correct or not ?
change https://localhost:8000 to http://localhost:8000 (remove s)

Ruby Net::HTTP.start giving connection refused

I have a ruby class in which I am making Net::HTTP.start call inside perform method.This is the code:
class Poller
def self.perform(args)
uri = URI('http://localhost:8081/adhoc/dummy_poll?noAuth=true')
begin
Net::HTTP.start(uri.host, uri.port, :read_timeout=>30) do |http|
request = Net::HTTP::Get.new uri
#response = http.request request
#responsecode = #response.code
end
rescue Exception => e
Rails.logger.error "error mess==>"+e.message()
#responsecode = "408"
end
When I enqueue this within resque from another class using this line:
Resque.enqueue_in_with_queue(:data_workflow_manager,10,Poller,args)
I get this error:
Connection refused - connect(2) for "::1" port 8081.
However, HTTP.start works fine without any errors when the perform method is called in some another class using:
Poller.perform(args)
Please help me understand, why is this happening?
try explicitly connecting to the loop back address. There may be resolution issues with localhost.
uri = URI('http://127.0.0.1:8081/adhoc/dummy_poll?noAuth=true')
It's likely you do not have any server running on port 8081 which is why the connection will be refused.
Check it with lsof -i and look for programs bound to 8081 on a linux machine.

Resources