Permalink
Browse files

+ multi_json support, utf normalize and all json parsers tests

  • Loading branch information...
Andrew Zinenko
Andrew Zinenko committed Aug 9, 2012
1 parent 8b6ede3 commit 9a8464c23e4e08b3a2571e3d7ef569e1e11833e9
View
@@ -1,12 +1,13 @@
PATH
remote: .
specs:
- vk-ruby (0.8.2)
+ vk-ruby (0.8.5)
faraday (~> 0.8.0)
faraday_middleware (~> 0.8.8)
multi_json (~> 1.3.6)
multi_xml (~> 0.5.1)
transformer (~> 0.2.2)
+ unicode_utils (~> 1.3.0)
GEM
remote: http://rubygems.org/
@@ -17,23 +18,28 @@ GEM
multipart-post (~> 1.1)
faraday_middleware (0.8.8)
faraday (>= 0.7.4, < 0.9)
+ json_pure (1.7.4)
minitest (2.12.1)
multi_json (1.3.6)
multi_xml (0.5.1)
multipart-post (1.1.5)
oj (1.0.6)
rake (0.9.2.2)
transformer (0.2.2)
+ unicode_utils (1.3.0)
webmock (1.8.7)
addressable (>= 2.2.7)
crack (>= 0.1.7)
+ yajl-ruby (1.1.0)
PLATFORMS
ruby
DEPENDENCIES
+ json_pure (~> 1.7.4)
minitest (~> 2.8)
oj (~> 1.0.6)
rake (~> 0.9)
vk-ruby!
webmock (~> 1.0)
+ yajl-ruby (~> 1.1.0)
View
@@ -2,13 +2,14 @@
require 'faraday'
require 'faraday_middleware'
-require 'cgi'
require 'transformer'
-require 'yaml'
require 'multi_json'
+require 'yaml'
+require 'cgi'
require 'forwardable'
-module VK
+FaradayMiddleware::ParseJson.define_parser do |body|
+ MultiJson.load(body)
end
-%w(configurable core secure serverside standalone vk_exception).each{|lib| require "vk-ruby/#{lib}"}
+%w(middleware/validate_utf middleware/normalize_utf configurable core secure serverside standalone vk_exception).each{|lib| require "vk-ruby/#{lib}"}
View
@@ -62,10 +62,17 @@ def request(path, options = {})
def faraday_middleware
@faraday_middleware ||= proc do |faraday|
- faraday.adapter self.adapter
faraday.request :url_encoded
+
faraday.response :json, content_type: /\bjson$/
faraday.response :xml, content_type: /\bxml$/
+
+ faraday.response :normalize_utf
+ faraday.response :validate_utf
+
+ faraday.response :logger
+
+ faraday.adapter self.adapter
end
end
@@ -0,0 +1,20 @@
+require "unicode_utils"
+
+module Faraday
+ class Response::NormalizeUtf < Response::Middleware
+
+ def initialize(app)
+ super(app)
+ end
+
+ def call(environment)
+ @app.call(environment).on_complete do |env|
+ env[:body] = UnicodeUtils.nfkd(env[:body])
+ env
+ end
+ end
+
+ end
+end
+
+Faraday.register_middleware :response, normalize_utf: Faraday::Response::NormalizeUtf
@@ -0,0 +1,20 @@
+require "unicode_utils"
+
+module Faraday
+ class Response::ValidateUtf < Response::Middleware
+
+ def initialize(app)
+ super(app)
+ end
+
+ def call(environment)
+ @app.call(environment).on_complete do |env|
+ puts env[:body] = env[:body].force_encoding('UTF-8').gsub(/[^\x00-\x7F]/,'').chars.select{|i| i.valid_encoding?}.join
+ env
+ end
+ end
+
+ end
+end
+
+Faraday.register_middleware :response, validate_utf: Faraday::Response::ValidateUtf
View
@@ -3,6 +3,7 @@
class ExceptionsTest < MiniTest::Unit::TestCase
def setup
@app = VK::Standalone.new app_id: 2657696
+ create_stubs!
end
def test_bad_requests
View
@@ -1,34 +1,37 @@
require "vk-ruby"
require 'minitest/autorun'
require 'webmock'
+
include WebMock::API
-stub_request(:get, /https:\/\/api.vk.com\/method/).to_return(lambda { |request|
- {
- body: {'response' => URI.parse(request.uri).query.to_params}.to_json,
- headers: {"Content-Type" => 'application/json'}
- }
-})
+def create_stubs!
+ stub_request(:get, /https:\/\/api.vk.com\/method/).to_return(lambda { |request|
+ {
+ body: {'response' => URI.parse(request.uri).query.to_params}.to_json,
+ headers: {"Content-Type" => 'application/json'}
+ }
+ })
-stub_request(:post, /https:\/\/api.vk.com\/method/).to_return(lambda { |request|
- body = request.body.to_params
- status = body.has_key?('http_error') ? 500 : 200
+ stub_request(:post, /https:\/\/api.vk.com\/method/).to_return(lambda { |request|
+ body = request.body.to_params
+ status = body.has_key?('http_error') ? 500 : 200
- response = body.has_key?('error') ? {'error' => {}} : {'response' => body}
+ response = body.has_key?('error') ? {'error' => {}} : {'response' => body}
- {
- body: response.to_json,
- headers: {"Content-Type" => 'application/json'},
- status: status
- }
-})
+ {
+ body: response.to_json,
+ headers: {"Content-Type" => 'application/json'},
+ status: status
+ }
+ })
-stub_request(:get, /https:\/\/oauth.vk.com\/access_token/).to_return(lambda { |request|
- {
- body: {'response' => URI.parse(request.uri).query.to_params}.to_json,
- headers: {"Content-Type" => 'application/json'}
- }
-})
+ stub_request(:get, /https:\/\/oauth.vk.com\/access_token/).to_return(lambda { |request|
+ {
+ body: {'response' => URI.parse(request.uri).query.to_params}.to_json,
+ headers: {"Content-Type" => 'application/json'}
+ }
+ })
+end
class String
def to_params
View
@@ -0,0 +1,73 @@
+require File.expand_path('../helpers', __FILE__)
+
+class JsonParseTest < MiniTest::Unit::TestCase
+
+ BAD_PROFILES = 21774501...21774512
+
+ def setup
+ @app = VK::Standalone.new app_id: '2878250',
+ app_secret: 'YUvaf67VoJgCuCvm2Rgj',
+ access_token: '7bde41fd7eb64c047eb64c04207e9da72e77eb07eb34c1226a10c40cb3418bc'
+
+ @fields = %w[uid first_name last_name nickname screen_name bdate sex city country photo_big has_mobile contacts education rate relation counters]
+
+ WebMock.reset!
+ WebMock.allow_net_connect!
+ end
+
+ def test_json
+ MultiJson.engine = :json_gem
+
+ BAD_PROFILES.each do |uid|
+ assert lambda{|i|
+ begin
+ @app.getProfiles(uids: i, fields: @fields.join(','), verb: :get); true
+ rescue Faraday::Error::ParsingError => ex
+ false
+ end
+ }.call(uid), 'parsing error'
+ end
+ end
+
+ def test_json_pure
+ MultiJson.engine = :json_pure
+
+ BAD_PROFILES.each do |uid|
+ assert lambda{|i|
+ begin
+ @app.getProfiles(uids: i, fields: @fields.join(','), verb: :get); true
+ rescue Faraday::Error::ParsingError
+ false
+ end
+ }.call(uid), 'parsing error'
+ end
+ end
+
+ def test_oj
+ MultiJson.engine = :oj
+
+ BAD_PROFILES.each do |uid|
+ assert lambda{|i|
+ begin
+ @app.getProfiles(uids: i, fields: @fields.join(','), verb: :get); true
+ rescue Faraday::Error::ParsingError
+ false
+ end
+ }.call(uid), 'parsing error'
+ end
+ end
+
+ def test_yajl
+ MultiJson.engine = :yajl
+
+ BAD_PROFILES.each do |uid|
+ assert lambda{|i|
+ begin
+ @app.getProfiles(uids: i, fields: @fields.join(','), verb: :get); false
+ rescue Faraday::Error::ParsingError
+ false
+ end
+ }.call(uid), 'parsing error'
+ end
+ end
+end
View
@@ -4,6 +4,7 @@ class SecureServerTest < MiniTest::Unit::TestCase
def setup
@app = ::VK::Secure.new app_id: :test_id, app_secret: :test_secret
@secure_api = YAML.load_file( File.expand_path( File.dirname(__FILE__) + "/../lib/vk-ruby/api/secure.yml" ))
+ create_stubs!
end
def test_init_attributes
View
@@ -3,8 +3,8 @@
class ServersideTest < MiniTest::Unit::TestCase
def setup
@base_api ||= YAML.load_file( File.expand_path( File.dirname(__FILE__) + "/../lib/vk-ruby/api/base.yml" ))
-
@app = ::VK::Serverside.new app_id: :test_id, app_secret: :test_secret, access_token: :test_token
+ create_stubs!
end
def test_init_attributes
View
@@ -5,6 +5,7 @@ def setup
@base_api ||= YAML.load_file( File.expand_path( File.dirname(__FILE__) + "/../lib/vk-ruby/api/base.yml" ))
@ext_api ||= YAML.load_file( File.expand_path( File.dirname(__FILE__) + "/../lib/vk-ruby/api/ext.yml" ))
@app = ::VK::Standalone.new app_id: :test_id, access_token: :test_token
+ create_stubs!
end
def test_init_attributes
View
@@ -16,16 +16,19 @@ Gem::Specification.new do |gem|
gem.extra_rdoc_files = ["LICENSE.txt", "README.md" ]
- gem.add_runtime_dependency 'transformer', '~> 0.2.2'
- gem.add_runtime_dependency 'multi_json', '~> 1.3.6'
- gem.add_runtime_dependency 'multi_xml', '~> 0.5.1'
- gem.add_runtime_dependency 'faraday', '~> 0.8.0'
+ gem.add_runtime_dependency 'transformer', '~> 0.2.2'
+ gem.add_runtime_dependency 'multi_json', '~> 1.3.6'
+ gem.add_runtime_dependency 'multi_xml', '~> 0.5.1'
+ gem.add_runtime_dependency 'faraday', '~> 0.8.0'
gem.add_runtime_dependency 'faraday_middleware', '~> 0.8.8'
+ gem.add_runtime_dependency 'unicode_utils', '~> 1.3.0'
- gem.add_development_dependency 'rake', '~> 0.9'
- gem.add_development_dependency 'minitest', '~> 2.8'
- gem.add_development_dependency 'webmock', '~> 1.0'
- gem.add_development_dependency 'oj', '~> 1.0.6'
+ gem.add_development_dependency 'rake', '~> 0.9'
+ gem.add_development_dependency 'minitest', '~> 2.8'
+ gem.add_development_dependency 'webmock', '~> 1.0'
+ gem.add_development_dependency 'oj', '~> 1.0.6'
+ gem.add_development_dependency 'yajl-ruby', '~> 1.1.0'
+ gem.add_development_dependency 'json_pure', '~> 1.7.4'
gem.require_paths = ['lib']
gem.required_rubygems_version = Gem::Requirement.new('>= 1.3.6')

0 comments on commit 9a8464c

Please sign in to comment.