-
Notifications
You must be signed in to change notification settings - Fork 983
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Converts autorization middleware tests.
Moves Faraday::Utils classes (Headers and ParamsHash) into separate files. Converts Faraday::Utils::Headers tests.
- Loading branch information
Showing
7 changed files
with
360 additions
and
318 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
module Faraday | ||
module Utils | ||
# Adapted from Rack::Utils::HeaderHash | ||
class Headers < ::Hash | ||
def self.from(value) | ||
new(value) | ||
end | ||
|
||
def self.allocate | ||
new_self = super | ||
new_self.initialize_names | ||
new_self | ||
end | ||
|
||
def initialize(hash = nil) | ||
super() | ||
@names = {} | ||
self.update(hash || {}) | ||
end | ||
|
||
def initialize_names | ||
@names = {} | ||
end | ||
|
||
# on dup/clone, we need to duplicate @names hash | ||
def initialize_copy(other) | ||
super | ||
@names = other.names.dup | ||
end | ||
|
||
# need to synchronize concurrent writes to the shared KeyMap | ||
keymap_mutex = Mutex.new | ||
|
||
# symbol -> string mapper + cache | ||
KeyMap = Hash.new do |map, key| | ||
value = if key.respond_to?(:to_str) | ||
key | ||
else | ||
key.to_s.split('_'). # :user_agent => %w(user agent) | ||
each { |w| w.capitalize! }. # => %w(User Agent) | ||
join('-') # => "User-Agent" | ||
end | ||
keymap_mutex.synchronize { map[key] = value } | ||
end | ||
KeyMap[:etag] = "ETag" | ||
|
||
def [](k) | ||
k = KeyMap[k] | ||
super(k) || super(@names[k.downcase]) | ||
end | ||
|
||
def []=(k, v) | ||
k = KeyMap[k] | ||
k = (@names[k.downcase] ||= k) | ||
# join multiple values with a comma | ||
v = v.to_ary.join(', ') if v.respond_to? :to_ary | ||
super(k, v) | ||
end | ||
|
||
def fetch(k, *args, &block) | ||
k = KeyMap[k] | ||
key = @names.fetch(k.downcase, k) | ||
super(key, *args, &block) | ||
end | ||
|
||
def delete(k) | ||
k = KeyMap[k] | ||
if k = @names[k.downcase] | ||
@names.delete k.downcase | ||
super(k) | ||
end | ||
end | ||
|
||
def include?(k) | ||
@names.include? k.downcase | ||
end | ||
|
||
alias_method :has_key?, :include? | ||
alias_method :member?, :include? | ||
alias_method :key?, :include? | ||
|
||
def merge!(other) | ||
other.each { |k, v| self[k] = v } | ||
self | ||
end | ||
alias_method :update, :merge! | ||
|
||
def merge(other) | ||
hash = dup | ||
hash.merge! other | ||
end | ||
|
||
def replace(other) | ||
clear | ||
@names.clear | ||
self.update other | ||
self | ||
end | ||
|
||
def to_hash() ::Hash.new.update(self) end | ||
|
||
def parse(header_string) | ||
return unless header_string && !header_string.empty? | ||
|
||
headers = header_string.split(/\r\n/) | ||
|
||
# Find the last set of response headers. | ||
start_index = headers.rindex { |x| x.match(/^HTTP\//) } || 0 | ||
last_response = headers.slice(start_index, headers.size) | ||
|
||
last_response. | ||
tap { |a| a.shift if a.first.index('HTTP/') == 0 }. # drop the HTTP status line | ||
map { |h| h.split(/:\s*/, 2) }.reject { |p| p[0].nil? }. # split key and value, ignore blank lines | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
iMacTia
Author
Member
|
||
each { |key, value| | ||
# join multiple values with a comma | ||
if self[key] | ||
self[key] << ', ' << value | ||
else | ||
self[key] = value | ||
end | ||
} | ||
end | ||
|
||
protected | ||
|
||
def names | ||
@names | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
module Faraday | ||
module Utils | ||
# Hash with stringified keys | ||
class ParamsHash < Hash | ||
def [](key) | ||
super(convert_key(key)) | ||
end | ||
|
||
def []=(key, value) | ||
super(convert_key(key), value) | ||
end | ||
|
||
def delete(key) | ||
super(convert_key(key)) | ||
end | ||
|
||
def include?(key) | ||
super(convert_key(key)) | ||
end | ||
|
||
alias_method :has_key?, :include? | ||
alias_method :member?, :include? | ||
alias_method :key?, :include? | ||
|
||
def update(params) | ||
params.each do |key, value| | ||
self[key] = value | ||
end | ||
self | ||
end | ||
alias_method :merge!, :update | ||
|
||
def merge(params) | ||
dup.update(params) | ||
end | ||
|
||
def replace(other) | ||
clear | ||
update(other) | ||
end | ||
|
||
def merge_query(query, encoder = nil) | ||
if query && !query.empty? | ||
update((encoder || Utils.default_params_encoder).decode(query)) | ||
end | ||
self | ||
end | ||
|
||
def to_query(encoder = nil) | ||
(encoder || Utils.default_params_encoder).encode(self) | ||
end | ||
|
||
private | ||
|
||
def convert_key(key) | ||
key.to_s | ||
end | ||
end | ||
end | ||
end |
Oops, something went wrong.
bad indentation. use one line for one method call (for better reading). Maybe it will be better to use lazy iterator for this chain