Skip to content

Commit

Permalink
Move method to Utils
Browse files Browse the repository at this point in the history
  • Loading branch information
wallin committed Sep 21, 2017
1 parent 1e3fcc9 commit 2aa905c
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 90 deletions.
1 change: 0 additions & 1 deletion lib/castle.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
require 'castle/failover_auth_response'
require 'castle/client'
require 'castle/header_formatter'
require 'castle/replace_invalid_characters'
require 'castle/secure_mode'
require 'castle/extractors/client_id'
require 'castle/extractors/headers'
Expand Down
19 changes: 0 additions & 19 deletions lib/castle/replace_invalid_characters.rb

This file was deleted.

2 changes: 1 addition & 1 deletion lib/castle/request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def build(endpoint, args, method)
request = Net::HTTP.const_get(method.to_s.capitalize).new(
"#{@config.api_endpoint.path}/#{endpoint}", @headers
)
request.body = ::Castle::ReplaceInvalidCharacters.call(args).to_json
request.body = ::Castle::Utils.replace_invalid_characters(args).to_json
add_basic_auth(request)
request
end
Expand Down
12 changes: 12 additions & 0 deletions lib/castle/utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,18 @@ def deep_symbolize_keys!(object, &block)
object
end
end

def replace_invalid_characters(arg)
if arg.is_a?(::String)
arg.encode('UTF-8', invalid: :replace, undef: :replace)
elsif arg.is_a?(::Hash)
arg.each_with_object({}) { |(k, v), h| h[k] = replace_invalid_characters(v) }
elsif arg.is_a?(::Array)
arg.map(&method(:replace_invalid_characters))
else
arg
end
end
end
end
end
69 changes: 0 additions & 69 deletions spec/lib/castle/replace_invalid_characters_spec.rb

This file was deleted.

66 changes: 66 additions & 0 deletions spec/lib/castle/utils_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,70 @@
it { is_expected.to eq(symbol_array_of_hashes) }
end
end

describe '::replace_invalid_characters' do
subject { described_class.replace_invalid_characters(input) }

context 'when input is a string' do
let(:input) { '1234' }

it { is_expected.to eq input }
end

context 'when input is an array' do
let(:input) { [1, 2, 3, '4'] }

it { is_expected.to eq input }
end

context 'when input is a hash' do
let(:input) { { user_id: 1 } }

it { is_expected.to eq input }
end

context 'when input is nil' do
let(:input) { nil }

it { is_expected.to eq input }
end

context 'when input is a nested hash' do
let(:input) { { user: { id: 1 } } }

it { is_expected.to eq input }
end

context 'with invalid UTF-8 characters' do
context 'when input is a hash' do
let(:input) { { user_id: "inv\xC4lid" } }

it { is_expected.to eq(user_id: 'inv�lid') }
end

context 'when input is a nested hash' do
let(:input) { { user: { id: "inv\xC4lid" } } }

it { is_expected.to eq(user: { id: 'inv�lid' }) }
end

context 'when input is an array of hashes' do
let(:input) { [{ user: "inv\xC4lid" }] * 2 }

it { is_expected.to eq([{ user: 'inv�lid' }, { user: 'inv�lid' }]) }
end

context 'when input is an array' do
let(:input) { ["inv\xC4lid"] * 2 }

it { is_expected.to eq(['inv�lid', 'inv�lid']) }
end

context 'when input is a hash with array in key' do
let(:input) { { items: ["inv\xC4lid"] * 2 } }

it { is_expected.to eq(items: ['inv�lid', 'inv�lid']) }
end
end
end
end

0 comments on commit 2aa905c

Please sign in to comment.