endo 0.4.0 → 0.4.1
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/endo/cli.rb +1 -1
- data/lib/endo/core.rb +51 -42
- data/lib/endo/error.rb +1 -1
- data/lib/endo/expectation_target.rb +4 -7
- data/lib/endo/matchers.rb +1 -2
- data/lib/endo/matchers/base_matcher.rb +1 -1
- data/lib/endo/matchers/eq.rb +5 -4
- data/lib/endo/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 30a81727bcc13e0b820d605e38a94e5de3efc804
|
4
|
+
data.tar.gz: 26ee7de9bacac40fc237f6a32d5f790f3976d9e9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b2a5f170105a8bbf39b851ffa613c0d89341b55ff4ff6113e1b328fdd8c89ce30532d399baba383a9122272bf826de39c232d2de6e33f915eb85b6b3128bd85e
|
7
|
+
data.tar.gz: 7db7ace6ca42dcacf4cc16a68e23af76adab5ff0f95abb0cc0417420bfca69087a9a773d291b0e3d3d1691ebd8c0c82a4843832212cc5bef330568f665f6e019
|
data/lib/endo/cli.rb
CHANGED
data/lib/endo/core.rb
CHANGED
@@ -9,7 +9,16 @@ module Endo
|
|
9
9
|
|
10
10
|
def initialize
|
11
11
|
@responses = {}
|
12
|
-
|
12
|
+
end
|
13
|
+
|
14
|
+
def base_url(url)
|
15
|
+
@base_url = url
|
16
|
+
end
|
17
|
+
|
18
|
+
def basic_auth(user, pass)
|
19
|
+
@basic_auth = {
|
20
|
+
user: user, pass: pass
|
21
|
+
}
|
13
22
|
end
|
14
23
|
|
15
24
|
def get(endpoint, &block)
|
@@ -32,17 +41,16 @@ module Endo
|
|
32
41
|
request(endpoint, :put, &block)
|
33
42
|
end
|
34
43
|
|
35
|
-
# TODO:
|
44
|
+
# TODO: Limit scope
|
36
45
|
def param(key, val = nil)
|
37
46
|
raise ArgumentError, 'DupValue' unless !val.nil? ^ block_given?
|
38
|
-
|
39
47
|
@params[key.to_s] = val ? val : yield
|
40
48
|
end
|
41
49
|
|
42
50
|
# TODO: Limit scope
|
43
51
|
def from(method, endpoint, lmd = nil, &_block)
|
44
52
|
key = build_response_key(method, endpoint)
|
45
|
-
raise
|
53
|
+
raise "NotFoundKey [#{key}]" unless @responses.key? key
|
46
54
|
|
47
55
|
res = @responses[key]
|
48
56
|
val = nil
|
@@ -61,32 +69,15 @@ module Endo
|
|
61
69
|
val
|
62
70
|
end
|
63
71
|
|
64
|
-
def base_url(url)
|
65
|
-
@base_url = url
|
66
|
-
end
|
67
|
-
|
68
|
-
def basic_auth(user, pass)
|
69
|
-
@basic_auth = {
|
70
|
-
user: user, pass: pass
|
71
|
-
}
|
72
|
-
end
|
73
|
-
|
74
72
|
private
|
75
73
|
|
76
|
-
def request(endpoint, method, &
|
77
|
-
@params = {}
|
78
|
-
@expects = []
|
79
|
-
yield if block_given?
|
80
|
-
|
81
|
-
endpoint = apply_pattern_vars(endpoint, @params)
|
82
|
-
url = @base_url + endpoint
|
83
|
-
|
74
|
+
def request(endpoint, method, &block)
|
84
75
|
begin
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
76
|
+
res_obj, duration_ms = request_proc(endpoint, method, &block)
|
77
|
+
message = "🍺 #{method.upcase} #{endpoint} [#{duration_ms}ms]"
|
78
|
+
rescue Errno::ECONNREFUSED => e
|
79
|
+
message = "💀 #{e.message}".red
|
80
|
+
exit 1
|
90
81
|
rescue Error::HttpError => e
|
91
82
|
message = "💩 #{method.upcase} #{endpoint} [code: #{e.code}]".red
|
92
83
|
exit 1
|
@@ -97,21 +88,21 @@ module Endo
|
|
97
88
|
end
|
98
89
|
end
|
99
90
|
|
100
|
-
def
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
patterns.each do |pattern|
|
105
|
-
raise "NotFoundPattern #{pattern}" unless params.key? pattern
|
91
|
+
def request_proc(endpoint, method, &_block)
|
92
|
+
@params = {}
|
93
|
+
@expects = []
|
94
|
+
yield if block_given?
|
106
95
|
|
107
|
-
|
108
|
-
|
96
|
+
endpoint = apply_params_to_pattern(endpoint, @params)
|
97
|
+
url = @base_url + endpoint
|
109
98
|
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
endpoint
|
99
|
+
res, duration_ms = request_with_timer(url, method, @params)
|
100
|
+
validate_expects(res) if @expects.any?
|
101
|
+
|
102
|
+
res_obj = parse_body_json(res)
|
103
|
+
@responses[build_response_key(method, endpoint)] = parse_body_json(res)
|
104
|
+
|
105
|
+
[res_obj, duration_ms]
|
115
106
|
end
|
116
107
|
|
117
108
|
def request_with_timer(url, method, params)
|
@@ -163,13 +154,31 @@ module Endo
|
|
163
154
|
"#{method}:#{endpoint}"
|
164
155
|
end
|
165
156
|
|
157
|
+
def apply_params_to_pattern(endpoint, params)
|
158
|
+
patterns = endpoint.scan(/:(\w+)/)
|
159
|
+
if patterns.any?
|
160
|
+
patterns.flatten!
|
161
|
+
patterns.each do |pattern|
|
162
|
+
raise "NotFoundPattern #{pattern}" unless params.key? pattern
|
163
|
+
endpoint.sub!(/:#{pattern}/, params[pattern].to_s)
|
164
|
+
end
|
165
|
+
|
166
|
+
patterns.uniq.each do |pattern|
|
167
|
+
params.delete(pattern)
|
168
|
+
end
|
169
|
+
end
|
170
|
+
endpoint
|
171
|
+
end
|
172
|
+
|
166
173
|
def expect(header: nil, body: nil)
|
174
|
+
unless !header.nil? ^ !body.nil?
|
175
|
+
raise ArgumentError, '"expect" must be called with header or body'
|
176
|
+
end
|
177
|
+
|
167
178
|
if header
|
168
179
|
expectation = ExpectationTarget.new(:header, header)
|
169
180
|
elsif body
|
170
181
|
expectation = ExpectationTarget.new(:body, body)
|
171
|
-
else
|
172
|
-
raise 'TODO'
|
173
182
|
end
|
174
183
|
@expects << expectation
|
175
184
|
expectation
|
data/lib/endo/error.rb
CHANGED
@@ -1,2 +1,2 @@
|
|
1
1
|
require 'endo/error/http_error'
|
2
|
-
require 'endo/error/validation_error'
|
2
|
+
require 'endo/error/validation_error'
|
@@ -12,21 +12,18 @@ module Endo
|
|
12
12
|
end
|
13
13
|
|
14
14
|
def matches?(res)
|
15
|
-
|
15
|
+
case @target
|
16
16
|
when :header
|
17
|
-
res.header[@query]
|
17
|
+
@actual = res.header[@query]
|
18
18
|
when :body
|
19
19
|
obj = JSON.parse res.body, symbolize_names: true
|
20
|
-
obj.instance_exec(&@query)
|
20
|
+
@actual = obj.instance_exec(&@query)
|
21
21
|
end
|
22
22
|
@matcher.matches?(@actual)
|
23
23
|
end
|
24
24
|
|
25
|
-
|
26
25
|
def expected
|
27
26
|
@matcher.expected
|
28
27
|
end
|
29
|
-
|
30
|
-
|
31
28
|
end
|
32
|
-
end
|
29
|
+
end
|
data/lib/endo/matchers.rb
CHANGED
data/lib/endo/matchers/eq.rb
CHANGED
data/lib/endo/version.rb
CHANGED