URI::InvalidURIError: bad URI(is not URI?) testing Rails controllers
Controller tests inherit from ActionController::TestCase
, while your test
inherits from ActionDispatch::IntegrationTest
. So you're using an integration test and not a controller test.
The error is:
http://www.example.com:80index
That doesn't look right, does it? ;-)
The solution is to use a full path:
get '/index'
Remember, integration tests aren't really tied to any specific controller (or anything else, for that matter). They test the integration of several components in your application. So if you're testing the index
action of a UserController
you'd probably need to use /users/index
.
If you intended to make a controller test and not an integration test, you want to set the correct superclass. Using get :index
(for the index method) should work fine then.
Fixing RSPEC URI::InvalidURIError: bad URI(is not URI?): error for post request test
You are making the request in your RSpec the wrong way:
context 'when the url is valid' do
# Change this line below
before { post :create, url: "www.google.com" }
it 'returns a status code of 201' do
expect(response).to have_http_status(201)
end
end
URI::InvalidURIError when performing Feature Tests in RSpec Capybara
After receiving external support, I realised this was an issue with legacy code and the https://github.com/biola/rack-cas (rack-cas gem), and needed this addition into the config/application.rb
config.rack_cas.exclude_paths = "/"
Apparently the site was trying to perform CAS authentication due to existing legacy code which is similar to the responses provided by @ThomasWalpole and @smathy as they mentioned that the request was unauthenticated.
bad URI(is not URI?) while testing | Rspec
Your edit action is wrong:
def edit
@user = User.find(params[:id])
render "edit", status: 302
end
A 302 status means that the response is a redirect and that the Location
header in the response contains the URI (relative or absolute) to redirect to. Capybara will be looking for this header and probably winds up trying to do URI.parse(nil)
.
It's not clear to me why you are setting the status here at all
Heroku Deploy: URI::InvalidURIError: bad URI(is not URI?): ://user:pass@127.0.0.1/dbname
Needed to fully remove use of ActiveRecord from the project. As Max commented, in a new app this can be done by doing rails new app_name --skip-active-record --api
, to do this for an existing project see this explanation
How to fix bad URI(is not URI?) in ruby on rails :URI::InvalidURIError
When you use HTTParty.get you pass the city and state that are "dynamic" values, as in this case city is "new york", the url is being created with a whitespace, and that's why you get the error:
URI::InvalidURIError (bad URI(is not URI?):
http://api.wunderground.com/api/apikey/forecast10day/q/new
york/NY.json)
You could use URI.encode(url)
and this way, the whitespace would be "converted" to %20
, like:
http://api.wunderground.com/api/apikey/forecast10day/q/new%20york/NY.json
Which is a "valid" url to work with. So you can tweak a little bit your fetch_week_forecast
method, like:
def fetch_week_forecast(city, state)
HTTParty.get(URI.encode("http://api.wunderground.com/api/apikey/forecast10day/q/#{city}/#{state}.json"))
end
Ruby URI::InvalidURIError: bad URI(is not URI?) besides encoding
I think you meant to require uri, not open-uri. Also, according to the documentation you can specify a second parameter with extra characters to encode. Try passing []
there.
The code for this solution would be:
safeurl = URI.parse(URI.encode(url, "[]"))
Related Topics
Update Field Through Link_To in Rails
Can't Dup Nilclass on Association Methods
How to Read a Clients Windows Login Name Using Ruby on Rails
Uploading New Products with Multiple Variant Options
Bootstrap Modal in Ruby on Rails Not Working
Ruby, Rails Gem Install Error - Error: While Executing Gem ... (Encoding::Undefinedconversionerror)
Idiomatically Mock Openuri.Open_Uri with Minitest
How to Upgrade Rvm When the Official Way Doesn't Work
Splitting a String into Words and Punctuation with Ruby
Return Values Were Not Extracted in Ruby
Ruby: Length of a Line of a File in Bytes
Use [].Replace to Make a Copy of an Array
"Bad Ecpoint" Ssl Error on Fresh Rvm Ruby 1.9.3 Install on Osx Mountain Lion
How to Print All the Staged File Names Using Ruby Git Pre-Commit Hook