maremma 4.4 → 4.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop.yml +653 -0
- data/Gemfile.lock +35 -16
- data/lib/maremma.rb +47 -45
- data/lib/maremma/version.rb +1 -1
- data/maremma.gemspec +23 -21
- data/spec/maremma_spec.rb +113 -113
- data/spec/spec_helper.rb +8 -8
- metadata +103 -62
data/Gemfile.lock
CHANGED
@@ -1,29 +1,30 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
maremma (4.
|
4
|
+
maremma (4.5)
|
5
5
|
activesupport (>= 4.2.5, < 6)
|
6
6
|
addressable (>= 2.3.6)
|
7
7
|
builder (~> 3.2, >= 3.2.2)
|
8
|
-
excon (~> 0.
|
9
|
-
faraday (
|
8
|
+
excon (~> 0.71.0)
|
9
|
+
faraday (= 0.17.0)
|
10
10
|
faraday-encoding (~> 0.0.4)
|
11
11
|
faraday_middleware (~> 0.13.1)
|
12
|
-
multi_json (~> 1.12)
|
13
12
|
nokogiri (~> 1.10.4)
|
14
13
|
oj (>= 2.8.3)
|
14
|
+
oj_mimic_json (~> 1.0, >= 1.0.1)
|
15
15
|
|
16
16
|
GEM
|
17
17
|
remote: https://rubygems.org/
|
18
18
|
specs:
|
19
|
-
activesupport (5.2.
|
19
|
+
activesupport (5.2.4.1)
|
20
20
|
concurrent-ruby (~> 1.0, >= 1.0.2)
|
21
21
|
i18n (>= 0.7, < 2)
|
22
22
|
minitest (~> 5.1)
|
23
23
|
tzinfo (~> 1.1)
|
24
24
|
addressable (2.7.0)
|
25
25
|
public_suffix (>= 2.0.2, < 5.0)
|
26
|
-
|
26
|
+
ast (2.4.0)
|
27
|
+
builder (3.2.4)
|
27
28
|
codeclimate-test-reporter (1.0.9)
|
28
29
|
simplecov (<= 0.13)
|
29
30
|
concurrent-ruby (1.1.5)
|
@@ -31,7 +32,7 @@ GEM
|
|
31
32
|
safe_yaml (~> 1.0.0)
|
32
33
|
diff-lcs (1.3)
|
33
34
|
docile (1.1.5)
|
34
|
-
excon (0.
|
35
|
+
excon (0.71.1)
|
35
36
|
faraday (0.17.0)
|
36
37
|
multipart-post (>= 1.2, < 3)
|
37
38
|
faraday-encoding (0.0.5)
|
@@ -41,19 +42,24 @@ GEM
|
|
41
42
|
hashdiff (1.0.0)
|
42
43
|
i18n (1.7.0)
|
43
44
|
concurrent-ruby (~> 1.0)
|
44
|
-
|
45
|
+
jaro_winkler (1.5.4)
|
46
|
+
json (2.3.0)
|
45
47
|
mini_portile2 (2.4.0)
|
46
|
-
minitest (5.
|
47
|
-
multi_json (1.13.1)
|
48
|
+
minitest (5.13.0)
|
48
49
|
multipart-post (2.1.1)
|
49
|
-
nokogiri (1.10.
|
50
|
+
nokogiri (1.10.7)
|
50
51
|
mini_portile2 (~> 2.4.0)
|
51
|
-
oj (3.
|
52
|
+
oj (3.10.0)
|
53
|
+
oj_mimic_json (1.0.1)
|
54
|
+
parallel (1.19.1)
|
55
|
+
parser (2.6.5.0)
|
56
|
+
ast (~> 2.4.0)
|
52
57
|
public_suffix (4.0.1)
|
53
|
-
rack (2.0.
|
58
|
+
rack (2.0.8)
|
54
59
|
rack-test (0.8.3)
|
55
60
|
rack (>= 1.0, < 3)
|
56
|
-
|
61
|
+
rainbow (3.0.0)
|
62
|
+
rake (13.0.1)
|
57
63
|
rspec (3.9.0)
|
58
64
|
rspec-core (~> 3.9.0)
|
59
65
|
rspec-expectations (~> 3.9.0)
|
@@ -67,6 +73,16 @@ GEM
|
|
67
73
|
diff-lcs (>= 1.2.0, < 2.0)
|
68
74
|
rspec-support (~> 3.9.0)
|
69
75
|
rspec-support (3.9.0)
|
76
|
+
rubocop (0.77.0)
|
77
|
+
jaro_winkler (~> 1.5.1)
|
78
|
+
parallel (~> 1.10)
|
79
|
+
parser (>= 2.6)
|
80
|
+
rainbow (>= 2.2.2, < 4.0)
|
81
|
+
ruby-progressbar (~> 1.7)
|
82
|
+
unicode-display_width (>= 1.4.0, < 1.7)
|
83
|
+
rubocop-performance (1.5.1)
|
84
|
+
rubocop (>= 0.71.0)
|
85
|
+
ruby-progressbar (1.10.1)
|
70
86
|
safe_yaml (1.0.5)
|
71
87
|
simplecov (0.13.0)
|
72
88
|
docile (~> 1.1.0)
|
@@ -76,6 +92,7 @@ GEM
|
|
76
92
|
thread_safe (0.3.6)
|
77
93
|
tzinfo (1.2.5)
|
78
94
|
thread_safe (~> 0.1)
|
95
|
+
unicode-display_width (1.6.0)
|
79
96
|
vcr (3.0.3)
|
80
97
|
webmock (3.7.6)
|
81
98
|
addressable (>= 2.3.6)
|
@@ -86,15 +103,17 @@ PLATFORMS
|
|
86
103
|
ruby
|
87
104
|
|
88
105
|
DEPENDENCIES
|
89
|
-
bundler (~>
|
106
|
+
bundler (~> 2.0)
|
90
107
|
codeclimate-test-reporter (~> 1.0)
|
91
108
|
maremma!
|
92
109
|
rack-test (~> 0)
|
93
110
|
rake
|
94
111
|
rspec (~> 3.4)
|
112
|
+
rubocop (~> 0.77.0)
|
113
|
+
rubocop-performance (~> 1.5, >= 1.5.1)
|
95
114
|
simplecov
|
96
115
|
vcr (~> 3.0, >= 3.0.3)
|
97
116
|
webmock (~> 3.0, >= 3.0.1)
|
98
117
|
|
99
118
|
BUNDLED WITH
|
100
|
-
|
119
|
+
2.0.2
|
data/lib/maremma.rb
CHANGED
@@ -1,16 +1,16 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require
|
4
|
-
require
|
5
|
-
require
|
6
|
-
require
|
7
|
-
require
|
8
|
-
require
|
9
|
-
require
|
10
|
-
require
|
11
|
-
require
|
12
|
-
require
|
13
|
-
require
|
3
|
+
require "active_support/all"
|
4
|
+
require "json"
|
5
|
+
require "nokogiri"
|
6
|
+
require "faraday"
|
7
|
+
require "faraday_middleware"
|
8
|
+
require "faraday/encoding"
|
9
|
+
require "excon"
|
10
|
+
require "uri"
|
11
|
+
require "addressable/uri"
|
12
|
+
require "maremma/xml_converter"
|
13
|
+
require "maremma/version"
|
14
14
|
|
15
15
|
module Maremma
|
16
16
|
DEFAULT_TIMEOUT = 60
|
@@ -26,7 +26,7 @@ module Maremma
|
|
26
26
|
NoMethodError,
|
27
27
|
TypeError]
|
28
28
|
|
29
|
-
# ActiveSupport::XmlMini.backend =
|
29
|
+
# ActiveSupport::XmlMini.backend = "Nokogiri"
|
30
30
|
|
31
31
|
def self.post(url, options={})
|
32
32
|
self.method(url, options.merge(method: "post"))
|
@@ -63,18 +63,20 @@ module Maremma
|
|
63
63
|
conn.options[:timeout] = options[:timeout] || DEFAULT_TIMEOUT
|
64
64
|
|
65
65
|
response = case options[:method]
|
66
|
-
when "get" then conn.get url, {}, options[:headers]
|
66
|
+
when "get" then conn.get url, {}, options[:headers] do |request|
|
67
|
+
request.headers["Host"] = URI.parse(url.to_s).host
|
68
|
+
end
|
67
69
|
when "post" then conn.post url, {}, options[:headers] do |request|
|
68
70
|
request.body = options[:data]
|
69
|
-
request.headers[
|
71
|
+
request.headers["Host"] = URI.parse(url.to_s).host
|
70
72
|
end
|
71
73
|
when "put" then conn.put url, {}, options[:headers] do |request|
|
72
74
|
request.body = options[:data]
|
73
|
-
request.headers[
|
75
|
+
request.headers["Host"] = URI.parse(url.to_s).host
|
74
76
|
end
|
75
77
|
when "patch" then conn.patch url, {}, options[:headers] do |request|
|
76
78
|
request.body = options[:data]
|
77
|
-
request.headers[
|
79
|
+
request.headers["Host"] = URI.parse(url.to_s).host
|
78
80
|
end
|
79
81
|
when "delete" then conn.delete url, {}, options[:headers]
|
80
82
|
when "head" then conn.head url, {}, options[:headers]
|
@@ -82,7 +84,7 @@ module Maremma
|
|
82
84
|
|
83
85
|
# return error if we are close to the rate limit, if supported in headers
|
84
86
|
if get_rate_limit_remaining(response.headers) < 3
|
85
|
-
return OpenStruct.new(body: { "errors" => [{
|
87
|
+
return OpenStruct.new(body: { "errors" => [{ "status" => 429, "title" => "Too many requests" }] },
|
86
88
|
headers: response.headers,
|
87
89
|
status: response.status)
|
88
90
|
end
|
@@ -115,12 +117,12 @@ module Maremma
|
|
115
117
|
Faraday.new do |c|
|
116
118
|
c.ssl.verify = false if options[:ssl_self_signed]
|
117
119
|
c.options.params_encoder = Faraday::FlatParamsEncoder
|
118
|
-
c.headers[
|
119
|
-
c.headers[
|
120
|
-
c.headers[
|
120
|
+
c.headers["Content-type"] = options[:headers]["Content-type"] if options[:headers]["Content-type"].present?
|
121
|
+
c.headers["Accept"] = options[:headers]["Accept"]
|
122
|
+
c.headers["User-Agent"] = options[:headers]["User-Agent"]
|
121
123
|
c.use FaradayMiddleware::FollowRedirects, limit: limit, cookie: :all if limit > 0
|
122
124
|
c.request :multipart
|
123
|
-
c.request :json if options[:headers][
|
125
|
+
c.request :json if options[:headers]["Accept"] == "application/json"
|
124
126
|
c.response :encoding
|
125
127
|
c.adapter :excon
|
126
128
|
end
|
@@ -132,30 +134,30 @@ module Maremma
|
|
132
134
|
end
|
133
135
|
|
134
136
|
def self.set_request_headers(url, options={})
|
135
|
-
header_options = { "html" =>
|
136
|
-
"xml" =>
|
137
|
-
"json" =>
|
137
|
+
header_options = { "html" => "text/html;charset=UTF-8",
|
138
|
+
"xml" => "application/xml;charset=UTF-8",
|
139
|
+
"json" => "application/json;charset=UTF-8" }
|
138
140
|
|
139
141
|
headers = options[:headers] ||= {}
|
140
142
|
|
141
143
|
# set useragent
|
142
|
-
headers[
|
144
|
+
headers["User-Agent"] = ENV["USER_AGENT"] || "Mozilla/5.0 (compatible; Maremma/#{Maremma::VERSION}; +https://github.com/datacite/maremma)"
|
143
145
|
|
144
146
|
# set host, needed for some services behind proxy
|
145
|
-
#headers[
|
147
|
+
#headers["Host"] = URI.parse(url).host #if options[:host]
|
146
148
|
|
147
149
|
# set Content-Type
|
148
|
-
headers[
|
150
|
+
headers["Content-type"] = header_options.fetch(options[:content_type], options[:content_type]) if options[:content_type].present?
|
149
151
|
|
150
152
|
if options[:accept].present?
|
151
|
-
headers[
|
153
|
+
headers["Accept"] = header_options.fetch(options[:accept], options[:accept])
|
152
154
|
else
|
153
155
|
# accept all content
|
154
|
-
headers[
|
156
|
+
headers["Accept"] ||= "text/html,application/json,application/xml;q=0.9, text/plain;q=0.8,image/png,*/*;q=0.5"
|
155
157
|
end
|
156
158
|
|
157
159
|
if options[:bearer].present?
|
158
|
-
headers[
|
160
|
+
headers["Authorization"] = "Bearer #{options[:bearer]}"
|
159
161
|
elsif options[:token].present?
|
160
162
|
headers["Authorization"] = "Token token=#{options[:token]}"
|
161
163
|
elsif options[:github_token].present?
|
@@ -171,18 +173,18 @@ module Maremma
|
|
171
173
|
|
172
174
|
def self.rescue_faraday_error(error, response)
|
173
175
|
if error.is_a?(Faraday::ResourceNotFound)
|
174
|
-
{
|
176
|
+
{ "errors" => [{ "status" => 404, "title" => "Not found" }] }
|
175
177
|
elsif error.message == "the server responded with status 401" || error.try(:response) && error.response[:status] == 401
|
176
|
-
{
|
178
|
+
{ "errors" => [{ "status" => 401, "title" =>"Unauthorized" }] }
|
177
179
|
elsif error.is_a?(Faraday::ConnectionFailed)
|
178
|
-
{
|
180
|
+
{ "errors" => [{ "status" => 403, "title" => parse_error_response(error.message) }] }
|
179
181
|
|
180
182
|
elsif error.is_a?(Faraday::TimeoutError) || (error.try(:response) && error.response[:status] == 408)
|
181
|
-
{
|
183
|
+
{ "errors" => [{ "status" => 408, "title" =>"Request timeout" }] }
|
182
184
|
else
|
183
185
|
status = response ? response.status : 400
|
184
186
|
title = response ? parse_error_response(response.body) : parse_error_response(error.message)
|
185
|
-
{
|
187
|
+
{ "errors" => [{ "status" => status, "title" => title }] }
|
186
188
|
end
|
187
189
|
end
|
188
190
|
|
@@ -193,9 +195,9 @@ module Maremma
|
|
193
195
|
|
194
196
|
if string.blank?
|
195
197
|
{ "data" => nil }
|
196
|
-
elsif string.is_a?(Hash) && string[
|
197
|
-
{ "data" => string[
|
198
|
-
elsif string.is_a?(Hash) && string[
|
198
|
+
elsif string.is_a?(Hash) && string["hash"]
|
199
|
+
{ "data" => string["hash"] }
|
200
|
+
elsif string.is_a?(Hash) && string["data"]
|
199
201
|
string
|
200
202
|
else
|
201
203
|
{ "data" => string }
|
@@ -205,19 +207,19 @@ module Maremma
|
|
205
207
|
def self.parse_error_response(string)
|
206
208
|
string = parse_response(string)
|
207
209
|
|
208
|
-
string = string[
|
210
|
+
string = string["hash"] if string.is_a?(Hash) && string["hash"]
|
209
211
|
|
210
|
-
if string.is_a?(Hash) && string[
|
211
|
-
string[
|
212
|
-
elsif string.is_a?(Hash) && string[
|
213
|
-
string.dig(
|
212
|
+
if string.is_a?(Hash) && string["error"]
|
213
|
+
string["error"]
|
214
|
+
elsif string.is_a?(Hash) && string["errors"]
|
215
|
+
string.dig("errors", 0, "title")
|
214
216
|
else
|
215
217
|
string
|
216
218
|
end
|
217
219
|
end
|
218
220
|
|
219
221
|
def self.parse_response(string, options={})
|
220
|
-
string = string.dup.force_encoding(
|
222
|
+
string = string.dup.force_encoding("UTF-8")
|
221
223
|
return string if options[:raw]
|
222
224
|
|
223
225
|
from_json(string) || from_xml(string) || from_string(string)
|
@@ -238,7 +240,7 @@ module Maremma
|
|
238
240
|
string.gsub!("</#{tag}>", "</#{tag}>")
|
239
241
|
end
|
240
242
|
|
241
|
-
if Nokogiri::XML(string, nil,
|
243
|
+
if Nokogiri::XML(string, nil, "UTF-8").errors.empty?
|
242
244
|
Hash.from_xml(string)
|
243
245
|
else
|
244
246
|
nil
|
data/lib/maremma/version.rb
CHANGED
data/maremma.gemspec
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
require "date"
|
2
|
-
require File.expand_path("
|
2
|
+
require File.expand_path("lib/maremma/version", __dir__)
|
3
3
|
|
4
4
|
Gem::Specification.new do |s|
|
5
5
|
s.authors = "Martin Fenner"
|
@@ -12,29 +12,31 @@ Gem::Specification.new do |s|
|
|
12
12
|
s.require_paths = ["lib"]
|
13
13
|
s.version = Maremma::VERSION
|
14
14
|
s.extra_rdoc_files = ["README.md"]
|
15
|
-
s.license =
|
15
|
+
s.license = "MIT"
|
16
16
|
|
17
17
|
s.files = `git ls-files`.split("\n")
|
18
18
|
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
19
|
-
s.require_paths = ["lib"]
|
20
19
|
|
21
20
|
# Declary dependencies here, rather than in the Gemfile
|
22
|
-
s.add_dependency
|
23
|
-
s.add_dependency
|
24
|
-
s.add_dependency
|
25
|
-
s.add_dependency
|
26
|
-
s.add_dependency
|
27
|
-
s.add_dependency
|
28
|
-
s.add_dependency
|
29
|
-
s.add_dependency
|
30
|
-
s.add_dependency
|
31
|
-
s.add_dependency
|
32
|
-
s.add_development_dependency
|
33
|
-
s.add_development_dependency
|
34
|
-
s.add_development_dependency
|
35
|
-
s.add_development_dependency
|
36
|
-
s.add_development_dependency
|
37
|
-
s.add_development_dependency
|
38
|
-
s.add_development_dependency
|
39
|
-
s.add_development_dependency
|
21
|
+
s.add_dependency "activesupport", ">= 4.2.5", "< 6"
|
22
|
+
s.add_dependency "addressable", ">= 2.3.6"
|
23
|
+
s.add_dependency "builder", "~> 3.2", ">= 3.2.2"
|
24
|
+
s.add_dependency "excon", "~> 0.71.0"
|
25
|
+
s.add_dependency "faraday", "0.17.0"
|
26
|
+
s.add_dependency "faraday-encoding", "~> 0.0.4"
|
27
|
+
s.add_dependency "faraday_middleware", "~> 0.13.1"
|
28
|
+
s.add_dependency "nokogiri", "~> 1.10.4"
|
29
|
+
s.add_dependency "oj", ">= 2.8.3"
|
30
|
+
s.add_dependency "oj_mimic_json", "~> 1.0", ">= 1.0.1"
|
31
|
+
s.add_development_dependency "bundler", "~> 2.0"
|
32
|
+
s.add_development_dependency "codeclimate-test-reporter", "~> 1.0"
|
33
|
+
s.add_development_dependency "rack-test", "~> 0"
|
34
|
+
s.add_development_dependency "rake"
|
35
|
+
s.add_development_dependency "rspec", "~> 3.4"
|
36
|
+
s.add_development_dependency "rubocop", "~> 0.77.0"
|
37
|
+
s.add_development_dependency "rubocop-performance", "~> 1.5", ">= 1.5.1"
|
38
|
+
s.add_development_dependency "simplecov"
|
39
|
+
s.add_development_dependency "vcr", "~> 3.0", ">= 3.0.3"
|
40
|
+
s.add_development_dependency "webmock", "~> 3.0", ">= 3.0.1"
|
41
|
+
|
40
42
|
end
|
data/spec/maremma_spec.rb
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require
|
3
|
+
require "spec_helper"
|
4
4
|
|
5
5
|
describe Maremma do
|
6
6
|
subject { Maremma }
|
@@ -20,7 +20,7 @@ describe Maremma do
|
|
20
20
|
|
21
21
|
it "get xml" do
|
22
22
|
stub = stub_request(:get, url).to_return(:body => data.to_xml, :status => 200, :headers => { "Content-Type" => "application/xml" })
|
23
|
-
response = subject.get(url, accept:
|
23
|
+
response = subject.get(url, accept: "xml")
|
24
24
|
expect(response.body).to eq("data"=>data)
|
25
25
|
expect(response.headers).to eq("Content-Type"=>"application/xml")
|
26
26
|
expect(stub).to have_been_requested
|
@@ -28,7 +28,7 @@ describe Maremma do
|
|
28
28
|
|
29
29
|
it "get html" do
|
30
30
|
stub = stub_request(:get, url).to_return(:body => data.to_s, :status => 200, :headers => { "Content-Type" => "text/html" })
|
31
|
-
response = subject.get(url, accept:
|
31
|
+
response = subject.get(url, accept: "html")
|
32
32
|
expect(response.body).to eq("data" => data.to_s)
|
33
33
|
expect(response.headers).to eq("Content-Type"=>"text/html")
|
34
34
|
expect(stub).to have_been_requested
|
@@ -39,9 +39,9 @@ describe Maremma do
|
|
39
39
|
fl: "doi,title,description,publisher,publicationYear,resourceType,resourceTypeGeneral,rightsURI,datacentre_symbol,xml,minted,updated",
|
40
40
|
fq: %w(has_metadata:true is_active:true),
|
41
41
|
facet: "true",
|
42
|
-
|
43
|
-
|
44
|
-
|
42
|
+
"facet.field" => %w(resourceType_facet publicationYear datacentre_facet),
|
43
|
+
"facet.limit" => 10,
|
44
|
+
"f.resourceType_facet.facet.limit" => 15,
|
45
45
|
wt: "json" }.compact
|
46
46
|
url = "https://search.datacite.org/api?" + URI.encode_www_form(params)
|
47
47
|
expect(url).to eq("https://search.datacite.org/api?q=*%3A*&fl=doi%2Ctitle%2Cdescription%2Cpublisher%2CpublicationYear%2CresourceType%2CresourceTypeGeneral%2CrightsURI%2Cdatacentre_symbol%2Cxml%2Cminted%2Cupdated&fq=has_metadata%3Atrue&fq=is_active%3Atrue&facet=true&facet.field=resourceType_facet&facet.field=publicationYear&facet.field=datacentre_facet&facet.limit=10&f.resourceType_facet.facet.limit=15&wt=json")
|
@@ -63,7 +63,7 @@ describe Maremma do
|
|
63
63
|
|
64
64
|
it "get xml raw" do
|
65
65
|
stub = stub_request(:get, url).to_return(:body => data.to_xml, :status => 200, :headers => { "Content-Type" => "application/xml" })
|
66
|
-
response = subject.get(url, accept:
|
66
|
+
response = subject.get(url, accept: "xml", raw: true)
|
67
67
|
expect(response.body).to eq("data"=>data.to_xml)
|
68
68
|
expect(response.headers).to eq("Content-Type"=>"application/xml")
|
69
69
|
expect(stub).to have_been_requested
|
@@ -73,7 +73,7 @@ describe Maremma do
|
|
73
73
|
context "head" do
|
74
74
|
it "head html" do
|
75
75
|
stub = stub_request(:head, url).to_return(:status => 200, :headers => { "Content-Type" => "text/html" })
|
76
|
-
response = subject.head(url, accept:
|
76
|
+
response = subject.head(url, accept: "html")
|
77
77
|
expect(response.body).to be_nil
|
78
78
|
expect(response.headers).to eq("Content-Type"=>"text/html")
|
79
79
|
expect(stub).to have_been_requested
|
@@ -83,13 +83,13 @@ describe Maremma do
|
|
83
83
|
context "post" do
|
84
84
|
it "post json" do
|
85
85
|
stub = stub_request(:post, url).with(:body => post_data.to_json).to_return(:body => data.to_json, :status => 200, :headers => { "Content-Type" => "application/json" })
|
86
|
-
subject.post(url, content_type:
|
86
|
+
subject.post(url, content_type: "json", data: post_data.to_json) { |response| expect(response.body).to eq(2) }
|
87
87
|
expect(stub).to have_been_requested
|
88
88
|
end
|
89
89
|
|
90
90
|
it "post xml" do
|
91
91
|
stub = stub_request(:post, url).with(:body => post_data.to_xml).to_return(:body => data.to_xml, :status => 200, :headers => { "Content-Type" => "text/html" })
|
92
|
-
subject.post(url, content_type:
|
92
|
+
subject.post(url, content_type: "xml", data: post_data.to_xml) { |response| expect(Hash.from_xml(response.body.to_s)["hash"]).to eq(data) }
|
93
93
|
expect(stub).to have_been_requested
|
94
94
|
end
|
95
95
|
end
|
@@ -97,13 +97,13 @@ describe Maremma do
|
|
97
97
|
context "put" do
|
98
98
|
it "put json" do
|
99
99
|
stub = stub_request(:put, url).with(:body => post_data.to_json).to_return(:body => data.to_json, :status => 200, :headers => { "Content-Type" => "application/json" })
|
100
|
-
subject.put(url, content_type:
|
100
|
+
subject.put(url, content_type: "json", data: post_data.to_json) { |response| expect(JSON.parse(response.body.to_s)).to eq(data) }
|
101
101
|
expect(stub).to have_been_requested
|
102
102
|
end
|
103
103
|
|
104
104
|
it "put xml" do
|
105
105
|
stub = stub_request(:put, url).with(:body => post_data.to_xml).to_return(:body => data.to_xml, :status => 200, :headers => { "Content-Type" => "text/html" })
|
106
|
-
subject.put(url, content_type:
|
106
|
+
subject.put(url, content_type: "xml", data: post_data.to_xml) { |response| expect(Hash.from_xml(response.body.to_s)["hash"]).to eq(data) }
|
107
107
|
expect(stub).to have_been_requested
|
108
108
|
end
|
109
109
|
end
|
@@ -111,13 +111,13 @@ describe Maremma do
|
|
111
111
|
context "patch" do
|
112
112
|
it "patch json" do
|
113
113
|
stub = stub_request(:patch, url).with(:body => post_data.to_json).to_return(:body => data.to_json, :status => 200, :headers => { "Content-Type" => "application/json" })
|
114
|
-
subject.patch(url, content_type:
|
114
|
+
subject.patch(url, content_type: "json", data: post_data.to_json) { |response| expect(JSON.parse(response.body.to_s)).to eq(data) }
|
115
115
|
expect(stub).to have_been_requested
|
116
116
|
end
|
117
117
|
|
118
118
|
it "patch xml" do
|
119
119
|
stub = stub_request(:patch, url).with(:body => post_data.to_xml).to_return(:body => data.to_xml, :status => 200, :headers => { "Content-Type" => "text/html" })
|
120
|
-
subject.patch(url, content_type:
|
120
|
+
subject.patch(url, content_type: "xml", data: post_data.to_xml) { |response| expect(Hash.from_xml(response.body.to_s)["hash"]).to eq(data) }
|
121
121
|
expect(stub).to have_been_requested
|
122
122
|
end
|
123
123
|
end
|
@@ -133,7 +133,7 @@ describe Maremma do
|
|
133
133
|
|
134
134
|
it "get xml" do
|
135
135
|
stub = stub_request(:get, url).to_return(:body => nil, :status => 200, :headers => { "Content-Type" => "application/xml" })
|
136
|
-
response = subject.get(url, accept:
|
136
|
+
response = subject.get(url, accept: "xml")
|
137
137
|
expect(response.body).to eq("data"=>nil)
|
138
138
|
expect(response.headers).to eq("Content-Type"=>"application/xml")
|
139
139
|
expect(stub).to have_been_requested
|
@@ -141,7 +141,7 @@ describe Maremma do
|
|
141
141
|
|
142
142
|
it "get html" do
|
143
143
|
stub = stub_request(:get, url).to_return(:body => nil, :status => 200, :headers => { "Content-Type" => "text/html" })
|
144
|
-
response = subject.get(url, accept:
|
144
|
+
response = subject.get(url, accept: "html")
|
145
145
|
expect(response.body).to eq("data" => nil)
|
146
146
|
expect(response.headers).to eq("Content-Type"=>"text/html")
|
147
147
|
expect(stub).to have_been_requested
|
@@ -149,13 +149,13 @@ describe Maremma do
|
|
149
149
|
|
150
150
|
it "post xml" do
|
151
151
|
stub = stub_request(:post, url).with(:body => post_data.to_xml).to_return(:body => nil, :status => 200, :headers => { "Content-Type" => "application/xml" })
|
152
|
-
subject.post(url, accept:
|
152
|
+
subject.post(url, accept: "xml", data: post_data.to_xml) { |response| expect(response.body).to eq("data" => nil) }
|
153
153
|
expect(stub).to have_been_requested
|
154
154
|
end
|
155
155
|
|
156
156
|
it "put xml" do
|
157
157
|
stub = stub_request(:put, url).with(:body => post_data.to_xml).to_return(:body => nil, :status => 200, :headers => { "Content-Type" => "application/xml" })
|
158
|
-
subject.put(url, accept:
|
158
|
+
subject.put(url, accept: "xml", data: post_data.to_xml) { |response| expect(response.body).to eq("data" => nil) }
|
159
159
|
expect(stub).to have_been_requested
|
160
160
|
end
|
161
161
|
end
|
@@ -165,41 +165,41 @@ describe Maremma do
|
|
165
165
|
|
166
166
|
it "get json" do
|
167
167
|
stub = stub_request(:get, url).to_return(:body => error.to_json, :status => [404], :headers => { "Content-Type" => "application/json" })
|
168
|
-
response = subject.get(url, accept:
|
168
|
+
response = subject.get(url, accept: "json")
|
169
169
|
expect(response.body).to eq(error)
|
170
170
|
expect(stub).to have_been_requested
|
171
171
|
end
|
172
172
|
|
173
173
|
it "get xml" do
|
174
174
|
stub = stub_request(:get, url).to_return(:body => error.to_xml, :status => [404], :headers => { "Content-Type" => "application/xml" })
|
175
|
-
response = subject.get(url, accept:
|
175
|
+
response = subject.get(url, accept: "xml")
|
176
176
|
expect(response.body).to eq(error)
|
177
177
|
expect(stub).to have_been_requested
|
178
178
|
end
|
179
179
|
|
180
180
|
it "get html" do
|
181
181
|
stub = stub_request(:get, url).to_return(:body => error.to_s, :status => [404], :headers => { "Content-Type" => "text/html" })
|
182
|
-
response = subject.get(url, accept:
|
182
|
+
response = subject.get(url, accept: "html")
|
183
183
|
expect(response.body).to eq(error)
|
184
184
|
expect(stub).to have_been_requested
|
185
185
|
end
|
186
186
|
|
187
187
|
it "head html" do
|
188
188
|
stub = stub_request(:head, url).to_return(:body => error.to_s, :status => [404], :headers => { "Content-Type" => "text/html" })
|
189
|
-
response = subject.head(url, accept:
|
189
|
+
response = subject.head(url, accept: "html")
|
190
190
|
expect(response.status).to eq(404)
|
191
191
|
expect(stub).to have_been_requested
|
192
192
|
end
|
193
193
|
|
194
194
|
it "post xml" do
|
195
195
|
stub = stub_request(:post, url).with(:body => post_data.to_xml).to_return(:body => error.to_xml, :status => [404], :headers => { "Content-Type" => "application/xml" })
|
196
|
-
subject.post(url, accept:
|
196
|
+
subject.post(url, accept: "xml", data: post_data.to_xml) { |response| expect(Hash.from_xml(response.to_s)["hash"]).to eq(error) }
|
197
197
|
expect(stub).to have_been_requested
|
198
198
|
end
|
199
199
|
|
200
200
|
it "put xml" do
|
201
201
|
stub = stub_request(:put, url).with(:body => post_data.to_xml).to_return(:body => error.to_xml, :status => [404], :headers => { "Content-Type" => "application/xml" })
|
202
|
-
subject.put(url, accept:
|
202
|
+
subject.put(url, accept: "xml", data: post_data.to_xml) { |response| expect(Hash.from_xml(response.to_s)["hash"]).to eq(error) }
|
203
203
|
expect(stub).to have_been_requested
|
204
204
|
end
|
205
205
|
end
|
@@ -209,41 +209,41 @@ describe Maremma do
|
|
209
209
|
|
210
210
|
it "get json" do
|
211
211
|
stub = stub_request(:get, url).to_return(:body => error.to_json, :status => [405], :headers => { "Content-Type" => "application/json" })
|
212
|
-
response = subject.get(url, accept:
|
212
|
+
response = subject.get(url, accept: "json")
|
213
213
|
expect(response.body).to eq(error)
|
214
214
|
expect(stub).to have_been_requested
|
215
215
|
end
|
216
216
|
|
217
217
|
it "get xml" do
|
218
218
|
stub = stub_request(:get, url).to_return(:body => error.to_xml, :status => [405], :headers => { "Content-Type" => "application/xml" })
|
219
|
-
response = subject.get(url, accept:
|
219
|
+
response = subject.get(url, accept: "xml")
|
220
220
|
expect(response.body).to eq(error)
|
221
221
|
expect(stub).to have_been_requested
|
222
222
|
end
|
223
223
|
|
224
224
|
it "get html" do
|
225
225
|
stub = stub_request(:get, url).to_return(:body => error.dig("errors", 0, "title"), :status => [405], :headers => { "Content-Type" => "text/html" })
|
226
|
-
response = subject.get(url, accept:
|
226
|
+
response = subject.get(url, accept: "html")
|
227
227
|
expect(response.body).to eq(error)
|
228
228
|
expect(stub).to have_been_requested
|
229
229
|
end
|
230
230
|
|
231
231
|
it "head html" do
|
232
232
|
stub = stub_request(:head, url).to_return(:body => error.to_s, :status => [405], :headers => { "Content-Type" => "text/html" })
|
233
|
-
response = subject.head(url, accept:
|
233
|
+
response = subject.head(url, accept: "html")
|
234
234
|
expect(response.status).to eq(405)
|
235
235
|
expect(stub).to have_been_requested
|
236
236
|
end
|
237
237
|
|
238
238
|
it "post xml" do
|
239
239
|
stub = stub_request(:post, url).with(:body => post_data.to_xml).to_return(:body => error.to_xml, :status => [405], :headers => { "Content-Type" => "application/xml" })
|
240
|
-
subject.post(url, accept:
|
240
|
+
subject.post(url, accept: "xml", data: post_data.to_xml) { |response| expect(Hash.from_xml(response.to_s)["hash"]).to eq(error) }
|
241
241
|
expect(stub).to have_been_requested
|
242
242
|
end
|
243
243
|
|
244
244
|
it "put xml" do
|
245
245
|
stub = stub_request(:put, url).with(:body => post_data.to_xml).to_return(:body => error.to_xml, :status => [405], :headers => { "Content-Type" => "application/xml" })
|
246
|
-
subject.put(url, accept:
|
246
|
+
subject.put(url, accept: "xml", data: post_data.to_xml) { |response| expect(Hash.from_xml(response.to_s)["hash"]).to eq(error) }
|
247
247
|
expect(stub).to have_been_requested
|
248
248
|
end
|
249
249
|
end
|
@@ -258,34 +258,34 @@ describe Maremma do
|
|
258
258
|
|
259
259
|
it "get xml" do
|
260
260
|
stub = stub_request(:get, url).to_return(:status => [408])
|
261
|
-
response = subject.get(url, accept:
|
261
|
+
response = subject.get(url, accept: "xml")
|
262
262
|
expect(response.body).to eq("errors"=>[{"status"=>408, "title"=>"Request timeout"}])
|
263
263
|
expect(stub).to have_been_requested
|
264
264
|
end
|
265
265
|
|
266
266
|
it "get html" do
|
267
267
|
stub = stub_request(:get, url).to_return(:status => [408])
|
268
|
-
response = subject.get(url, accept:
|
268
|
+
response = subject.get(url, accept: "html")
|
269
269
|
expect(response.body).to eq("errors"=>[{"status"=>408, "title"=>"Request timeout"}])
|
270
270
|
expect(stub).to have_been_requested
|
271
271
|
end
|
272
272
|
|
273
273
|
it "head html" do
|
274
274
|
stub = stub_request(:head, url).to_return(:status => [408])
|
275
|
-
response = subject.head(url, accept:
|
275
|
+
response = subject.head(url, accept: "html")
|
276
276
|
expect(response.status).to eq(408)
|
277
277
|
expect(stub).to have_been_requested
|
278
278
|
end
|
279
279
|
|
280
280
|
it "post xml" do
|
281
281
|
stub = stub_request(:post, url).with(:body => post_data.to_xml).to_return(:status => [408])
|
282
|
-
subject.post(url, accept:
|
282
|
+
subject.post(url, accept: "xml", data: post_data.to_xml) { |response| expect(response.body).to be_nil }
|
283
283
|
expect(stub).to have_been_requested
|
284
284
|
end
|
285
285
|
|
286
286
|
it "put xml" do
|
287
287
|
stub = stub_request(:put, url).with(:body => post_data.to_xml).to_return(:status => [408])
|
288
|
-
subject.put(url, accept:
|
288
|
+
subject.put(url, accept: "xml", data: post_data.to_xml) { |response| expect(response.body).to be_nil }
|
289
289
|
expect(stub).to have_been_requested
|
290
290
|
end
|
291
291
|
end
|
@@ -311,27 +311,27 @@ describe Maremma do
|
|
311
311
|
|
312
312
|
it "get xml" do
|
313
313
|
stub = stub_request(:get, url).to_raise(Faraday::ConnectionFailed.new("Connection refused - connect(2)"))
|
314
|
-
response = subject.get(url, accept:
|
314
|
+
response = subject.get(url, accept: "xml")
|
315
315
|
expect(response.body).to eq("errors"=>[{"status"=>403, "title"=>"Connection refused - connect(2)"}])
|
316
316
|
expect(stub).to have_been_requested
|
317
317
|
end
|
318
318
|
|
319
319
|
it "get html" do
|
320
320
|
stub = stub_request(:get, url).to_raise(Faraday::ConnectionFailed.new("Connection refused - connect(2)"))
|
321
|
-
response = subject.get(url, accept:
|
321
|
+
response = subject.get(url, accept: "html")
|
322
322
|
expect(response.body).to eq("errors"=>[{"status"=>403, "title"=>"Connection refused - connect(2)"}])
|
323
323
|
expect(stub).to have_been_requested
|
324
324
|
end
|
325
325
|
|
326
326
|
it "post xml" do
|
327
327
|
stub = stub_request(:post, url).with(:body => post_data.to_xml).to_raise(Faraday::ConnectionFailed.new("Connection refused - connect(2)"))
|
328
|
-
subject.post(url, accept:
|
328
|
+
subject.post(url, accept: "xml", data: post_data.to_xml) { |response| expect(response.body).to be_nil }
|
329
329
|
expect(stub).to have_been_requested
|
330
330
|
end
|
331
331
|
|
332
332
|
it "put xml" do
|
333
333
|
stub = stub_request(:put, url).with(:body => post_data.to_xml).to_raise(Faraday::ConnectionFailed.new("Connection refused - connect(2)"))
|
334
|
-
subject.put(url, accept:
|
334
|
+
subject.put(url, accept: "xml", data: post_data.to_xml) { |response| expect(response.body).to be_nil }
|
335
335
|
expect(stub).to have_been_requested
|
336
336
|
end
|
337
337
|
end
|
@@ -346,21 +346,21 @@ describe Maremma do
|
|
346
346
|
|
347
347
|
it "get xml" do
|
348
348
|
stub = stub_request(:get, url).to_raise(Faraday::Error::ClientError.new("the server responded with status 401"))
|
349
|
-
response = subject.get(url, accept:
|
349
|
+
response = subject.get(url, accept: "xml")
|
350
350
|
expect(response.body).to eq("errors"=>[{"status"=>401, "title"=>"Unauthorized"}])
|
351
351
|
expect(stub).to have_been_requested
|
352
352
|
end
|
353
353
|
|
354
354
|
it "get html" do
|
355
355
|
stub = stub_request(:get, url).to_raise(Faraday::Error::ClientError.new("the server responded with status 401"))
|
356
|
-
response = subject.get(url, accept:
|
356
|
+
response = subject.get(url, accept: "html")
|
357
357
|
expect(response.body).to eq("errors"=>[{"status"=>401, "title"=>"Unauthorized"}])
|
358
358
|
expect(stub).to have_been_requested
|
359
359
|
end
|
360
360
|
|
361
361
|
it "post xml" do
|
362
362
|
stub = stub_request(:post, url).with(:body => post_data.to_xml).to_raise(Faraday::Error::ClientError.new("the server responded with status 401"))
|
363
|
-
subject.post(url, accept:
|
363
|
+
subject.post(url, accept: "xml", data: post_data.to_xml) do |response|
|
364
364
|
expect(response.body).to be_nil
|
365
365
|
expect(response.status).to eq("errors"=>[{"status"=>401, "title"=>"Unauthorized"}])
|
366
366
|
end
|
@@ -369,7 +369,7 @@ describe Maremma do
|
|
369
369
|
|
370
370
|
it "put xml" do
|
371
371
|
stub = stub_request(:put, url).with(:body => post_data.to_xml).to_raise(Faraday::Error::ClientError.new("the server responded with status 401"))
|
372
|
-
subject.put(url, accept:
|
372
|
+
subject.put(url, accept: "xml", data: post_data.to_xml) do |response|
|
373
373
|
expect(response.body).to be_nil
|
374
374
|
expect(response.status).to eq("errors"=>[{"status"=>401, "title"=>"Unauthorized"}])
|
375
375
|
end
|
@@ -387,64 +387,64 @@ describe Maremma do
|
|
387
387
|
|
388
388
|
it "get xml" do
|
389
389
|
stub = stub_request(:get, url).to_timeout
|
390
|
-
response = subject.get(url, accept:
|
390
|
+
response = subject.get(url, accept: "xml")
|
391
391
|
expect(response.body).to eq("errors"=>[{"status"=>408, "title"=>"Request timeout"}])
|
392
392
|
expect(stub).to have_been_requested
|
393
393
|
end
|
394
394
|
|
395
395
|
it "get html" do
|
396
396
|
stub = stub_request(:get, url).to_timeout
|
397
|
-
response = subject.get(url, accept:
|
397
|
+
response = subject.get(url, accept: "html")
|
398
398
|
expect(response.body).to eq("errors"=>[{"status"=>408, "title"=>"Request timeout"}])
|
399
399
|
expect(stub).to have_been_requested
|
400
400
|
end
|
401
401
|
|
402
402
|
it "post xml" do
|
403
403
|
stub = stub_request(:post, url).with(:body => post_data.to_xml).to_timeout
|
404
|
-
subject.post(url, accept:
|
404
|
+
subject.post(url, accept: "xml", data: post_data.to_xml) { |response| expect(response.body).to be_nil }
|
405
405
|
expect(stub).to have_been_requested
|
406
406
|
end
|
407
407
|
|
408
408
|
it "put xml" do
|
409
409
|
stub = stub_request(:put, url).with(:body => post_data.to_xml).to_timeout
|
410
|
-
subject.put(url, accept:
|
410
|
+
subject.put(url, accept: "xml", data: post_data.to_xml) { |response| expect(response.body).to be_nil }
|
411
411
|
expect(stub).to have_been_requested
|
412
412
|
end
|
413
413
|
end
|
414
414
|
|
415
415
|
context "rate limit exceeded" do
|
416
416
|
it "get json" do
|
417
|
-
stub = stub_request(:get, url).to_return(status: 200, headers: {
|
417
|
+
stub = stub_request(:get, url).to_return(status: 200, headers: { "X-Rate-Limit-Remaining" => 2 })
|
418
418
|
response = subject.get(url)
|
419
419
|
expect(response.body).to eq("errors"=>[{"status"=>429, "title"=>"Too many requests"}])
|
420
420
|
expect(stub).to have_been_requested
|
421
421
|
end
|
422
422
|
|
423
423
|
it "get xml" do
|
424
|
-
stub = stub_request(:get, url).to_return(status: 200, headers: {
|
425
|
-
response = subject.get(url, accept:
|
424
|
+
stub = stub_request(:get, url).to_return(status: 200, headers: { "X-Rate-Limit-Remaining" => 2 })
|
425
|
+
response = subject.get(url, accept: "xml")
|
426
426
|
expect(response.body).to eq("errors"=>[{"status"=>429, "title"=>"Too many requests"}])
|
427
427
|
expect(stub).to have_been_requested
|
428
428
|
end
|
429
429
|
|
430
430
|
it "get html" do
|
431
|
-
stub = stub_request(:get, url).to_return(status: 200, headers: {
|
432
|
-
response = subject.get(url, accept:
|
431
|
+
stub = stub_request(:get, url).to_return(status: 200, headers: { "X-Rate-Limit-Remaining" => 2 })
|
432
|
+
response = subject.get(url, accept: "html")
|
433
433
|
expect(response.body).to eq("errors"=>[{"status"=>429, "title"=>"Too many requests"}])
|
434
434
|
expect(stub).to have_been_requested
|
435
435
|
end
|
436
436
|
|
437
437
|
it "post xml" do
|
438
438
|
stub = stub_request(:post, url).with(:body => post_data.to_xml)
|
439
|
-
.to_return(status: 200, headers: {
|
440
|
-
subject.post(url, accept:
|
439
|
+
.to_return(status: 200, headers: { "X-Rate-Limit-Remaining" => 3 })
|
440
|
+
subject.post(url, accept: "xml", data: post_data.to_xml) { |response| expect(response.body).to be_nil }
|
441
441
|
expect(stub).to have_been_requested
|
442
442
|
end
|
443
443
|
|
444
444
|
it "put xml" do
|
445
445
|
stub = stub_request(:put, url).with(:body => post_data.to_xml)
|
446
|
-
.to_return(status: 200, headers: {
|
447
|
-
subject.put(url, accept:
|
446
|
+
.to_return(status: 200, headers: { "X-Rate-Limit-Remaining" => 3 })
|
447
|
+
subject.put(url, accept: "xml", data: post_data.to_xml) { |response| expect(response.body).to be_nil }
|
448
448
|
expect(stub).to have_been_requested
|
449
449
|
end
|
450
450
|
end
|
@@ -530,159 +530,159 @@ describe Maremma do
|
|
530
530
|
end
|
531
531
|
end
|
532
532
|
|
533
|
-
context
|
534
|
-
it
|
535
|
-
string =
|
533
|
+
context "parse_error_response" do
|
534
|
+
it "json" do
|
535
|
+
string = "{ \"error\": \"An error occured.\" }"
|
536
536
|
expect(subject.parse_error_response(string)).to eq("An error occured.")
|
537
537
|
end
|
538
538
|
|
539
|
-
it
|
540
|
-
string =
|
539
|
+
it "json not error" do
|
540
|
+
string = "{ \"customError\": \"An error occured.\" }"
|
541
541
|
expect(subject.parse_error_response(string)).to eq("customError"=>"An error occured.")
|
542
542
|
end
|
543
543
|
|
544
|
-
it
|
545
|
-
string =
|
544
|
+
it "xml" do
|
545
|
+
string = "<error>An error occured.</error>"
|
546
546
|
expect(subject.parse_error_response(string)).to eq("An error occured.")
|
547
547
|
end
|
548
548
|
end
|
549
549
|
|
550
|
-
context
|
551
|
-
it
|
552
|
-
string =
|
553
|
-
expect(subject.parse_success_response(string)).to eq({"data"=>{"word"=>"abc"}})
|
550
|
+
context "parse_success_response" do
|
551
|
+
it "from_json" do
|
552
|
+
string = "{ \"word\": \"abc\" }"
|
553
|
+
expect(subject.parse_success_response(string)).to eq({ "data" => { "word"=>"abc" }})
|
554
554
|
end
|
555
555
|
|
556
|
-
it
|
557
|
-
string =
|
556
|
+
it "from_json with data" do
|
557
|
+
string = "{ \"data\": { \"word\": \"abc\" }}"
|
558
558
|
expect(subject.parse_success_response(string)).to eq({"data"=>{"word"=>"abc"}})
|
559
559
|
end
|
560
560
|
|
561
|
-
it
|
562
|
-
string =
|
561
|
+
it "from_json with data and meta" do
|
562
|
+
string = "{ \"data\": { \"word\": \"abc\" }, \"meta\": { \"total\": 12 }}"
|
563
563
|
expect(subject.parse_success_response(string)).to eq({"data"=>{"word"=>"abc"},"meta"=>{"total"=>12}})
|
564
564
|
end
|
565
565
|
|
566
|
-
it
|
566
|
+
it "from_xml" do
|
567
567
|
string = "<word>abc</word>"
|
568
568
|
expect(subject.parse_success_response(string)).to eq("data"=>{"word"=>"abc"})
|
569
569
|
end
|
570
570
|
|
571
|
-
it
|
572
|
-
string =
|
571
|
+
it "from_xml with attribute" do
|
572
|
+
string = "<word type='small'>abc</word>"
|
573
573
|
expect(subject.parse_success_response(string)).to eq("data"=>{"word"=>{"type"=>"small", "__content__"=>"abc"}})
|
574
574
|
end
|
575
575
|
|
576
|
-
# it
|
577
|
-
# string =
|
576
|
+
# it "from_xml with allowed content attributes" do
|
577
|
+
# string = "<title>Sexual conflict and correlated evolution between male persistence and female resistance traits in the seed beetle <i>Callosobruchus maculatus</i></title>"
|
578
578
|
# expect(subject.parse_success_response(string)).to eq("data"=>{"title"=>"Sexual conflict and correlated evolution between male persistence and female resistance traits in the seed beetle <i>Callosobruchus maculatus</i>"})
|
579
579
|
# end
|
580
580
|
|
581
|
-
it
|
582
|
-
string =
|
581
|
+
it "from_xml with attribute type string" do
|
582
|
+
string = "<crm-item name='publisher-name' type='string'>eLife Sciences Publications, Ltd</crm-item>"
|
583
583
|
expect(subject.parse_success_response(string)).to eq("data"=>{"crm_item"=>"eLife Sciences Publications, Ltd"})
|
584
584
|
end
|
585
585
|
|
586
|
-
it
|
587
|
-
string =
|
586
|
+
it "from_xml with mixed attribute" do
|
587
|
+
string = "<word type='small'>abc<footnote>1</footnote></word>"
|
588
588
|
expect(subject.parse_success_response(string)).to eq("data"=>{"word"=>{"type"=>"small", "footnote"=>"1", "__content__"=>"abc"}})
|
589
589
|
end
|
590
590
|
|
591
|
-
it
|
591
|
+
it "from_string" do
|
592
592
|
string = "abc"
|
593
593
|
expect(subject.parse_success_response(string)).to eq("data"=>"abc")
|
594
594
|
end
|
595
595
|
|
596
|
-
it
|
596
|
+
it "from_string with utf-8" do
|
597
597
|
string = "fön "
|
598
598
|
expect(subject.parse_success_response(string)).to eq("data"=>"fön")
|
599
599
|
end
|
600
600
|
end
|
601
601
|
|
602
|
-
context
|
603
|
-
it
|
602
|
+
context "accept headers" do
|
603
|
+
it "default" do
|
604
604
|
headers = subject.set_request_headers(url)
|
605
605
|
expect(headers["Accept"]).to eq("text/html,application/json,application/xml;q=0.9, text/plain;q=0.8,image/png,*/*;q=0.5")
|
606
606
|
end
|
607
607
|
|
608
|
-
it
|
609
|
-
headers = subject.set_request_headers(url, accept:
|
608
|
+
it "json" do
|
609
|
+
headers = subject.set_request_headers(url, accept: "json")
|
610
610
|
expect(headers["Accept"]).to eq("application/json;charset=UTF-8")
|
611
611
|
end
|
612
612
|
|
613
|
-
it
|
614
|
-
headers = subject.set_request_headers(url, accept:
|
613
|
+
it "xml" do
|
614
|
+
headers = subject.set_request_headers(url, accept: "xml")
|
615
615
|
expect(headers["Accept"]).to eq("application/xml;charset=UTF-8")
|
616
616
|
end
|
617
617
|
|
618
|
-
it
|
619
|
-
headers = subject.set_request_headers(url, accept:
|
618
|
+
it "html" do
|
619
|
+
headers = subject.set_request_headers(url, accept: "html")
|
620
620
|
expect(headers["Accept"]).to eq("text/html;charset=UTF-8")
|
621
621
|
end
|
622
622
|
|
623
|
-
it
|
624
|
-
headers = subject.set_request_headers(url, accept:
|
623
|
+
it "other" do
|
624
|
+
headers = subject.set_request_headers(url, accept: "application/x-bibtex")
|
625
625
|
expect(headers["Accept"]).to eq("application/x-bibtex")
|
626
626
|
end
|
627
627
|
end
|
628
628
|
|
629
|
-
context
|
630
|
-
it
|
629
|
+
context "authentication" do
|
630
|
+
it "no auth" do
|
631
631
|
options = {}
|
632
632
|
expect(subject.set_request_headers(url, options)["Authorization"]).to be nil
|
633
633
|
end
|
634
634
|
|
635
|
-
it
|
636
|
-
options = { bearer:
|
635
|
+
it "bearer" do
|
636
|
+
options = { bearer: "mF_9.B5f-4.1JqM" }
|
637
637
|
expect(subject.set_request_headers(url, options)["Authorization"]).to eq("Bearer mF_9.B5f-4.1JqM")
|
638
638
|
end
|
639
639
|
|
640
|
-
it
|
641
|
-
options = { token:
|
640
|
+
it "token" do
|
641
|
+
options = { token: "12345" }
|
642
642
|
expect(subject.set_request_headers(url, options)["Authorization"]).to eq("Token token=12345")
|
643
643
|
end
|
644
644
|
|
645
|
-
it
|
646
|
-
options = { github_token:
|
645
|
+
it "github_token" do
|
646
|
+
options = { github_token: "12345" }
|
647
647
|
expect(subject.set_request_headers(url, options)["Authorization"]).to eq("Token 12345")
|
648
648
|
end
|
649
649
|
|
650
|
-
it
|
651
|
-
options = { github_token:
|
650
|
+
it "github_token" do
|
651
|
+
options = { github_token: "12345" }
|
652
652
|
expect(subject.set_request_headers(url, options)).to eq("User-Agent"=>"Mozilla/5.0 (compatible; Maremma/#{Maremma::VERSION}; +https://github.com/datacite/maremma)", "Accept"=>accept_header, "Authorization"=>"Token 12345")
|
653
653
|
end
|
654
654
|
|
655
|
-
it
|
656
|
-
options = { username:
|
655
|
+
it "basic" do
|
656
|
+
options = { username: "foo", password: "12345" }
|
657
657
|
basic = Base64.strict_encode64("foo:12345")
|
658
658
|
expect(subject.set_request_headers(url, options)["Authorization"]).to eq("Basic #{basic}")
|
659
659
|
end
|
660
660
|
end
|
661
661
|
|
662
|
-
context
|
663
|
-
it
|
662
|
+
context "set host" do
|
663
|
+
it "crossref", vcr: true do
|
664
664
|
url = "https://api.eventdata.crossref.org/v1/events?mailto=info@datacite.org&rows=0&source=crossref"
|
665
665
|
response = subject.get(url)
|
666
666
|
expect(response.status).to eq(200)
|
667
667
|
expect(response.body.dig("data", "message", "total-results")).to eq(57969)
|
668
668
|
end
|
669
669
|
|
670
|
-
it
|
670
|
+
it "ornl.gov", vcr: true do
|
671
671
|
url = "https://daac.ornl.gov/cgi-bin/dsviewer.pl?ds_id=1339"
|
672
672
|
response = subject.get(url)
|
673
673
|
expect(response.status).to eq(200)
|
674
|
-
doc = Nokogiri::XML(response.body.fetch("data", nil), nil,
|
674
|
+
doc = Nokogiri::XML(response.body.fetch("data", nil), nil, "UTF-8")
|
675
675
|
nodeset = doc.css("script")
|
676
676
|
string = nodeset.find { |element| element["type"] == "application/ld+json" }
|
677
677
|
json = JSON.parse(string)
|
678
678
|
expect(json["@id"]).to eq("https://doi.org/10.3334/ORNLDAAC/1339")
|
679
679
|
end
|
680
680
|
|
681
|
-
it
|
681
|
+
it "redirection", vcr: true do
|
682
682
|
url = "https://doi.org/10.3334/ornldaac/1339"
|
683
683
|
response = subject.get(url)
|
684
684
|
expect(response.status).to eq(200)
|
685
|
-
doc = Nokogiri::XML(response.body.fetch("data", nil), nil,
|
685
|
+
doc = Nokogiri::XML(response.body.fetch("data", nil), nil, "UTF-8")
|
686
686
|
nodeset = doc.css("script")
|
687
687
|
string = nodeset.find { |element| element["type"] == "application/ld+json" }
|
688
688
|
json = JSON.parse(string)
|
@@ -690,14 +690,14 @@ describe Maremma do
|
|
690
690
|
end
|
691
691
|
end
|
692
692
|
|
693
|
-
context
|
694
|
-
it
|
693
|
+
context "ssl verify", vcr: true do
|
694
|
+
it "default" do
|
695
695
|
url = "https://doi.org/10.5281/ZENODO.21430"
|
696
696
|
response = subject.get(url)
|
697
697
|
expect(response.status).to eq(200)
|
698
698
|
end
|
699
699
|
|
700
|
-
it
|
700
|
+
it "self-signed" do
|
701
701
|
url = "https://38.100.138.135:8000/api/handles/10.5281/ZENODO.21430?index=1"
|
702
702
|
options = { ssl_self_signed: true}
|
703
703
|
response = subject.get(url, options)
|