How do I & ldquo; Test & rdquo; If the response charset is utf-8 in a sinatra application using rspec?


I use

before do
  content_type "text/html", :charset => "utf-8"

in my sinatra app.

How can I check if this works in my rspec tests?
I though it should be something like:

it "should be utf-8 encoded" do
  get '/'
  last_response.body.encoding.should == 'utf-8'

But encoding does not return a string.

You have two encoding to take care:

  • the encoding of the response declared in the Content-Type header,
  • the encoding in which Ruby has stored the response body inside the last_reponse.body object.

A good Rack application should make sure that the two are kept in sync and are coherent with each other, but some middleware component or coding error could make them mismatch them. So you have to test both.

This test will make sure that the body string is encoded in 'UTF-8'.

it "should be UTF-8 encoded" do
    get '/' == 'UTF-8'

Here we are testing how the body string is encoded as Ruby object.

(Please beware that code this will work only on Ruby 1.9.x.)

Instead, if you want to test whether the server has declared an UTF-8 content type for the body, you should use

it "should have UTF-8 content type" do
    get '/'
    last_response.content_type.should =~ /UTF-8/

In this second test we check whether the server declared that it is using the UTF-8 encoding setting the Content-Type header to something that contains UTF-8.