Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Don't crash when used while middleware was not applied.

Middleware are not always applied when running specs.
  • Loading branch information...
commit fa57cfbf8203964d01cdb1866f63a7c6bb6356bc 1 parent 7738e45
Douwe Maan authored
Showing with 3 additions and 1 deletion.
  1. +3 −1 lib/http_accept_language/railtie.rb
4 lib/http_accept_language/railtie.rb
View
@@ -12,9 +12,11 @@ class Railtie < ::Rails::Railtie
end
module EasyAccess
+
def http_accept_language
- env.http_accept_language
+ @http_accept_language ||= env.respond_to?(:http_accept_language) ? env.http_accept_language : Parser.new("")
end
+
end
end

7 comments on commit fa57cfb

Martin Carel

From what I understand, if @http_accept_language is nil, it will not parse the HTTP_ACCEPT_LANGUAGE header if the :http_accept_language method is not defined on env.

In that case, how is it possible to test it (with a controller spec)?

Douwe Maan
Owner

The #http_accept_language method is defined on env only when we're dealing with an actual request that goes through this gem's middleware which isn't the case for test requests. So when testing your controllers, the HTTP_ACCEPT_LANGUAGE wouldn't be taken into account anyway. If you want to test your controller's behavior depending on the accept language, you can just stub #http_accept_language on your controller, which is the method defined in the EasyAccess module.

Martin Carel

Some people succeeded to test it using request.env['HTTP_ACCEPT_LANGUAGE'] = 'fr'. But it didn't work for me.

Inspired by your comment, this worked for me:

describe ApplicationController do

  context "when the HTTP_ACCEPT_LANGUAGE header is set to 'fr'" do
    it "sets the locale to 'fr'" do
      set_http_accept_language_header 'fr-CA'
      @controller.send :set_locale
      expect(I18n.locale).to eq(:fr)
    end
  end

  context "when the HTTP_ACCEPT_LANGUAGE header is all messed-up" do
    it "sets the locale to 'en'" do
      set_http_accept_language_header 'abcdef'
      @controller.send :set_locale
      expect(I18n.locale).to eq(:en)
    end
  end

  def set_http_accept_language_header value
    @controller.class_eval do
      define_method :http_accept_language do
        HttpAcceptLanguage::Parser.new(value)
      end
    end
  end
end
Douwe Maan
Owner

Why not just stub it? controller.stub(:http_accept_language).and_return(HttpAcceptLanguage::Parser.new('fr-CA')

Martin Carel

That gives us a terser version, indeed:

def set_http_accept_language_header value
  @controller.stub(:http_accept_language).and_return(HttpAcceptLanguage::Parser.new(value))
end

Thanks for your help!

Douwe Maan
Owner

No problem. And you're a fan of ninja-editing your own comments, aren't you? :)

Martin Carel

I'd say a fan of keeping things clean and readable for posterity :)

Please sign in to comment.
Something went wrong with that request. Please try again.