Skip to content

Commit

Permalink
Setup project architecture; Added real specs
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremyvdw committed Apr 6, 2010
1 parent 78cfffa commit 6955234
Show file tree
Hide file tree
Showing 7 changed files with 19 additions and 464 deletions.
42 changes: 0 additions & 42 deletions lib/rack/locale-selector.rb.bak

This file was deleted.

118 changes: 0 additions & 118 deletions lib/rack/locale-selector/context.rb

This file was deleted.

75 changes: 0 additions & 75 deletions lib/rack/locale-selector/options.rb

This file was deleted.

23 changes: 0 additions & 23 deletions spec/spec_context.rb

This file was deleted.

35 changes: 19 additions & 16 deletions spec/spec_locale_selector.rb
Expand Up @@ -3,7 +3,10 @@
require 'rack/locale_selector'

def mock_env(http_host, path = '/', cookie = '', http_accept_language = 'fr,en-EN;q=0.8')
Rack::MockRequest.env_for(path, { 'HTTP_HOST' => http_host, 'HTTP_ACCEPT_LANGUAGE' => http_accept_language, 'HTTP_COOKIE' => cookie })
opts = {'HTTP_HOST' => http_host}
opts.merge!({'HTTP_COOKIE' => cookie}) if cookie
opts.merge!({'HTTP_ACCEPT_LANGUAGE' => http_accept_language}) if http_accept_language
Rack::MockRequest.env_for(path, opts)
end

def middleware(options = {})
Expand All @@ -18,21 +21,21 @@ def middleware(options = {})

context "Root domain host without cookie does redirect" do
before do
app = middleware
@app = middleware
end

it "should follow HTTP_ACCEPT_LANGUAGE headers" do
status, headers, body = app.call(mock_env("example.com"))
status, headers, body = @app.call(mock_env("example.com"))
status.should.equal 403
locale.should.equal 'fr'
I18n.locale.should.equal 'fr'
headers["Set-Cookie"].should.equal "language=fr"
headers["Location"].should.equal "fr.example.com"
end

it "should follow default locale if no HTTP_ACCEPT_LANGUAGE headers" do
status, headers, body = app.call(mock_env("example.com"))
status, headers, body = @app.call(mock_env('example.com', '/', '', false))
status.should.equal 403
locale.should.equal 'en'
I18n.locale.should.equal 'en'
headers["Set-Cookie"].should.equal "language=en"
headers["Location"].should.equal "en.example.com"
end
Expand All @@ -41,18 +44,18 @@ def middleware(options = {})
context "Root domain host with cookie" do
specify "redirect should follow cookie's value" do
app = middleware
status, headers, body = app.call(mock_env("example.com", '/', 'language=fr'))
status, headers, body = @app.call(mock_env("example.com", '/', 'language=fr'))
status.should.equal 403
locale.should.equal 'fr'
I18n.locale.should.equal 'fr'
headers["Set-Cookie"].should.equal "language=fr"
headers["Location"].should.equal "fr.example.com"
end

specify "should be redirected to spanish website" do
app = middleware
status, headers, body = app.call(mock_env("example.com", '/', 'language=es'))
status, headers, body = @app.call(mock_env("example.com", '/', 'language=es'))
status.should.equal 403
locale.should.equal 'es'
I18n.locale.should.equal 'es'
headers["Set-Cookie"].should.equal ""
headers["Location"].should.equal "es.example.com"
end
Expand All @@ -61,9 +64,9 @@ def middleware(options = {})
context "Home page with cookie" do
specify "should be redirected to french website with french locale (when cookie)" do
app = middleware
status, headers, body = app.call(mock_env('www.example.com', '/', 'language=fr'))
status, headers, body = @app.call(mock_env('www.example.com', '/', 'language=fr'))
status.should.equal 403
locale.should.equal 'fr'
I18n.locale.should.equal 'fr'
headers["Set-Cookie"].should.equal ""
headers["Location"].should.equal "fr.example.com"
end
Expand All @@ -72,19 +75,19 @@ def middleware(options = {})
context "Locale page without cookie" do
specify "should display the french website with french locale and set a cookie" do
app = middleware
status, headers, body = app.call(mock_env('fr.example.com'))
status, headers, body = @app.call(mock_env('fr.example.com'))
status.should.equal 200
locale.should.equal 'fr'
I18n.locale.should.equal 'fr'
headers["Set-Cookie"].should.equal "language=fr"
end
end

context "Locale page with cookie" do
specify "should display the french website with french locale" do
app = middleware
status, headers, body = app.call(mock_env('fr.example.com', '/', 'language=fr'))
status, headers, body = @app.call(mock_env('fr.example.com', '/', 'language=fr'))
status.should.equal 200
locale.should.equal 'fr'
I18n.locale.should.equal 'fr'
headers["Set-Cookie"].should.equal ''
end
end
Expand Down

0 comments on commit 6955234

Please sign in to comment.