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

Merged
merged 1 commit into from Sep 6, 2013

Projects

None yet
@DouweM
Collaborator
DouweM commented Sep 7, 2012

Middleware are not always applied when running specs.

@DouweM DouweM Don't crash when used while middleware was not applied.
Middleware are not always applied when running specs.
fa57cfb
zzet commented Oct 7, 2012

+1

clee704 commented Oct 16, 2012

It worked for me.

Just tested this, works fine.
It corrects the error from Issue #22.

+1

slbug commented Dec 22, 2012

tested. works only sometimes. first time after rails server starts it works well, but net request just fails with undefined method http_accept_language for SomeController

@inkstak inkstak referenced this pull request Jan 11, 2013
Closed

does not work with RSpec #20

lacco commented Jan 16, 2013

Works for me, please merge!

+1 Please merge

+1 a merge would be great, tried the branch, works fine
had do use

env.http_accept_language.compatible_language_from(I18n.available_locales)
thefron commented May 21, 2013

+1

gem 'http_accept_language', github: 'DouweM/http_accept_language', branch: 'no-middleware-no-crash' did the trick. I really wished this would be merged into the oficial gem.

Collaborator
DouweM commented Jul 1, 2013

@AlexanderEkdahl Yeah, it's kind of rediculous that this should take so long. I also pinged @iain on twitter a couple of months ago but got no response.

My fork has this patch applied in master. Feel free to use it, while we wait for @iain.

Collaborator
DouweM commented Jul 2, 2013

Where's the point in that? Why not just use the original fork by the patch's author, i.e. mine?

@cawel
cawel commented on fa57cfb Jul 18, 2013

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)?

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.

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
Owner

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

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!

Owner

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

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

@DouweM DouweM merged commit d501535 into iain:master Sep 6, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment