rack-rest-rspec 0.0.3 → 0.0.5

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 991c3ce92453468607c170e5546f355d3ce1cec02c970651906599af34e3ec0e
4
- data.tar.gz: 77955b805aed17ad1639496176821b9203dde097228b5aebaf237db9727c8947
3
+ metadata.gz: 71ef01252294ee9e868956d7022e23990f81557cc4025940da96b2783b4c2d04
4
+ data.tar.gz: ab4553953c6cb4bc1f428741133889fa3defd63fa4309be087db6ba95b7f9cb8
5
5
  SHA512:
6
- metadata.gz: 5ba8ada8c6a9d413484b75b71fa368e9e9b59a09f1e903e1d8728a3a49a59f516054937e316b0f6d5948c155ef0cf4b304f55eeace61c479757a15c32497c880
7
- data.tar.gz: b8ce3dad6fcac708eac0b5f0d50fc140dd5f6e98a4455c5b0e275a9d5a307740ec9f934f0da98297e80baf5f652a98abe8b6f52514e92741a1f07fe4826351a2
6
+ metadata.gz: 7e2fe753acd7375aa77f1672768b6c150e68df0a1f2ff5d58f68fba12c0290ea7906510b6ca29d2d877aaf4b185e80a0a50e78955c62b867b8e538445a434bfb
7
+ data.tar.gz: f6b5c79124bcf35173281e7b66ce0a22348d3892b7b8ff547d15c3ed1018541550712fdf97102e30e0f55fe013b10e5aca8204f67844c7ff6c282c2e083dc49e
data/README.md CHANGED
@@ -19,55 +19,122 @@ or
19
19
 
20
20
  ### Basic usage : sample of spec matchers usage
21
21
 
