Simulating browser language

148 views
Skip to first unread message

Jaime Iniesta

unread,
Nov 26, 2010, 11:03:53 AM11/26/10
to ste...@googlegroups.com
Let's say you want to have your web app detect the browser's preferred language (say, HTTP_ACCEPT_LANGUAGE) to show the user a notice asking if she'd prefer to see the localized version. Just like Github.com does, where a bar uppears saying "Would you rather see this site in Spanish (¿Prefieres ver este sitio en español?) YES (Sí) / NO (No)".

How would you spec this using Steak?

I've asked at the Capybara list but received no answer yet. Maybe this can't be done with Capybara? Should I only spec it at the controller's level, not on an integration spec?

#########
Is there any way we can set the HTTP_ACCEPT_LANGUAGE headers with
Capybara?

I've been trying directly through the driver (rack-test) but it
doesn't seem to get it:

   page.driver.request.env["HTTP_ACCEPT_LANGUAGE"] = "es"
   visit "/"
   save_and_open_page

Then on the page if I print request.env["HTTP_ACCEPT_LANGUAGE"], it's
empty.
#########

Thanks!
Jaime

Raul Murciano

unread,
Nov 27, 2010, 6:41:29 AM11/27/10
to ste...@googlegroups.com
Hi Jaime!

I tried to add this feature with a patch largely based in another
Aslak's gist which adds the set_headers feature for cucumber

https://gist.github.com/717819

(I don't like the 'page.set_headers' syntax in the test because
conceptually we're altering the request headers which have nothing to
do with the page, but you can see the point) :)

Aslak's approach dynamically alters the current driver and I'm
directly extending RackTest so the env manipulation may potentially
break some RackTest functionalities, please drop me a line in that
case so we can hack something better ;)

Have a nice weekend!

--
Raul Murciano
Freelance Web Developer - http://raul.murciano.net
Ruby/Rails Trainer & Consultant - http://prorubyteam.com

Jaime Iniesta

unread,
Dec 2, 2010, 1:44:44 PM12/2/10
to ste...@googlegroups.com
Hey Raúl!

Thanks for the patch. I've tried and it works quite well. It really sets your headers and in this way I've been able to spec this behaviour. Yay!

But, it's kinda dangerous and breaks some other things. Like, the rest of your specs :P

I've found that you need to do a call to page.set_headers({}) on every other scenario to keep things working, and even in this case, there were problems in some cases.

I'll have a look for a better solution when I have some time to do it. In the meantime I'm happy with having a partial solution, now I'm archiving this spec for later review.

Thanks!

2010/11/27 Raul Murciano <ra...@murciano.net>
Reply all
Reply to author
Forward
0 new messages