provides + chrome beta broken #676

Closed
rkh opened this Issue Mar 13, 2013 · 2 comments

Comments

Projects
None yet
2 participants
Owner

rkh commented Mar 13, 2013

Run chat example to reproduce.

Might be related to Accept changes.

Owner

rkh commented Mar 13, 2013

I think it's not related to Chrome.

Member

patriciomacadden commented Mar 14, 2013

I think that the problem is how HTTP_ACCEPT headers are parsed. I've modified a test case [1] and ran the tests:

  it "filters by accept header" do
    mock_app {
      get '/', :provides => :xml do
        env['HTTP_ACCEPT']
      end
      get '/foo', :provides => :html do
        env['HTTP_ACCEPT']
      end
      get '/stream', :provides => 'text/event-stream' do
        env['HTTP_ACCEPT']
      end
    }

    get '/', {}, { 'HTTP_ACCEPT' => 'application/xml' }
    assert ok?
    assert_equal 'application/xml', body
    assert_equal 'application/xml;charset=utf-8', response.headers['Content-Type']

    get '/', {}, { :accept => 'text/html' }
    assert !ok?

    get '/foo', {}, { 'HTTP_ACCEPT' => 'text/html;q=0.9' }
    assert ok?
    assert_equal 'text/html;q=0.9', body

    get '/foo', {}, { 'HTTP_ACCEPT' => '' }
    assert !ok?

    get '/stream', {}, { 'HTTP_ACCEPT' => 'text/event-stream' }
    assert ok?
    assert_equal 'text/event-stream', body

    get '/stream', {}, { 'HTTP_ACCEPT' => '' }
    assert !ok?
  end

The test fails on get '/stream', {}, { 'HTTP_ACCEPT' => 'text/event-stream' }

Then, I've looked at the provides method:

def provides(*types)
  types.map! { |t| mime_types(t) }
  types.flatten!
  condition do
    # added this for debugging purposes
    puts request.accept # <---- 'text/event'
    if type = response['Content-Type'] # <---- this is false
      types.include? type or types.include? type[/^[^;]+/]
    elsif type = request.preferred_type(types) # <---- this is false
      params = (type.respond_to?(:params) ? type.params : {})
      content_type(type, params)
      true
    else # <---- so provides defaults to 'text/html'
      false
    end
  end
end

And that's why the browser complains (HTTP_ACCEPT doesn't match the Content-Type)

I'll try to fix this. Hope I can.

[1] https://github.com/sinatra/sinatra/blob/master/test/routing_test.rb#L771

rkh closed this Mar 14, 2013

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment