rspec-document_requests 1.2.1 → 1.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c8a8b92b750aba417d7ef6d9eef52520817c53e4
4
- data.tar.gz: 536922a6d9a9d571fe3b3378bcc4b6875d830e19
3
+ metadata.gz: 4fedfef2a8965ead1fee4d9cb58371e3ea061814
4
+ data.tar.gz: f6712172151b7e9ba4a8c8897ccfdcfd4c25c5df
5
5
  SHA512:
6
- metadata.gz: 2dc3124c74cdba5eec489570d43b6f4539a3c705dc073c67651c3bb96f080d31df753c463d79a35689547051fbc4b5f765c608f9301dde9c50a74f764fffd93e
7
- data.tar.gz: 02af6f9b8c38d2723c2ec5b31b6816e130f20e318ef50f4cdde2dde5418622bef382ed1a928f78da57d5f755b904fdff24e4eaaa9e805c8f8d022abb0b6d5cfe
6
+ metadata.gz: 50c3b3868f1a0a7c32851ddab4eba661901ff0a7675ebbc50d34b9854396ccff66939f50a23fd3378d051b3f028d6668c19a9d350758da37600a1eb3390368ec
7
+ data.tar.gz: 4fc2781de76cc5ed2fdda452a19079604e95a20dcac27023d96b2da05de2aed597bb1283fd4f6b9084fc98cd229ad2e5b03df6acd1ee5a816b142de6624996c3
data/README.md CHANGED
@@ -104,8 +104,8 @@ RSpec.describe "Session resource", type: :request, doc: true do
104
104
  before do
105
105
  explain do
106
106
  request do # No request explanation
107
- parameter 'session[username]', "The username", required: true, type: 'string'
108
- parameter 'session[password]', required: true, type: 'string' # No explanation
107
+ parameter 'session[username]', "The username", required: true, type: 'string'
108
+ parameter 'session[password]', required: "when not SSO", type: 'string' # No explanation
109
109
  header 'Content-Type', ... # you get the point
110
110
  end
111
111
  response do # No response explanation
@@ -124,6 +124,46 @@ end
124
124
  **NOTE:** Explaining response parameters only works when this gem can