22
- describe 'Posts REST API' do
23
- before :all do
24
- $service = RestService::new :service => MYRack::ModularApp
25
- end
26
-
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
-
35
- context "POST /api/v1/post : create a new record" do
36
- it { expect(subject.post('/api/v1/post',{id: 1, title: "test", body: "content"}.to_json)).to be_correctly_sent }
37
- it { expect(subject).to respond_with_status 201 }
38
- end
39
-
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
-
47
- context "GET /api/v1/post/:id : get a record by code" do
48
- it { expect(subject.get("/api/v1/post/1")).to be_correctly_sent }
49
- it { expect(subject).to respond_with_status 200 }
50
- it { expect(subject).to respond_a_record }
51
- it { expect(subject).to respond_with_data({id: 1, title: "test", body: "content"}) }
52
- end
53
-
54
- context "DELETE /api/v1/post/:id : delete a pots by id" do
55
- it{ expect(subject.delete("/api/v1/post/1")).to be_correctly_sent }
56
- it { expect(subject).to respond_with_status 204 }
57
- end
22
+ Considering this API made with [Sinatra](https://sinatrarb.com/) :
58
23
 
59
24
 
25
+ ```ruby
26
+ class Application < Sinatra::Base
60
27
 
28
+
29
+ before do
30
+ content_type 'application/json'
31
+ end
32
+
33
+ get '/status' do
34
+ status 208
35
+ return {name: "Sample App", version: "0.0.1", status: 'OK'}.to_json
36
+ end
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
42
+
43
+ get '/collection' do
44
+ return [{one: 1}, {two: 2}, {three: 3}].to_json
45
+ end
46
+
47
+ get '/embeded_collection' do
48
+ return {code: 200, data: [{one: 1}, {two: 2}, {three: 3}]}.to_json
49
+ end
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 }
61
95
  end
62
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
133
  * be_correctly_send : check if return is HTTP valid
67
- * respond_with_a_collection_of_record : check if the return is a JSON Array
68
- * respond_a_record : check if the return is a JSON Hash
69
- * respond_with_collection_size <Integer> : check if the collection have the good size
70
- * respond_with_data <Object> : check if the return match the given object
134
+ * respond_with_a_collection_of_record OPTIONAL[root: <:symbol>] : check if the return is a JSON Array, could be chroot with root:
135
+ * respond_a_record OPTIONAL[root: <:symbol>] : check if the return is a JSON Hash, could be chroot with root:
136
+ * respond_with_collection size: <Integer> OPTIONAL[root: <:symbol>] : check if the collection have the good size, could be chroot with root:
137
+ * respond_with data: <Object> OPTIONAL[root: <:symbol>] : check if the return match the given object, could be chroot with root:
71
138
  * respond_with_status <Integer> : check if the HTTP response code is the good.
72
139
 
73
140
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.3
1
+ 0.0.5
@@ -22,4 +22,8 @@ class RestService
22
22
 
23
23
  def_delegators :@browser, :get, :post, :put, :delete
24
24
 
25
+ def returned_data
26
+ return JSON::parse(@browser.last_response.body, symbolize_names: true)
27
+ end
28
+
25
29
  end
@@ -1,14 +1,21 @@
1
- RSpec::Matchers.define :respond_a_collection_of_record do
1
+ RSpec::Matchers.define :respond_a_collection_of_record do |options= {}|
2
+
2
3
  match do |actual|
3
- JSON::parse(actual.browser.last_response.body).class == Array
4
+ result = JSON::parse(actual.browser.last_response.body, symbolize_names: true)
5
+ result = result[options[:root]] unless options[:root].nil?
6
+ result.class == Array
4
7
  end
5
8
  description do
6
- "respond with a record collection (Array)"
9
+ "respond with a records collection (Array)"
7
10
  end
8
11
  failure_message do |actual|
9
- "expected #{actual} response body would be a collection \nneed : Array, \ngot : #{JSON::parse(actual.browser.last_response.body).class}"
12
+ result = JSON::parse(actual.browser.last_response.body, symbolize_names: true)
13
+ result = result[options[:root]] unless options[:oot].nil?
14
+ "expected #{actual} response body would be a collection \nneed : Array, \ngot : #{result.class}"
10
15
  end
11
16
  failure_message_when_negated do |actual|
12
- "expected #{actual} response body would not be a collection \nneed : Array, \ngot : #{JSON::parse(actual.browser.last_response.body).class}"
17
+ result = JSON::parse(actual.browser.last_response.body, symbolize_names: true)
18
+ result = result[options[:root]] unless options[:root].nil?
19
+ "expected #{actual} response body would not be a collection \nneed : Array, \ngot : #{result.class}"
13
20
  end
14
21
  end
@@ -1,14 +1,20 @@
1
- RSpec::Matchers.define :respond_a_record do
1
+ RSpec::Matchers.define :respond_a_record do |options={}|
2
2
  match do |actual|
3
- JSON::parse(actual.browser.last_response.body).class == Hash
3
+ result = JSON::parse(actual.browser.last_response.body, symbolize_names: true)
4
+ result = result[options[:root]] unless options[:root].nil?
5
+ result.class == Hash
4
6
  end
5
7
  description do
6
8
  "respond with a record (Hash)"
7
9
  end
8
10
  failure_message do |actual|
9
- "expected #{actual} response body would be a record \nneed : Hash, \ngot : #{JSON::parse(actual.browser.last_response.body).class}"
11
+ result = JSON::parse(actual.browser.last_response.body, symbolize_names: true)
12
+ result = result[options[:root]] unless options[:root].nil?
13
+ "expected #{actual} response body would be a record \nneed : Hash, \ngot : #{result.class}"
10
14
  end
11
15
  failure_message_when_negated do |actual|
12
- "expected #{actual} response body would not be a record \nneed : Hash, \ngot : #{JSON::parse(actual.browser.last_response.body).class}"
16
+ result = JSON::parse(actual.browser.last_response.body, symbolize_names: true)
17
+ result = result[options[:root]] unless options[:root].nil?
18
+ "expected #{actual} response body would not be a record \nneed : Hash, \ngot : #{result.class}"
13
19
  end
14
20
  end
@@ -1,14 +1,22 @@
1
- RSpec::Matchers.define :respond_with_collection_size do |expected|
1
+ RSpec::Matchers.define :respond_with_collection do |options={}|
2
+ expected = options[:size]
2
3
  match do |actual|
3
- JSON::parse(actual.browser.last_response.body).size == expected
4
+ result = JSON::parse(actual.browser.last_response.body, symbolize_names: true)
5
+ result = result[options[:root]] unless options[:root].nil?
6
+ result.size == expected
4
7
  end
5
8
  description do
6
- "respond with a collection of record size of #{expected}"
9
+ "respond with a collection of records size of #{expected}"
7
10
  end
11
+
8
12
  failure_message do |actual|
9
- "expected #{actual} response body collection would have a size of #{expected}, \ngot #{JSON::parse(actual.browser.last_response.body).size}"
13
+ result = JSON::parse(actual.browser.last_response.body, symbolize_names: true)
14
+ result = result[options[:root]] unless options[:root].nil?
15
+ "expected #{actual} response body collection would have a size of #{expected}, \ngot #{result.size}"
10
16
  end
11
17
  failure_message_when_negated do |actual|
12
- "expected #{actual} response body collection would not have a size of #{expected}, \ngot #{JSON::parse(actual.browser.last_response.body).size}"
18
+ result = JSON::parse(actual.browser.last_response.body, symbolize_names: true)
19
+ result = result[options[:root]] unless options[:root].nil?
20
+ "expected #{actual} response body collection would not have a size of #{expected}, \ngot #{result.size}"
13
21
  end
14
22
  end
@@ -11,22 +11,18 @@ def compare_array_of_hash expected, actual
11
11
  end
12
12
 
13
13
  def prepare_array data
14
- data = (data.class == Hash)? [data] : data
15
- data.each do |ahash|
16
- ahash.symbolize!
17
- end
18
- return data
14
+ return (data.class == Hash)? [data] : data
19
15
  end
20
16
 
21
17
 
22
- RSpec::Matchers.define :respond_with_data do |expected|
23
- result = ""
18
+ RSpec::Matchers.define :respond_with do |options={}|
19
+ expected = options[:data]
24
20
  match do |actual|
25
- result = actual.browser.last_response.body
21
+ result = JSON::parse(actual.browser.last_response.body, symbolize_names: true)
22
+ result = result[options[:root]] unless options[:root].nil?
26
23
  if result.empty? then
27
24
  false
28
25
  else
29
- result = JSON::parse(result)
30
26
  result = prepare_array result
31
27
  expected = prepare_array expected
32
28
  compare_array_of_hash expected,result
@@ -36,9 +32,13 @@ RSpec::Matchers.define :respond_with_data do |expected|
36
32
  "respond with corresponding data"
37
33
  end
38
34
  failure_message do |actual|
35
+ result = JSON::parse(actual.browser.last_response.body, symbolize_names: true)
36
+ result = result[options[:root]] unless options[:root].nil?
39
37
  "expected #{actual} response body would be equal \nto : #{expected}, \ngot : #{result}"
40
38
  end
41
39
  failure_message_when_negated do |actual|
40
+ result = JSON::parse(actual.browser.last_response.body, symbolize_names: true)
41
+ result = result[options[:root]] unless options[:root].nil?
42
42
  "expected #{actual} response body would not be equal \nto : #{expected}, \ngot : #{result}"
43
43
  end
44
44
  end
@@ -1,14 +1,18 @@
1
- RSpec::Matchers.define :respond_with_status do |expected|
1
+ RSpec::Matchers.define :respond_with_status do |options={}|
2
+ expected = options[:code]
2
3
  match do |actual|
3
- actual.browser.last_response.status == expected
4
+ result = actual.browser.last_response
5
+ result.status == expected
4
6
  end
5
7
  description do
6
8
  "respond with status code equal to #{expected}"
7
9
  end
8
10
  failure_message do |actual|
9
- "expected #{actual} response would be equal \nto : #{expected} \ngot : #{actual.browser.last_response.status}"
11
+ result = actual.browser.last_response
12
+ "expected #{actual} response would be equal \nto : #{expected} \ngot : #{result.status}"
10
13
  end
11
14
  failure_message_when_negated do |actual|
12
- "expected #{actual} response would not be equal \nto : #{expected} \ngot : #{actual.browser.last_response.status}"
15
+ result = actual.browser.last_response
16
+ "expected #{actual} response would not be equal \nto : #{expected} \ngot : #{result.status}"
13
17
  end
14
18
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rack-rest-rspec
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Romain GEORGES
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-10-13 00:00:00.000000000 Z
11
+ date: 2024-05-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: roodi
@@ -153,7 +153,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
153
153
  - !ruby/object:Gem::Version
154
154
  version: '0'
155
155
  requirements: []
156
- rubygems_version: 3.1.2
156
+ rubygems_version: 3.4.19
157
157
  signing_key:
158
158
  specification_version: 4
159
159
  summary: 'Rack-rest-rspec: RSpec Matchers for REST Rack webservice'