detailed_log_subscriber_test.rb 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. require 'test_helper'
  2. require "active_support/log_subscriber/test_helper"
  3. class LogSubscriberTest < Test::Unit::TestCase
  4. include ActiveSupport::LogSubscriber::TestHelper
  5. def setup
  6. super
  7. @page = { :page => { :id => 1, :title => 'Shopify API' } }.to_json
  8. @ua_header = "\"User-Agent\"=>\"ShopifyAPI/#{ShopifyAPI::VERSION} ActiveResource/#{ActiveResource::VERSION::STRING} Ruby/#{RUBY_VERSION}\""
  9. ShopifyAPI::Base.clear_session
  10. ShopifyAPI::Base.site = "http://localhost/admin"
  11. ActiveResource::LogSubscriber.attach_to :active_resource
  12. ActiveResource::DetailedLogSubscriber.attach_to :active_resource_detailed
  13. end
  14. def set_logger(logger)
  15. ActiveResource::Base.logger = logger
  16. end
  17. test "logging on #find" do
  18. fake "pages/1", :method => :get, :body => @page
  19. ShopifyAPI::Page.find(1)
  20. assert_equal 4, @logger.logged(:info).size
  21. assert_equal "GET http://localhost:80/admin/pages/1.json", @logger.logged(:info)[0]
  22. assert_match /\-\-\> 200/, @logger.logged(:info)[1]
  23. assert_equal "Headers: {\"Accept\"=>\"application/json\", #{@ua_header}}", @logger.logged(:info)[2]
  24. assert_match /Response:\n\{\"page\"\:\{((\"id\"\:1)|(\"title\"\:\"Shopify API\")),((\"id\"\:1)|(\"title\"\:\"Shopify API\"))\}\}/, @logger.logged(:info)[3]
  25. end
  26. test "logging on #find with an error" do
  27. fake "pages/2", :method => :get, :body => nil, :status => 404
  28. assert_raise ActiveResource::ResourceNotFound do
  29. ShopifyAPI::Page.find(2)
  30. end
  31. assert_equal 4, @logger.logged(:info).size
  32. assert_equal "GET http://localhost:80/admin/pages/2.json", @logger.logged(:info)[0]
  33. assert_match /\-\-\> 404/, @logger.logged(:info)[1]
  34. assert_equal "Headers: {\"Accept\"=>\"application/json\", #{@ua_header}}", @logger.logged(:info)[2]
  35. assert_equal "Response:", @logger.logged(:info)[3]
  36. end
  37. end