endpoint-flux2 1.1.6
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 +7 -0
- data/.github/FUNDING.yml +3 -0
- data/.github/workflows/gem-push.yml +42 -0
- data/.gitignore +36 -0
- data/CONTRIBUTING.md +18 -0
- data/Gemfile +6 -0
- data/Gemfile.lock +49 -0
- data/README.md +634 -0
- data/Rakefile +1 -0
- data/_config.yml +1 -0
- data/circle.yml +14 -0
- data/endpoint_flux.gemspec +20 -0
- data/lib/endpoint_flux.rb +22 -0
- data/lib/endpoint_flux/class_loader.rb +58 -0
- data/lib/endpoint_flux/config.rb +85 -0
- data/lib/endpoint_flux/config/interceptor.rb +23 -0
- data/lib/endpoint_flux/config/middleware.rb +38 -0
- data/lib/endpoint_flux/config/rescue_from.rb +28 -0
- data/lib/endpoint_flux/endpoint.rb +81 -0
- data/lib/endpoint_flux/exceptions.rb +10 -0
- data/lib/endpoint_flux/exceptions/base.rb +21 -0
- data/lib/endpoint_flux/exceptions/forbidden.rb +12 -0
- data/lib/endpoint_flux/exceptions/not_found.rb +12 -0
- data/lib/endpoint_flux/exceptions/service_unavailable.rb +12 -0
- data/lib/endpoint_flux/exceptions/unauthorized.rb +12 -0
- data/lib/endpoint_flux/exceptions/validation.rb +13 -0
- data/lib/endpoint_flux/middlewares.rb +8 -0
- data/lib/endpoint_flux/middlewares/authenticator/skip.rb +11 -0
- data/lib/endpoint_flux/middlewares/authorizator/skip.rb +11 -0
- data/lib/endpoint_flux/middlewares/decorator/add_status.rb +12 -0
- data/lib/endpoint_flux/middlewares/decorator/skip.rb +11 -0
- data/lib/endpoint_flux/middlewares/policy/skip.rb +11 -0
- data/lib/endpoint_flux/middlewares/validator/empty.rb +12 -0
- data/lib/endpoint_flux/rails/concerns/endpoint_controller.rb +43 -0
- data/lib/endpoint_flux/railtie.rb +14 -0
- data/lib/endpoint_flux/request.rb +17 -0
- data/lib/endpoint_flux/response.rb +30 -0
- data/lib/endpoint_flux/tasks/endpoint_flux/generators/endpoint_flux/decorators/articles/base.rb +12 -0
- data/lib/endpoint_flux/tasks/endpoint_flux/generators/endpoint_flux/decorators/boards/base.rb +12 -0
- data/lib/endpoint_flux/tasks/endpoint_flux/generators/endpoint_flux/decorators/boards/show.rb +22 -0
- data/lib/endpoint_flux/tasks/endpoint_flux/generators/endpoint_flux/decorators/tasks/base.rb +12 -0
- data/lib/endpoint_flux/tasks/endpoint_flux/generators/endpoint_flux/endpoints/articles/create.rb +27 -0
- data/lib/endpoint_flux/tasks/endpoint_flux/generators/endpoint_flux/endpoints/articles/destroy.rb +23 -0
- data/lib/endpoint_flux/tasks/endpoint_flux/generators/endpoint_flux/endpoints/articles/index.rb +26 -0
- data/lib/endpoint_flux/tasks/endpoint_flux/generators/endpoint_flux/endpoints/boards/create.rb +24 -0
- data/lib/endpoint_flux/tasks/endpoint_flux/generators/endpoint_flux/endpoints/boards/index.rb +21 -0
- data/lib/endpoint_flux/tasks/endpoint_flux/generators/endpoint_flux/endpoints/boards/show.rb +23 -0
- data/lib/endpoint_flux/tasks/endpoint_flux/generators/endpoint_flux/endpoints/tasks/create.rb +27 -0
- data/lib/endpoint_flux/tasks/endpoint_flux/generators/endpoint_flux/endpoints/tasks/destroy.rb +23 -0
- data/lib/endpoint_flux/tasks/endpoint_flux/generators/endpoint_flux/endpoints/tasks/index.rb +25 -0
- data/lib/endpoint_flux/tasks/endpoint_flux/generators/endpoint_flux/endpoints/tasks/update.rb +28 -0
- data/lib/endpoint_flux/tasks/endpoint_flux/generators/endpoint_flux/middlewares/decorator/paginate.rb +19 -0
- data/lib/endpoint_flux/tasks/endpoint_flux/generators/endpoint_flux/middlewares/decorator/representable.rb +24 -0
- data/lib/endpoint_flux/tasks/endpoint_flux/generators/endpoint_flux/middlewares/validator/inline.rb +17 -0
- data/lib/endpoint_flux/tasks/endpoint_flux/generators/endpoint_flux/validations/base.rb +21 -0
- data/lib/endpoint_flux/tasks/endpoint_flux/generators/endpoint_flux/validations/concern/error.rb +7 -0
- data/lib/endpoint_flux/tasks/endpoint_flux/generators/endpoint_flux/validations/predicates/base.rb +12 -0
- data/lib/endpoint_flux/tasks/endpoint_flux/generators/endpoint_flux/validations/predicates/bool.rb +20 -0
- data/lib/endpoint_flux/tasks/endpoint_flux/generators/endpoint_flux/validations/predicates/dates.rb +34 -0
- data/lib/endpoint_flux/tasks/endpoint_flux/generators/endpoint_flux/validations/predicates/decimal.rb +24 -0
- data/lib/endpoint_flux/tasks/endpoint_flux/generators/endpoint_flux/validations/predicates/email.rb +18 -0
- data/lib/endpoint_flux/tasks/endpoint_flux/generators/endpoint_flux/validations/predicates/password.rb +29 -0
- data/lib/endpoint_flux/tasks/endpoint_flux/generators/initializers/endpoint_flux.rb +41 -0
- data/lib/endpoint_flux/tasks/endpoint_flux/init.rake +12 -0
- data/lib/endpoint_flux/version.rb +3 -0
- data/spec/lib/class_loader_spec.rb +31 -0
- data/spec/lib/config/default_middlewares_spec.rb +21 -0
- data/spec/lib/config/endpoints_namespace_spec.rb +13 -0
- data/spec/lib/config/flow_spec.rb +8 -0
- data/spec/lib/config/interceptor_spec.rb +34 -0
- data/spec/lib/config/middleware_spec.rb +62 -0
- data/spec/lib/config/rescue_from_spec.rb +45 -0
- data/spec/lib/endpoint/flow_spec.rb +43 -0
- data/spec/lib/endpoint/middlewares_spec.rb +110 -0
- data/spec/lib/endpoint/perform_spec.rb +61 -0
- data/spec/lib/endpoint/rescue_from_spec.rb +61 -0
- data/spec/lib/endpoint_flux/rails/concerns/endpoint_controller_spec.rb +158 -0
- data/spec/lib/endpoint_flux/request_spec.rb +44 -0
- data/spec/lib/exceptions/forbidden_spec.rb +12 -0
- data/spec/lib/exceptions/not_found_spec.rb +12 -0
- data/spec/lib/exceptions/service_unavailable_spec.rb +12 -0
- data/spec/lib/exceptions/unauthorized_spec.rb +12 -0
- data/spec/lib/exceptions/validation_spec.rb +14 -0
- data/spec/lib/middlewares/authenticator/skip_spec.rb +5 -0
- data/spec/lib/middlewares/authorizator/skip_spec.rb +5 -0
- data/spec/lib/middlewares/decorator/add_status_spec.rb +17 -0
- data/spec/lib/middlewares/decorator/skip_spec.rb +5 -0
- data/spec/lib/middlewares/policy/skip_spec.rb +5 -0
- data/spec/lib/middlewares/shared_examples.rb +19 -0
- data/spec/lib/middlewares/validator/empty_spec.rb +15 -0
- data/spec/lib/response_spec.rb +131 -0
- data/spec/spec_helper.rb +56 -0
- metadata +203 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
require 'spec_helper'
|
|
2
|
+
|
|
3
|
+
RSpec.describe EndpointFlux::Config do
|
|
4
|
+
describe '#default_middlewares' do
|
|
5
|
+
before do
|
|
6
|
+
stub_const('EndpointFlux::Middlewares::Authenticator::Sample', Class.new)
|
|
7
|
+
EndpointFlux::Middlewares::Authenticator::Sample.class_eval do
|
|
8
|
+
def self.perform(_, headers, response)
|
|
9
|
+
[{}, headers, response]
|
|
10
|
+
end
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
subject.default_middlewares :authenticator, 'sample'
|
|
14
|
+
end
|
|
15
|
+
let(:middleware) { subject.default_middlewares[:authenticator].first }
|
|
16
|
+
|
|
17
|
+
it 'sets default middleware' do
|
|
18
|
+
expect(middleware.klass).to be(EndpointFlux::Middlewares::Authenticator::Sample)
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
end
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
require 'spec_helper'
|
|
2
|
+
|
|
3
|
+
RSpec.describe EndpointFlux::Config do
|
|
4
|
+
describe '#endpoints_namespace' do
|
|
5
|
+
it 'sets default endpoints namespace' do
|
|
6
|
+
stub_const 'Some::Sample', Class.new
|
|
7
|
+
|
|
8
|
+
subject.endpoints_namespace 'some/sample'
|
|
9
|
+
|
|
10
|
+
expect(subject.endpoints_namespace).to be == 'some/sample'
|
|
11
|
+
end
|
|
12
|
+
end
|
|
13
|
+
end
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
require 'spec_helper'
|
|
2
|
+
|
|
3
|
+
RSpec.describe EndpointFlux::Config::Interceptor do
|
|
4
|
+
describe '#add' do
|
|
5
|
+
context 'when wrong params given' do
|
|
6
|
+
it 'fails when no block given' do
|
|
7
|
+
expect { subject.add() }.to raise_error('Block not given')
|
|
8
|
+
end
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
context 'when valid params given' do
|
|
12
|
+
let(:block) { proc {} }
|
|
13
|
+
|
|
14
|
+
it 'adds the handler' do
|
|
15
|
+
subject.add(&block)
|
|
16
|
+
|
|
17
|
+
expect(subject.handlers).to include(block)
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
describe '#run' do
|
|
23
|
+
let(:sample_exception) { stub_const 'SampleException', Class.new(Exception) }
|
|
24
|
+
let(:block) { proc { raise SampleException } }
|
|
25
|
+
|
|
26
|
+
context 'when valid params given' do
|
|
27
|
+
it 'runs the handler' do
|
|
28
|
+
subject.add(&block)
|
|
29
|
+
expect { subject.run('attrs') }
|
|
30
|
+
.to raise_error(sample_exception)
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
end
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
require 'spec_helper'
|
|
2
|
+
|
|
3
|
+
RSpec.describe EndpointFlux::Config::Middleware do
|
|
4
|
+
let(:block) { proc { raise BlockException, 'bla bla' } }
|
|
5
|
+
let(:options) { { option: :option } }
|
|
6
|
+
|
|
7
|
+
before do
|
|
8
|
+
stub_const 'BlockException', Class.new(Exception)
|
|
9
|
+
stub_const 'PerformException', Class.new(Exception)
|
|
10
|
+
stub_const 'MiddlewareSample', Class.new
|
|
11
|
+
|
|
12
|
+
MiddlewareSample.class_eval do
|
|
13
|
+
def self.perform(*)
|
|
14
|
+
raise PerformException, 'bla bla'
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
context 'when wrong params given' do
|
|
20
|
+
it 'fails when there is no params given' do
|
|
21
|
+
expect { described_class.new }.to raise_error('You must provide block or existing klass')
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
it 'fails when there is no perform method' do
|
|
25
|
+
stub_const 'NoPerformMiddlewareSample', Class.new
|
|
26
|
+
|
|
27
|
+
expect do
|
|
28
|
+
described_class.new(NoPerformMiddlewareSample)
|
|
29
|
+
end.to raise_error('The [NoPerformMiddlewareSample] class should define perform class method')
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
context 'when valid params given' do
|
|
34
|
+
let(:subject) { described_class.new(MiddlewareSample, options, &block) }
|
|
35
|
+
|
|
36
|
+
it 'creates middleware instance with class' do
|
|
37
|
+
expect(subject.klass).to be(MiddlewareSample)
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
it 'creates middleware instance with handler' do
|
|
41
|
+
expect(subject.handler).to be(block)
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
it 'creates middleware instance with options' do
|
|
45
|
+
expect(subject.options).to be(options)
|
|
46
|
+
end
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
describe '#run' do
|
|
50
|
+
it 'runs klass perform when it is given' do
|
|
51
|
+
middleware = described_class.new(MiddlewareSample)
|
|
52
|
+
|
|
53
|
+
expect { middleware.run({}) }.to raise_error(PerformException)
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
it 'runs klass handler when it is given' do
|
|
57
|
+
middleware = described_class.new(&block)
|
|
58
|
+
|
|
59
|
+
expect { middleware.run({}) }.to raise_error(BlockException)
|
|
60
|
+
end
|
|
61
|
+
end
|
|
62
|
+
end
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
require 'spec_helper'
|
|
2
|
+
|
|
3
|
+
RSpec.describe EndpointFlux::Config::RescueFrom do
|
|
4
|
+
describe '#add' do
|
|
5
|
+
let(:sample_exception) { stub_const 'SampleException', Class.new(Exception) }
|
|
6
|
+
|
|
7
|
+
context 'when wrong params given' do
|
|
8
|
+
it 'fails when no block given' do
|
|
9
|
+
expect { subject.add(sample_exception) }.to raise_error('Block not given')
|
|
10
|
+
end
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
context 'when valid params given' do
|
|
14
|
+
let(:block) { proc {} }
|
|
15
|
+
|
|
16
|
+
it 'adds the exception handler' do
|
|
17
|
+
subject.add(sample_exception, &block)
|
|
18
|
+
|
|
19
|
+
expect(subject.exceptions).to include(sample_exception)
|
|
20
|
+
expect(subject.handlers.values).to include(block)
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
describe '#run' do
|
|
26
|
+
let(:sample_exception) { stub_const 'SampleException', Class.new(Exception) }
|
|
27
|
+
let(:block_exception) { stub_const 'BlockException', Class.new(Exception) }
|
|
28
|
+
let(:block) { proc { raise BlockException } }
|
|
29
|
+
|
|
30
|
+
context 'when wrong params given' do
|
|
31
|
+
it 'fails when handler not given' do
|
|
32
|
+
expect { subject.run('name', 'attrs', sample_exception.new) }
|
|
33
|
+
.to raise_error('No handler given')
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
context 'when valid params given' do
|
|
38
|
+
it 'runs the handler' do
|
|
39
|
+
subject.add(sample_exception, &block)
|
|
40
|
+
expect { subject.run('name', 'attrs', sample_exception.new) }
|
|
41
|
+
.to raise_error(block_exception)
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
end
|
|
45
|
+
end
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
require 'spec_helper'
|
|
2
|
+
|
|
3
|
+
RSpec.describe EndpointFlux::Endpoint do
|
|
4
|
+
let(:klass) { stub_const 'Sample', Class.new }
|
|
5
|
+
|
|
6
|
+
before do
|
|
7
|
+
klass.include EndpointFlux::Endpoint
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
describe '#flow' do
|
|
11
|
+
let(:params) { { some: :val } }
|
|
12
|
+
let(:middleware_request) { EndpointFlux::Request.new(headers: {}, params: params) }
|
|
13
|
+
let(:middleware_response) { EndpointFlux::Response.new }
|
|
14
|
+
|
|
15
|
+
before do
|
|
16
|
+
stub_const('EndpointFlux::Middlewares::Validator::Sample', Class.new)
|
|
17
|
+
EndpointFlux::Middlewares::Validator::Sample.class_eval do
|
|
18
|
+
def self.perform(request, response, _)
|
|
19
|
+
request.params = {}
|
|
20
|
+
[request, response]
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
stub_const('EndpointFlux::Middlewares::Decorator::Sample', Class.new)
|
|
25
|
+
EndpointFlux::Middlewares::Decorator::Sample.class_eval do
|
|
26
|
+
def self.perform(request, _response, _)
|
|
27
|
+
[request, EndpointFlux::Response.new(headers: {}, body: {})]
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
klass.flow %i[validator decorator]
|
|
32
|
+
|
|
33
|
+
EndpointFlux.config.default_middlewares :validator, :sample
|
|
34
|
+
EndpointFlux.config.default_middlewares :decorator, 'sample'
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
it 'uses default middleware if not defined' do
|
|
38
|
+
request, response = klass.perform(middleware_request)
|
|
39
|
+
expect(request.params).to eq({})
|
|
40
|
+
expect(response.body).to eq({})
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
end
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
require 'spec_helper'
|
|
2
|
+
|
|
3
|
+
RSpec.describe EndpointFlux::Endpoint do
|
|
4
|
+
let(:klass) { class_double('Sample') }
|
|
5
|
+
|
|
6
|
+
before do
|
|
7
|
+
EndpointFlux.config EndpointFlux::Config.new
|
|
8
|
+
|
|
9
|
+
klass.include EndpointFlux::Endpoint
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
context 'with exceptions' do
|
|
13
|
+
it 'fails with wrong string' do
|
|
14
|
+
klass.flow [:validator]
|
|
15
|
+
|
|
16
|
+
expect do
|
|
17
|
+
klass.validator 'some_not_existing_class'
|
|
18
|
+
end.to raise_error('You must provide block or existing klass')
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
it 'fails whit wrong class passed' do
|
|
22
|
+
stub_const 'EndpointFlux::Middlewares::Validator::SampleMiddleware', Class.new
|
|
23
|
+
|
|
24
|
+
klass.flow [:validator]
|
|
25
|
+
|
|
26
|
+
expect { klass.validator 'sample_middleware' }.to raise_error(
|
|
27
|
+
'The [EndpointFlux::Middlewares::Validator::SampleMiddleware] '\
|
|
28
|
+
'class should define perform class method'
|
|
29
|
+
)
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
context 'when wrong param passed' do
|
|
33
|
+
it do
|
|
34
|
+
klass.flow [:validator]
|
|
35
|
+
|
|
36
|
+
expect { klass.validator 1334 }.to raise_error(
|
|
37
|
+
'You must provide block or existing klass'
|
|
38
|
+
)
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
it do
|
|
42
|
+
stub_const 'SampleMiddleware', Class.new
|
|
43
|
+
|
|
44
|
+
klass.flow [:validator]
|
|
45
|
+
|
|
46
|
+
expect { klass.validator SampleMiddleware }.to raise_error(
|
|
47
|
+
'You must provide block or existing klass'
|
|
48
|
+
)
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
describe 'middlewares' do
|
|
54
|
+
before do
|
|
55
|
+
stub_const 'SomeNamespace::MiddlewareName::SampleMiddleware', Class.new
|
|
56
|
+
SomeNamespace::MiddlewareName::SampleMiddleware.class_eval do
|
|
57
|
+
def self.perform(*args)
|
|
58
|
+
args
|
|
59
|
+
end
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
EndpointFlux.config.middlewares_namespaces << 'some_namespace'
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
before do
|
|
66
|
+
klass.flow [:middleware_name]
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
let(:middleware) { klass.middleware_name.first }
|
|
70
|
+
|
|
71
|
+
it 'sets middleware with options' do
|
|
72
|
+
klass.middleware_name 'sample_middleware', some: :option
|
|
73
|
+
|
|
74
|
+
expect(middleware.klass).to be(SomeNamespace::MiddlewareName::SampleMiddleware)
|
|
75
|
+
expect(middleware.options).to eq(some: :option)
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
it 'sets middleware' do
|
|
79
|
+
klass.middleware_name 'sample_middleware'
|
|
80
|
+
|
|
81
|
+
expect(middleware.klass).to be(SomeNamespace::MiddlewareName::SampleMiddleware)
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
it 'does not add twice' do
|
|
85
|
+
klass.middleware_name 'sample_middleware'
|
|
86
|
+
klass.middleware_name 'sample_middleware'
|
|
87
|
+
|
|
88
|
+
middleware = klass.middleware_name.first
|
|
89
|
+
|
|
90
|
+
expect(middleware.klass).to be(SomeNamespace::MiddlewareName::SampleMiddleware)
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
context 'block' do
|
|
94
|
+
let(:block) { proc {} }
|
|
95
|
+
|
|
96
|
+
it 'sets middleware' do
|
|
97
|
+
klass.middleware_name(&block)
|
|
98
|
+
|
|
99
|
+
expect(middleware.handler).to be == block
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
it 'does not adds twice' do
|
|
103
|
+
klass.middleware_name(&block)
|
|
104
|
+
klass.middleware_name(&block)
|
|
105
|
+
|
|
106
|
+
expect(middleware.handler).to be == block
|
|
107
|
+
end
|
|
108
|
+
end
|
|
109
|
+
end
|
|
110
|
+
end
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
require 'spec_helper'
|
|
2
|
+
|
|
3
|
+
RSpec.describe EndpointFlux::Endpoint do
|
|
4
|
+
let(:klass) { stub_const 'Sample', Class.new }
|
|
5
|
+
let(:middleware_request) { EndpointFlux::Request.new(headers: {}, params: params) }
|
|
6
|
+
let(:middleware_response) { EndpointFlux::Response.new }
|
|
7
|
+
|
|
8
|
+
before do
|
|
9
|
+
EndpointFlux.config EndpointFlux::Config.new
|
|
10
|
+
|
|
11
|
+
klass.include EndpointFlux::Endpoint
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
describe '#perform' do
|
|
15
|
+
before do
|
|
16
|
+
stub_const 'EndpointFlux::Middlewares::Validator::SampleValidator', Class.new
|
|
17
|
+
EndpointFlux::Middlewares::Validator::SampleValidator.class_eval do
|
|
18
|
+
def self.perform(request, response, _)
|
|
19
|
+
request.params = {}
|
|
20
|
+
[request, response, {}]
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
let(:params) { { some: :val } }
|
|
26
|
+
|
|
27
|
+
it 'checks if middleware exists' do
|
|
28
|
+
klass.flow [:validator]
|
|
29
|
+
|
|
30
|
+
expect { klass.perform(middleware_request) }
|
|
31
|
+
.to raise_error('No middleware registred for [:validator]')
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
context 'middleware passed as string' do
|
|
35
|
+
it 'runs middleware passed as string' do
|
|
36
|
+
klass.flow [:validator]
|
|
37
|
+
klass.validator 'sample_validator'
|
|
38
|
+
|
|
39
|
+
request, response = klass.perform(middleware_request)
|
|
40
|
+
|
|
41
|
+
expect(request.params).to eq({})
|
|
42
|
+
expect(response.body).to eq({})
|
|
43
|
+
end
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
context 'default middleware' do
|
|
47
|
+
before do
|
|
48
|
+
klass.flow [:validator]
|
|
49
|
+
|
|
50
|
+
EndpointFlux.config.default_middlewares :validator, 'sample_validator'
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
it 'uses default if not defined' do
|
|
54
|
+
request, response = klass.perform(middleware_request)
|
|
55
|
+
|
|
56
|
+
expect(request.params).to eq({})
|
|
57
|
+
expect(response.body).to eq({})
|
|
58
|
+
end
|
|
59
|
+
end
|
|
60
|
+
end
|
|
61
|
+
end
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
require 'spec_helper'
|
|
2
|
+
|
|
3
|
+
RSpec.describe EndpointFlux::Endpoint do
|
|
4
|
+
let(:klass) { stub_const 'Sample', Class.new }
|
|
5
|
+
let(:middleware_request) { EndpointFlux::Request.new(headers: {}, params: {}) }
|
|
6
|
+
let(:middleware_response) { EndpointFlux::Response.new }
|
|
7
|
+
|
|
8
|
+
before do
|
|
9
|
+
EndpointFlux.config EndpointFlux::Config.new
|
|
10
|
+
|
|
11
|
+
klass.include EndpointFlux::Endpoint
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
describe '#rescue_from' do
|
|
15
|
+
let(:sample_exception) { stub_const 'SampleException', Class.new(Exception) }
|
|
16
|
+
let!(:sample_validator) do
|
|
17
|
+
stub_const 'EndpointFlux::Middlewares::Validator::Sample', Class.new
|
|
18
|
+
EndpointFlux::Middlewares::Validator::Sample.class_eval do
|
|
19
|
+
def self.perform(*)
|
|
20
|
+
raise SampleException, 'bla bla'
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
EndpointFlux::Middlewares::Validator::Sample
|
|
24
|
+
end
|
|
25
|
+
let!(:sample_decorator) do
|
|
26
|
+
stub_const 'EndpointFlux::Middlewares::Decorator::Sample', Class.new
|
|
27
|
+
EndpointFlux::Middlewares::Decorator::Sample.class_eval do
|
|
28
|
+
def self.perform(request, response, options)
|
|
29
|
+
response.body[:decorator] = true
|
|
30
|
+
[request, response, options]
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
EndpointFlux::Middlewares::Decorator::Sample
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
it 'fails if exception not found' do
|
|
37
|
+
expect do
|
|
38
|
+
EndpointFlux.config.rescue_from 'NonExsistingClasss' do |_, attrs|
|
|
39
|
+
# code
|
|
40
|
+
end
|
|
41
|
+
end.to raise_error('The [NonExsistingClasss] should be a string representing a class')
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
context 'default middleware' do
|
|
45
|
+
before do
|
|
46
|
+
klass.flow %i[validator decorator]
|
|
47
|
+
klass.validator 'sample'
|
|
48
|
+
klass.decorator 'sample'
|
|
49
|
+
|
|
50
|
+
EndpointFlux.config.rescue_from sample_exception do |_, attrs|
|
|
51
|
+
[attrs[0], attrs[1], { status: false }]
|
|
52
|
+
end
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
it 'uses default if not defined' do
|
|
56
|
+
_, response = klass.perform(middleware_request)
|
|
57
|
+
expect(response.body[:status]).to be_falsey
|
|
58
|
+
end
|
|
59
|
+
end
|
|
60
|
+
end
|
|
61
|
+
end
|