I use
before do
content_type "text/html", :charset => "utf-8"
end
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'
end
But encoding does not return a string.
You have two encoding to take care:
Content-Typeheader,last_reponse.bodyobject.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’.
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
In this second test we check whether the server declared that it is using the UTF-8 encoding setting the
Content-Typeheader to something that containsUTF-8.