rester 0.3.2 → 0.3.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/rester/client/adapters/stub_adapter.rb +2 -2
- data/lib/rester/errors.rb +24 -10
- data/lib/rester/middleware/error_handling.rb +21 -12
- data/lib/rester/rspec.rb +36 -15
- data/lib/rester/service/resource.rb +2 -2
- data/lib/rester/utils.rb +12 -0
- data/lib/rester/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: feecdb5b8bdfda29d9df2683370a79eb805365ef
|
4
|
+
data.tar.gz: e56faba40a47774b17e4db017ec4b7b2fb09213a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 32e24fa563229bcad341c0b06668c67e93fd96196124bcc74e23a1b9d4222765a82d95179d97ad7b9a7430a9c78af85660c66620c433b49ae55ad21220b61aeb
|
7
|
+
data.tar.gz: 57982fbf4923f471ddaf64387d4de8b4ede4c1f12f8654ca3dbdc963fa6490cb0998e19fe173099c4cde289bb6c381a9635c7e36f6df04eda300157af1e6ff0f
|
@@ -66,7 +66,7 @@ module Rester
|
|
66
66
|
end
|
67
67
|
|
68
68
|
# Verify body, if there is one
|
69
|
-
unless (request = action['request'] || {}) == params
|
69
|
+
unless (request = Utils.stringify_vals(action['request'] || {})) == params
|
70
70
|
fail Errors::StubError,
|
71
71
|
"#{verb} #{path} with context '#{context}' params don't match stub. Expected: #{request} Got: #{params}"
|
72
72
|
end
|
@@ -82,7 +82,7 @@ module Rester
|
|
82
82
|
# Useful for testing without having to set the context.
|
83
83
|
def _find_context_by_params(path, verb, params)
|
84
84
|
(stub[path][verb].find { |_, action|
|
85
|
-
(action['request'] || {}) == params
|
85
|
+
(Utils.stringify_vals(action['request'] || {})) == params
|
86
86
|
} || []).first
|
87
87
|
end
|
88
88
|
end # StubAdapter
|
data/lib/rester/errors.rb
CHANGED
@@ -4,8 +4,8 @@ module Rester
|
|
4
4
|
##
|
5
5
|
# Throws an error instead of raising it, which is more performant. Must
|
6
6
|
# be caught by an appropriate error handling wrapper.
|
7
|
-
def throw_error!(
|
8
|
-
error =
|
7
|
+
def throw_error!(error, message = nil)
|
8
|
+
error = error.new(message) if error.is_a?(Class)
|
9
9
|
throw :error, error
|
10
10
|
end
|
11
11
|
end # Class Methods
|
@@ -23,22 +23,36 @@ module Rester
|
|
23
23
|
class HttpError < Error; end
|
24
24
|
|
25
25
|
##
|
26
|
-
#
|
26
|
+
# 400 Errors
|
27
|
+
class RequestError < HttpError
|
28
|
+
attr_reader :error
|
27
29
|
|
28
|
-
|
29
|
-
|
30
|
-
|
30
|
+
def initialize(error, message = nil)
|
31
|
+
@error = error
|
32
|
+
super(message)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
class ValidationError < RequestError
|
37
|
+
def initialize(message = nil)
|
38
|
+
super('validation', message)
|
39
|
+
end
|
40
|
+
end
|
31
41
|
|
42
|
+
##
|
32
43
|
# 401 Error
|
33
|
-
class AuthenticationError <
|
44
|
+
class AuthenticationError < HttpError; end
|
34
45
|
|
46
|
+
##
|
35
47
|
# 403 Error
|
36
|
-
class ForbiddenError <
|
48
|
+
class ForbiddenError < HttpError; end
|
37
49
|
|
50
|
+
##
|
38
51
|
# 404 Not Found
|
39
|
-
class NotFoundError <
|
52
|
+
class NotFoundError < HttpError; end
|
40
53
|
|
54
|
+
##
|
41
55
|
# 500 ServerError
|
42
|
-
class ServerError <
|
56
|
+
class ServerError < HttpError; end
|
43
57
|
end # Errors
|
44
58
|
end # Rester
|
@@ -24,10 +24,11 @@ module Rester
|
|
24
24
|
code = _error_to_http_code(error)
|
25
25
|
|
26
26
|
unless [401, 403, 404].include?(code)
|
27
|
-
body_h = {
|
28
|
-
|
29
|
-
|
30
|
-
|
27
|
+
body_h = { error: _error_name(error) }
|
28
|
+
|
29
|
+
if error.message && error.message != error.class.name
|
30
|
+
body_h.merge!(message: error.message)
|
31
|
+
end
|
31
32
|
|
32
33
|
if code == 500
|
33
34
|
body_h[:backtrace] = error.backtrace
|
@@ -40,14 +41,14 @@ module Rester
|
|
40
41
|
|
41
42
|
def _error_to_http_code(error)
|
42
43
|
case error
|
43
|
-
when Errors::NotFoundError
|
44
|
-
404
|
45
|
-
when Errors::ForbiddenError
|
46
|
-
403
|
47
|
-
when Errors::AuthenticationError
|
48
|
-
401
|
49
44
|
when Errors::RequestError
|
50
45
|
400
|
46
|
+
when Errors::AuthenticationError
|
47
|
+
401
|
48
|
+
when Errors::ForbiddenError
|
49
|
+
403
|
50
|
+
when Errors::NotFoundError
|
51
|
+
404
|
51
52
|
when Errors::ServerError
|
52
53
|
500
|
53
54
|
else
|
@@ -55,8 +56,16 @@ module Rester
|
|
55
56
|
end
|
56
57
|
end
|
57
58
|
|
58
|
-
|
59
|
-
|
59
|
+
##
|
60
|
+
# Takes an exception and returns an appropriate name to return to the
|
61
|
+
# client.
|
62
|
+
def _error_name(error)
|
63
|
+
case error
|
64
|
+
when Errors::RequestError
|
65
|
+
error.error
|
66
|
+
else
|
67
|
+
Utils.underscore(error.class.name.split('::').last.sub('Error', ''))
|
68
|
+
end
|
60
69
|
end
|
61
70
|
end # ErrorHandling
|
62
71
|
end # Middleware
|
data/lib/rester/rspec.rb
CHANGED
@@ -29,35 +29,56 @@ RSpec.configure do |config|
|
|
29
29
|
#
|
30
30
|
# would produce:
|
31
31
|
#
|
32
|
-
#
|
33
|
-
|
34
|
-
request_args = ex.example_group.parent_groups.map { |a|
|
32
|
+
# context, verb, path = ['With some context', 'GET', '/v1/tests']
|
33
|
+
context, verb, path = ex.example_group.parent_groups.map { |a|
|
35
34
|
a.description unless a.metadata[:description] == a.described_class.to_s
|
36
35
|
}.compact
|
37
36
|
|
38
|
-
context = request_args[0]
|
39
|
-
verb = request_args[1]
|
40
|
-
path = request_args[2]
|
41
|
-
|
42
37
|
begin
|
43
|
-
|
44
|
-
|
38
|
+
stub_params = @rester_stub[path][verb][context]['request']
|
39
|
+
raw_stub_response = @rester_stub[path][verb][context]['response']
|
45
40
|
rescue NoMethodError
|
46
41
|
fail Rester::Errors::StubError,
|
47
|
-
"Could not find path: #{path.inspect} verb: #{verb.inspect} context:
|
42
|
+
"Could not find path: #{path.inspect} verb: #{verb.inspect} context: "\
|
43
|
+
"#{context.inspect} in #{@rester_stub_filepath}"
|
48
44
|
end
|
49
45
|
|
50
|
-
|
51
|
-
|
46
|
+
##
|
47
|
+
# Raw response from the service.
|
48
|
+
# [HTTP CODE, JSON String]
|
49
|
+
ex.example_group.let(:raw_service_response) {
|
50
|
+
@rester_adapter.request(verb.downcase.to_sym, path, stub_params)
|
51
|
+
}
|
52
|
+
|
53
|
+
##
|
54
|
+
# Parsed service response
|
55
|
+
ex.example_group.let(:service_response) {
|
56
|
+
JSON.parse(raw_service_response.last, symbolize_names: true)
|
52
57
|
}
|
53
58
|
|
59
|
+
##
|
60
|
+
# HTTP status code returned by service.
|
61
|
+
ex.example_group.let(:service_response_code) { raw_service_response.first }
|
62
|
+
|
63
|
+
##
|
64
|
+
# Expected response body specified in by the stub.
|
54
65
|
ex.example_group.let(:stub_response) {
|
55
|
-
[
|
66
|
+
JSON.parse((raw_stub_response['body'] || {}).to_json,
|
67
|
+
symbolize_names: true)
|
56
68
|
}
|
69
|
+
|
70
|
+
##
|
71
|
+
# HTTP status code expected by the stub.
|
72
|
+
ex.example_group.let(:stub_response_code) { raw_stub_response['code'] }
|
73
|
+
|
74
|
+
##
|
75
|
+
# Set the subject to be the service response (parsed ruby hash of the
|
76
|
+
# returned data).
|
77
|
+
ex.example_group.let(:subject) { service_response }
|
57
78
|
end
|
58
79
|
|
59
80
|
config.after :each, rester: // do |ex|
|
60
|
-
expect(
|
81
|
+
expect(service_response_code).to eq stub_response_code
|
61
82
|
end
|
62
83
|
|
63
84
|
##
|
@@ -76,7 +97,7 @@ RSpec.configure do |config|
|
|
76
97
|
|
77
98
|
missing_contexts.each { |missing_context, _|
|
78
99
|
context_group = _find_or_create_child(verb_group, missing_context)
|
79
|
-
context_group.it { is_expected.to
|
100
|
+
context_group.it { is_expected.to include stub_response }
|
80
101
|
}
|
81
102
|
}
|
82
103
|
}
|
@@ -96,8 +96,8 @@ module Rester
|
|
96
96
|
self.class.method_params[method_name.to_sym]
|
97
97
|
end
|
98
98
|
|
99
|
-
def error!(message=nil)
|
100
|
-
Errors.throw_error!(Errors::RequestError, message)
|
99
|
+
def error!(error, message = nil)
|
100
|
+
Errors.throw_error!(Errors::RequestError.new(error, message))
|
101
101
|
end
|
102
102
|
|
103
103
|
private
|
data/lib/rester/utils.rb
CHANGED
@@ -46,6 +46,18 @@ module Rester
|
|
46
46
|
hash.inject({}){|memo,(k,v)| memo[k.to_sym] = v; memo}
|
47
47
|
end
|
48
48
|
|
49
|
+
def stringify_vals(hash={})
|
50
|
+
hash.inject({}) { |memo,(k,v)|
|
51
|
+
case v
|
52
|
+
when Hash
|
53
|
+
memo[k] = stringify_vals(v)
|
54
|
+
else
|
55
|
+
memo[k] = v.to_s
|
56
|
+
end
|
57
|
+
memo
|
58
|
+
}
|
59
|
+
end
|
60
|
+
|
49
61
|
def classify(str)
|
50
62
|
str.to_s.split("_").map(&:capitalize).join
|
51
63
|
end
|
data/lib/rester/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rester
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.3.
|
4
|
+
version: 0.3.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Robert Honer
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2015-11-
|
12
|
+
date: 2015-11-13 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rack
|