forked from zipmark/rspec_api_documentation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtextile_documentation.feature
285 lines (210 loc) · 7.01 KB
/
textile_documentation.feature
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
Feature: Generate Textile documentation from test examples
Background:
Given a file named "app.rb" with:
"""
require 'sinatra'
class App < Sinatra::Base
get '/orders' do
content_type :json
[200, {
:page => 1,
:orders => [
{ name: 'Order 1', amount: 9.99, description: nil },
{ name: 'Order 2', amount: 100.0, description: 'A great order' }
]
}.to_json]
end
get '/orders/:id' do
content_type :json
[200, { order: { name: 'Order 1', amount: 100.0, description: 'A great order' } }.to_json]
end
post '/orders' do
201
end
put '/orders/:id' do
200
end
delete '/orders/:id' do
200
end
get '/help' do
[200, 'Welcome Henry !']
end
end
"""
And a file named "app_spec.rb" with:
"""
require "rspec_api_documentation"
require "rspec_api_documentation/dsl"
RspecApiDocumentation.configure do |config|
config.app = App
config.api_name = "Example API"
config.api_explanation = "Example API Description"
config.format = :textile
config.request_headers_to_include = %w[Content-Type Host]
config.response_headers_to_include = %w[Content-Type Content-Length]
end
resource 'Orders' do
get '/orders' do
response_field :page, "Current page"
example_request 'Getting a list of orders' do
expect(status).to eq(200)
expect(response_body).to eq('{"page":1,"orders":[{"name":"Order 1","amount":9.99,"description":null},{"name":"Order 2","amount":100.0,"description":"A great order"}]}')
end
end
get '/orders/:id' do
let(:id) { 1 }
example_request 'Getting a specific order' do
expect(status).to eq(200)
expect(response_body).to eq('{"order":{"name":"Order 1","amount":100.0,"description":"A great order"}}')
end
end
post '/orders' do
parameter :name, 'Name of order', :required => true
parameter :amount, 'Amount paid', :required => true
parameter :description, 'Some comments on the order'
let(:name) { "Order 3" }
let(:amount) { 33.0 }
example_request 'Creating an order' do
expect(status).to eq(201)
end
end
put '/orders/:id' do
parameter :name, 'Name of order', :required => true
parameter :amount, 'Amount paid', :required => true
parameter :description, 'Some comments on the order'
let(:id) { 2 }
let(:name) { "Updated name" }
example_request 'Updating an order' do
expect(status).to eq(200)
end
end
delete "/orders/:id" do
let(:id) { 1 }
example_request "Deleting an order" do
expect(status).to eq(200)
end
end
end
resource 'Help' do
explanation 'Getting help'
get '/help' do
example_request 'Getting welcome message' do
expect(status).to eq(200)
expect(response_body).to eq('Welcome Henry !')
end
end
end
"""
When I run `rspec app_spec.rb --require ./app.rb --format RspecApiDocumentation::ApiFormatter`
Scenario: Output helpful progress to the console
Then the output should contain:
"""
Generating API Docs
Orders
GET /orders
* Getting a list of orders
GET /orders/:id
* Getting a specific order
POST /orders
* Creating an order
PUT /orders/:id
* Updating an order
DELETE /orders/:id
* Deleting an order
Help
GET /help
* Getting welcome message
"""
And the output should contain "6 examples, 0 failures"
And the exit status should be 0
Scenario: Index file should look like we expect
Then the file "doc/api/index.textile" should contain exactly:
"""
h1. Example API
Example API Description
h2. Help
Getting help
* "Getting welcome message":help/getting_welcome_message.textile
h2. Orders
* "Creating an order":orders/creating_an_order.textile
* "Deleting an order":orders/deleting_an_order.textile
* "Getting a list of orders":orders/getting_a_list_of_orders.textile
* "Getting a specific order":orders/getting_a_specific_order.textile
* "Updating an order":orders/updating_an_order.textile
"""
Scenario: Example 'Getting a list of orders' file should look like we expect
Then the file "doc/api/orders/getting_a_list_of_orders.textile" should contain exactly:
"""
h1. Orders API
h2. Getting a list of orders
h3. GET /orders
h3. Response Fields
Name : page
Description : Current page
h3. Request
h4. Headers
<pre>Host: example.org</pre>
h4. Route
<pre>GET /orders</pre>
h3. Response
h4. Headers
<pre>Content-Type: application/json
Content-Length: 137</pre>
h4. Status
<pre>200 OK</pre>
h4. Body
<pre>{
"page": 1,
"orders": [
{
"name": "Order 1",
"amount": 9.99,
"description": null
},
{
"name": "Order 2",
"amount": 100.0,
"description": "A great order"
}
]
}</pre>
"""
Scenario: Example 'Creating an order' file should look like we expect
Then the file "doc/api/orders/creating_an_order.textile" should contain exactly:
"""
h1. Orders API
h2. Creating an order
h3. POST /orders
h3. Parameters
Name : name *- required -*
Description : Name of order
Name : amount *- required -*
Description : Amount paid
Name : description
Description : Some comments on the order
h3. Request
h4. Headers
<pre>Host: example.org
Content-Type: application/x-www-form-urlencoded</pre>
h4. Route
<pre>POST /orders</pre>
h4. Body
<pre>name=Order+3&amount=33.0</pre>
h3. Response
h4. Headers
<pre>Content-Type: text/html;charset=utf-8
Content-Length: 0</pre>
h4. Status
<pre>201 Created</pre>
"""
Scenario: Example 'Deleting an order' file should be created
Then a file named "doc/api/orders/deleting_an_order.textile" should exist
Scenario: Example 'Getting a list of orders' file should be created
Then a file named "doc/api/orders/getting_a_list_of_orders.textile" should exist
Scenario: Example 'Getting a specific order' file should be created
Then a file named "doc/api/orders/getting_a_specific_order.textile" should exist
Scenario: Example 'Updating an order' file should be created
Then a file named "doc/api/orders/updating_an_order.textile" should exist
Scenario: Example 'Getting welcome message' file should be created
Then a file named "doc/api/help/getting_welcome_message.textile" should exist