detailed_log_subscriber_test.rb 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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.site = "http://localhost/admin"
  10. ActiveResource::LogSubscriber.attach_to :active_resource
  11. ActiveResource::DetailedLogSubscriber.attach_to :active_resource_detailed
  12. end
  13. def set_logger(logger)
  14. ActiveResource::Base.logger = logger
  15. end
  16. test "logging on #find" do
  17. fake "pages/1", :method => :get, :body => @page
  18. ShopifyAPI::Page.find(1)
  19. assert_equal 4, @logger.logged(:info).size
  20. assert_equal "GET http://localhost:80/admin/pages/1.json", @logger.logged(:info)[0]
  21. assert_match /\-\-\> 200/, @logger.logged(:info)[1]
  22. assert_equal "Headers: {\"Accept\"=>\"application/json\", #{@ua_header}}", @logger.logged(:info)[2]
  23. assert_match /Response:\n\{\"page\"\:\{((\"id\"\:1)|(\"title\"\:\"Shopify API\")),((\"id\"\:1)|(\"title\"\:\"Shopify API\"))\}\}/, @logger.logged(:info)[3]
  24. end
  25. test "logging on #find with an error" do
  26. fake "pages/2", :method => :get, :body => nil, :status => 404
  27. assert_raise ActiveResource::ResourceNotFound do
  28. ShopifyAPI::Page.find(2)
  29. end
  30. assert_equal 4, @logger.logged(:info).size
  31. assert_equal "GET http://localhost:80/admin/pages/2.json", @logger.logged(:info)[0]
  32. assert_match /\-\-\> 404/, @logger.logged(:info)[1]
  33. assert_equal "Headers: {\"Accept\"=>\"application/json\", #{@ua_header}}", @logger.logged(:info)[2]
  34. assert_equal "Response:", @logger.logged(:info)[3]
  35. end
  36. end