forked from zipmark/rspec_api_documentation
-
Notifications
You must be signed in to change notification settings - Fork 2
/
api_blueprint_documentation.feature
482 lines (364 loc) · 13.1 KB
/
api_blueprint_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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
Feature: Generate API Blueprint documentation from test examples
Background:
Given a file named "app.rb" with:
"""
require 'sinatra'
class App < Sinatra::Base
get '/orders' do
content_type "application/vnd.api+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
content_type :json
[201, { order: { name: 'Order 1', amount: 100.0, description: 'A great order' } }.to_json]
end
put '/orders/:id' do
content_type :json
if params[:id].to_i > 0
[200, { data: { id: "1", type: "order", attributes: { name: "Order 1", amount: 100.0, description: "A description" } } }.to_json]
else
[400, ""]
end
end
delete '/orders/:id' do
200
end
get '/instructions' do
response_body = {
data: {
id: "1",
type: "instructions",
attributes: {}
}
}
[200, response_body.to_json]
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 = :api_blueprint
config.request_body_formatter = :json
config.request_headers_to_include = %w[Content-Type Host]
config.response_headers_to_include = %w[Content-Type Content-Length]
end
resource 'Orders' do
explanation "Orders resource"
route '/orders', 'Orders Collection' do
explanation "This URL allows users to interact with all orders."
get 'Return all orders' do
explanation "This is used to return all orders."
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
post 'Creates an order' do
explanation "This is used to create orders."
header "Content-Type", "application/json"
example 'Creating an order' do
request = {
data: {
type: "order",
attributes: {
name: "Order 1",
amount: 100.0,
description: "A description"
}
}
}
do_request(request)
expect(status).to eq(201)
end
end
end
route '/orders/:id{?optional=:optional}', "Single Order" do
parameter :id, 'Order id', required: true, type: 'string', :example => '1'
parameter :optional
attribute :name, 'The order name', required: true, :example => 'a name'
attribute :amount, required: false
attribute :description, 'The order description', type: 'string', required: false, example: "a description"
attribute :category, 'The order category', type: 'string', required: false, default: 'normal', enum: %w[normal priority]
attribute :metadata, 'The order metadata', type: 'json', required: false, annotation: <<-MARKDOWN
+ instructions (optional, string)
+ notes (optional, string)
MARKDOWN
get 'Returns a single order' do
explanation "This is used to return orders."
let(:id) { 1 }
example_request 'Getting a specific order' do
explanation 'Returns a specific order.'
expect(status).to eq(200)
expect(response_body).to eq('{"order":{"name":"Order 1","amount":100.0,"description":"A great order"}}')
end
end
put 'Updates a single order' do
explanation "This is used to update orders."
header "Content-Type", "application/json; charset=utf-16"
context "with a valid id" do
let(:id) { 1 }
example 'Update an order' do
request = {
data: {
id: "1",
type: "order",
attributes: {
name: "Order 1",
}
}
}
do_request(request)
expected_response = {
data: {
id: "1",
type: "order",
attributes: {
name: "Order 1",
amount: 100.0,
description: "A description",
}
}
}
expect(status).to eq(200)
expect(response_body).to eq(expected_response.to_json)
end
end
context "with an invalid id" do
let(:id) { "a" }
example_request 'Invalid request' do
expect(status).to eq(400)
expect(response_body).to eq("")
end
end
end
delete "Deletes a specific order" do
explanation "This is used to delete orders."
let(:id) { 1 }
example_request "Deleting an order" do
explanation 'Deletes the requested order.'
expect(status).to eq(200)
expect(response_body).to eq('')
end
end
end
end
resource 'Instructions' do
explanation 'Instructions help the users use the app.'
route '/instructions', 'Instructions Collection' do
explanation 'This endpoint allows users to interact with all instructions.'
get 'Returns all instructions' do
explanation 'This should be used to get all instructions.'
example_request 'List all instructions' do
explanation 'Returns all instructions.'
expected_response = {
data: {
id: "1",
type: "instructions",
attributes: {}
}
}
expect(status).to eq(200)
expect(response_body).to eq(expected_response.to_json)
end
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
/orders Orders Collection
GET Return all orders
* Getting a list of orders
POST Creates an order
* Creating an order
/orders/:id{?optional=:optional} Single Order
GET Returns a single order
* Getting a specific order
PUT Updates a single order
with a valid id
* Update an order
with an invalid id
* Invalid request
DELETE Deletes a specific order
* Deleting an order
Instructions
/instructions Instructions Collection
GET Returns all instructions
* List all instructions
"""
And the output should contain "7 examples, 0 failures"
And the exit status should be 0
Scenario: Index file should look like we expect
Then the file "doc/api/index.apib" should contain exactly:
"""
FORMAT: 1A
# Example API
Example API Description
# Group Instructions
Instructions help the users use the app.
## Instructions Collection [/instructions]
### Returns all instructions [GET]
+ Request List all instructions
+ Headers
Host: example.org
+ Response 200 (text/html;charset=utf-8)
+ Headers
Content-Length: 57
+ Body
{"data":{"id":"1","type":"instructions","attributes":{}}}
# Group Orders
Orders resource
## Orders Collection [/orders]
### Creates an order [POST]
+ Request Creating an order (application/json)
+ Headers
Host: example.org
+ Body
{
"data": {
"type": "order",
"attributes": {
"name": "Order 1",
"amount": 100.0,
"description": "A description"
}
}
}
+ Response 201 (application/json)
+ Headers
Content-Length: 73
+ Body
{
"order": {
"name": "Order 1",
"amount": 100.0,
"description": "A great order"
}
}
### Return all orders [GET]
+ Request Getting a list of orders
+ Headers
Host: example.org
+ Response 200 (application/vnd.api+json)
+ Headers
Content-Length: 137
+ Body
{
"page": 1,
"orders": [
{
"name": "Order 1",
"amount": 9.99,
"description": null
},
{
"name": "Order 2",
"amount": 100.0,
"description": "A great order"
}
]
}
## Single Order [/orders/{id}{?optional=:optional}]
+ Parameters
+ id: 1 (required, string) - Order id
+ optional (optional)
+ Attributes (object)
+ name: a name (required) - The order name
+ amount (optional)
+ description: a description (optional, string) - The order description
+ category (optional, string) - The order category
+ Default: `normal`
+ Members
+ `normal`
+ `priority`
+ metadata (optional, json) - The order metadata
+ instructions (optional, string)
+ notes (optional, string)
### Deletes a specific order [DELETE]
+ Request Deleting an order (application/x-www-form-urlencoded)
+ Headers
Host: example.org
+ Response 200 (text/html;charset=utf-8)
+ Headers
Content-Length: 0
### Returns a single order [GET]
+ Request Getting a specific order
+ Headers
Host: example.org
+ Response 200 (application/json)
+ Headers
Content-Length: 73
+ Body
{
"order": {
"name": "Order 1",
"amount": 100.0,
"description": "A great order"
}
}
### Updates a single order [PUT]
+ Request Invalid request (application/json; charset=utf-16)
+ Headers
Host: example.org
+ Response 400 (application/json)
+ Headers
Content-Length: 0
+ Request Update an order (application/json; charset=utf-16)
+ Headers
Host: example.org
+ Body
{
"data": {
"id": "1",
"type": "order",
"attributes": {
"name": "Order 1"
}
}
}
+ Response 200 (application/json)
+ Headers
Content-Length: 111
+ Body
{
"data": {
"id": "1",
"type": "order",
"attributes": {
"name": "Order 1",
"amount": 100.0,
"description": "A description"
}
}
}
"""
Scenario: Example 'Deleting an order' file should not be created
Then a file named "doc/api/orders/deleting_an_order.apib" should not exist
Scenario: Example 'Getting a list of orders' file should be created
Then a file named "doc/api/orders/getting_a_list_of_orders.apib" should not exist
Scenario: Example 'Getting a specific order' file should be created
Then a file named "doc/api/orders/getting_a_specific_order.apib" should not exist
Scenario: Example 'Updating an order' file should be created
Then a file named "doc/api/orders/updating_an_order.apib" should not exist
Scenario: Example 'Getting welcome message' file should be created
Then a file named "doc/api/help/getting_welcome_message.apib" should not exist