Skip to content

Commit

Permalink
spec CountriesController#get_code_for_current_ip and #get_code_for
Browse files Browse the repository at this point in the history
  • Loading branch information
jhass committed Aug 11, 2011
1 parent 52df8cb commit 196d25c
Show file tree
Hide file tree
Showing 2 changed files with 117 additions and 0 deletions.
111 changes: 111 additions & 0 deletions spec/controllers/countries_controller_spec.rb
Expand Up @@ -197,4 +197,115 @@
end
end
end

describe '#get_code_for_current_ip' do
before do
Location.stub!(:from_host).and_return(location)
end

it 'responds to json' do
controller.request.stub!(:remote_ip).and_return('127.0.0.2')

get :get_code_for_current_ip, :format => :json

response.should be_success
end

it 'does not respond to a normal request' do
controller.request.stub!(:remote_ip).and_return('127.0.0.2')

get :get_code_for_current_ip

response.should_not be_success
end

it 'returns the correct location' do
controller.request.stub!(:remote_ip).and_return('127.0.0.2')

get :get_code_for_current_ip, :format => :json

response.body.should == location.to_json
end

it 'returns 404 if the location cannnot be determined' do
controller.request.stub!(:remote_ip).and_return('127.0.0.2')
Location.stub!(:from_host).and_return(nil)

get :get_code_for_current_ip, :format => :json

response.status.should == 404
end

it 'uses the HTTP_X_FORWARDED_FOR header if it is present and the remote ip is 127.0.0.1' do
controller.request.stub!(:remote_ip).and_return('127.0.0.1')
controller.request.stub!(:env).and_return({ 'HTTP_X_FORWARDED_FOR' => '127.0.0.2' })

Location.should_receive(:from_host).with('127.0.0.2')

get :get_code_for_current_ip, :format => :json
end
end

describe '#get_code_for' do
before do
Location.stub!(:from_host).and_return(location)
end

it 'fails on an invalid request' do
get :get_code_for, :host => ' ', :format => :json

response.should_not be_success
end


it 'responds to json' do
get :get_code_for, :host => '127.0.0.2', :format => :json

response.should be_success
end

it 'does not respond to a normal request' do
get :get_code_for, :host => '127.0.0.2'

response.should_not be_success
end

it 'parses the host parameter' do
URI.should_receive(:parse).with("http://example.org").and_return(URIFake.new("example.org"))

get :get_code_for, :host => "http://example.org", :format => :json
end

it 'should call Location.from_host with the host' do
Location.should_receive(:from_host).with("example.org").and_return(location)

get :get_code_for, :host => "http://example.org", :format => :json
end

it 'expands :/ to :// in the host parameter' do
URI.should_receive(:parse).with("http://example.org").and_return(URIFake.new("example.org"))

get :get_code_for, :host => "http:/example.org", :format => :json
end

it 'does not expand :// to :/// in the host parameter' do
URI.should_receive(:parse).with("http://example.org").and_return(URIFake.new("example.org"))

get :get_code_for, :host => "http://example.org", :format => :json
end

it 'returns the correct location' do
get :get_code_for, :host => "127.0.0.2", :format => :json

response.body.should == location.to_json
end

it 'returns a 404 if the location cannot be determined' do
Location.stub!(:from_host).and_return(nil)

get :get_code_for, :host => "127.0.0.1", :format => :json

response.status.should == 404
end
end
end
6 changes: 6 additions & 0 deletions spec/support/uri_fake.rb
@@ -0,0 +1,6 @@
class URIFake
attr_accessor :host
def initialize(host)
@host = host
end
end

0 comments on commit 196d25c

Please sign in to comment.