Patrick J. Collins
2011-Jul-21 19:49 UTC
[rspec-users] question regarding how to test posting form data with Net::HTTP
So I have a class that does this: def perform @response = Net::HTTP.new(url.host, url.port).start {|http| http.request(do_post) } unless @response.is_a?(Net::HTTPSuccess) || @response.is_a?(Net::HTTPRedirection) @response.error! end end def do_post Net::HTTP::Post.new(url.request_uri).tap do |req| req.set_form_data({:foo => "Bar") end end def url "http://myurl.com/something" end ... My question is-- Is there anything worth testing here? Since this is involving a 3rd party API, I shouldn''t perform an actual post (for many reasons)... On the other hand, this is pretty much all Net::HTTP functionality so I should be able to rely on it just working as is. The only thing I can see in this that MIGHT be worth testing is that @response raises an error if it''s not a HTTPSuccess or HTTPRedirection, but--- is that really worth doing? Thank you in advance for your input. Patrick J. Collins http://collinatorstudios.com
Sidu Ponnappa
2011-Jul-21 20:50 UTC
[rspec-users] question regarding how to test posting form data with Net::HTTP
Hi Patrick, I''d spec out the possible consequences of the different response codes as well what should happen in the event of a HTTP timeout. The requests themselves I would mock out because, as you pointed out, you''re calling a 3rd party and you don''t want live HTTP calls in your unit tests. Best, Sidu. http://sidu.in#code http://goldberg.c42.in On 22 July 2011 01:19, Patrick J. Collins <patrick at collinatorstudios.com>wrote:> So I have a class that does this: > > def perform > @response = Net::HTTP.new(url.host, url.port).start {|http| > http.request(do_post) } > > unless @response.is_a?(Net::HTTPSuccess) || > @response.is_a?(Net::HTTPRedirection) > @response.error! > end > end > > def do_post > Net::HTTP::Post.new(url.request_uri).tap do |req| > req.set_form_data({:foo => "Bar") > end > end > > def url > "http://myurl.com/something" > end > ... > > My question is-- Is there anything worth testing here? Since this is > involving a 3rd party API, I shouldn''t perform an actual post (for many > reasons)... On the other hand, this is pretty much all Net::HTTP > functionality > so I should be able to rely on it just working as is. > > The only thing I can see in this that MIGHT be worth testing is that > @response > raises an error if it''s not a HTTPSuccess or HTTPRedirection, but--- is > that > really worth doing? > > Thank you in advance for your input. > > > Patrick J. Collins > http://collinatorstudios.com > > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users >-------------- next part -------------- An HTML attachment was scrubbed... URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20110722/bbba1d08/attachment.html>