opinionated_http 0.0.3 → 0.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/opinionated_http/client.rb +19 -4
- data/lib/opinionated_http/version.rb +1 -1
- data/test/client_test.rb +77 -0
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6279db8b72fbf65adbc3e271ea079be89b2c54227a343c5dd252cf79799e7adf
|
4
|
+
data.tar.gz: 6951e99ac22b9237ed2a2220bc858cb2e1f1c05b0cd15a4793631226b296ced9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a1139e62e3fcf12a4ed985d96a9d561c4f7a3b9859fb2296699d5b70942aa53448a50fc0a1a7f3fb97cd7a9ee1bff528c9ef9fdc704ae25ec42eee845e31dd39
|
7
|
+
data.tar.gz: 954fe864fef5772be8658bbf78f20ec43e0fc32ed6d456025c1ed3bffbd0b638dd880c6c4e1efc66e1a9e2034930410b0f25e41952508891b51c909ee1977589
|
@@ -49,22 +49,32 @@ module OpinionatedHTTP
|
|
49
49
|
path = "/#{path}" unless path.start_with?("/")
|
50
50
|
path = "#{path}?#{URI.encode_www_form(parameters)}" if parameters
|
51
51
|
|
52
|
-
request =
|
52
|
+
request = generic_request(path: path, verb: 'Get')
|
53
53
|
response = request_with_retry(action: action, path: path, request: request)
|
54
54
|
|
55
55
|
response.body
|
56
56
|
end
|
57
57
|
|
58
|
-
def post(action:, path: "/#{action}",
|
58
|
+
def post(action:, path: "/#{action}", headers: nil, body: nil, form_data: nil, username: nil, password: nil)
|
59
59
|
path = "/#{path}" unless path.start_with?("/")
|
60
|
-
request =
|
61
|
-
request.set_form_data(parameters) if parameters
|
60
|
+
request = generic_request(path: path, verb: 'Post', headers: headers, body: body, form_data: form_data, auth: auth)
|
62
61
|
|
63
62
|
response = request_with_retry(action: action, path: path, request: request)
|
64
63
|
|
65
64
|
response.body
|
66
65
|
end
|
67
66
|
|
67
|
+
def generic_request(path:, verb:, headers: nil, body: nil, form_data: nil, username: nil, password: nil)
|
68
|
+
raise(ArgumentError, 'setting form data will overwrite supplied content-type') unless headers_and_form_data_compatible? headers, form_data
|
69
|
+
raise(ArgumentError, 'setting form data will overwrite supplied body') if body && form_data
|
70
|
+
|
71
|
+
request = Net::HTTP.const_get(verb).new(path, headers)
|
72
|
+
request.body = body if body
|
73
|
+
request.set_form_data form_data if form_data
|
74
|
+
request.basic_auth(username, password) if username && password
|
75
|
+
request
|
76
|
+
end
|
77
|
+
|
68
78
|
private
|
69
79
|
|
70
80
|
def request_with_retry(action:, path: "/#{action}", request:, try_count: 0)
|
@@ -113,5 +123,10 @@ module OpinionatedHTTP
|
|
113
123
|
return 0 if retry_count <= 1
|
114
124
|
(retry_multiplier ** (retry_count - 1)) * retry_interval
|
115
125
|
end
|
126
|
+
|
127
|
+
def headers_and_form_data_compatible?(headers, form_data)
|
128
|
+
return true if headers.nil? || form_data.nil?
|
129
|
+
!headers.keys.map(&:downcase).include? 'content-type'
|
130
|
+
end
|
116
131
|
end
|
117
132
|
end
|
data/test/client_test.rb
CHANGED
@@ -28,6 +28,83 @@ module OpinionatedHTTP
|
|
28
28
|
# end
|
29
29
|
end
|
30
30
|
end
|
31
|
+
|
32
|
+
describe "generic_request" do
|
33
|
+
let(:path) { '/fake_action' }
|
34
|
+
let(:post_verb) { 'Post' }
|
35
|
+
let(:get_verb) { 'Get' }
|
36
|
+
|
37
|
+
it 'creates a request corresponding to the supplied verb' do
|
38
|
+
req = http.generic_request(path: path, verb: post_verb)
|
39
|
+
req2 = http.generic_request(path: path, verb: get_verb)
|
40
|
+
|
41
|
+
assert_kind_of Net::HTTP::Post, req
|
42
|
+
assert_kind_of Net::HTTP::Get, req2
|
43
|
+
end
|
44
|
+
|
45
|
+
it 'returns a request with supplied headers' do
|
46
|
+
test_headers = {'test1' => 'yes_test_1', 'test2' => 'yes_test_2'}
|
47
|
+
req = http.generic_request(path: path, verb: get_verb, headers: test_headers)
|
48
|
+
|
49
|
+
assert_equal test_headers['test1'], req['test1']
|
50
|
+
assert_equal test_headers['test2'], req['test2']
|
51
|
+
end
|
52
|
+
|
53
|
+
it 'returns a request with supplied body' do
|
54
|
+
test_body = "nice bod"
|
55
|
+
req = http.generic_request(path: path, verb: get_verb, body: test_body)
|
56
|
+
|
57
|
+
assert_equal test_body, req.body
|
58
|
+
end
|
59
|
+
|
60
|
+
it 'returns a request with supplied form data in x-www-form-urlencoded Content-Type' do
|
61
|
+
test_data = {test1: 'yes', test2: 'no'}
|
62
|
+
expected_string = "test1=yes&test2=no"
|
63
|
+
req = http.generic_request(path: path, verb: post_verb, form_data: test_data)
|
64
|
+
|
65
|
+
assert_equal expected_string, req.body
|
66
|
+
assert_equal 'application/x-www-form-urlencoded', req['Content-Type']
|
67
|
+
end
|
68
|
+
|
69
|
+
it 'add supplied authentication to the request' do
|
70
|
+
test_un = 'admin'
|
71
|
+
test_pw = 'hunter2'
|
72
|
+
req = http.generic_request(path: path, verb: get_verb, username: test_un, password: test_pw)
|
73
|
+
req2 = Net::HTTP::Get.new(path)
|
74
|
+
req2.basic_auth test_un, test_pw
|
75
|
+
|
76
|
+
assert_equal req2['authorization'], req['authorization']
|
77
|
+
end
|
78
|
+
|
79
|
+
it 'raise an error if supplied content-type header would be overwritten by setting form_data' do
|
80
|
+
downcase_headers = {'unimportant' => 'blank', 'content-type' => 'application/json'}
|
81
|
+
capitalized_headers = {'Unimportant' => 'blank', 'Content-Type' => 'application/json'}
|
82
|
+
no_conflict_headers = {'whatever' => 'blank', 'irrelevant' => 'test'}
|
83
|
+
form_data = {thing1: 1, thing2: 2}
|
84
|
+
|
85
|
+
assert_raises ArgumentError do
|
86
|
+
http.generic_request(path: path, verb: post_verb, headers: downcase_headers, form_data: form_data)
|
87
|
+
end
|
88
|
+
|
89
|
+
assert_raises ArgumentError do
|
90
|
+
http.generic_request(path: path, verb: post_verb, headers: capitalized_headers, form_data: form_data)
|
91
|
+
end
|
92
|
+
|
93
|
+
assert http.generic_request(path: path, verb: post_verb, headers: no_conflict_headers, form_data: form_data)
|
94
|
+
end
|
95
|
+
|
96
|
+
it 'raise an error if there is a collision between supplied body and form_data' do
|
97
|
+
form_data = {thing1: 1, thing2: 2}
|
98
|
+
body = "not form data"
|
99
|
+
|
100
|
+
assert_raises ArgumentError do
|
101
|
+
http.generic_request(path: path, verb: post_verb, body: body, form_data: form_data)
|
102
|
+
end
|
103
|
+
|
104
|
+
assert http.generic_request(path: path, verb: post_verb, body: body)
|
105
|
+
assert http.generic_request(path: path, verb: post_verb, form_data: form_data)
|
106
|
+
end
|
107
|
+
end
|
31
108
|
end
|
32
109
|
end
|
33
110
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: opinionated_http
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Reid Morrison
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-
|
11
|
+
date: 2020-04-16 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: persistent_http
|
@@ -87,7 +87,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
87
87
|
- !ruby/object:Gem::Version
|
88
88
|
version: '0'
|
89
89
|
requirements: []
|
90
|
-
rubygems_version: 3.0.
|
90
|
+
rubygems_version: 3.0.8
|
91
91
|
signing_key:
|
92
92
|
specification_version: 4
|
93
93
|
summary: Opinionated HTTP Client
|