rack-rest-rspec 0.0.4 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.debride_whitelist +6 -0
- data/.github/workflows/main.yml +23 -0
- data/.rspec +2 -0
- data/.rubocop.yml +44 -0
- data/Gemfile +2 -17
- data/LICENSE.txt +21 -0
- data/README.md +198 -37
- data/Rakefile +42 -35
- data/VERSION +1 -1
- data/bom.xml +690 -0
- data/lib/rack-rest-rspec/helpers/init.rb +3 -2
- data/lib/rack-rest-rspec/helpers/memorizer.rb +6 -5
- data/lib/rack-rest-rspec/helpers/yml.rb +3 -1
- data/lib/rack-rest-rspec/lib/rest_service.rb +10 -13
- data/lib/rack-rest-rspec/matchers/be_correctly_sent.rb +4 -2
- data/lib/rack-rest-rspec/matchers/init.rb +3 -2
- data/lib/rack-rest-rspec/matchers/respond_a_collection_of_record.rb +13 -5
- data/lib/rack-rest-rspec/matchers/respond_a_record.rb +13 -5
- data/lib/rack-rest-rspec/matchers/respond_with_collection_size.rb +15 -5
- data/lib/rack-rest-rspec/matchers/respond_with_data.rb +23 -25
- data/lib/rack-rest-rspec/matchers/respond_with_status.rb +10 -4
- data/lib/rack-rest-rspec/prepare.rb +5 -3
- data/lib/rack-rest-rspec/version.rb +2 -0
- data/rack-rest-rspec.gemspec +34 -23
- data/samples/.rspec +2 -0
- data/samples/Gemfile +14 -0
- data/samples/Rakefile +8 -0
- data/samples/app.rb +27 -0
- data/samples/config.ru +10 -0
- data/samples/spec/api_spec.rb +40 -0
- data/samples/spec/spec_helper.rb +4 -0
- metadata +116 -14
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b5eeab5a42b95fad3925b35beb70d00b9b5ba87cb0813e42d45dd1f7f3f9f7da
|
4
|
+
data.tar.gz: bc8e33415d1fd2ebc6b9f9a4095a575c0f37c5d24792355c9c914082edeefcf6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a0204cb39a36df05af11f9de67cfa808d5754861de3ca86931c848a974b2f6d6c625f44e90adfdb636f708291216dfea3b59e91f651d5b53fa5642b29754e58c
|
7
|
+
data.tar.gz: 955bb74528ca5ec0d4945fd53564990f1ee0d9c19be86f669137dbdabeef0bedff4f99b3e0c871ba36c2b15b23512d24e4800cc18ec439829f4c354d98181afb
|
data/.debride_whitelist
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
name: Ruby
|
2
|
+
|
3
|
+
on: [push,pull_request]
|
4
|
+
|
5
|
+
jobs:
|
6
|
+
build:
|
7
|
+
runs-on: ubuntu-latest
|
8
|
+
steps:
|
9
|
+
- uses: actions/checkout@v2
|
10
|
+
- name: Set up Ruby
|
11
|
+
uses: ruby/setup-ruby@v1
|
12
|
+
with:
|
13
|
+
ruby-version: 3.2.3
|
14
|
+
- name: Getting dependencies (Bundle)
|
15
|
+
run: |
|
16
|
+
gem install bundler -v 2.2.3
|
17
|
+
bundle install
|
18
|
+
- name: Running test Rspec
|
19
|
+
run: |
|
20
|
+
bundle exec rake
|
21
|
+
- name: Running CVE security audit
|
22
|
+
run: |
|
23
|
+
bundle exec rake audit
|
data/.rspec
ADDED
data/.rubocop.yml
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
AllCops:
|
2
|
+
NewCops: enable
|
3
|
+
|
4
|
+
# This configuration was generated by
|
5
|
+
# `rubocop --auto-gen-config`
|
6
|
+
# on 2024-05-03 14:08:48 UTC using RuboCop version 1.63.4.
|
7
|
+
# The point is for the user to remove these configuration records
|
8
|
+
# one by one as the offenses are removed from the code base.
|
9
|
+
# Note that changes in the inspected code, or installation of new
|
10
|
+
# versions of RuboCop, may require this file to be generated again.
|
11
|
+
|
12
|
+
# Offense count: 2
|
13
|
+
# Configuration parameters: CountComments, CountAsOne, AllowedMethods, AllowedPatterns.
|
14
|
+
# AllowedMethods: refine
|
15
|
+
Metrics/BlockLength:
|
16
|
+
Max: 32
|
17
|
+
|
18
|
+
Gemspec/DevelopmentDependencies:
|
19
|
+
Enabled: false
|
20
|
+
|
21
|
+
# Offense count: 3
|
22
|
+
# Configuration parameters: AllowedConstants.
|
23
|
+
Style/Documentation:
|
24
|
+
Exclude:
|
25
|
+
- 'spec/**/*'
|
26
|
+
- 'test/**/*'
|
27
|
+
- 'lib/rack-rest-rspec/lib/rest_service.rb'
|
28
|
+
- 'lib/rack-rest-rspec/version.rb'
|
29
|
+
- 'samples/app.rb'
|
30
|
+
|
31
|
+
# Offense count: 23
|
32
|
+
# Configuration parameters: AllowedVariables.
|
33
|
+
Style/GlobalVars:
|
34
|
+
Exclude:
|
35
|
+
- 'lib/rack-rest-rspec/helpers/memorizer.rb'
|
36
|
+
- 'samples/spec/api_spec.rb'
|
37
|
+
- 'spec/api_spec.rb'
|
38
|
+
|
39
|
+
# Offense count: 1
|
40
|
+
# This cop supports safe autocorrection (--autocorrect).
|
41
|
+
# Configuration parameters: AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, AllowedPatterns.
|
42
|
+
# URISchemes: http, https
|
43
|
+
Layout/LineLength:
|
44
|
+
Max: 122
|
data/Gemfile
CHANGED
@@ -1,20 +1,5 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
source
|
3
|
+
source 'https://rubygems.org'
|
4
4
|
|
5
|
-
|
6
|
-
|
7
|
-
group :test, :development do
|
8
|
-
gem "roodi", "~> 5.0"
|
9
|
-
gem "rake", "~> 13.0"
|
10
|
-
gem "code_statistics", "~> 0.2.13"
|
11
|
-
gem "rspec", "~> 3.11"
|
12
|
-
gem "yard", "~> 0.9.28"
|
13
|
-
gem "yard-rspec", "~> 0.1"
|
14
|
-
end
|
15
|
-
|
16
|
-
gem "rack-test", "~> 2.0"
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
gem "version", "~> 1.1"
|
5
|
+
gemspec
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2024 Romain GEORGES
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
CHANGED
@@ -19,60 +19,221 @@ or
|
|
19
19
|
|
20
20
|
### Basic usage : sample of spec matchers usage
|
21
21
|
|
22
|
-
|
23
|
-
before :all do
|
24
|
-
$service = RestService::new :service => MYRack::ModularApp
|
25
|
-
end
|
22
|
+
Considering this API made with [Sinatra](https://sinatrarb.com/) :
|
26
23
|
|
27
|
-
subject { $service }
|
28
|
-
context "GET /api/v1/posts : test for a collections of Post (empty)" do
|
29
|
-
it { expect(subject.get('/api/v1/posts')).to be_correctly_sent }
|
30
|
-
it { expect(subject).to respond_with_status 200 }
|
31
|
-
it { expect(subject).to respond_a_collection_of_record }
|
32
|
-
it { expect(subject).to respond_with_collection_size 0 }
|
33
|
-
end
|
34
24
|
|
35
|
-
|
36
|
-
|
37
|
-
it { expect(subject).to respond_with_status 201 }
|
38
|
-
end
|
25
|
+
```ruby
|
26
|
+
class Application < Sinatra::Base
|
39
27
|
|
40
|
-
context "GET /api/v1/posts : test for a collections of Post" do
|
41
|
-
it { expect(subject.get('/api/v1/posts')).to be_correctly_sent }
|
42
|
-
it { expect(subject).to respond_with_status 200 }
|
43
|
-
it { expect(subject).to respond_a_collection_of_record }
|
44
|
-
it { expect(subject).to respond_with_collection_size 1 }
|
45
|
-
end
|
46
28
|
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
it { expect(subject).to respond_a_record }
|
51
|
-
it { expect(subject).to respond_with_data({id: 1, title: "test", body: "content"}) }
|
52
|
-
end
|
29
|
+
before do
|
30
|
+
content_type 'application/json'
|
31
|
+
end
|
53
32
|
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
33
|
+
get '/status' do
|
34
|
+
status 208
|
35
|
+
return {name: "Sample App", version: "0.0.1", status: 'OK'}.to_json
|
36
|
+
end
|
58
37
|
|
38
|
+
get '/embeded_status' do
|
39
|
+
status 208
|
40
|
+
return {code: 208, data: {name: "Sample App", version: "0.0.1", status: 'OK'}}.to_json
|
41
|
+
end
|
59
42
|
|
43
|
+
get '/collection' do
|
44
|
+
return [{one: 1}, {two: 2}, {three: 3}].to_json
|
45
|
+
end
|
60
46
|
|
47
|
+
get '/embeded_collection' do
|
48
|
+
return {code: 200, data: [{one: 1}, {two: 2}, {three: 3}]}.to_json
|
61
49
|
end
|
62
50
|
|
51
|
+
end
|
52
|
+
```
|
53
|
+
|
54
|
+
You could test this with rack-rest-rspec :
|
55
|
+
|
56
|
+
```ruby
|
57
|
+
describe 'Test REST API' do
|
58
|
+
before :all do
|
59
|
+
$service = RestService::new :service => Application
|
60
|
+
$data = {name: "Sample App", version: "0.0.1", status: 'OK'}
|
61
|
+
$collection = [{one: 1}, {two: 2}, {three: 3}]
|
62
|
+
end
|
63
|
+
|
64
|
+
subject { $service }
|
65
|
+
context "GET /status : test for status" do
|
66
|
+
it { expect(subject.get('/status')).to be_correctly_sent }
|
67
|
+
it { expect(subject).to respond_with_status code: 208 }
|
68
|
+
it { expect(subject).to respond_a_record }
|
69
|
+
it { expect(subject).to respond_with data: $data }
|
70
|
+
end
|
71
|
+
|
72
|
+
context "GET /embeded_status : test for status" do
|
73
|
+
it { expect(subject.get('/embeded_status')).to be_correctly_sent }
|
74
|
+
it { expect(subject).to respond_with_status code: 208 }
|
75
|
+
it { expect(subject).to respond_a_record root: :data }
|
76
|
+
it { expect(subject).to respond_with data: $data, root: :data }
|
77
|
+
end
|
78
|
+
|
79
|
+
|
80
|
+
context "GET /collection : test for Array" do
|
81
|
+
it { expect(subject.get('/collection')).to be_correctly_sent }
|
82
|
+
it { expect(subject).to respond_with_status code: 200 }
|
83
|
+
it { expect(subject).to respond_a_collection_of_record }
|
84
|
+
it { expect(subject).to respond_with_collection size: 3 }
|
85
|
+
it { expect(subject).to respond_with data: $collection }
|
86
|
+
end
|
87
|
+
|
88
|
+
|
89
|
+
context "GET /embeded_collection : test for Array" do
|
90
|
+
it { expect(subject.get('/embeded_collection')).to be_correctly_sent }
|
91
|
+
it { expect(subject).to respond_with_status code: 200 }
|
92
|
+
it { expect(subject).to respond_a_collection_of_record root: :data}
|
93
|
+
it { expect(subject).to respond_with_collection size: 3,root: :data }
|
94
|
+
it { expect(subject).to respond_with data: $collection, root: :data }
|
95
|
+
end
|
96
|
+
|
97
|
+
|
98
|
+
end
|
99
|
+
```
|
100
|
+
Output :
|
101
|
+
|
102
|
+
```
|
103
|
+
Test REST API
|
104
|
+
GET /status : test for status
|
105
|
+
is expected to be a valid HTTP verb request with a valid response return
|
106
|
+
is expected to respond with status code equal to 208
|
107
|
+
is expected to respond with a record (Hash)
|
108
|
+
is expected to respond with corresponding data
|
109
|
+
GET /embeded_status : test for status
|
110
|
+
is expected to be a valid HTTP verb request with a valid response return
|
111
|
+
is expected to respond with status code equal to 208
|
112
|
+
is expected to respond with a record (Hash)
|
113
|
+
is expected to respond with corresponding data
|
114
|
+
GET /collection : test for Array
|
115
|
+
is expected to be a valid HTTP verb request with a valid response return
|
116
|
+
is expected to respond with status code equal to 200
|
117
|
+
is expected to respond with a records collection (Array)
|
118
|
+
is expected to respond with a collection of records size of 3
|
119
|
+
is expected to respond with corresponding data
|
120
|
+
GET /embeded_collection : test for Array
|
121
|
+
is expected to be a valid HTTP verb request with a valid response return
|
122
|
+
is expected to respond with status code equal to 200
|
123
|
+
is expected to respond with a records collection (Array)
|
124
|
+
is expected to respond with a collection of records size of 3
|
125
|
+
is expected to respond with corresponding data
|
126
|
+
|
127
|
+
Finished in 0.01585 seconds (files took 0.25845 seconds to load)
|
128
|
+
18 examples, 0 failures
|
129
|
+
```
|
63
130
|
|
64
131
|
## Available matchers :
|
65
132
|
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
*
|
70
|
-
|
71
|
-
|
133
|
+
|
134
|
+
### be_correctly_send
|
135
|
+
|
136
|
+
* check if return is HTTP valid
|
137
|
+
|
138
|
+
### respond_with_a_collection_of_record
|
139
|
+
|
140
|
+
* params :
|
141
|
+
- OPTIONAL[root: <:symbol>]
|
142
|
+
* goal : check if the return is a JSON Array, could be chroot with *root:*
|
72
143
|
|
73
144
|
|
145
|
+
### respond_a_record
|
74
146
|
|
147
|
+
* params :
|
148
|
+
- OPTIONAL[root: <:symbol>] :
|
149
|
+
* goal : check if the return is a JSON Hash, could be chroot with *root:*
|
75
150
|
|
151
|
+
### respond_with_collection
|
152
|
+
* params :
|
153
|
+
- size: Integer
|
154
|
+
- OPTIONAL[root: <:symbol>]
|
155
|
+
* goal : check if the collection have the good size, could be chroot with *root:*
|
156
|
+
|
157
|
+
### respond_with
|
158
|
+
* params :
|
159
|
+
- data: Object
|
160
|
+
- OPTIONAL[root: <:symbol>]
|
161
|
+
* goal : check if the return match the given object, could be chroot with root:
|
76
162
|
|
163
|
+
### respond_with_status
|
164
|
+
* params :
|
165
|
+
- code: Integer
|
166
|
+
* goal : check if the HTTP response code is the good.
|
167
|
+
|
168
|
+
|
169
|
+
## More
|
170
|
+
|
171
|
+
### Data dumper RestService#returned_data
|
172
|
+
|
173
|
+
You could, for debug your tests and during dev, dump your data with
|
174
|
+
|
175
|
+
```ruby
|
176
|
+
describe 'Test REST API' do
|
177
|
+
before :all do
|
178
|
+
$service = RestService::new :service => Application
|
179
|
+
end
|
180
|
+
|
181
|
+
subject { $service }
|
182
|
+
context "GET /data" do
|
183
|
+
it { expect(subject.get('/data')).to be_correctly_sent }
|
184
|
+
it { subject.returned_data }
|
185
|
+
end
|
186
|
+
end
|
187
|
+
```
|
77
188
|
|
189
|
+
It display the returned data of API.
|
190
|
+
|
191
|
+
### Fixtures getter
|
192
|
+
|
193
|
+
If you want to compare or inject data you could create fixture YAML files with :
|
194
|
+
|
195
|
+
|
196
|
+
```ruby
|
197
|
+
|
198
|
+
describe 'Test REST API' do
|
199
|
+
before :all do
|
200
|
+
$service = RestService::new :service => Application
|
201
|
+
$mydata = get_file 'spec/fixtures/mydata.yml'
|
202
|
+
end
|
203
|
+
|
204
|
+
subject { $service }
|
205
|
+
context "POST /data : test" do
|
206
|
+
it { expect(subject.post('/data',$mydata.to_json)).to be_correctly_sent }
|
207
|
+
end
|
208
|
+
|
209
|
+
|
210
|
+
end
|
211
|
+
```
|
212
|
+
|
213
|
+
### Statefullies helpers
|
214
|
+
|
215
|
+
To be able to record ids of record, rack-rest-spec offer two helpers, to transit data from tests to tests.
|
216
|
+
|
217
|
+
```ruby
|
218
|
+
|
219
|
+
describe 'Test REST API' do
|
220
|
+
before :all do
|
221
|
+
$service = RestService::new :service => Application
|
222
|
+
$mydata = get_file 'spec/fixtures/mydata.yml'
|
223
|
+
end
|
224
|
+
|
225
|
+
subject { $service }
|
226
|
+
context "POST /data : test" do
|
227
|
+
it { expect(subject.post('/data',$mydata.to_json)).to be_correctly_sent }
|
228
|
+
it { memorize id: subject.returned_data[:id] }
|
229
|
+
end
|
230
|
+
|
231
|
+
context "GET /data/<id> : test" do
|
232
|
+
it { expect(subject.get("/data#{retrieve(:id)}")).to be_correctly_sent }
|
233
|
+
end
|
234
|
+
end
|
235
|
+
|
236
|
+
end
|
237
|
+
```
|
238
|
+
|
78
239
|
|
data/Rakefile
CHANGED
@@ -1,54 +1,61 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
require '
|
4
|
-
require 'rake/version_task'
|
5
|
-
require 'rspec'
|
6
|
-
require 'rake'
|
7
|
-
require "rake/clean"
|
8
|
-
require "rubygems/package_task"
|
9
|
-
require "rdoc/task"
|
10
|
-
require 'code_statistics'
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'bundler/gem_tasks'
|
11
4
|
require 'rspec/core/rake_task'
|
5
|
+
require 'rubocop//rake_task'
|
6
|
+
|
12
7
|
require 'yard'
|
13
|
-
require 'yard/rake/yardoc_task
|
14
|
-
require "rake/tasklib"
|
15
|
-
require "roodi"
|
16
|
-
require "roodi_task"
|
8
|
+
require 'yard/rake/yardoc_task'
|
17
9
|
|
10
|
+
require 'code_statistics'
|
11
|
+
|
12
|
+
RuboCop::RakeTask.new
|
13
|
+
RSpec::Core::RakeTask.new(:spec)
|
18
14
|
|
19
15
|
task default: :spec
|
20
16
|
|
17
|
+
require 'version'
|
18
|
+
require 'rake/version_task'
|
21
19
|
Rake::VersionTask.new
|
22
20
|
|
23
|
-
RoodiTask.new() do | t |
|
24
|
-
t.patterns = %w(lib/**/*.rb)
|
25
|
-
t.config = "ultragreen_roodi_coding_convention.yml"
|
26
|
-
end
|
27
|
-
|
28
|
-
|
29
|
-
CLEAN.include('*.tmp','*.old')
|
30
|
-
CLOBBER.include('*.tmp', 'build/*','#*#')
|
31
|
-
|
32
|
-
|
33
|
-
content = File::readlines(File.join(File.dirname(__FILE__), 'rack-rest-rspec.gemspec')).join
|
34
|
-
spec = eval(content)
|
35
|
-
|
36
|
-
RSpec::Core::RakeTask.new('spec')
|
37
|
-
|
38
21
|
YARD::Rake::YardocTask.new do |t|
|
39
|
-
t.files
|
40
|
-
t.options += ['
|
41
|
-
t.options += ['-o', "yardoc"]
|
22
|
+
t.files = ['lib/**/*.rb', '-', 'doc/**/*', 'spec/**/*_spec.rb']
|
23
|
+
t.options += ['-o', 'yardoc']
|
42
24
|
end
|
43
25
|
YARD::Config.load_plugin('yard-rspec')
|
44
26
|
|
45
27
|
namespace :yardoc do
|
46
28
|
task :clobber do
|
47
|
-
|
48
|
-
|
29
|
+
begin
|
30
|
+
rm_r 'yardoc'
|
31
|
+
rescue StandardError
|
32
|
+
nil
|
33
|
+
end
|
34
|
+
begin
|
35
|
+
rm_r '.yardoc'
|
36
|
+
rescue StandardError
|
37
|
+
nil
|
38
|
+
end
|
39
|
+
begin
|
40
|
+
rm_r 'pkg'
|
41
|
+
rescue StandardError
|
42
|
+
nil
|
43
|
+
end
|
49
44
|
end
|
50
45
|
end
|
51
|
-
task :
|
46
|
+
task clobber: 'yardoc:clobber'
|
52
47
|
|
48
|
+
desc 'Run CVE security audit over bundle'
|
49
|
+
task :audit do
|
50
|
+
system('bundle audit')
|
51
|
+
end
|
53
52
|
|
53
|
+
desc 'Run dead line of code detection'
|
54
|
+
task :debride do
|
55
|
+
system('debride -w .debride_whitelist .')
|
56
|
+
end
|
54
57
|
|
58
|
+
desc 'Run SBOM CycloneDX Xml format file'
|
59
|
+
task :sbom do
|
60
|
+
system('cyclonedx-ruby -p .')
|
61
|
+
end
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.0
|
1
|
+
1.0.0
|