diff --git a/.travis.yml b/.travis.yml index 9b9f3b7..70c1660 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,8 +1,7 @@ language: ruby rvm: - - 2.0.0 - - 2.1.5 - - 2.2.1 - - 2.3.0 - - ruby-head + - 2.4 + - 2.5 + - 2.6 + - 2.7 script: bundle exec rake test diff --git a/Gemfile b/Gemfile index 9c451ee..7844371 100644 --- a/Gemfile +++ b/Gemfile @@ -6,5 +6,5 @@ gem "json" group :test do gem "rake" gem "minitest" - gem "fakeweb" + gem "webmock" end diff --git a/Gemfile.lock b/Gemfile.lock index b42e040..3bc12ac 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,23 +1,37 @@ GEM remote: https://rubygems.org/ specs: - fakeweb (1.3.0) - httparty (0.14.0) + addressable (2.7.0) + public_suffix (>= 2.0.2, < 5.0) + crack (0.4.3) + safe_yaml (~> 1.0.0) + hashdiff (1.0.1) + httparty (0.18.1) + mime-types (~> 3.0) multi_xml (>= 0.5.2) - json (2.0.2) - minitest (5.10.1) + json (2.3.0) + mime-types (3.3.1) + mime-types-data (~> 3.2015) + mime-types-data (3.2020.0512) + minitest (5.14.1) multi_xml (0.6.0) - rake (12.0.0) + public_suffix (4.0.5) + rake (13.0.1) + safe_yaml (1.0.5) + webmock (3.8.3) + addressable (>= 2.3.6) + crack (>= 0.3.2) + hashdiff (>= 0.4.0, < 2.0.0) PLATFORMS ruby DEPENDENCIES - fakeweb httparty json minitest rake + webmock BUNDLED WITH - 1.12.5 + 2.1.4 diff --git a/lib/woocommerce_api.rb b/lib/woocommerce_api.rb index 4ef29b2..d363a31 100644 --- a/lib/woocommerce_api.rb +++ b/lib/woocommerce_api.rb @@ -97,7 +97,7 @@ def add_query_params endpoint, data endpoint += "?" unless endpoint.include? "?" endpoint += "&" unless endpoint.end_with? "?" - endpoint + URI.encode(flatten_hash(data).join("&")) + endpoint + URI.encode_www_form(flatten_hash(data)) end # Internal: Get URL for requests @@ -189,12 +189,12 @@ def flatten_hash hash case value when Hash value.map do |inner_key, inner_value| - "#{key}[#{inner_key}]=#{inner_value}" + ["#{key}[#{inner_key}]", "#{inner_value}"] end when Array - value.map { |inner_value| "#{key}[]=#{inner_value}" } + value.map { |inner_value| ["#{key}[]", "#{inner_value}"] } else - "#{key}=#{value}" + [["#{key}", "#{value}"]] end end end diff --git a/lib/woocommerce_api/oauth.rb b/lib/woocommerce_api/oauth.rb index c5bc44e..3df7c66 100644 --- a/lib/woocommerce_api/oauth.rb +++ b/lib/woocommerce_api/oauth.rb @@ -42,7 +42,7 @@ def get_oauth_url params["oauth_timestamp"] = Time.new.to_i params["oauth_signature"] = CGI::escape(generate_oauth_signature(params, url)) - query_string = URI::encode(params.map{|key, value| "#{key}=#{value}"}.join("&")) + query_string = URI.encode_www_form(params) "#{url}?#{query_string}" end diff --git a/test/test.rb b/test/test.rb index 58f25f8..d273805 100644 --- a/test/test.rb +++ b/test/test.rb @@ -1,5 +1,5 @@ require "minitest/autorun" -require "fakeweb" +require 'webmock/minitest' require "json" require "woocommerce_api" @@ -19,9 +19,11 @@ def setup end def test_basic_auth_get - FakeWeb.register_uri(:get, "https://user:pass@dev.test/wc-api/v3/customers", - body: '{"customers":[]}', - content_type: "application/json" + stub_request(:get, "https://dev.test/wc-api/v3/customers") + .with(headers: { 'Authorization'=>'Basic dXNlcjpwYXNz' }) + .to_return( + body: '{"customers":[]}', + headers: { content_type: "application/json" }, ) response = @basic_auth.get "customers" @@ -29,9 +31,9 @@ def test_basic_auth_get end def test_oauth_get - FakeWeb.register_uri(:get, /http:\/\/dev\.test\/wc-api\/v3\/customers\?oauth_consumer_key=user&oauth_nonce=(.*)&(.*)oauth_signature_method=HMAC-SHA256&oauth_timestamp=(.*)/, + stub_request(:get, /http:\/\/dev\.test\/wc-api\/v3\/customers\?oauth_consumer_key=user&oauth_nonce=(.*)&(.*)oauth_signature_method=HMAC-SHA256&oauth_timestamp=(.*)/).and_return( body: '{"customers":[]}', - content_type: "application/json" + headers: { content_type: "application/json" }, ) response = @oauth.get "customers" @@ -39,9 +41,9 @@ def test_oauth_get end def test_oauth_get_puts_data_in_alpha_order - FakeWeb.register_uri(:get, /http:\/\/dev\.test\/wc-api\/v3\/customers\?abc=123&oauth_consumer_key=user&oauth_d=456&oauth_nonce=(.*)&(.*)oauth_signature_method=HMAC-SHA256&oauth_timestamp=(.*)&xyz=789/, + stub_request(:get, /http:\/\/dev\.test\/wc-api\/v3\/customers\?abc=123&oauth_consumer_key=user&oauth_d=456&oauth_nonce=(.*)&(.*)oauth_signature_method=HMAC-SHA256&oauth_timestamp=(.*)&xyz=789/).and_return( body: '{"customers":[]}', - content_type: "application/json" + headers: { content_type: "application/json" }, ) response = @oauth.get "customers", abc: '123', oauth_d: '456', xyz: '789' @@ -49,10 +51,12 @@ def test_oauth_get_puts_data_in_alpha_order end def test_basic_auth_post - FakeWeb.register_uri(:post, "https://user:pass@dev.test/wc-api/v3/products", - body: '{"products":[]}', - content_type: "application/json", - status: ["201", "Created"] + stub_request(:post, "https://dev.test/wc-api/v3/products") + .with(headers: { 'Authorization'=>'Basic dXNlcjpwYXNz' }) + .to_return( + body: '{"products":[]}', + headers: { content_type: "application/json" }, + status: 201 ) data = { @@ -66,10 +70,10 @@ def test_basic_auth_post end def test_oauth_post - FakeWeb.register_uri(:post, /http:\/\/dev\.test\/wc-api\/v3\/products\?oauth_consumer_key=user&oauth_nonce=(.*)&(.*)oauth_signature_method=HMAC-SHA256&oauth_timestamp=(.*)/, + stub_request(:post, /http:\/\/dev\.test\/wc-api\/v3\/products\?oauth_consumer_key=user&oauth_nonce=(.*)&(.*)oauth_signature_method=HMAC-SHA256&oauth_timestamp=(.*)/).and_return( body: '{"products":[]}', - content_type: "application/json", - status: ["201", "Created"] + headers: { content_type: "application/json" }, + status: 201 ) data = { @@ -83,10 +87,16 @@ def test_oauth_post end def test_basic_auth_put - FakeWeb.register_uri(:put, "https://user:pass@dev.test/wc-api/v3/products/1234", - body: '{"customers":[]}', - content_type: "application/json" - ) + stub_request(:put, "https://dev.test/wc-api/v3/products/1234"). + with( + body: "{\"product\":{\"title\":\"Updating product title\"}}", + headers: { + 'Authorization'=>'Basic dXNlcjpwYXNz', + }). + to_return( + body: '{"customers":[]}', + headers: { content_type: "application/json" } + ) data = { product: { @@ -99,9 +109,9 @@ def test_basic_auth_put end def test_oauth_put - FakeWeb.register_uri(:put, /http:\/\/dev\.test\/wc-api\/v3\/products\?oauth_consumer_key=user&oauth_nonce=(.*)&(.*)oauth_signature_method=HMAC-SHA256&oauth_timestamp=(.*)/, + stub_request(:put, /http:\/\/dev\.test\/wc-api\/v3\/products\?oauth_consumer_key=user&oauth_nonce=(.*)&(.*)oauth_signature_method=HMAC-SHA256&oauth_timestamp=(.*)/).and_return( body: '{"products":[]}', - content_type: "application/json" + headers: { content_type: "application/json" }, ) data = { @@ -115,47 +125,57 @@ def test_oauth_put end def test_basic_auth_delete - FakeWeb.register_uri(:delete, "https://user:pass@dev.test/wc-api/v3/products/1234?force=true", - body: '{"message":"Permanently deleted product"}', - content_type: "application/json", - status: ["202", "Accepted"] - ) + stub_request(:delete, "https://dev.test/wc-api/v3/products/1234?force=true"). + with( + headers: { + 'Authorization'=>'Basic dXNlcjpwYXNz', + }). + to_return( + body: '{"message":"Permanently deleted product"}', + headers: { content_type: "application/json" }, + status: 202, + ) response = @basic_auth.delete "products/1234?force=true" assert_equal 202, response.code - assert_equal '{"message":"Permanently deleted product"}', response.to_json + assert_equal '{"message":"Permanently deleted product"}', response.body end def test_basic_auth_delete_params - FakeWeb.register_uri(:delete, "https://user:pass@dev.test/wc-api/v3/products/1234?force=true", - body: '{"message":"Permanently deleted product"}', - content_type: "application/json", - status: ["202", "Accepted"] - ) + stub_request(:delete, "https://dev.test/wc-api/v3/products/1234?force=true"). + with( + headers: { + 'Authorization'=>'Basic dXNlcjpwYXNz', + }). + to_return( + body: '{"message":"Permanently deleted product"}', + headers: { content_type: "application/json" }, + status: 202, + ) response = @basic_auth.delete "products/1234", force: true assert_equal 202, response.code - assert_equal '{"message":"Permanently deleted product"}', response.to_json + assert_equal '{"message":"Permanently deleted product"}', response.body end - def test_oauth_put - FakeWeb.register_uri(:delete, /http:\/\/dev\.test\/wc-api\/v3\/products\/1234\?force=true&oauth_consumer_key=user&oauth_nonce=(.*)&(.*)oauth_signature_method=HMAC-SHA256&oauth_timestamp=(.*)/, + def test_oauth_delete + stub_request(:delete, /http:\/\/dev\.test\/wc-api\/v3\/products\/1234\?force=true&oauth_consumer_key=user&oauth_nonce=(.*)&(.*)oauth_signature_method=HMAC-SHA256&oauth_timestamp=(.*)/).and_return( body: '{"message":"Permanently deleted product"}', - content_type: "application/json", - status: ["202", "Accepted"] + headers: { content_type: "application/json" }, + status: 202 ) response = @oauth.delete "products/1234?force=true" assert_equal 202, response.code - assert_equal '{"message":"Permanently deleted product"}', response.to_json + assert_equal '{"message":"Permanently deleted product"}', response.body end def test_adding_query_params url = @oauth.send(:add_query_params, 'foo.com', filter: { sku: '123' }, order: 'created_at') - assert_equal url, URI.encode('foo.com?filter[sku]=123&order=created_at') + assert_equal url, 'foo.com?filter%5Bsku%5D=123&order=created_at' end def test_invalid_signature_method