HTTP Status 406 When Testing :json

Hello all,

My first question, in my controllers, do I need to test respond_to is
returning the correct data for its mime-type?

In my controller spec I have

it “creates a list of available types for that category” do
xhr :get, :index_available, :category_id => 1, :format => :json
assigns[:types].should have(1).record
end

and in my logs I see (406 Not acceptable…)

Processing TypesController#index_available to json (for 0.0.0.0 at
2011-05-02 13:20:01) [GET]
Parameters: {“category_id”=>“1”}
Completed in 1ms (View: 0, DB: 0) | 406 Not Acceptable [
http://test.host/categories/1/types/index_available.json]
SQL (0.1ms) ROLLBACK
SQL (0.0ms) BEGIN

But I get a green pass.

If I change :format => :json to ‘json’, I get a load of errors (which I
have
mentioned before on this list but haven’t had time to address the
situation)

/home/anthony/.rvm/gems/ruby-1.8.7-p302@rails2311/gems/activesupport-2.3.11/lib/active_support/json/encoders/hash.rb:37:in
each' /home/anthony/.rvm/gems/ruby-1.8.7-p302@rails2311/gems/activesupport-2.3.11/lib/active_support/json/encoders/hash.rb:37:in map’
/home/anthony/.rvm/gems/ruby-1.8.7-p302@rails2311/gems/activesupport-2.3.11/lib/active_support/json/encoders/hash.rb:37:in
to_json' /home/anthony/.rvm/gems/ruby-1.8.7-p302@rails2311/gems/activesupport-2.3.11/lib/active_support/json/encoding.rb:79:in encode’
/home/anthony/.rvm/gems/ruby-1.8.7-p302@rails2311/gems/activesupport-2.3.11/lib/active_support/json/encoding.rb:18:in
__send__' /home/anthony/.rvm/gems/ruby-1.8.7-p302@rails2311/gems/activesupport-2.3.11/lib/active_support/json/encoding.rb:18:in encode’
/home/anthony/.rvm/gems/ruby-1.8.7-p302@rails2311/gems/activesupport-2.3.11/lib/active_support/json/encoders/object.rb:4:in
to_json' /home/anthony/.rvm/gems/ruby-1.8.7-p302@rails2311/gems/activesupport-2.3.11/lib/active_support/json/encoding.rb:79:in encode’
/home/anthony/.rvm/gems/ruby-1.8.7-p302@rails2311/gems/activesupport-2.3.11/lib/active_support/json/encoding.rb:18:in
__send__' /home/anthony/.rvm/gems/ruby-1.8.7-p302@rails2311/gems/activesupport-2.3.11/lib/active_support/json/encoding.rb:18:in encode’
/home/anthony/.rvm/gems/ruby-1.8.7-p302@rails2311/gems/activesupport-2.3.11/lib/active_support/json/encoders/enumerable.rb:11:in
to_json' /home/anthony/.rvm/gems/ruby-1.8.7-p302@rails2311/gems/activesupport-2.3.11/lib/active_support/json/encoders/enumerable.rb:11:in map’
/home/anthony/.rvm/gems/ruby-1.8.7-p302@rails2311/gems/activesupport-2.3.11/lib/active_support/json/encoders/enumerable.rb:11:in
to_json' /home/anthony/Development/websites/ruby/GMFT/trunk/app/controllers/types_controller.rb:30:in index_available’
/home/anthony/.rvm/gems/ruby-1.8.7-p302@rails2311/gems/actionpack-2.3.11/lib/action_controller/mime_responds.rb:135:in
call' /home/anthony/.rvm/gems/ruby-1.8.7-p302@rails2311/gems/actionpack-2.3.11/lib/action_controller/mime_responds.rb:135:in custom’
/home/anthony/.rvm/gems/ruby-1.8.7-p302@rails2311/gems/actionpack-2.3.11/lib/action_controller/mime_responds.rb:179:in
call' /home/anthony/.rvm/gems/ruby-1.8.7-p302@rails2311/gems/actionpack-2.3.11/lib/action_controller/mime_responds.rb:179:in respond’
/home/anthony/.rvm/gems/ruby-1.8.7-p302@rails2311/gems/actionpack-2.3.11/lib/action_controller/mime_responds.rb:173:in
each' /home/anthony/.rvm/gems/ruby-1.8.7-p302@rails2311/gems/actionpack-2.3.11/lib/action_controller/mime_responds.rb:173:in respond’
/home/anthony/.rvm/gems/ruby-1.8.7-p302@rails2311/gems/actionpack-2.3.11/lib/action_controller/mime_responds.rb:107:in
respond_to' /home/anthony/Development/websites/ruby/GMFT/trunk/app/controllers/types_controller.rb:29:in index_available’
/home/anthony/.rvm/gems/ruby-1.8.7-p302@rails2311/gems/actionpack-2.3.11/lib/action_controller/base.rb:1333:in
send' /home/anthony/.rvm/gems/ruby-1.8.7-p302@rails2311/gems/actionpack-2.3.11/lib/action_controller/base.rb:1333:in perform_action_without_filters’
/home/anthony/.rvm/gems/ruby-1.8.7-p302@rails2311/gems/actionpack-2.3.11/lib/action_controller/filters.rb:617:in
call_filters' /home/anthony/.rvm/gems/ruby-1.8.7-p302@rails2311/gems/actionpack-2.3.11/lib/action_controller/filters.rb:610:in perform_action_without_benchmark’
/home/anthony/.rvm/gems/ruby-1.8.7-p302@rails2311/gems/actionpack-2.3.11/lib/action_controller/benchmarking.rb:68:in
perform_action_without_rescue' /home/anthony/.rvm/gems/ruby-1.8.7-p302@rails2311/gems/activesupport-2.3.11/lib/active_support/core_ext/benchmark.rb:17:in ms’
/home/anthony/.rvm/gems/ruby-1.8.7-p302@rails2311/gems/activesupport-2.3.11/lib/active_support/core_ext/benchmark.rb:17:in
ms' /home/anthony/.rvm/gems/ruby-1.8.7-p302@rails2311/gems/actionpack-2.3.11/lib/action_controller/benchmarking.rb:68:in perform_action_without_rescue’
/home/anthony/.rvm/gems/ruby-1.8.7-p302@rails2311/gems/actionpack-2.3.11/lib/action_controller/rescue.rb:160:in
perform_action_without_flash' /home/anthony/.rvm/gems/ruby-1.8.7-p302@rails2311/gems/actionpack-2.3.11/lib/action_controller/flash.rb:151:in perform_action’
/home/anthony/.rvm/gems/ruby-1.8.7-p302@rails2311/gems/actionpack-2.3.11/lib/action_controller/base.rb:532:in
send' /home/anthony/.rvm/gems/ruby-1.8.7-p302@rails2311/gems/actionpack-2.3.11/lib/action_controller/base.rb:532:in process_without_filters’
/home/anthony/.rvm/gems/ruby-1.8.7-p302@rails2311/gems/actionpack-2.3.11/lib/action_controller/filters.rb:606:in
process' /home/anthony/.rvm/gems/ruby-1.8.7-p302@rails2311/gems/actionpack-2.3.11/lib/action_controller/test_process.rb:567:in process_with_test’
/home/anthony/.rvm/gems/ruby-1.8.7-p302@rails2311/gems/actionpack-2.3.11/lib/action_controller/test_process.rb:447:in
process' /home/anthony/.rvm/gems/ruby-1.8.7-p302@rails2311/gems/actionpack-2.3.11/lib/action_controller/test_process.rb:398:in get’
/home/anthony/.rvm/gems/ruby-1.8.7-p302@rails2311/gems/actionpack-2.3.11/lib/action_controller/test_process.rb:453:in
__send__' /home/anthony/.rvm/gems/ruby-1.8.7-p302@rails2311/gems/actionpack-2.3.11/lib/action_controller/test_process.rb:453:in xhr’
./spec/controllers/types_controller_spec.rb:34:

So, two pronged question, do I need to test :json (seeing as I’m not
testing
the contents) and can anyone shed any light on my 406 and/or the errors
when
changing the format to ‘json’

Many thanks

-ants

Hi,

When testing Rails APIs, always assert on response codes and where
relevant, the Location, Content-Type and other headers. We wound up
doing this on every single project and so extracted it into a gem that
you might find useful: GitHub - c42/rspec-http: RSpec HTTP is an extension library that makes it easier to write specs for HTTP/REST APIs

Best,
Sidu.
http://c42.in

By the way, asserting on the response code will only help ensure that
you don’t get green specs
even though you’re request fails with a 406 (or something similar).
You’re getting a 406
because whatever the Accepts header is on that request
(‘application/json’ I’d presume) does not have a responds_to clause
that matches it. Not sure about :json vs ‘json.’

On 2 May 2011 14:35, Sidu P. [email protected] wrote:

  assigns[:types].should have(1).record

SQL (0.0ms) BEGIN
/gems/activesupport-2.3.11/lib/active_support/json/encoders/hash.rb:37:in
/home/anthony/.rvm/gems/ruby-1.8.7-p302@rails2311
encode' /gems/activesupport-2.3.11/lib/active_support/json/encoders/enumerable.rb:11:in /home/anthony/.rvm/gems/ruby-1.8.7-p302@rails2311 each’
/gems/actionpack-2.3.11/lib/action_controller/base.rb:1333:in
/home/anthony/.rvm/gems/ruby-1.8.7-p302@rails2311
`perform_action_without_rescue’
/gems/actionpack-2.3.11/lib/action_controller/base.rb:532:in
/home/anthony/.rvm/gems/ruby-1.8.7-p302@rails2311
So, two pronged question, do I need to test :json (seeing as I’m not


rspec-users mailing list
[email protected]
http://rubyforge.org/mailman/listinfo/rspec-users


rspec-users mailing list
[email protected]
http://rubyforge.org/mailman/listinfo/rspec-users

I forgot to add my controller as I render the json inline and not in a
template. I wonder if this has a bearing?!

def index_available
@types = @category.types.available

respond_to do |format|
  format.json { render :json => @types.to_json(:only => [:id, 

:name]) }
format.html
end

end

-ants