Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

response.content_type should not remove charset #1371

Closed
joelpresence opened this issue May 19, 2015 · 3 comments
Closed

response.content_type should not remove charset #1371

joelpresence opened this issue May 19, 2015 · 3 comments

Comments

@joelpresence
Copy link

Hi,

When I run rails and perform a certain request, I get a response with:

Content-Type: application/json; charset=utf-8

However, when performing an Rspec controller test against that same controller, the response.content_type is just application/json which is not correct. Somehow, rspec is splitting off charset into response.charset which comes back as utf-8. This feels odd to do this since the actual header name where the charset is returned is Content-Type.

I'm not sure which code is doing this and obviously not breaking a bunch of existing tests is a concern, but this seems like a mismatch to me ...

Thoughts?

Thanks!

@JonRowe JonRowe closed this as completed May 19, 2015
@JonRowe
Copy link
Member

JonRowe commented May 19, 2015

As per rspec/rspec-core#1965 this is a Rails issue.

@joelpresence
Copy link
Author

OK, sorry, misunderstood, my fault. I'll file against rails.

@cupakromer
Copy link
Member

The response object in controller specs is an instance of ActionController::TestResponse. However, content_type is inherited from ActionDispatch::Response. Per ActionDispatch::Response#content_type this is by design:

    # Sets the HTTP response's content MIME type. For example, in the controller
    # you could write this:
    #
    #  response.content_type = "text/plain"
    #
    # If a character set has been defined for this response (see charset=) then
    # the character set information will also be included in the content type
    # information.
    attr_reader   :content_type

    # The charset of the response. HTML wants to know the encoding of the
    # content you're giving them, so we need to send that along.
    attr_accessor :charset

You are asking about the actual header value. You can get that from:

expect(response.headers).to include(
  "Content-Type" => "application/json; charset=utf-8"
)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants