Skip to content

Commit

Permalink
release 1.7.14 allowing an option strict: true so both ways will be a…
Browse files Browse the repository at this point in the history
…llowed. close #10
  • Loading branch information
MarioRuiz committed Jul 4, 2019
1 parent f211387 commit 4d27898
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 4 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,8 @@ resp = @http.get("/HTTP/Basic/")

```

In case you want to use strict base64 use the option `strict: true`

Remember for other kind of authentication systems NiceHttp take care of the redirections and cookies that are requested to be set. In case you need to add a header with a token you can add it by using your NiceHttp object and the key headers, for example:

```ruby
Expand Down
3 changes: 3 additions & 0 deletions lib/nice_http/manage_request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ def manage_request(*arguments)
!headers_t["Content-Type"][/application\/jxml/].nil?)
if arguments[0].include?(:values_for)
arguments[0][:values_for].each { |key, value|
#todo: implement set_nested
data = NiceHttpUtils.set_value_xml_tag(key.to_s(), data, value.to_s(), true)
}
end
Expand All @@ -114,6 +115,7 @@ def manage_request(*arguments)
if data.kind_of?(String)
if arguments[0].include?(:values_for)
arguments[0][:values_for].each { |key, value|
#todo: implement set_nested
data.gsub!(/"(#{key})":\s*"([^"]*)"/, '"\1": "' + value + '"') # "key":"value"
data.gsub!(/(#{key}):\s*"([^"]*)"/, '\1: "' + value + '"') # key:"value"
data.gsub!(/(#{key}):\s*'([^']*)'/, '\1: \'' + value + "'") # key:'value'
Expand All @@ -127,6 +129,7 @@ def manage_request(*arguments)
end
data = data.to_json()
elsif data.kind_of?(Array)
#todo: implement set_nested
data_arr = Array.new()
data.each_with_index { |row, indx|
unless row.kind_of?(Hash)
Expand Down
9 changes: 7 additions & 2 deletions lib/nice_http/utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -98,12 +98,17 @@ def self.set_value_xml_tag(tag_name, xml_string, value, take_off_prefix = false)
# input:
# user
# password
# strict: (default: false) use strict_encode64 if true, if not encode64
# output:
# seed string to be used on Authorization key header on a get request
####################################################
def self.basic_authentication(user:, password:)
def self.basic_authentication(user:, password:, strict: false)
require "base64"
seed = "Basic " + Base64.encode64(user + ":" + password)
if strict
seed = "Basic " + Base64.strict_encode64(user + ":" + password)
else
seed = "Basic " + Base64.encode64(user + ":" + password)
end
return seed
end
end
4 changes: 2 additions & 2 deletions nice_http.gemspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Gem::Specification.new do |s|
s.name = 'nice_http'
s.version = '1.7.13'
s.version = '1.7.14'
s.summary = "NiceHttp -- simplest library for accessing and testing HTTP and REST resources. Get http logs and statistics automatically. Use hashes on your requests. Access JSON even easier."
s.description = "NiceHttp -- simplest library for accessing and testing HTTP and REST resources. Get http logs and statistics automatically. Use hashes on your requests. Access JSON even easier."
s.authors = ["Mario Ruiz"]
Expand All @@ -11,7 +11,7 @@ Gem::Specification.new do |s|
s.extra_rdoc_files = ["LICENSE","README.md"]
s.homepage = 'https://github.com/MarioRuiz/nice_http'
s.license = 'MIT'
s.add_runtime_dependency 'nice_hash', '~> 1.12', '>= 1.12.4'
s.add_runtime_dependency 'nice_hash', '~> 1.12', '>= 1.12.14'
s.add_development_dependency 'rspec', '~> 3.8', '>= 3.8.0'
s.test_files = s.files.grep(%r{^(test|spec|features)/})
s.require_paths = ["lib"]
Expand Down
4 changes: 4 additions & 0 deletions spec/nice_http/utils/nice_http_utils_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,8 @@
res = NiceHttpUtils.basic_authentication(user: "guest", password: "guest")
expect(res).to eq "Basic Z3Vlc3Q6Z3Vlc3Q=\n"
end
it "return the correct enconding for basic authentication, strict" do
res = NiceHttpUtils.basic_authentication(user: "guest", password: "guest", strict: true)
expect(res).to eq "Basic Z3Vlc3Q6Z3Vlc3Q="
end
end

0 comments on commit 4d27898

Please sign in to comment.