125
125
  parse the response body, see [here](#configuration) how to configure it.
126
126
 
127
+ ### Reference
128
+
129
+ #### Example Group Metadatas
130
+
131
+ (The hash you pass to `describe`/`context`).
132
+
133
+ | Symbol | Meaning |
134
+ |---------------|---------|
135
+ | `doc` | When set to `true`, all requests in the example group (`describe`/`context`) are automatically documented. Use `doc: false` to override a parent example group. |
136
+ | `explanation` | When set, this will be written below the title of the example group (`describe`/`context`) (depending on the writer). Use this to further explain the meaning of the request in your API. **NOTE:** grouped example groups (see `group_levels`) will not show their explanation in the default markdown writer. |
137
+
138
+ #### DSL
139
+
140
+ These functions are available from within your example (inside the `it`, `specify`, `before`, `after`, `around`).
141
+
142
+ **NOTE:** This is only available inside example groups that have the `doc: true` metadata.
143
+
144
+ | Method | Meaning |
145
+ |-------------------|---------|
146
+ | `nodoc` | Skips documentation for the rest of this example. When given a block, only skips requests made inside the block. Use this to skip specific requests that are used to prepare the request, or use the `doc: false` metadata on the entire example group. |
147
+ | `explain`| Receives a block in which you can explain your request and its response. Inside the block, see [Explain Block](#explain-block). |
148
+
149
+ ##### Explain Block
150
+
151
+ These methods are used inside the block passed to `explain`.
152
+
153
+ | Method | Meaning |
154
+ |------------|---------|
155
+ | `request` | Receives an optional parameter that describes the meaning of this request to the entire test sequence. Also receives an optional block, see [Request/Response Block](#requestresponse-block). |
156
+ | `response` | Receives an optional parameter that describes the meaning of this request's response to the entire test sequence. Also receives an optional block, see [Request/Response Block](#requestresponse-block). |
157
+
158
+ ###### Request/Response Block
159
+
160
+ These methods are used inside the block passed to `request` and `response` within the `explain` block.
161
+
162
+ | Method | Meaning |
163
+ |-------------|---------|
164
+ | `parameter` | Receives a string that is the name of the parameter, use `[]` for nested arrays & objects. Receives an additional optional string that describes the parameter, and a hash with these optional keys: `:required`, and `:type`. **NOTE:** This only works in response when the response is parsed, see [Configuration's](#configuration) `response_parser` to make sure it does. |
165
+ | `header` | Receives a string that is the name of the header, use `[]` for nested arrays & objects. Receives an additional optional string that describes the header, and a hash with these optional keys: `:required`, and `:type`. |
166
+
127
167
  ### Configuration
128
168
 
129
169
  These are the possible configurations:
@@ -153,12 +193,26 @@ RSpec::DocumentRequests.configure do |config|
153
193
  end
154
194
  }
155
195
 
196
+ # Request parameters
156
197
  config.include_request_parameters = nil # nil means not used
157
198
  config.exclude_request_parameters = []
158
199
  config.hide_request_parameters = [] # Displays '...' instead of actual value
200
+ config.enforce_explain_request_parameters = false # Raise error if an unexplained included parameter is used
201
+ # Request headers
202
+ config.include_request_headers = nil # nil means not used
203
+ config.exclude_request_headers = []
204
+ config.hide_request_headers = [] # Displays '...' instead of actual value
205
+ config.enforce_explain_request_headers = false
206
+ # Response parameters
159
207
  config.include_response_parameters = nil
160
208
  config.exclude_response_parameters = []
161
209
  config.hide_response_parameters = []
210
+ config.enforce_explain_response_parameters = false
211
+ # Response headers
212
+ config.include_response_headers = nil
213
+ config.exclude_response_headers = []
214
+ config.hide_response_headers = []
215
+ config.enforce_explain_response_headers = false
162
216
  end
163
217
  ```
164
218
 
@@ -236,7 +290,8 @@ The gem works, but it's missing some basics:
236
290
 
237
291
  * Unit tests (specs).
238
292
  * Example generated documentations.
239
- * More writers (see `lib/rspec/document_requests/writers/base.rb`).
293
+ * More writers (see [lib/rspec/document_requests/writers/base.rb](lib/rspec/document_requests/writers/base.rb)).
294
+ * More default response parsers (see [lib/rspec/document_requests/configuration.rb](lib/rspec/document_requests/configuration.rb)).
240
295
 
241
296
  So...
242
297
 
@@ -24,6 +24,7 @@ module RSpec
24
24
  add_property :"include_#{side}_#{part}", nil
25
25
  add_property :"exclude_#{side}_#{part}", []
26
26
  add_property :"hide_#{side}_#{part}", []
27
+ add_property :"enforce_explain_#{side}_#{part}", false
27
28
  end
28
29
  end
29
30
 
@@ -2,19 +2,18 @@ module RSpec
2
2
  module DocumentRequests
3
3
  module DSL
4
4
  class << self
5
- attr_accessor :documented_requests, :currently_documented_example
5
+ attr_accessor :documented_requests
6
6
  end
7
7
  self.documented_requests = []
8
- self.currently_documented_example = nil
9
8
 
10
9
  [:get, :post, :patch, :put, :delete, :head].each do |method|
11
10
  define_method(method) do |path, parameters = nil, headers_or_env = nil|
12
11
  result = super(path, parameters, headers_or_env)
13
12
 
14
- if not @document_request_prevented and DSL.currently_documented_example
13
+ if not @document_requests_prevented and @currently_documented_example
15
14
  DSL.documented_requests << Request.new(
16
15
  explanation: document_request_explanation,
17
- example: DSL.currently_documented_example,
16
+ example: @currently_documented_example,
18
17
  method: method.to_s.upcase,
19
18
  path: path,
20
19
  request_parameters: parameters,
@@ -37,11 +36,14 @@ module RSpec
37
36
  end
38
37
 
39
38
  def nodoc
40
- @document_request_prevented = true
41
- begin
42
- yield
43
- ensure
44
- @document_request_prevented = false
39
+ was_prevented = @document_requests_prevented
40
+ @document_requests_prevented = true
41
+ if block_given?
42
+ begin
43
+ yield
44
+ ensure
45
+ @document_requests_prevented = false if not was_prevented
46
+ end
45
47
  end
46
48
  end
47
49
  end
@@ -54,8 +56,8 @@ RSpec.configure do |config|
54
56
  config.run_all_when_everything_filtered = false
55
57
 
56
58
  config.after(:suite) { RSpec::DocumentRequests::Builder.new }
57
- config.before { |ex| RSpec::DocumentRequests::DSL.currently_documented_example = ex }
58
- config.after { |ex| RSpec::DocumentRequests::DSL.currently_documented_example = nil }
59
+ config.before(doc: true) { |ex| @currently_documented_example = ex }
60
+ config.after(doc: true) { |ex| @currently_documented_example = nil }
59
61
  end
60
62
 
61
63
  config.include RSpec::DocumentRequests::DSL, doc: true
@@ -2,12 +2,13 @@ module RSpec
2
2
  module DocumentRequests
3
3
  class Request
4
4
  class Parameter
5
- attr_accessor :message, :required, :type, :value
6
- def initialize(message = nil, required: false, type: nil, value: nil)
5
+ attr_accessor :message, :required, :type, :value, :generated
6
+ def initialize(message = nil, required: false, type: nil, value: nil, generated: false)
7
7
  @message = message
8
8
  @required = required
9
9
  @type = type
10
10
  @value = value
11
+ @generated = generated
11
12
  end
12
13
  end
13
14
 
@@ -33,46 +34,41 @@ module RSpec
33
34
 
34
35
  private
35
36
 
36
- def self.filter_values(name)
37
- define_method(name) do
38
- values = instance_variable_get(:"@#{name}")
39
- next values if values.nil?
37
+ def filter_values(name)
38
+ values = instance_variable_get(:"@#{name}")
39
+ return values if values.nil?
40
40
 
41
- included_values = DocumentRequests.configuration.send(:"include_#{name}")
42
- excluded_values = DocumentRequests.configuration.send(:"exclude_#{name}")
43
- hidden_values = DocumentRequests.configuration.send(:"hide_#{name}")
41
+ included_values = DocumentRequests.configuration.send(:"include_#{name}")
42
+ excluded_values = DocumentRequests.configuration.send(:"exclude_#{name}")
43
+ hidden_values = DocumentRequests.configuration.send(:"hide_#{name}")
44
+ enforce = DocumentRequests.configuration.send(:"enforce_explain_#{name}")
44
45
 
45
- values.select! do |k, v|
46
- next false if included_values and included_values.exclude?(k)
47
- next false if excluded_values.include?(k)
48
- values[k].value = "..." if hidden_values.include?(k)
49
- true
50
- end
51
- values
46
+ unexplained = []
47
+ values.select! do |k, v|
48
+ next false if included_values and included_values.exclude?(k)
49
+ next false if excluded_values.include?(k)
50
+ unexplained << k if v.generated
51
+ v.value = "..." if hidden_values.include?(k)
52
+ true
52
53
  end
54
+ raise "Unexplained parameters used: #{unexplained.join(", ")}" if enforce and unexplained.any?
55
+ values
53
56
  end
54
57
 
55
- public
56
-
57
- filter_values :request_parameters
58
- filter_values :request_headers
59
- filter_values :response_parameters
60
- filter_values :response_headers
61
-
62
- private
63
-
64
58
  def process_request_parameters(parameters, prefix: nil)
65
59
  @request_parameters = {}
66
60
  process_parameters(parameters, @request_parameters, explanation: @explanation.request.parameters)
61
+ filter_values :request_parameters
67
62
  end
68
63
 
69
64
  def process_request_headers(headers)
70
65
  @request_headers = {}
71
66
  headers.each do |name, value|
72
- @request_headers[name] = @explanation.request.headers[name] || Parameter.new
67
+ @request_headers[name] = @explanation.request.headers[name] || Parameter.new(generated: true)
73
68
  @request_headers[name].value = value
74
69
  end
75
70
  @explanation.request.headers.each { |name, header| @request_headers[name] ||= header }
71
+ filter_values :request_headers
76
72
  end
77
73
 
78
74
  def process_response_parameters(parameters = nil)
@@ -81,20 +77,22 @@ module RSpec
81
77
  @response_parameters = {}
82
78
  process_parameters(@parsed_response, @response_parameters, explanation: @explanation.response.parameters)
83
79
  end
80
+ filter_values :response_parameters
84
81
  end
85
82
 
86
83
  def process_response_headers
87
84
  @response_headers = {}
88
85
  @response.headers.each do |name, value|
89
- @response_headers[name] = @explanation.response.headers[name] || Parameter.new
86
+ @response_headers[name] = @explanation.response.headers[name] || Parameter.new(generated: true)
90
87
  @response_headers[name].value = value
91
88
  end
92
89
  @explanation.response.headers.each { |name, header| @response_headers[name] ||= header }
90
+ filter_values :response_headers
93
91
  end
94
92
 
95
93
  def process_parameters(input, output, explanation:, prefix: nil)
96
94
  input.each do |key, value|
97
- name = prefix ? "#{prefix}[#{key}]" : key
95
+ name = prefix ? "#{prefix}[#{key}]" : key.to_s
98
96
  case value
99
97
  when Hash
100
98
  process_parameters(value, output, explanation: explanation, prefix: name)
@@ -110,11 +108,11 @@ module RSpec
110
108
  end
111
109
  end
112
110
  if base_values.any?
113
- output[name] ||= explanation[name] || Parameter.new
111
+ output[name] ||= explanation[name] || Parameter.new(generated: true)
114
112
  output[name].value = [output[name].value, base_values.to_s].compact.join(", ")
115
113
  end
116
114
  else
117
- output[name] ||= explanation[name] || Parameter.new
115
+ output[name] ||= explanation[name] || Parameter.new(generated: true)
118
116
  output[name].value = [output[name].value, value].compact.join(", ")
119
117
  end
120
118
  end
@@ -1,5 +1,5 @@
1
1
  module RSpec
2
2
  module DocumentRequests
3
- VERSION = "1.2.1"
3
+ VERSION = "1.3.0"
4
4
  end
5
5
  end
@@ -44,7 +44,7 @@ module RSpec
44
44
  @file.puts "| Name | Type | Required? | Value | |"
45
45
  @file.puts "|------|------|-----------|-------|---|"
46
46
  parameters.each do |name, parameter|
47
- @file.puts "| #{name} | #{parameter.type} | #{"Required" if parameter.required} | #{parameter.value} | #{parameter.message} |"
47
+ @file.puts "| #{name} | #{parameter.type} | #{parameter.required || false} | #{parameter.value} | #{parameter.message} |"
48
48
  end
49
49
  @file.puts
50
50
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rspec-document_requests
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.1
4
+ version: 1.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Oded Niv
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2015-06-24 00:00:00.000000000 Z
11
+ date: 2015-06-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec-rails