functional testing of JSON api (Rspec or TestCase)

1,210 views
Skip to first unread message

Lille

unread,
Nov 5, 2011, 7:19:49 PM11/5/11
to Service Oriented Design With Ruby
Hi,

(Sorry to seek to wake the members of this long-sleeping list, but it
strikes me as a better forum for Ruby Web Services posting...)

I'm trying to do functional testing for a JSON API, using RSpec or
TestCase (shouldn't matter which I think), but I am failing at the
first step!

Before I turn in frustration to some other means of testing the API,
e.g., Cucumber, can folks please consider the following and comment
from their experience?

I now have the following error from the (Rails 3.1) controller...

JSON::ParserError: unexpected token at 'loan_request=4000000'

...where the controller code line in question is...

data = ActiveSupport::JSON.decode(request.body.read)

...and the ActionController::TestCase test initiation of the error
was...

request = 4_000_000
post :loan, {:loan_request=>request, :format => :json}

...finally the test log of the request is...

Processing by Sec223fAcquisitionController#loan as JSON
Parameters: {"loan_request"=>"4000000"}
Completed 500 Internal Server Error in 28ms

Can someone please offer thoughts, or, perhaps better, an example of
the
same kind of attempt, successfully made using either Rais TestCase or
RSpec?

(What especially bugs me is the format of the log -- I'm expecting to
see something in JSON format!)

Thanks,

Grar

Sidu Ponnappa

unread,
Nov 6, 2011, 2:47:47 AM11/6/11
to service-oriented...@googlegroups.com

I've had a fairly good experience simply by using rspec controller tests and a json parser.

For stateless apis this is usually sufficient.

You could also take a look at http://github.com/c42/rspec-http to help test the rest of your response.

Thanks,
Sidu Ponnappa.
http://c42.in
http://rubymonk.com

--
You received this message because you are subscribed to the Google Groups "Service Oriented Design With Ruby" group.
To post to this group, send email to service-oriented...@googlegroups.com.
To unsubscribe from this group, send email to service-oriented-desig...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/service-oriented-design-with-ruby?hl=en.

G-man

unread,
Nov 6, 2011, 10:14:05 AM11/6/11
to Service Oriented Design With Ruby
Sidu,

Thanks for confirming the utility of functional testing (a la RSpec)
for JSON APIs, and I shall explore rspec-http.

Unfortunately, I'm still stuck on the problem I outlined above. If you
or anyone would be so kind as to include a snippet of something
similar so I can determine my obstacle(s), it would be greatly
appreciated.

G

On Nov 6, 3:47 am, Sidu Ponnappa <ckponna...@gmail.com> wrote:
> I've had a fairly good experience simply by using rspec controller tests
> and a json parser.
>
> For stateless apis this is usually sufficient.
>
> You could also take a look athttp://github.com/c42/rspec-httpto help test
> the rest of your response.
>
> Thanks,
> Sidu Ponnappa.http://c42.inhttp://rubymonk.com

Sidu Ponnappa

unread,
Nov 6, 2011, 12:56:57 PM11/6/11
to service-oriented...@googlegroups.com
Take a look at https://github.com/c42/goldberg/tree/master/spec/controllers

If you're getting a 500, it's no fault of your tests - if you can
throw in the stack trace that accompanies that 500 is your test.log, I
could help you further.

Best,

G-man

unread,
Nov 6, 2011, 4:12:31 PM11/6/11
to Service Oriented Design With Ruby
Sidu,

Below is the backtrace...

MultiJson::DecodeError:
unexpected token at 'loan_request=4000000'
# json/ext/Parser.java:249:in `parse'
# /Users/gallagher/.rvm/gems/jruby-1.6.3@rails310/gems/
json-1.6.0.1-java/lib/json/common.rb:148:in `parse'
# /Users/gallagher/.rvm/gems/jruby-1.6.3@rails310/gems/
multi_json-1.0.3/lib/multi_json/engines/json_gem.rb:13:in `decode'
# /Users/gallagher/.rvm/gems/jruby-1.6.3@rails310/gems/
multi_json-1.0.3/lib/multi_json.rb:65:in `decode'
# /Users/gallagher/.rvm/gems/jruby-1.6.3@rails310/gems/
activesupport-3.1.0/lib/active_support/json/decoding.rb:12:in `decode'
# ./app/controllers/sec223f_acquisition_controller.rb:13:in
`loan'
# org/jruby/RubyKernel.java:2092:in `send'
# /Users/gallagher/.rvm/gems/jruby-1.6.3@rails310/gems/
actionpack-3.1.0/lib/action_controller/metal/implicit_render.rb:4:in
`send_action'
# /Users/gallagher/.rvm/gems/jruby-1.6.3@rails310/gems/
actionpack-3.1.0/lib/abstract_controller/base.rb:167:in
`process_action'
# /Users/gallagher/.rvm/gems/jruby-1.6.3@rails310/gems/
actionpack-3.1.0/lib/action_controller/metal/rendering.rb:10:in
`process_action'
# /Users/gallagher/.rvm/gems/jruby-1.6.3@rails310/gems/
actionpack-3.1.0/lib/abstract_controller/callbacks.rb:18:in
`process_action'
# /Users/gallagher/.rvm/gems/jruby-1.6.3@rails310/gems/
activesupport-3.1.0/lib/active_support/callbacks.rb:419:in
`_run__1477426868__process_action__693554978__callbacks'
# org/jruby/RubyKernel.java:2092:in `send'
# /Users/gallagher/.rvm/gems/jruby-1.6.3@rails310/gems/
activesupport-3.1.0/lib/active_support/callbacks.rb:401:in
`_run_process_action_callbacks'
# org/jruby/RubyKernel.java:2096:in `send'
# /Users/gallagher/.rvm/gems/jruby-1.6.3@rails310/gems/
activesupport-3.1.0/lib/active_support/callbacks.rb:81:in
`run_callbacks'
# /Users/gallagher/.rvm/gems/jruby-1.6.3@rails310/gems/
actionpack-3.1.0/lib/abstract_controller/callbacks.rb:17:in
`process_action'
# /Users/gallagher/.rvm/gems/jruby-1.6.3@rails310/gems/
actionpack-3.1.0/lib/action_controller/metal/rescue.rb:17:in
`process_action'
# /Users/gallagher/.rvm/gems/jruby-1.6.3@rails310/gems/
actionpack-3.1.0/lib/action_controller/metal/instrumentation.rb:30:in
`process_action'
# /Users/gallagher/.rvm/gems/jruby-1.6.3@rails310/gems/
activesupport-3.1.0/lib/active_support/notifications.rb:53:in
`instrument'
# /Users/gallagher/.rvm/gems/jruby-1.6.3@rails310/gems/
activesupport-3.1.0/lib/active_support/notifications/instrumenter.rb:
21:in `instrument'
# /Users/gallagher/.rvm/gems/jruby-1.6.3@rails310/gems/
activesupport-3.1.0/lib/active_support/notifications.rb:53:in
`instrument'
# /Users/gallagher/.rvm/gems/jruby-1.6.3@rails310/gems/
actionpack-3.1.0/lib/action_controller/metal/instrumentation.rb:29:in
`process_action'
# /Users/gallagher/.rvm/gems/jruby-1.6.3@rails310/gems/
actionpack-3.1.0/lib/action_controller/metal/params_wrapper.rb:201:in
`process_action'
# /Users/gallagher/.rvm/gems/jruby-1.6.3@rails310/gems/
activerecord-3.1.0/lib/active_record/railties/controller_runtime.rb:
18:in `process_action'
# /Users/gallagher/.rvm/gems/jruby-1.6.3@rails310/gems/
actionpack-3.1.0/lib/abstract_controller/base.rb:121:in `process'
# /Users/gallagher/.rvm/gems/jruby-1.6.3@rails310/gems/
actionpack-3.1.0/lib/abstract_controller/rendering.rb:45:in `process'
# /Users/gallagher/.rvm/gems/jruby-1.6.3@rails310/gems/
actionpack-3.1.0/lib/action_controller/metal/testing.rb:17:in
`process_with_new_base_test'
# /Users/gallagher/.rvm/gems/jruby-1.6.3@rails310/gems/
actionpack-3.1.0/lib/action_controller/test_case.rb:449:in `process'
# /Users/gallagher/.rvm/gems/jruby-1.6.3@rails310/gems/
actionpack-3.1.0/lib/action_controller/test_case.rb:49:in `process'
# /Users/gallagher/.rvm/gems/jruby-1.6.3@rails310/gems/
actionpack-3.1.0/lib/action_controller/test_case.rb:370:in `post'
# ./spec/controllers/sec223f_acquisition_controller_spec.rb:13:in
`(root)'
# org/jruby/RubyKernel.java:2061:in `instance_eval'
# /Users/gallagher/.rvm/gems/jruby-1.6.3@rails310/gems/rspec-
core-2.6.4/lib/rspec/core/example.rb:48:in `run'
# /Users/gallagher/.rvm/gems/jruby-1.6.3@rails310/gems/rspec-
core-2.6.4/lib/rspec/core/example.rb:107:in `with_around_hooks'
# /Users/gallagher/.rvm/gems/jruby-1.6.3@rails310/gems/rspec-
core-2.6.4/lib/rspec/core/example.rb:45:in `run'
# /Users/gallagher/.rvm/gems/jruby-1.6.3@rails310/gems/rspec-
core-2.6.4/lib/rspec/core/example_group.rb:294:in `run_examples'
# org/jruby/RubyArray.java:2336:in `collect'
# /Users/gallagher/.rvm/gems/jruby-1.6.3@rails310/gems/rspec-
core-2.6.4/lib/rspec/core/example_group.rb:290:in `run_examples'
# /Users/gallagher/.rvm/gems/jruby-1.6.3@rails310/gems/rspec-
core-2.6.4/lib/rspec/core/example_group.rb:262:in `run'
# /Users/gallagher/.rvm/gems/jruby-1.6.3@rails310/gems/rspec-
core-2.6.4/lib/rspec/core/command_line.rb:24:in `run'
# org/jruby/RubyArray.java:2336:in `collect'
# /Users/gallagher/.rvm/gems/jruby-1.6.3@rails310/gems/rspec-
core-2.6.4/lib/rspec/core/command_line.rb:24:in `run'
# /Users/gallagher/.rvm/gems/jruby-1.6.3@rails310/gems/rspec-
core-2.6.4/lib/rspec/core/reporter.rb:12:in `report'
# /Users/gallagher/.rvm/gems/jruby-1.6.3@rails310/gems/rspec-
core-2.6.4/lib/rspec/core/command_line.rb:21:in `run'
# /Users/gallagher/.rvm/gems/jruby-1.6.3@rails310/gems/rspec-
core-2.6.4/lib/rspec/core/runner.rb:80:in `run_in_process'
# /Users/gallagher/.rvm/gems/jruby-1.6.3@rails310/gems/rspec-
core-2.6.4/lib/rspec/core/runner.rb:69:in `run'


On Nov 6, 12:56 pm, Sidu Ponnappa <ckponna...@gmail.com> wrote:
> Take a look athttps://github.com/c42/goldberg/tree/master/spec/controllers
>
> If you're getting a 500, it's no fault of your tests - if you can
> throw in the stack trace that accompanies that 500 is your test.log, I
> could help you further.
>
> Best,
> Sidu Ponnappa.http://c42.inhttp://rubymonk.com
>
> On 6 November 2011 20:44, G-man <gallagher.po...@gmail.com> wrote:
>
>
>
>
>
>
>
> > Sidu,
>
> > Thanks for confirming the utility of functional testing (a la RSpec)
> > for JSON APIs, and I shall explore rspec-http.
>
> > Unfortunately, I'm still stuck on the problem I outlined above. If you
> > or anyone would be so kind as to include a snippet of something
> > similar so I can determine my obstacle(s), it would be greatly
> > appreciated.
>
> > G
>
> > On Nov 6, 3:47 am, Sidu Ponnappa <ckponna...@gmail.com> wrote:
> >> I've had a fairly good experience simply by using rspec controller tests
> >> and a json parser.
>
> >> For stateless apis this is usually sufficient.
>
> >> You could also take a look athttp://github.com/c42/rspec-httptohelp test

Sidu Ponnappa

unread,
Nov 7, 2011, 2:44:30 AM11/7/11
to service-oriented...@googlegroups.com
> unexpected token at 'loan_request=4000000'
Looks like the json is malformed, so you may want to dig into whatever
is generating it. As far as I can tell, this should be

loan_request: 4000000

Let me know if this helps. You may also want to consider upgrading to
JRuby 1.6.5, because each minor release has fixed a ton of bugs. I
doubt it's actually causing this particular problem, but it can't
hurt.

Best,
Sidu.
http://c42.in
http://rubymonk.com

Steve Agalloco

unread,
Nov 7, 2011, 12:28:58 PM11/7/11
to Service Oriented Design With Ruby
Are you actually sending it JSON? From the code you pasted above, it
looks like you are actually sending parameterized values (Note that
rails is parsing that into a params hash). Unless you are sending a
body as JSON, this line should fail; data =
ActiveSupport::JSON.decode(request.body.read)

As for asserting JSON responses, you might want to look at json_spec
and jsonpath

https://github.com/collectiveidea/json_spec
https://github.com/joshbuddy/jsonpath

I haven't used json_spec personally but it looks nice. I have used
jsonpath quite a bit to assert responses and it works well.

Best,
Steve

On Nov 7, 2:44 am, Sidu Ponnappa <ckponna...@gmail.com> wrote:
> > unexpected token at 'loan_request=4000000'
>
> Looks like the json is malformed, so you may want to dig into whatever
> is generating it. As far as I can tell, this should be
>
> loan_request: 4000000
>
> Let me know if this helps. You may also want to consider upgrading to
> JRuby 1.6.5, because each minor release has fixed a ton of bugs. I
> doubt it's actually causing this particular problem, but it can't
> hurt.
>
> Best,
> Sidu.http://c42.inhttp://rubymonk.com
> ...
>
> read more »

Raju

unread,
May 1, 2013, 6:57:46 AM5/1/13
to service-oriented...@googlegroups.com
Hi Sidu,

We have to automate JSON responses. Can you please help me how we have to do for this scenario ? I have explained every thing in stackoverflow link I will page that link here so that you can get frull details.


Thanks
Raju
Reply all
Reply to author
Forward
0 new messages