detailed_log_subscriber_test.rb 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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 = "https://this-is-my-test-shop.myshopify.com/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 https://this-is-my-test-shop.myshopify.com:443/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_raises ActiveResource::ResourceNotFound do
  29. ShopifyAPI::Page.find(2)
  30. end
  31. if ar_version_before?('5.1.0')
  32. assert_equal(4, @logger.logged(:info).size)
  33. assert_equal("GET https://this-is-my-test-shop.myshopify.com:443/admin/pages/2.json", @logger.logged(:info)[0])
  34. assert_match(/\-\-\> 404/, @logger.logged(:info)[1])
  35. assert_equal("Headers: {\"Accept\"=>\"application/json\", #{@ua_header}}", @logger.logged(:info)[2])
  36. assert_equal("Response:", @logger.logged(:info)[3])
  37. else
  38. assert_equal(2, @logger.logged(:error).size)
  39. assert_equal("GET https://this-is-my-test-shop.myshopify.com:443/admin/pages/2.json", @logger.logged(:error)[0])
  40. assert_match(/\-\-\> 404/, @logger.logged(:error)[1])
  41. assert_equal(2, @logger.logged(:info).size)
  42. assert_equal("Headers: {\"Accept\"=>\"application/json\", #{@ua_header}}", @logger.logged(:info)[0])
  43. assert_equal("Response:", @logger.logged(:info)[1])
  44. end
  45. end
  46. test "warns when the server responds with a x-shopify-api-deprecated-reason header" do
  47. fake(
  48. "pages/1",
  49. method: :get,
  50. body: @page,
  51. x_shopify_api_deprecated_reason: 'https://help.shopify.com/en/api/getting-started/api-deprecations'
  52. )
  53. ShopifyAPI::Page.find(1)
  54. assert_equal 1, @logger.logged(:warn).size
  55. assert_match %r{\[DEPRECATED\] ShopifyAPI made a call to GET \/admin\/pages\/1.json}, @logger.logged(:warn).first
  56. assert_match(
  57. %r{See https:\/\/help.shopify.com\/en\/api\/getting-started\/api-deprecations for more details.},
  58. @logger.logged(:warn).first
  59. )
  60. end
  61. end