Quellcode durchsuchen

compatible with AR threadsafe branch

Peter McCracken vor 11 Jahren
Ursprung
Commit
398f254716
4 geänderte Dateien mit 49 neuen und 17 gelöschten Zeilen
  1. 2 0
      Gemfile
  2. 16 9
      Gemfile.lock
  3. 4 4
      lib/shopify_api/resources/base.rb
  4. 27 4
      test/base_test.rb

+ 2 - 0
Gemfile

@@ -1,3 +1,5 @@
 source "https://rubygems.org"
 
 gemspec
+
+gem 'activeresource', :git => 'git://github.com/peterjm/activeresource', :ref => '96c57a9069'

+ 16 - 9
Gemfile.lock

@@ -1,3 +1,13 @@
+GIT
+  remote: git://github.com/peterjm/activeresource
+  revision: 96c57a9069c4dadd60df6e341dbd902d5cdd2c1b
+  ref: 96c57a9069
+  specs:
+    activeresource (4.0.0)
+      activemodel (~> 4.0)
+      activesupport (~> 4.0)
+      rails-observers (~> 0.1.2)
+
 PATH
   remote: .
   specs:
@@ -8,14 +18,10 @@ PATH
 GEM
   remote: https://rubygems.org/
   specs:
-    activemodel (4.0.0)
-      activesupport (= 4.0.0)
+    activemodel (4.0.2)
+      activesupport (= 4.0.2)
       builder (~> 3.1.0)
-    activeresource (4.0.0)
-      activemodel (~> 4.0)
-      activesupport (~> 4.0)
-      rails-observers (~> 0.1.1)
-    activesupport (4.0.0)
+    activesupport (4.0.2)
       i18n (~> 0.6, >= 0.6.4)
       minitest (~> 4.2)
       multi_json (~> 1.3)
@@ -24,12 +30,12 @@ GEM
     atomic (1.1.14)
     builder (3.1.4)
     fakeweb (1.3.0)
-    i18n (0.6.5)
+    i18n (0.6.9)
     metaclass (0.0.1)
     minitest (4.7.5)
     mocha (0.14.0)
       metaclass (~> 0.0.1)
-    multi_json (1.8.2)
+    multi_json (1.8.4)
     rails-observers (0.1.2)
       activemodel (~> 4.0)
     rake (10.1.0)
@@ -42,6 +48,7 @@ PLATFORMS
   ruby
 
 DEPENDENCIES
+  activeresource!
   fakeweb
   mocha (>= 0.9.8)
   rake

+ 4 - 4
lib/shopify_api/resources/base.rb

@@ -14,7 +14,7 @@ module ShopifyAPI
 
       same.send("to_#{self.class.format.extension}", options)
     end
-    
+
     def as_json(options = nil)
       root = options[:root] if options.try(:key?, :root)
       if include_root_in_json
@@ -27,12 +27,12 @@ module ShopifyAPI
 
     class << self
       def headers
-        if defined?(@headers)
-          @headers
+        if _headers_defined?
+          _headers
         elsif superclass != Object && superclass.headers
           superclass.headers
         else
-          @headers ||= {}
+          _headers ||= {}
         end
       end
 

+ 27 - 4
test/base_test.rb

@@ -14,14 +14,14 @@ class BaseTest < Test::Unit::TestCase
     assert_nil ActiveResource::Base.site
     assert_equal 'https://shop1.myshopify.com/admin', ShopifyAPI::Base.site.to_s
     assert_equal 'https://shop1.myshopify.com/admin', ShopifyAPI::Shop.site.to_s
-    
+
     assert_nil ActiveResource::Base.headers['X-Shopify-Access-Token']
     assert_equal 'token1', ShopifyAPI::Base.headers['X-Shopify-Access-Token']
     assert_equal 'token1', ShopifyAPI::Shop.headers['X-Shopify-Access-Token']
   end
 
   test '#clear_session should clear site and headers from Base' do
-    ShopifyAPI::Base.activate_session @session1    
+    ShopifyAPI::Base.activate_session @session1
     ShopifyAPI::Base.clear_session
 
     assert_nil ActiveResource::Base.site
@@ -34,8 +34,8 @@ class BaseTest < Test::Unit::TestCase
   end
 
   test '#activate_session with one session, then clearing and activating with another session should send request to correct shop' do
-    ShopifyAPI::Base.activate_session @session1   
-    ShopifyAPI::Base.clear_session    
+    ShopifyAPI::Base.activate_session @session1
+    ShopifyAPI::Base.clear_session
     ShopifyAPI::Base.activate_session @session2
 
     assert_nil ActiveResource::Base.site
@@ -54,4 +54,27 @@ class BaseTest < Test::Unit::TestCase
     ShopifyAPI::Base.delete "1"
   end
 
+  test "#headers includes the User-Agent" do
+    assert_includes ShopifyAPI::Base.headers.keys, 'User-Agent'
+    thread = Thread.new do
+      assert_includes ShopifyAPI::Base.headers.keys, 'User-Agent'
+    end
+    thread.join
+  end
+
+  if ActiveResource::VERSION::MAJOR >= 4
+    test "#headers is threadsafe" do
+      thread1 = Thread.new do
+        ShopifyAPI::Base.headers['X-Custom'] = "My header value"
+        assert_includes ShopifyAPI::Base.headers.keys, 'X-Custom'
+      end
+      thread1.join
+      thread2 = Thread.new do
+        assert_not_includes ShopifyAPI::Base.headers.keys, 'X-Custom'
+      end
+      thread2.join
+
+      assert_nil ShopifyAPI::Base.headers['X-Custom']
+    end
+  end
 end