andreo-FdlSlcb4kYpknbxzx/v8hQ@public.gmane.org
2013-Sep-05 15:14 UTC
testing a REST API that receives params in json and returns data in json
Hey everyone, This is a problem that has been bothering me for some time. I am building an API function that should receive data in json and response in json. My controller tests run fine(Since I abstract that the data gets there already decode from JSON and only the answer needs to be interpreted ). I Also know that the function runs fine since I have used curl to test it with JSON arguments and it works perfectly. (ex: curl -i --header "Accept: application/json" --header "Content-Type: application/json" -d ''{"test":{"email":"andreo-FdlSlcb4kYpknbxzx/v8hQ@public.gmane.org"}}'' <url_with_the_service>) But obviously I would like to write request(feature) tests to test this automatically and the way I see it they should work exactly like curl, i.e., hit my service like it was an external call. That means that I would like to pass the arguments in JSON and receive an answer. I am pretty lost since all the examples I can see people treat arguments as it was already decoded. My question is: I am following a wrong premise in wanting to send the arguments and request as a JSON one since i will be testing that rails works, because this is its responsibility? But I would like to see how robust my code his to wrong arguments and would like to try with JSON. something of this type: it "should return an error if there is no correct email" do params = {:subscription => {:email => "andre"}} post "/magazine_subscriptions", { ''HTTP_ACCEPT'' => "application/json", ''Content-Type'' => ''application/json'', ''RAW_POST_DATA'' => params.to_json } end Do you know how this is possible? and please let me know if you think I am testing it wrong. all the best, Andre -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/2e689330-f4cd-426c-9c63-e42ee4e707de%40googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.
Emil S
2013-Sep-05 20:06 UTC
Re: testing a REST API that receives params in json and returns data in json
What you are asking for is a way to test your API from the perspective of a client ( like curl ) . I wanted to do the same thing in a project I worked on, and I used Cucumber + Rack::Test + some JSON helper methods and I am really happy with how it worked out. I''ve blogged about this here : http://www.emilsoman.com/blog/2013/05/18/building-a-tested/ . I''ve set up an example Rails 4 app with tests here : https://github.com/emilsoman/rails-4-api . Hope that helps. On 5 September 2013 20:44, <andreo-FdlSlcb4kYpknbxzx/v8hQ@public.gmane.org> wrote:> Hey everyone, > > This is a problem that has been bothering me for some time. I am building > an API function that should receive data in json and response in json. My > controller tests run fine(Since I abstract that the data gets there already > decode from JSON and only the answer needs to be interpreted ). > > I Also know that the function runs fine since I have used curl to test it > with JSON arguments and it works perfectly. > (ex: curl -i --header "Accept: application/json" --header "Content-Type: > application/json" -d ''{"test":{"email":"andreo-FdlSlcb4kYpknbxzx/v8hQ@public.gmane.org"}}'' > <url_with_the_service>) > > But obviously I would like to write request(feature) tests to test this > automatically and the way I see it they should work exactly like curl, > i.e., hit my service like it was an external call. That means that I would > like to pass the arguments in JSON and receive an answer. I am pretty lost > since all the examples I can see people treat arguments as it was already > decoded. > > My question is: I am following a wrong premise in wanting to send the > arguments and request as a JSON one since i will be testing that rails > works, because this is its responsibility? But I would like to see how > robust my code his to wrong arguments and would like to try with JSON. > > something of this type: > > it "should return an error if there is no correct email" do > params = {:subscription => {:email => "andre"}} > > post "/magazine_subscriptions", { ''HTTP_ACCEPT'' => "application/json", > ''Content-Type'' => ''application/json'', ''RAW_POST_DATA'' => params.to_json } > end > > Do you know how this is possible? and please let me know if you think I am > testing it wrong. > > all the best, > > Andre > > -- > You received this message because you are subscribed to the Google Groups > "Ruby on Rails: Talk" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To view this discussion on the web visit > https://groups.google.com/d/msgid/rubyonrails-talk/2e689330-f4cd-426c-9c63-e42ee4e707de%40googlegroups.com > . > For more options, visit https://groups.google.com/groups/opt_out. >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/CAJ%3Dox-DDjORidKrScC%3DkTSeLnO71-sys4ah0gc8MRP_VO_hkVQ%40mail.gmail.com. For more options, visit https://groups.google.com/groups/opt_out.
André Orvalho
2013-Sep-07 23:03 UTC
Re: testing a REST API that receives params in json and returns data in json
Hello there, I have found ur post while googlin. Its very useful if you wanna use cucumber. I dont like cucumber for many reasons and would like to use RSPEC to test this. do you know how to do it? If so can you help me? thanks for the help though, all the best, Andre -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/CAA6P50Y528gvZoRtMcxXC3ahy8wZu%3D81h9FTBVnH%3DiLDx17aKw%40mail.gmail.com. For more options, visit https://groups.google.com/groups/opt_out.
Emil S
2013-Sep-08 05:42 UTC
Re: testing a REST API that receives params in json and returns data in json
This may help you : http://matthewlehner.net/rails-api-testing-guidelines/ On 8 September 2013 04:33, André Orvalho <andreo-FdlSlcb4kYpknbxzx/v8hQ@public.gmane.org> wrote:> Hello there, > > I have found ur post while googlin. Its very useful if you wanna use > cucumber. I dont like cucumber for many reasons and would like to use RSPEC > to test this. do you know how to do it? If so can you help me? > > thanks for the help though, > > all the best, > > Andre > > -- > You received this message because you are subscribed to the Google Groups > "Ruby on Rails: Talk" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To view this discussion on the web visit > https://groups.google.com/d/msgid/rubyonrails-talk/CAA6P50Y528gvZoRtMcxXC3ahy8wZu%3D81h9FTBVnH%3DiLDx17aKw%40mail.gmail.com > . > > For more options, visit https://groups.google.com/groups/opt_out. >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/CAJ%3Dox-DDnxWas8Mbi97QtOuTrVeaUPfBe4iY9fMJXnUuLnkctw%40mail.gmail.com. For more options, visit https://groups.google.com/groups/opt_out.
André Orvalho
2013-Sep-08 11:14 UTC
Re: testing a REST API that receives params in json and returns data in json
Hi Emil, I have also found that link and that only shows GET requests. Its a very good example for those. But I need to make a pull request where I need to send data in JSON just like the example you posted here in cucumber, and I dont seem to find the way to do this in Rspec. at least I cant make it receive the arguments in JSON. all the best, Andre -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/CAA6P50awaCSBsyfm%2BpwJms8DoC-qPSzv40VCu7hyaq6nAdLd%3DA%40mail.gmail.com. For more options, visit https://groups.google.com/groups/opt_out.
Dheeraj Kumar
2013-Sep-08 11:17 UTC
Re: testing a REST API that receives params in json and returns data in json
Did you try reading the Rspec docs? Also, this would help you: http://edgeguides.rubyonrails.org/testing.html#what-to-include-in-your-functional-tests -- Dheeraj Kumar On Sunday 8 September 2013 at 4:44 PM, André Orvalho wrote:> Hi Emil, > > I have also found that link and that only shows GET requests. Its a very good example for those. But I need to make a pull request where I need to send data in JSON just like the example you posted here in cucumber, and I dont seem to find the way to do this in Rspec. at least I cant make it receive the arguments in JSON. > > all the best, > > Andre > > -- > You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. > To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org (mailto:rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org). > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org (mailto:rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org). > To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/CAA6P50awaCSBsyfm%2BpwJms8DoC-qPSzv40VCu7hyaq6nAdLd%3DA%40mail.gmail.com. > For more options, visit https://groups.google.com/groups/opt_out.-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/75D980CB036F4A77AD00CA723A687E2E%40gmail.com. For more options, visit https://groups.google.com/groups/opt_out.
André Orvalho
2013-Sep-08 15:19 UTC
Re: testing a REST API that receives params in json and returns data in json
Thanks everyone for your help, I have found the answer here http://stackoverflow.com/questions/18337609/rspec-request-test-merges-hashes-in-array-in-post-json-params It seems that the request parameters I was sending was not the correct ones. this worked it "should return an error if there is no correct email" do params = {:subscription => {:email => "andre"}} post "/magazine_subscriptions", params.to_json, {''ACCEPT'' => "application/json", ''CONTENT_TYPE'' => ''application/json''}end -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/CAA6P50bicuMMMQQ5uJA7_%3Dk%3DF-t16DK5DirRqmBMQBVdKuxF%3Dw%40mail.gmail.com. For more options, visit https://groups.google.com/groups/opt_out.