I'm try to run Capybara server before all tests to get server port which I should include in url like 'http://subdomain.domain.au:CURRENT_PORT_NUMBER/'
so
# I create new session:
session = Capybara::Session.new(:selenium)
# then I start browser
session.driver.browser
# And try to get port number like some folks suggested
Capybara.current_session.driver.rack_server.port # => nil
# And get nil
In my opinion when browser started you should have port number somewere. Or I'm wrong?
I do not want hardcode port number for all requests.
Can anyboady suggest how can I get port to visit my application.
As far as I understand browser it's another process and can be started without local server. Thats make sence only when you query server over network. There should be mechanism to run rack server when 'selenium' test start. Isn't it?
You can use Capybara.default_port to get/set the port.
Related
I am new here and working on a Rails application that was handed to me for a research project.
I have created my own instance of the project and I am able to access the server running the instance from my computer, BUT, only if I modify my hosts file and map the IP address to an arbitrary domain name.
If I try to access the rails server via an IP address, I get the following error:
"Routing Error
No route matches [GET] "/"
Try running rake routes for more information on available routes."
So my question is, how would I configure the Rails server and application to be accessible via IP address only, for example: http://52.78.233.65:3000/, and why would the server work if I modify the hosts file but not visit the site directly?
If you need remote access, you can choose one of below methods:
Apply in each start
Start rails server by appending -b 0.0.0.0, so command to start rails server is: rails s -b 52.78.233.65 (from your sample IP)
Change default binding option
Modify config/boot.rb by adding below code and start rails server with normal command rails s
config/boot.rb
require 'rubygems'
require 'rails/commands/server'
module Rails
class Server
alias :default_options_bk :default_options
def default_options
default_options_bk.merge!(Host: '0.0.0.0')
end
end
end
I am testing a scenario and getting this error:
Net::ReadTimeout in controller#method-one. I tried three ways
Console: (works fine):
HTTParty.get("http://localhost:3000/controller/method-two?prams=abc")
Single Server running on localhost:3000: (shows error):
class Controller < ApplicationController
require 'net/http'
def method-one
abc = params[:abc]
res=HTTParty.get("http://localhost:3000/controller/method-two?prams=#{abc}")
end
end
Two Server running on different ports localhost:3001/0: (shows error):
both server call each other controller#method just to change port i.e
Server on Port 3000 call:
HTTParty.get("http://localhost:3001/controller/method-other-server?prams=#{abc}")
above Get call the server on port 3001 controller#method-other-server which again call back origin server another method to record response i.e.
HTTParty.get("http://localhost:3000/controller/method-two?prams=#{abc}")
and origin server running on port 3000 get stuck for while and produces
Net::ReadTimeout error on origin server port 3000
you might be wonder why I am using two server for one call. because I read over internet that:
Localhost server is single threaded so you can't call a localhost url inside the localhost server.
please help me I am stuck being beginner in rails.
P.S: I've tested these method independently and works fine. but problem rise when I call url from one controller method to another method through HTTParty
in ruby on rails console 'net/http' works, but in controller it doesn't and gives timeout error.
require 'net/http'
uri = URI('http://localhost:3000/api_json.json')
json = Net::HTTP.get(uri)
parsed_json = ActiveSupport::JSON.decode(json)
Most likely you're using default Webrick server, that serves one request a time. So, from console it works fine, but fails when you try to call it from controller (when the Webrick worker is already busy).
You can try to setup and run another server like unicorn or thin, or run two Webrick instances on different ports:
rails server
rails server -p 3001
and go to localhost:3001
#dimuch's solution might have solved your issue, but it might help someone facing similar situation. I will explain the issue, and the solution in detail (extension of #dimuch's solution).
Issue:
You might have a controller like some:"/test_controller/test_method", and you might want to call a method in a controller, like /api/v1/some_test_api, and facing error like Completed 500 Internal Server Error in 60004.4ms
[27580c5c46770812c550188346c2dd3e] [127.0.0.1] [/xauth_test/sanity_oauth_login]
Timeout::Error (Timeout::Error):
Solution:
As said by #dimuch, "
Most likely you're using default Webrick server, that serves one request a time.....". 1. You need to run the application on different ports, like
rails s -p 3000, and rails s -p 3001, then make the request from 3001.
If you face an issue like "A server is already running. Check /tmp/pids/server.pid. Exiting", then try running rails s -p 3001 -P PROCESS_ID.
2. Use other server's like Unicorn, or Puma.
Note: If you want it for just testing purpose in local, then I would suggest to go with the first solution, which is easy and simple. I am sorry for poor English, and I found most of solutions from other stack overflow pages, and websites, which I am attaching (links for refs) below, and sorry if I missed some one or some thing to refer. Hope this helps someone.
Refs:
For running multiple instances:
Running multiple instances of Rails Server
Similar errors and way they are handled:
Rails HTTParty Getting Timeout::Error
Faraday timeout error with omniauth (custom strategy)/doorkeeper
Strange Timeout::Error with render_to_string and HTTParty in Controller Action
Configuring Unicorn &Puma:
http://vladigleba.com/blog/2014/03/21/deploying-rails-apps-part-3-configuring-unicorn/
https://github.com/puma/puma
I've noticed that when I run tests (using Rspec and spork, if that matters), my hostname is set to www.example.com. So if I do:
visit sports_url
the test is actually going to www.example.com/sports.
This is a problem because I need to run some of the tests with selenium (to get javascript support). Also, I noticed my emails where being marked as coming from www.example.com.
What did I mess up? Why is the test environment running as example.com? Can I configure this somewhere? I would assume it should be programatic (depending on what port the test server starts up on).
You can configure the test environment domain, then set up DNS to do what you need.
How do I change the default "www.example.com" domain for testing in rails?
So I would like my Rails app instances to register themselves on a "I'm up" kind of thing I'm playing with, and I'd like it to be able to mention what local port it's running on. I can't seem to find how to do it - in fact just finding out its IP is tricky and needs a bit of a hack.
But no problem, I have the IP - but how can I find what port my mongrel/thin/webrick server is running on?
To be super explicit, if I start a rails app using script/server -p 3001, what can I do to pull that 3001 inside the app.
You can call Rails::Server.new.options[:Port] to get the port that your Rails server is running on. This will parse the -p 3001 args from your rails server command, or default to port 3000.
From inside any controller action, check the content of request.port, thus:
class SomeController < ApplicationController
def some_action
raise "I'm running on port #{request.port}."
end
end
Two ways.
If you're responding to a request in a controller or view, use the request object:
request.port
If you're in an initialiser and don't have access to the request object use the server options hash:
Rails::Server.new.options[:Port]
I played around with this a bit, and this might be the best solution for Rails 5.1:
Rails::Server::Options.new.parse!(ARGV)[:Port]
Building on the other answers (which saved my bacon!), I expanded this to give sane fallbacks:
In development:
port = Rails::Server::Options.new.parse!(ARGV)[:Port] || 3000 rescue 3000
In all other env's:
port = Rails::Server::Options.new.parse!(ARGV)[:Port] || 80 rescue 80
The rescue 80 covers you if you're running rails console. Otherwise, it raises NameError: uninitialized constant Rails::Server. (Maybe also early in initializers? I forget...)
The || 80 covers you if no -p option is given to server. Otherwise you get nil.
For Rails 5.1 development server.
if Rack::Server.new.options[:Port] != 9292 # rals s -p PORT
local_port = Rack::Server.new.options[:Port]
else
local_port = (ENV['PORT'] || '3000').to_i # ENV['PORT'] for foreman
end