How to set the env['SERVER_NAME'] in rack/test? - ruby

In Sinatra tests, env['SERVER_NAME'] defaults to www.example.com. How can I set this to some arbitrary domain?
Capybara has .default_host method, but not using Capybara.
Or, is it possible to change the env[DEFAULT_HOST]?
Using RSpec, Sinatra, WebMock.
EDIT: Adding env['SERVER_NAME'] = 'www.foo.com' to RSpec test raises exception:
NameError: undefined local variable or method 'env' for #<RSpec::Core::ExampleGroup::Nested_1::Nested_1:0x007fe6ce3b5ff8>

The env helper is only accessible within a Sinatra app.
One way to change it is when making a request:
get "/blah", {}, {'HTTP_SERVER_NAME' => 'www.foo.com' }
The 3rd argument of a rack/test get or post is the headers hash.

Related

undefined method `file' for #<Parse::Client:0x000000065b5738>

I am using https://github.com/adelevie/parse-ruby-client#files ruby gem for parse cloud platform.
I am trying to upload images on parse with their documented method as below:
photo = client.file({
:body => IO.read("test/parsers.jpg"),
:local_filename => "parsers.jpg",
:content_type => "image/jpeg"
})
When I run this in console, getting the error below.
undefined method `file' for #
You will need to initialize your client first:
require 'parse-ruby-client'
client = Parse.create :application_id => '<your_app_id>',
:api_key => '<your_api_key>',
:quiet => true | false
https://github.com/adelevie/parse-ruby-client#client-initialization
Since you're getting an error while trying to call a file method on whatever is stored in your client variable, it appears that client isn't holding what you expect it to hold — i.e. it doesn't seem to actually hold an object that responds to the :file message.
While it's not clear from your code snippet what the context of your code is, my guess is that you never set it up to hold a client object.
Check out the section towards the top of that gem's README for information about initializing the client — that should help you get on your way. Once you've setup your client and stored it in a the client variable, you'll be able to call all the methods that the Gem creates for instances of the Client class.

Ruby no method error with HTTParty

I am trying to make a post request in Ruby and I am getting inconsistent behaviour. I am currently using ruby-2.0.0-p598 on OSX.
When using PRY and I type the following post command:
HTTParty.post(#base_uri + '/method/?argument1&api_key=' + #api_key)
I get a successful respond from the API. However when I run it through my specs or inside the class I get:
undefined method `+' for nil:NilClass
I know it has to do with the plus sign, but I find it weird that I am getting a different behaviour. Can you please suggest what is the correct way of doing this?
Thanks in advance.
Good day
Behavior correct - some variable = nil.
You have check variables, or (in this case it is better not to do) call to_s:
HTTParty.post(#base_uri.to_s + '/method/?argument1&api_key=' + #api_key.to_s)
It looks like #base_uri and/or #api_key is null. Please double check if they are initialized with valid strings or not. Then try
HTTParty.post("#{#base_uri}/method/?argument1&api_key=#{#api_key}")
In this case, ruby will automatically try to convert #base_uri and #api_key to string so no need to call to_s method explicitly.

Ruby load module in test

I am running a padrino application and have started with the included mailers. I want to test that a mail is sent and had previously had no trouble accessing the Mail::TestMailer object to look at the mails delivered during the test.
That is the background about what I am doing but not precisely the question. I want to know how can a module become available to the runtime environment.
I have this test in two versions
first
def test_mailer
Mail::TestMailer.deliveries.clear
get '/owners/test'
e = Mail::TestMailer.deliveries.pop
puts e.to.to_s
end
second
def test_mailer
get '/owners/test'
Mail::TestMailer.deliveries.clear
e = Mail::TestMailer.deliveries.pop
puts e.to.to_s
end
In the second version this test fails with the error message NoMethodError: undefined method to' for nil:NilClass This makes sense to me. I clear the messages then ask for the last one which should be nil. However when I run the test on the first version the error is NameError: uninitialized constant OwnersControllerTest::Mail
So somehow the get method is causing the Mail object/module to be made available. I don't understand how it can do this. I don't know if this is a rack-test or padrino thing so am unsure what extra information to copy in here.
Add require 'mail' to your test helper.
The issue is explained here: https://github.com/padrino/padrino-framework/issues/1797

Displaying url parameter in Ruby

I am building a VERY simple ruby test application, just to see how it works, but I'm already stuck in overly complex tutorials.
Say that my ruby app is running at heroku at : http://example.herokuapp.com
And that I am calling it like this: http://example.herokuapp.com/test=3 or perhaps http://example.herokuapp.com/page.rb?test=3 ?
How do I get the value from "test" in my ruby output?
My Heroku demo code:
require 'sinatra'
get '/' do
"The value of test is..."
end
In Sinatra the route patterns may consist of named parameters. These parameters can be accessed using the params hash.You need to do something like this:
get '/:test' do
"The value of test is...#{params[:test]}"
end
Url:
http://example.herokuapp.com/3

Rails3: Functional tests fail with NoMethodError: undefined method `user' for nil:NilClass

I'm using Devise (v2.1.2) with Omniauth for user verification. I'm working on a functional test for a controller that takes a JSON object as the POST body and thus using the technique from this question to set the raw POST body. This works fine for development, but when I run tests I get an exception on a method that's completely unauthenticated:
NoMethodError: undefined method `user' for nil:NilClass
Example test:
test "should be able to create an item" do
m = FactoryGirl.attributes_for(:item)
raw_post :create, {}, m.to_json
assert_response :success
end
None of my models have a user method, and nothing in this controller uses authentication, so I was pretty confused. A full stack trace shows that the error comes from the first line of this function in Devise:
def sign_out_all_scopes(lock=true)
users = Devise.mappings.keys.map { |s| warden.user(:scope => s, :run_callbacks => false) }
warden.raw_session.inspect
warden.logout
expire_devise_cached_variables!
warden.clear_strategies_cache!
warden.lock! if lock
users.any?
end
So it looks like in my functional tests (and only in my functional tests) the warden object is nil.
Why is this function being called on an unauthenticated request?
Why doesn't the warden object exist here?
What can I do to fix it?
No idea, Devise is doing its own thing.
See 1.
Include Devise::TestHelpers.
The Devise documentation says that you need to include the helpers in order to use them. It does not say that if you don't include the helpers your functional tests will fail, including those that don't use any authentication, but that's what happens.
(Note the JSON handling here, which I originally thought was the problem, ended up being just a red herring. Even with standard post or get you will have this problem.)

Resources