aitch 0.5.0 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +3 -5
- data/CHANGELOG.md +38 -0
- data/README.md +50 -14
- data/Rakefile +10 -0
- data/aitch.gemspec +4 -2
- data/lib/aitch.rb +7 -2
- data/lib/aitch/configuration.rb +1 -12
- data/lib/aitch/dsl.rb +1 -0
- data/lib/aitch/errors.rb +1 -0
- data/lib/aitch/ext/to_query.rb +1 -0
- data/lib/aitch/location.rb +3 -2
- data/lib/aitch/namespace.rb +1 -0
- data/lib/aitch/redirect.rb +8 -2
- data/lib/aitch/request.rb +22 -9
- data/lib/aitch/response.rb +4 -13
- data/lib/aitch/response/body.rb +1 -0
- data/lib/aitch/response/description.rb +52 -51
- data/lib/aitch/response/errors.rb +1 -0
- data/lib/aitch/response_parser.rb +29 -0
- data/lib/aitch/response_parser/default_parser.rb +18 -0
- data/lib/aitch/response_parser/html_parser.rb +18 -0
- data/lib/aitch/response_parser/json_parser.rb +18 -0
- data/lib/aitch/response_parser/xml_parser.rb +18 -0
- data/lib/aitch/uri.rb +1 -0
- data/lib/aitch/utils.rb +1 -0
- data/lib/aitch/version.rb +2 -1
- data/test/aitch/aitch_test.rb +27 -0
- data/test/aitch/configuration_test.rb +29 -0
- data/test/aitch/dsl_test.rb +48 -0
- data/test/aitch/execute_test.rb +45 -0
- data/test/aitch/namespace_test.rb +12 -0
- data/test/aitch/request/client_https_test.rb +20 -0
- data/test/aitch/request/follow_redirect_test.rb +64 -0
- data/test/aitch/request/request_class_test.rb +28 -0
- data/test/aitch/request/status_code_validation_test.rb +19 -0
- data/test/aitch/request_test.rb +138 -0
- data/test/aitch/response/custom_response_parser_test.rb +31 -0
- data/test/aitch/response/errors_test.rb +16 -0
- data/test/aitch/response/html_response_test.rb +18 -0
- data/test/aitch/response/json_response_test.rb +18 -0
- data/test/aitch/response/raw_response_test.rb +11 -0
- data/test/aitch/response/status_3xx_test.rb +56 -0
- data/test/aitch/response/status_4xx_test.rb +18 -0
- data/test/aitch/response/status_5xx_test.rb +18 -0
- data/test/aitch/response/xml_response_test.rb +18 -0
- data/test/aitch/response_parser/html_parser_test.rb +9 -0
- data/test/aitch/response_parser/json_parser_test.rb +9 -0
- data/test/aitch/response_parser/xml_parser_test.rb +17 -0
- data/test/aitch/response_test.rb +114 -0
- data/test/aitch/uri_test.rb +37 -0
- data/test/aitch/utils/symbolize_keys_test.rb +9 -0
- data/test/aitch/utils/underscore_test.rb +12 -0
- data/{spec → test}/fixtures/iso8859-1.xml +0 -0
- data/{spec/support/request_uri.rb → test/support/helpers.rb} +12 -3
- data/test/test_helper.rb +15 -0
- metadata +70 -37
- data/.rspec +0 -1
- data/lib/aitch/html_parser.rb +0 -7
- data/lib/aitch/xml_parser.rb +0 -7
- data/spec/aitch/aitch_spec.rb +0 -71
- data/spec/aitch/configuration_spec.rb +0 -33
- data/spec/aitch/dsl_spec.rb +0 -47
- data/spec/aitch/html_parser_spec.rb +0 -8
- data/spec/aitch/namespace_spec.rb +0 -11
- data/spec/aitch/request_spec.rb +0 -261
- data/spec/aitch/response_spec.rb +0 -259
- data/spec/aitch/uri_spec.rb +0 -36
- data/spec/aitch/utils_spec.rb +0 -19
- data/spec/aitch/xml_parser_spec.rb +0 -16
- data/spec/spec_helper.rb +0 -24
- data/spec/support/webmock.rb +0 -15
data/test/test_helper.rb
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
require "codeclimate-test-reporter"
|
3
|
+
CodeClimate::TestReporter.start
|
4
|
+
|
5
|
+
require "bundler/setup"
|
6
|
+
|
7
|
+
require "minitest/utils"
|
8
|
+
require "minitest/autorun"
|
9
|
+
|
10
|
+
require "aitch"
|
11
|
+
|
12
|
+
require "base64"
|
13
|
+
require "nokogiri"
|
14
|
+
|
15
|
+
require_relative "support/helpers"
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: aitch
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 1.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Nando Vieira
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2016-03-31 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: nokogiri
|
@@ -67,7 +67,7 @@ dependencies:
|
|
67
67
|
- !ruby/object:Gem::Version
|
68
68
|
version: '0'
|
69
69
|
- !ruby/object:Gem::Dependency
|
70
|
-
name:
|
70
|
+
name: minitest-utils
|
71
71
|
requirement: !ruby/object:Gem::Requirement
|
72
72
|
requirements:
|
73
73
|
- - ">="
|
@@ -81,7 +81,7 @@ dependencies:
|
|
81
81
|
- !ruby/object:Gem::Version
|
82
82
|
version: '0'
|
83
83
|
- !ruby/object:Gem::Dependency
|
84
|
-
name:
|
84
|
+
name: mocha
|
85
85
|
requirement: !ruby/object:Gem::Requirement
|
86
86
|
requirements:
|
87
87
|
- - ">="
|
@@ -116,8 +116,8 @@ extensions: []
|
|
116
116
|
extra_rdoc_files: []
|
117
117
|
files:
|
118
118
|
- ".gitignore"
|
119
|
-
- ".rspec"
|
120
119
|
- ".travis.yml"
|
120
|
+
- CHANGELOG.md
|
121
121
|
- Gemfile
|
122
122
|
- LICENSE.txt
|
123
123
|
- README.md
|
@@ -128,7 +128,6 @@ files:
|
|
128
128
|
- lib/aitch/dsl.rb
|
129
129
|
- lib/aitch/errors.rb
|
130
130
|
- lib/aitch/ext/to_query.rb
|
131
|
-
- lib/aitch/html_parser.rb
|
132
131
|
- lib/aitch/location.rb
|
133
132
|
- lib/aitch/namespace.rb
|
134
133
|
- lib/aitch/redirect.rb
|
@@ -137,24 +136,43 @@ files:
|
|
137
136
|
- lib/aitch/response/body.rb
|
138
137
|
- lib/aitch/response/description.rb
|
139
138
|
- lib/aitch/response/errors.rb
|
139
|
+
- lib/aitch/response_parser.rb
|
140
|
+
- lib/aitch/response_parser/default_parser.rb
|
141
|
+
- lib/aitch/response_parser/html_parser.rb
|
142
|
+
- lib/aitch/response_parser/json_parser.rb
|
143
|
+
- lib/aitch/response_parser/xml_parser.rb
|
140
144
|
- lib/aitch/uri.rb
|
141
145
|
- lib/aitch/utils.rb
|
142
146
|
- lib/aitch/version.rb
|
143
|
-
-
|
144
|
-
-
|
145
|
-
-
|
146
|
-
-
|
147
|
-
-
|
148
|
-
-
|
149
|
-
-
|
150
|
-
-
|
151
|
-
-
|
152
|
-
-
|
153
|
-
-
|
154
|
-
-
|
155
|
-
-
|
156
|
-
-
|
157
|
-
-
|
147
|
+
- test/aitch/aitch_test.rb
|
148
|
+
- test/aitch/configuration_test.rb
|
149
|
+
- test/aitch/dsl_test.rb
|
150
|
+
- test/aitch/execute_test.rb
|
151
|
+
- test/aitch/namespace_test.rb
|
152
|
+
- test/aitch/request/client_https_test.rb
|
153
|
+
- test/aitch/request/follow_redirect_test.rb
|
154
|
+
- test/aitch/request/request_class_test.rb
|
155
|
+
- test/aitch/request/status_code_validation_test.rb
|
156
|
+
- test/aitch/request_test.rb
|
157
|
+
- test/aitch/response/custom_response_parser_test.rb
|
158
|
+
- test/aitch/response/errors_test.rb
|
159
|
+
- test/aitch/response/html_response_test.rb
|
160
|
+
- test/aitch/response/json_response_test.rb
|
161
|
+
- test/aitch/response/raw_response_test.rb
|
162
|
+
- test/aitch/response/status_3xx_test.rb
|
163
|
+
- test/aitch/response/status_4xx_test.rb
|
164
|
+
- test/aitch/response/status_5xx_test.rb
|
165
|
+
- test/aitch/response/xml_response_test.rb
|
166
|
+
- test/aitch/response_parser/html_parser_test.rb
|
167
|
+
- test/aitch/response_parser/json_parser_test.rb
|
168
|
+
- test/aitch/response_parser/xml_parser_test.rb
|
169
|
+
- test/aitch/response_test.rb
|
170
|
+
- test/aitch/uri_test.rb
|
171
|
+
- test/aitch/utils/symbolize_keys_test.rb
|
172
|
+
- test/aitch/utils/underscore_test.rb
|
173
|
+
- test/fixtures/iso8859-1.xml
|
174
|
+
- test/support/helpers.rb
|
175
|
+
- test/test_helper.rb
|
158
176
|
homepage: http://rubygems.org/gems/aitch
|
159
177
|
licenses:
|
160
178
|
- MIT
|
@@ -167,7 +185,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
167
185
|
requirements:
|
168
186
|
- - ">="
|
169
187
|
- !ruby/object:Gem::Version
|
170
|
-
version: '0'
|
188
|
+
version: '2.0'
|
171
189
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
172
190
|
requirements:
|
173
191
|
- - ">="
|
@@ -175,22 +193,37 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
175
193
|
version: '0'
|
176
194
|
requirements: []
|
177
195
|
rubyforge_project:
|
178
|
-
rubygems_version: 2.
|
196
|
+
rubygems_version: 2.5.1
|
179
197
|
signing_key:
|
180
198
|
specification_version: 4
|
181
199
|
summary: A simple HTTP client
|
182
200
|
test_files:
|
183
|
-
-
|
184
|
-
-
|
185
|
-
-
|
186
|
-
-
|
187
|
-
-
|
188
|
-
-
|
189
|
-
-
|
190
|
-
-
|
191
|
-
-
|
192
|
-
-
|
193
|
-
-
|
194
|
-
-
|
195
|
-
-
|
196
|
-
-
|
201
|
+
- test/aitch/aitch_test.rb
|
202
|
+
- test/aitch/configuration_test.rb
|
203
|
+
- test/aitch/dsl_test.rb
|
204
|
+
- test/aitch/execute_test.rb
|
205
|
+
- test/aitch/namespace_test.rb
|
206
|
+
- test/aitch/request/client_https_test.rb
|
207
|
+
- test/aitch/request/follow_redirect_test.rb
|
208
|
+
- test/aitch/request/request_class_test.rb
|
209
|
+
- test/aitch/request/status_code_validation_test.rb
|
210
|
+
- test/aitch/request_test.rb
|
211
|
+
- test/aitch/response/custom_response_parser_test.rb
|
212
|
+
- test/aitch/response/errors_test.rb
|
213
|
+
- test/aitch/response/html_response_test.rb
|
214
|
+
- test/aitch/response/json_response_test.rb
|
215
|
+
- test/aitch/response/raw_response_test.rb
|
216
|
+
- test/aitch/response/status_3xx_test.rb
|
217
|
+
- test/aitch/response/status_4xx_test.rb
|
218
|
+
- test/aitch/response/status_5xx_test.rb
|
219
|
+
- test/aitch/response/xml_response_test.rb
|
220
|
+
- test/aitch/response_parser/html_parser_test.rb
|
221
|
+
- test/aitch/response_parser/json_parser_test.rb
|
222
|
+
- test/aitch/response_parser/xml_parser_test.rb
|
223
|
+
- test/aitch/response_test.rb
|
224
|
+
- test/aitch/uri_test.rb
|
225
|
+
- test/aitch/utils/symbolize_keys_test.rb
|
226
|
+
- test/aitch/utils/underscore_test.rb
|
227
|
+
- test/fixtures/iso8859-1.xml
|
228
|
+
- test/support/helpers.rb
|
229
|
+
- test/test_helper.rb
|
data/.rspec
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
--color --order random
|
data/lib/aitch/html_parser.rb
DELETED
data/lib/aitch/xml_parser.rb
DELETED
data/spec/aitch/aitch_spec.rb
DELETED
@@ -1,71 +0,0 @@
|
|
1
|
-
require "spec_helper"
|
2
|
-
|
3
|
-
describe Aitch do
|
4
|
-
context "request methods" do
|
5
|
-
it { should respond_to(:get) }
|
6
|
-
it { should respond_to(:get!) }
|
7
|
-
it { should respond_to(:post) }
|
8
|
-
it { should respond_to(:post!) }
|
9
|
-
it { should respond_to(:put) }
|
10
|
-
it { should respond_to(:put!) }
|
11
|
-
it { should respond_to(:patch) }
|
12
|
-
it { should respond_to(:patch!) }
|
13
|
-
it { should respond_to(:delete) }
|
14
|
-
it { should respond_to(:delete!) }
|
15
|
-
it { should respond_to(:head) }
|
16
|
-
it { should respond_to(:head!) }
|
17
|
-
it { should respond_to(:options) }
|
18
|
-
it { should respond_to(:options!) }
|
19
|
-
it { should respond_to(:trace) }
|
20
|
-
it { should respond_to(:trace!) }
|
21
|
-
it { should respond_to(:execute) }
|
22
|
-
it { should respond_to(:execute!) }
|
23
|
-
it { should respond_to(:config) }
|
24
|
-
it { should respond_to(:configuration) }
|
25
|
-
end
|
26
|
-
|
27
|
-
describe "#execute" do
|
28
|
-
let(:request) { double.as_null_object }
|
29
|
-
|
30
|
-
it "delegates to Request" do
|
31
|
-
options = {}
|
32
|
-
|
33
|
-
expected = {
|
34
|
-
request_method: "get",
|
35
|
-
url: "URL",
|
36
|
-
data: "DATA",
|
37
|
-
headers: "HEADERS",
|
38
|
-
options: options.merge(Aitch.config.to_h)
|
39
|
-
}
|
40
|
-
|
41
|
-
expect(Aitch::Request).to receive(:new).with(expected).and_return(request)
|
42
|
-
|
43
|
-
Aitch.get("URL", "DATA", "HEADERS", options)
|
44
|
-
end
|
45
|
-
|
46
|
-
it "performs request" do
|
47
|
-
allow(Aitch::Request).to receive(:new).and_return(request)
|
48
|
-
expect(request).to receive(:perform)
|
49
|
-
|
50
|
-
Aitch.get("URL")
|
51
|
-
end
|
52
|
-
end
|
53
|
-
|
54
|
-
describe "#execute!" do
|
55
|
-
it "returns response when successful" do
|
56
|
-
response = double(error?: false)
|
57
|
-
allow_any_instance_of(Aitch::Request).to receive(:perform).and_return(response)
|
58
|
-
|
59
|
-
expect(Aitch.get!("URL")).to eq(response)
|
60
|
-
end
|
61
|
-
|
62
|
-
it "raises when has errors" do
|
63
|
-
response = double(error?: true, error: "ERROR")
|
64
|
-
allow_any_instance_of(Aitch::Request).to receive(:perform).and_return(response)
|
65
|
-
|
66
|
-
expect {
|
67
|
-
Aitch.get!("URL")
|
68
|
-
}.to raise_error("ERROR")
|
69
|
-
end
|
70
|
-
end
|
71
|
-
end
|
@@ -1,33 +0,0 @@
|
|
1
|
-
require "spec_helper"
|
2
|
-
|
3
|
-
describe Aitch::Configuration do
|
4
|
-
it "sets default timeout" do
|
5
|
-
expect(Aitch::Configuration.new.timeout).to eq(10)
|
6
|
-
end
|
7
|
-
|
8
|
-
it "sets default user agent" do
|
9
|
-
user_agent = "Aitch/#{Aitch::VERSION} (http://rubygems.org/gems/aitch)"
|
10
|
-
expect(Aitch::Configuration.new.user_agent).to eq(user_agent)
|
11
|
-
end
|
12
|
-
|
13
|
-
it "sets default maximum redirections" do
|
14
|
-
expect(Aitch::Configuration.new.redirect_limit).to eq(5)
|
15
|
-
end
|
16
|
-
|
17
|
-
it "sets default headers" do
|
18
|
-
expect(Aitch::Configuration.new.default_headers).to eq({})
|
19
|
-
end
|
20
|
-
|
21
|
-
it "sets default XML parser" do
|
22
|
-
expect(Aitch::Configuration.new.xml_parser).to eq(Aitch::XMLParser)
|
23
|
-
end
|
24
|
-
|
25
|
-
it "configures aitch" do
|
26
|
-
Aitch.configure do |config|
|
27
|
-
config.timeout = 15
|
28
|
-
end
|
29
|
-
|
30
|
-
expect(Aitch.configuration.timeout).to eq(15)
|
31
|
-
end
|
32
|
-
end
|
33
|
-
|
data/spec/aitch/dsl_spec.rb
DELETED
@@ -1,47 +0,0 @@
|
|
1
|
-
require "spec_helper"
|
2
|
-
|
3
|
-
describe Aitch::DSL do
|
4
|
-
subject(:dsl) { Aitch::DSL.new }
|
5
|
-
|
6
|
-
it "sets url" do
|
7
|
-
dsl.url "URL"
|
8
|
-
expect(dsl.url).to eq("URL")
|
9
|
-
end
|
10
|
-
|
11
|
-
it "sets options" do
|
12
|
-
dsl.options "OPTIONS"
|
13
|
-
expect(dsl.options).to eq("OPTIONS")
|
14
|
-
end
|
15
|
-
|
16
|
-
it "sets headers" do
|
17
|
-
dsl.headers "HEADERS"
|
18
|
-
expect(dsl.headers).to eq("HEADERS")
|
19
|
-
end
|
20
|
-
|
21
|
-
it "sets data" do
|
22
|
-
dsl.data "DATA"
|
23
|
-
expect(dsl.data).to eq("DATA")
|
24
|
-
end
|
25
|
-
|
26
|
-
it "sets data through params" do
|
27
|
-
dsl.params "PARAMS"
|
28
|
-
expect(dsl.data).to eq("PARAMS")
|
29
|
-
end
|
30
|
-
|
31
|
-
it "sets data through body" do
|
32
|
-
dsl.body "BODY"
|
33
|
-
expect(dsl.data).to eq("BODY")
|
34
|
-
end
|
35
|
-
|
36
|
-
it "returns hash" do
|
37
|
-
dsl.options "OPTIONS"
|
38
|
-
dsl.headers "HEADERS"
|
39
|
-
dsl.url "URL"
|
40
|
-
dsl.data "DATA"
|
41
|
-
|
42
|
-
expect(dsl.to_h).to include(data: "DATA")
|
43
|
-
expect(dsl.to_h).to include(headers: "HEADERS")
|
44
|
-
expect(dsl.to_h).to include(url: "URL")
|
45
|
-
expect(dsl.to_h).to include(options: "OPTIONS")
|
46
|
-
end
|
47
|
-
end
|
@@ -1,11 +0,0 @@
|
|
1
|
-
require "spec_helper"
|
2
|
-
|
3
|
-
describe Aitch::Namespace do
|
4
|
-
it "isolates namespace configuration" do
|
5
|
-
ns = Aitch::Namespace.new
|
6
|
-
ns.config.user_agent = "MyLib/1.0.0"
|
7
|
-
|
8
|
-
expect(ns.config.user_agent).to eq("MyLib/1.0.0")
|
9
|
-
expect(Aitch.config.user_agent).to match(%r[^Aitch])
|
10
|
-
end
|
11
|
-
end
|
data/spec/aitch/request_spec.rb
DELETED
@@ -1,261 +0,0 @@
|
|
1
|
-
require "spec_helper"
|
2
|
-
|
3
|
-
describe Aitch::Request do
|
4
|
-
def build_request(options = {})
|
5
|
-
Aitch::Request.new({
|
6
|
-
request_method: "get",
|
7
|
-
url: "http://example.org",
|
8
|
-
options: {}
|
9
|
-
}.merge(options))
|
10
|
-
end
|
11
|
-
|
12
|
-
it "sets content type" do
|
13
|
-
request = build_request(content_type: 'application/json')
|
14
|
-
expect(request.content_type).to eq('application/json')
|
15
|
-
end
|
16
|
-
|
17
|
-
it "raises with invalid uri" do
|
18
|
-
expect {
|
19
|
-
build_request(url: "\\").uri
|
20
|
-
}.to raise_error(Aitch::InvalidURIError)
|
21
|
-
end
|
22
|
-
|
23
|
-
it "raises on timeout", ruby: 2.0 do
|
24
|
-
request = build_request(request_method: "post", url: "http://example.org")
|
25
|
-
allow(request).to receive_message_chain(:client, :request).and_raise(Net::ReadTimeout)
|
26
|
-
|
27
|
-
expect {
|
28
|
-
request.perform
|
29
|
-
}.to raise_error(Aitch::RequestTimeoutError)
|
30
|
-
end
|
31
|
-
|
32
|
-
it "raises on timeout", ruby: 1.9 do
|
33
|
-
request = build_request(request_method: "post", url: "http://example.org")
|
34
|
-
allow(request).to receive_message_chain(:client, :request).and_raise(Timeout::Error)
|
35
|
-
|
36
|
-
expect {
|
37
|
-
request.perform
|
38
|
-
}.to raise_error(Aitch::RequestTimeoutError)
|
39
|
-
end
|
40
|
-
|
41
|
-
it "raises exception for invalid http method" do
|
42
|
-
request = build_request(request_method: "invalid", url: "http://example.org")
|
43
|
-
|
44
|
-
expect {
|
45
|
-
request.perform
|
46
|
-
}.to raise_error(Aitch::InvalidHTTPMethodError)
|
47
|
-
end
|
48
|
-
|
49
|
-
it "sets user agent" do
|
50
|
-
requester = build_request
|
51
|
-
request = requester.request
|
52
|
-
expect(request["User-Agent"]).to eq(requester.options[:user_agent])
|
53
|
-
end
|
54
|
-
|
55
|
-
it "requests gzip encoding" do
|
56
|
-
request = build_request.request
|
57
|
-
expect(request["Accept-Encoding"]).to eq("gzip,deflate")
|
58
|
-
end
|
59
|
-
|
60
|
-
it "sets path" do
|
61
|
-
request = build_request(url: "http://example.org/some/path").request
|
62
|
-
expect(request.path).to eq("/some/path")
|
63
|
-
end
|
64
|
-
|
65
|
-
it "sets request body from hash" do
|
66
|
-
request = build_request(request_method: "post", data: {a: 1}).request
|
67
|
-
expect(request.body).to eq("a=1")
|
68
|
-
end
|
69
|
-
|
70
|
-
it "sets request body from string" do
|
71
|
-
request = build_request(request_method: "post", data: "some body").request
|
72
|
-
expect(request.body).to eq("some body")
|
73
|
-
end
|
74
|
-
|
75
|
-
it "sets json body from object" do
|
76
|
-
request = build_request(
|
77
|
-
request_method: "post",
|
78
|
-
data: {a: 1},
|
79
|
-
content_type: "application/json",
|
80
|
-
options: {json_parser: JSON}
|
81
|
-
).request
|
82
|
-
|
83
|
-
expect(request.body).to eq({a: 1}.to_json)
|
84
|
-
end
|
85
|
-
|
86
|
-
it "sets json body from object (default headers)" do
|
87
|
-
request = build_request(
|
88
|
-
request_method: "post",
|
89
|
-
data: {a: 1},
|
90
|
-
options: {json_parser: JSON, default_headers: {'Content-Type' => 'application/json'}}
|
91
|
-
).request
|
92
|
-
|
93
|
-
expect(request.body).to eq({a: 1}.to_json)
|
94
|
-
end
|
95
|
-
|
96
|
-
it "sets request body from to_h protocol" do
|
97
|
-
data = double(to_h: {a: 1})
|
98
|
-
request = build_request(request_method: "post", data: data).request
|
99
|
-
expect(request.body).to eq("a=1")
|
100
|
-
end
|
101
|
-
|
102
|
-
it "sets request body from to_s protocol" do
|
103
|
-
data = double(to_s: "some body")
|
104
|
-
request = build_request(request_method: "post", data: data).request
|
105
|
-
|
106
|
-
expect(request.body).to eq("some body")
|
107
|
-
end
|
108
|
-
|
109
|
-
it "sets query string from hash data" do
|
110
|
-
register_uri :get, "http://example.org/?a=1&b=2", body: "hello"
|
111
|
-
requester = build_request(data: {a: 1, b: 2})
|
112
|
-
|
113
|
-
expect(requester.perform.body).to eq("hello")
|
114
|
-
end
|
115
|
-
|
116
|
-
it "sets default headers" do
|
117
|
-
requester = build_request
|
118
|
-
requester.options[:default_headers] = {"HEADER" => "VALUE"}
|
119
|
-
request = requester.request
|
120
|
-
|
121
|
-
expect(request["HEADER"]).to eq("VALUE")
|
122
|
-
end
|
123
|
-
|
124
|
-
it "sets custom headers" do
|
125
|
-
request = build_request(headers: {"HEADER" => "VALUE"}).request
|
126
|
-
expect(request["HEADER"]).to eq("VALUE")
|
127
|
-
end
|
128
|
-
|
129
|
-
it "executes headers with callable protocol" do
|
130
|
-
request = build_request(headers: {"HEADER" => -> { "VALUE" }}).request
|
131
|
-
expect(request["HEADER"]).to eq("VALUE")
|
132
|
-
end
|
133
|
-
|
134
|
-
it "sets basic auth credentials" do
|
135
|
-
request = build_request(options: {user: "USER", password: "PASS"}).request
|
136
|
-
credentials = Base64.decode64(request["Authorization"].gsub(/Basic /, ""))
|
137
|
-
|
138
|
-
expect(credentials).to eq("USER:PASS")
|
139
|
-
end
|
140
|
-
|
141
|
-
describe "#client" do
|
142
|
-
context "https" do
|
143
|
-
let(:request) { build_request(url: "https://example.org") }
|
144
|
-
subject(:client) { request.client }
|
145
|
-
|
146
|
-
it "sets https" do
|
147
|
-
expect(client.use_ssl?).to be_truthy
|
148
|
-
end
|
149
|
-
|
150
|
-
it "sets verification mode" do
|
151
|
-
expect(client.verify_mode).to eq(OpenSSL::SSL::VERIFY_PEER)
|
152
|
-
end
|
153
|
-
|
154
|
-
it "sets timeout" do
|
155
|
-
request.options[:timeout] = 20
|
156
|
-
expect(client.read_timeout).to eq(20)
|
157
|
-
end
|
158
|
-
end
|
159
|
-
end
|
160
|
-
|
161
|
-
describe "Request class" do
|
162
|
-
it "raises with invalid method" do
|
163
|
-
expect {
|
164
|
-
build_request(request_method: "invalid").request
|
165
|
-
}.to raise_error(Aitch::InvalidHTTPMethodError, %[unexpected HTTP verb: "invalid"])
|
166
|
-
end
|
167
|
-
|
168
|
-
%w[
|
169
|
-
get
|
170
|
-
post
|
171
|
-
put
|
172
|
-
patch
|
173
|
-
delete
|
174
|
-
head
|
175
|
-
options
|
176
|
-
trace
|
177
|
-
].each do |method|
|
178
|
-
it "instantiates #{method.upcase} method" do
|
179
|
-
request = build_request(request_method: method).request
|
180
|
-
expect(request.class.name).to eq("Net::HTTP::#{method.capitalize}")
|
181
|
-
end
|
182
|
-
end
|
183
|
-
end
|
184
|
-
|
185
|
-
describe "follow redirection" do
|
186
|
-
before { Aitch.configuration.follow_redirect = true }
|
187
|
-
|
188
|
-
it "follows redirect" do
|
189
|
-
Aitch.configuration.redirect_limit = 5
|
190
|
-
|
191
|
-
register_uri(:get, "http://example.org/", location: "http://example.com/", status: 301)
|
192
|
-
register_uri(:get, "http://example.com/", location: "http://www.example.com/", status: 301)
|
193
|
-
register_uri(:get, "http://www.example.com/", body: "Hello")
|
194
|
-
|
195
|
-
response = Aitch.get("http://example.org/")
|
196
|
-
|
197
|
-
expect(response).not_to be_redirect
|
198
|
-
expect(response.body).to eq("Hello")
|
199
|
-
expect(response.redirected_from).to eq(["http://example.org/", "http://example.com/"])
|
200
|
-
expect(response.url).to eq("http://www.example.com/")
|
201
|
-
end
|
202
|
-
|
203
|
-
it "raises when doing too many redirects" do
|
204
|
-
Aitch.configuration.redirect_limit = 1
|
205
|
-
|
206
|
-
register_uri(:get, "http://example.org/", location: "http://example.com/", status: 301)
|
207
|
-
register_uri(:get, "http://example.com/", location: "https://example.com/", status: 301)
|
208
|
-
|
209
|
-
expect {
|
210
|
-
Aitch.get("http://example.org/")
|
211
|
-
}.to raise_error(Aitch::TooManyRedirectsError)
|
212
|
-
end
|
213
|
-
end
|
214
|
-
|
215
|
-
describe "GET requests" do
|
216
|
-
it "sets data as query string" do
|
217
|
-
register_uri(:get, /.+/)
|
218
|
-
Aitch.get("http://example.org/", a: 1, b: 2)
|
219
|
-
|
220
|
-
expect(last_request.uri.request_uri).to eq("/?a=1&b=2")
|
221
|
-
end
|
222
|
-
end
|
223
|
-
|
224
|
-
describe "using the DSL" do
|
225
|
-
it "performs request" do
|
226
|
-
register_uri(:post, /.+/)
|
227
|
-
|
228
|
-
response = Aitch.post do
|
229
|
-
url "http://example.org/some/path"
|
230
|
-
params a: 1, b: 2
|
231
|
-
headers Rendering: "0.1"
|
232
|
-
options user: "user", password: "pass"
|
233
|
-
end
|
234
|
-
|
235
|
-
expect(last_request.uri.request_uri).to eq("/some/path")
|
236
|
-
expect(last_request.method).to eq(:post)
|
237
|
-
expect(last_request.body).to eq("a=1&b=2")
|
238
|
-
expect(last_request.headers["Rendering"]).to eq("0.1")
|
239
|
-
expect(last_request.uri.user).to eq("user")
|
240
|
-
expect(last_request.uri.password).to eq("pass")
|
241
|
-
end
|
242
|
-
end
|
243
|
-
|
244
|
-
describe "status code validation" do
|
245
|
-
it "raises exception when status code isn't valid" do
|
246
|
-
register_uri(:get, "http://example.org/", status: 404)
|
247
|
-
|
248
|
-
expect {
|
249
|
-
Aitch.get("http://example.org/", {}, {}, expect: [200])
|
250
|
-
}.to raise_error(Aitch::StatusCodeError, "Expected(200 OK) <=> Actual(404 Not Found)")
|
251
|
-
end
|
252
|
-
|
253
|
-
it "accepts valid status code" do
|
254
|
-
register_uri(:get, "http://example.org/", status: 200)
|
255
|
-
|
256
|
-
expect {
|
257
|
-
Aitch.get("http://example.org/", {}, {}, expect: [200])
|
258
|
-
}.not_to raise_error
|
259
|
-
end
|
260
|
-
end
|
261
|
-
end
|