Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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>

like image 395
B Seven Avatar asked Dec 28 '12 01:12

B Seven


1 Answers

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.

like image 50
ian Avatar answered Nov 05 '22 03:11

ian