Permalink
Browse files

Switch to RSpec

  • Loading branch information...
iain committed Jul 13, 2012
1 parent 7bc6b19 commit 0503fc8e65e4e102cb5055d908584178a899239b
View
2 .rspec
@@ -0,0 +1,2 @@
+--color
+--format progress
View
@@ -0,0 +1,8 @@
+#!/usr/bin/env ruby
+
+guard 'rspec', :cli => "-fd", :version => 2 do
+ watch(%r{^spec/.+_spec\.rb$})
+ watch(%r{^lib/(.+)\.rb$}) { "spec" }
+ watch('spec/spec_helper.rb') { "spec" }
+end
+
@@ -1,7 +1,9 @@
# -*- encoding: utf-8 -*-
+require File.expand_path('../lib/http_accept_language/version', __FILE__)
+
Gem::Specification.new do |s|
s.name = "http_accept_language"
- s.version = '1.0.2'
+ s.version = HttpAcceptLanguage::VERSION
s.authors = ["iain"]
s.email = ["iain@iain.nl"]
s.homepage = "https://github.com/iain/http_accept_language"
@@ -16,5 +18,7 @@ Gem::Specification.new do |s|
s.require_paths = ["lib"]
s.add_development_dependency 'rake'
- s.add_development_dependency 'minitest'
+ s.add_development_dependency 'rspec'
+ s.add_development_dependency 'rack-test'
+ s.add_development_dependency 'guard-rspec'
end
@@ -90,13 +90,7 @@ def language_region_compatible_from(available_languages)
lang_group.find{|l| l == x} || lang_group.first #en-US, en-UK
end.compact.first
end
-end
-if defined?(ActionPack)
- classes = if ActionPack::VERSION::MAJOR == 2
- [ActionController::Request, ActionController::CgiRequest]
- else
- [ActionDispatch::Request]
- end
- classes.each{|c| c.send :include, HttpAcceptLanguage }
end
+
+require 'http_accept_language/rails' if defined?(ActionPack)
@@ -0,0 +1,30 @@
+require 'http_accept_language'
+
+module HttpAcceptLanguage
+ class Rack
+
+ def initialize(app)
+ @app = app
+ end
+
+ def call(env)
+ def env.http_accept_language
+ @http_accept_language ||= HttpAcceptLanguage.new(self)
+ end
+ @app.call(env)
+ end
+
+ class HttpAcceptLanguage
+ include ::HttpAcceptLanguage
+
+ attr_reader :env
+
+ def initialize(env)
+ @env = env
+ end
+
+ end
+
+ end
+end
+
@@ -0,0 +1,7 @@
+classes = if ActionPack::VERSION::MAJOR == 2
+ [ActionController::Request, ActionController::CgiRequest]
+ else
+ [ActionDispatch::Request]
+ end
+
+classes.each { |c| c.send :include, HttpAcceptLanguage }
@@ -0,0 +1,3 @@
+module HttpAcceptLanguage
+ VERSION = '2.0.0.pre'
+end
@@ -0,0 +1,58 @@
+require 'http_accept_language'
+
+class MockedCgiRequest
+ include HttpAcceptLanguage
+ def env
+ @env ||= {'HTTP_ACCEPT_LANGUAGE' => 'en-us,en-gb;q=0.8,en;q=0.6,es-419'}
+ end
+end
+
+describe HttpAcceptLanguage do
+
+ it "should return empty array" do
+ request.env['HTTP_ACCEPT_LANGUAGE'] = nil
+ request.user_preferred_languages.should eq []
+ end
+
+ it "should properly split" do
+ request.user_preferred_languages.should eq %w{en-US es-419 en-GB en}
+ end
+
+ it "should ignore jambled header" do
+ request.env['HTTP_ACCEPT_LANGUAGE'] = 'odkhjf89fioma098jq .,.,'
+ request.user_preferred_languages.should eq []
+ end
+
+ it "should find first available language" do
+ request.preferred_language_from(%w{en en-GB}).should eq "en-GB"
+ end
+
+ it "should find first compatible language" do
+ request.compatible_language_from(%w{en-hk}).should eq "en-hk"
+ request.compatible_language_from(%w{en}).should eq "en"
+ end
+
+ it "should find first compatible from user preferred" do
+ request.env['HTTP_ACCEPT_LANGUAGE'] = 'en-us,de-de'
+ request.compatible_language_from(%w{de en}).should eq 'en'
+ end
+
+ it "should accept symbols as available languages" do
+ request.env['HTTP_ACCEPT_LANGUAGE'] = 'en-us'
+ request.compatible_language_from([:"en-HK"]).should eq :"en-HK"
+ end
+
+ it "should sanitize available language names" do
+ request.sanitize_available_locales(%w{en_UK-x3 en-US-x1 ja_JP-x2 pt-BR-x5}).should eq ["en-UK", "en-US", "ja-JP", "pt-BR"]
+ end
+
+ it "should find most compatible language from user preferred" do
+ request.env['HTTP_ACCEPT_LANGUAGE'] = 'ja,en-gb,en-us,fr-fr'
+ request.language_region_compatible_from(%w{en-UK en-US ja-JP}).should eq "ja-JP"
+ end
+
+ def request
+ @request ||= MockedCgiRequest.new
+ end
+
+end
View
@@ -0,0 +1,49 @@
+require 'http_accept_language/rack'
+require 'rack/test'
+require 'json'
+
+class TestRackApp
+
+ def call(env)
+ request = Rack::Request.new(env)
+ http_accept_language = env.http_accept_language
+ result = {
+ :user_preferred_languages => http_accept_language.user_preferred_languages,
+ }
+ if request.params['preferred']
+ result[:preferred_language_from] = http_accept_language.preferred_language_from(request.params['preferred'])
+ end
+ [ 200, {}, [ JSON.generate(result) ]]
+ end
+
+end
+
+describe "Rack integration" do
+ include Rack::Test::Methods
+
+ def app
+ Rack::Builder.new do
+ use HttpAcceptLanguage::Rack
+ run TestRackApp.new
+ end.to_app
+ end
+
+ it "decodes the HTTP_ACCEPT_LANGUAGE header" do
+ request_with_header 'en-us,en-gb;q=0.8,en;q=0.6,es-419'
+ r['user_preferred_languages'].should eq %w{en-US es-419 en-GB en}
+ end
+
+ it "finds the first available language" do
+ request_with_header 'en-us,en-gb;q=0.8,en;q=0.6,es-419', :preferred => %w(en en-GB)
+ r['preferred_language_from'].should eq 'en-GB'
+ end
+
+ def request_with_header(header, params = {})
+ get "/", params, 'HTTP_ACCEPT_LANGUAGE' => header
+ end
+
+ def r
+ JSON.parse(last_response.body)
+ end
+
+end
View
@@ -0,0 +1,48 @@
+require 'http_accept_language'
+
+module ActionPack
+ module VERSION
+ MAJOR = 3
+ end
+end
+
+module ActionDispatch
+ class Request
+ end
+end
+
+module ActionController
+ class CgiRequest
+ end
+ class Request
+ end
+end
+
+describe "Rails integration" do
+
+ it "should be included into actionpack v2" do
+ silence_warnings do
+ ActionPack::VERSION.const_set(:MAJOR, 2)
+ end
+ load "http_accept_language/rails.rb"
+
+ ActionController::Request.ancestors.should include HttpAcceptLanguage
+ ActionController::CgiRequest.ancestors.should include HttpAcceptLanguage
+ end
+
+ it "should be included into actionpack v3" do
+ silence_warnings do
+ ActionPack::VERSION.const_set(:MAJOR, 3)
+ end
+ load "http_accept_language/rails.rb"
+ ActionDispatch::Request.ancestors.should include HttpAcceptLanguage
+ end
+
+ def silence_warnings
+ old_verbose, $VERBOSE = $VERBOSE, nil
+ yield
+ ensure
+ $VERBOSE = old_verbose
+ end
+
+end
@@ -1,103 +0,0 @@
-$:.unshift(File.dirname(__FILE__) + '/../lib')
-require 'http_accept_language'
-require 'minitest/autorun'
-
-module ActionPack
- module VERSION
- MAJOR = 3
- end
-end
-
-module ActionDispatch
- class Request
- end
-end
-
-module ActionController
- class CgiRequest
- end
- class Request
- end
-end
-
-class MockedCgiRequest
- include HttpAcceptLanguage
- def env
- @env ||= {'HTTP_ACCEPT_LANGUAGE' => 'en-us,en-gb;q=0.8,en;q=0.6,es-419'}
- end
-end
-
-class HttpAcceptLanguageTest < MiniTest::Unit::TestCase
- def test_should_return_empty_array
- request.env['HTTP_ACCEPT_LANGUAGE'] = nil
- assert_equal [], request.user_preferred_languages
- end
-
- def test_should_properly_split
- assert_equal %w{en-US es-419 en-GB en}, request.user_preferred_languages
- end
-
- def test_should_ignore_jambled_header
- request.env['HTTP_ACCEPT_LANGUAGE'] = 'odkhjf89fioma098jq .,.,'
- assert_equal [], request.user_preferred_languages
- end
-
- def test_should_find_first_available_language
- assert_equal 'en-GB', request.preferred_language_from(%w{en en-GB})
- end
-
- def test_should_find_first_compatible_language
- assert_equal 'en-hk', request.compatible_language_from(%w{en-hk})
- assert_equal 'en', request.compatible_language_from(%w{en})
- end
-
- def test_should_find_first_compatible_from_user_preferred
- request.env['HTTP_ACCEPT_LANGUAGE'] = 'en-us,de-de'
- assert_equal 'en', request.compatible_language_from(%w{de en})
- end
-
- def test_should_accept_symbols_as_available_languages
- request.env['HTTP_ACCEPT_LANGUAGE'] = 'en-us'
- assert_equal :"en-HK", request.compatible_language_from([:"en-HK"])
- end
-
- def test_should_sanitize_available_language_names
- assert_equal ["en-UK", "en-US", "ja-JP", "pt-BR"], request.sanitize_available_locales(%w{en_UK-x3 en-US-x1 ja_JP-x2 pt-BR-x5})
- end
-
- def test_should_find_most_compatible_language_from_user_preferred
- request.env['HTTP_ACCEPT_LANGUAGE'] = 'ja,en-gb,en-us,fr-fr'
- assert_equal "ja-JP", request.language_region_compatible_from(%w{en-UK en-US ja-JP})
- end
-
- def test_should_be_included_into_actionpack_v2
- silence_warnings do
- ActionPack::VERSION.const_set(:MAJOR, 2)
- end
- load "http_accept_language.rb"
-
- assert_includes ActionController::Request.ancestors, HttpAcceptLanguage
- assert_includes ActionController::CgiRequest.ancestors, HttpAcceptLanguage
- end
-
- def test_should_be_included_into_actionpack_v3
- silence_warnings do
- ActionPack::VERSION.const_set(:MAJOR, 3)
- end
- load "http_accept_language.rb"
- assert_includes ActionDispatch::Request.ancestors, HttpAcceptLanguage
- end
-
- private
-
- def silence_warnings
- old_verbose, $VERBOSE = $VERBOSE, nil
- yield
- ensure
- $VERBOSE = old_verbose
- end
-
- def request
- @request ||= MockedCgiRequest.new
- end
-end

1 comment on commit 0503fc8

Collaborator

grosser commented on 0503fc8 Jul 13, 2012

👍

Please sign in to comment.