blest 0.0.1 → 0.1.0
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/README.md +64 -111
- data/lib/blest.rb +808 -143
- data/spec/blest_spec.rb +219 -0
- metadata +3 -2
data/spec/blest_spec.rb
ADDED
@@ -0,0 +1,219 @@
|
|
1
|
+
require 'rspec'
|
2
|
+
require 'securerandom'
|
3
|
+
require_relative '../lib/blest'
|
4
|
+
|
5
|
+
RSpec.describe Router do
|
6
|
+
|
7
|
+
benchmarks = []
|
8
|
+
|
9
|
+
router = Router.new(timeout: 1000)
|
10
|
+
router2 = Router.new(timeout: 10)
|
11
|
+
router3 = Router.new
|
12
|
+
|
13
|
+
testId1 = nil
|
14
|
+
testValue1 = nil
|
15
|
+
result1 = nil
|
16
|
+
error1 = nil
|
17
|
+
testId2 = nil
|
18
|
+
testValue2 = nil
|
19
|
+
result2 = nil
|
20
|
+
error2 = nil
|
21
|
+
testId3 = nil
|
22
|
+
testValue3 = nil
|
23
|
+
result3 = nil
|
24
|
+
error3 = nil
|
25
|
+
testId4 = nil
|
26
|
+
testValue4 = nil
|
27
|
+
result4 = nil
|
28
|
+
error4 = nil
|
29
|
+
testId5 = nil
|
30
|
+
testValue5 = nil
|
31
|
+
result5 = nil
|
32
|
+
error5 = nil
|
33
|
+
testId6 = nil
|
34
|
+
testValue6 = nil
|
35
|
+
result6 = nil
|
36
|
+
error6 = nil
|
37
|
+
|
38
|
+
before(:all) do
|
39
|
+
router.route('basicRoute') do |parameters, context|
|
40
|
+
{ 'route'=> 'basicRoute', 'parameters' => parameters, 'context' => context }
|
41
|
+
end
|
42
|
+
|
43
|
+
router.before do |parameters, context|
|
44
|
+
context['test'] = { 'value' => parameters['testValue'] }
|
45
|
+
nil
|
46
|
+
end
|
47
|
+
|
48
|
+
router.after do |_, context|
|
49
|
+
complete_time = Time.now
|
50
|
+
difference = (complete_time - context['requestTime'])
|
51
|
+
benchmarks.push(difference)
|
52
|
+
nil
|
53
|
+
end
|
54
|
+
|
55
|
+
router2.route('mergedRoute') do |parameters, context|
|
56
|
+
{ 'route' => 'mergedRoute', 'parameters' => parameters, 'context' => context }
|
57
|
+
end
|
58
|
+
|
59
|
+
router2.route('timeoutRoute') do |parameters|
|
60
|
+
sleep(0.2)
|
61
|
+
{ 'testValue' => parameters['testValue'] }
|
62
|
+
end
|
63
|
+
|
64
|
+
router.merge(router2)
|
65
|
+
|
66
|
+
router3.route('errorRoute') do |parameters|
|
67
|
+
error = BlestError.new(parameters['testValue'])
|
68
|
+
error.code = "ERROR_#{(parameters['testValue'].to_f * 10).round}"
|
69
|
+
raise error
|
70
|
+
end
|
71
|
+
|
72
|
+
router.namespace('subRoutes', router3)
|
73
|
+
|
74
|
+
# puts router.routes
|
75
|
+
|
76
|
+
# Basic route
|
77
|
+
testId1 = SecureRandom.uuid
|
78
|
+
testValue1 = rand
|
79
|
+
result1, error1 = router.handle([[testId1, 'basicRoute', { 'testValue' => testValue1 }]], { 'testValue' => testValue1 })
|
80
|
+
|
81
|
+
# Merged route
|
82
|
+
testId2 = SecureRandom.uuid
|
83
|
+
testValue2 = rand
|
84
|
+
result2, error2 = router.handle([[testId2, 'mergedRoute', { 'testValue' => testValue2 }]], { 'testValue' => testValue2 })
|
85
|
+
|
86
|
+
# Error route
|
87
|
+
testId3 = SecureRandom.uuid
|
88
|
+
testValue3 = rand
|
89
|
+
result3, error3 = router.handle([[testId3, 'subRoutes/errorRoute', { 'testValue' => testValue3 }]], { 'testValue' => testValue3 })
|
90
|
+
|
91
|
+
# Missing route
|
92
|
+
testId4 = SecureRandom.uuid
|
93
|
+
testValue4 = rand
|
94
|
+
result4, error4 = router.handle([[testId4, 'missingRoute', { 'testValue' => testValue4 }]], { 'testValue' => testValue4 })
|
95
|
+
|
96
|
+
# Timeout route
|
97
|
+
testId5 = SecureRandom.uuid
|
98
|
+
testValue5 = rand
|
99
|
+
result5, error5 = router.handle([[testId5, 'timeoutRoute', { 'testValue' => testValue5 }]], { 'testValue' => testValue5 })
|
100
|
+
|
101
|
+
# Malformed request
|
102
|
+
testId6 = SecureRandom.uuid
|
103
|
+
result6, error6 = router.handle([[testId6], {}, [true, 1.25]])
|
104
|
+
|
105
|
+
end
|
106
|
+
|
107
|
+
it 'should have class properties' do
|
108
|
+
expect(router.is_a?(Router)).to be_truthy
|
109
|
+
expect(router.routes.keys.length).to eq(4)
|
110
|
+
expect(router).to respond_to(:handle)
|
111
|
+
end
|
112
|
+
|
113
|
+
it 'should have class properties' do
|
114
|
+
expect(router).to be_a(Router)
|
115
|
+
expect(router.routes.keys.length).to eq(4)
|
116
|
+
expect(router).to respond_to(:handle)
|
117
|
+
end
|
118
|
+
|
119
|
+
it 'should process all valid requests' do
|
120
|
+
expect(error1).to be_nil
|
121
|
+
expect(error2).to be_nil
|
122
|
+
expect(error3).to be_nil
|
123
|
+
expect(error4).to be_nil
|
124
|
+
expect(error5).to be_nil
|
125
|
+
end
|
126
|
+
|
127
|
+
it 'should return matching IDs' do
|
128
|
+
expect(result1[0][0]).to eq(testId1)
|
129
|
+
expect(result2[0][0]).to eq(testId2)
|
130
|
+
expect(result3[0][0]).to eq(testId3)
|
131
|
+
expect(result4[0][0]).to eq(testId4)
|
132
|
+
expect(result5[0][0]).to eq(testId5)
|
133
|
+
end
|
134
|
+
|
135
|
+
it 'should return matching routes' do
|
136
|
+
expect(result1[0][1]).to eq('basicRoute')
|
137
|
+
expect(result2[0][1]).to eq('mergedRoute')
|
138
|
+
expect(result3[0][1]).to eq('subRoutes/errorRoute')
|
139
|
+
expect(result4[0][1]).to eq('missingRoute')
|
140
|
+
expect(result5[0][1]).to eq('timeoutRoute')
|
141
|
+
end
|
142
|
+
|
143
|
+
it 'should accept parameters' do
|
144
|
+
expect(result1[0][2]['parameters']['testValue']).to eq(testValue1)
|
145
|
+
expect(result2[0][2]['parameters']['testValue']).to eq(testValue2)
|
146
|
+
end
|
147
|
+
|
148
|
+
it 'should respect context' do
|
149
|
+
expect(result1[0][2]['context']['testValue']).to eq(testValue1)
|
150
|
+
expect(result2[0][2]['context']['testValue']).to eq(testValue2)
|
151
|
+
end
|
152
|
+
|
153
|
+
it 'should support middleware' do
|
154
|
+
expect(result1[0][2]['context']['test']).to be_nil
|
155
|
+
expect(result2[0][2]['context']['test']['value']).to eq(testValue2)
|
156
|
+
end
|
157
|
+
|
158
|
+
it 'should handle errors correctly' do
|
159
|
+
expect(result1[0][3]).to be_nil
|
160
|
+
expect(result2[0][3]).to be_nil
|
161
|
+
expect(result3[0][3]['message']).to eq(testValue3.to_s)
|
162
|
+
expect(result3[0][3]['status']).to eq(500)
|
163
|
+
expect(result3[0][3]['code']).to eq("ERROR_#{(testValue3 * 10).round}")
|
164
|
+
expect(result4[0][3]['message']).to eq('Not Found')
|
165
|
+
expect(result4[0][3]['status']).to eq(404)
|
166
|
+
end
|
167
|
+
|
168
|
+
it 'should support timeout setting' do
|
169
|
+
expect(result5[0][2]).to be_nil
|
170
|
+
expect(result5[0][3]['message']).to eq('Internal Server Error')
|
171
|
+
expect(result5[0][3]['status']).to eq(500)
|
172
|
+
end
|
173
|
+
|
174
|
+
it 'should reject malformed requests' do
|
175
|
+
expect(error6['message']).not_to be_nil
|
176
|
+
end
|
177
|
+
|
178
|
+
it 'should allow trailing middleware' do
|
179
|
+
expect(benchmarks.length).to eq(1)
|
180
|
+
end
|
181
|
+
|
182
|
+
it 'should throw an error for invalid routes' do
|
183
|
+
handler = proc {}
|
184
|
+
expect { router.route('a', &handler) }.to raise_error(ArgumentError)
|
185
|
+
expect { router.route('0abc', &handler) }.to raise_error(ArgumentError)
|
186
|
+
expect { router.route('_abc', &handler) }.to raise_error(ArgumentError)
|
187
|
+
expect { router.route('-abc', &handler) }.to raise_error(ArgumentError)
|
188
|
+
expect { router.route('abc_', &handler) }.to raise_error(ArgumentError)
|
189
|
+
expect { router.route('abc-', &handler) }.to raise_error(ArgumentError)
|
190
|
+
expect { router.route('abc/0abc', &handler) }.to raise_error(ArgumentError)
|
191
|
+
expect { router.route('abc/_abc', &handler) }.to raise_error(ArgumentError)
|
192
|
+
expect { router.route('abc/-abc', &handler) }.to raise_error(ArgumentError)
|
193
|
+
expect { router.route('abc/', &handler) }.to raise_error(ArgumentError)
|
194
|
+
expect { router.route('/abc', &handler) }.to raise_error(ArgumentError)
|
195
|
+
expect { router.route('abc//abc', &handler) }.to raise_error(ArgumentError)
|
196
|
+
expect { router.route('abc/a/abc', &handler) }.to raise_error(ArgumentError)
|
197
|
+
expect { router.route('abc/0abc/abc', &handler) }.to raise_error(ArgumentError)
|
198
|
+
expect { router.route('abc/_abc/abc', &handler) }.to raise_error(ArgumentError)
|
199
|
+
expect { router.route('abc/-abc/abc', &handler) }.to raise_error(ArgumentError)
|
200
|
+
expect { router.route('abc/abc_/abc', &handler) }.to raise_error(ArgumentError)
|
201
|
+
expect { router.route('abc/abc-/abc', &handler) }.to raise_error(ArgumentError)
|
202
|
+
end
|
203
|
+
|
204
|
+
end
|
205
|
+
|
206
|
+
|
207
|
+
|
208
|
+
RSpec.describe HttpClient do
|
209
|
+
client = HttpClient.new('http://localhost:8080', max_batch_size = 25, buffer_delay = 10)
|
210
|
+
|
211
|
+
it 'should have class properties' do
|
212
|
+
expect(client.is_a?(HttpClient)).to be_truthy
|
213
|
+
expect(client.url).to eq('http://localhost:8080')
|
214
|
+
expect(client.max_batch_size).to eq(25)
|
215
|
+
expect(client.buffer_delay).to eq(10)
|
216
|
+
expect(client).to respond_to(:request)
|
217
|
+
end
|
218
|
+
|
219
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: blest
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- JHunt
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2023-
|
11
|
+
date: 2023-08-01 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: The Ruby reference implementation of BLEST (Batch-able, Lightweight,
|
14
14
|
Encrypted State Transfer), an improved communication protocol for web APIs which
|
@@ -23,6 +23,7 @@ files:
|
|
23
23
|
- LICENSE
|
24
24
|
- README.md
|
25
25
|
- lib/blest.rb
|
26
|
+
- spec/blest_spec.rb
|
26
27
|
homepage: https://blest.jhunt.dev
|
27
28
|
licenses:
|
28
29
|
- MIT
|