apidoc 0.1.2 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,18 +1,7 @@
1
1
  require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
- require 'sinatra/base'
3
2
 
4
3
  describe ApiDoc do
5
4
  before do
6
- class TestApp < Sinatra::Base
7
- get '/tacos.json' do
8
- JSON.generate [ { meat: 'beef' }, { meat: 'chicken' } ]
9
- end
10
-
11
- post '/tacos.json' do
12
- request.body
13
- end
14
- end
15
-
16
5
  @doc = ApiDoc.new(TestApp) do
17
6
  get '/tacos.json' do
18
7
  desc "Get all tacos"
@@ -37,127 +26,8 @@ describe ApiDoc do
37
26
  its(:response_body) { should == JSON.generate([ { meat: 'beef' }, { meat: 'chicken' } ]) }
38
27
  end
39
28
 
40
- describe "when writing to HTML" do
41
- subject do
42
- stream = StringIO.new
43
- ApiDoc::HtmlWriter.new(@doc).write(stream)
44
- stream.read
45
- end
46
-
47
- it "should write the request method" do
48
- subject.should include('GET')
49
- end
50
-
51
- it "should write the request path" do
52
- subject.should include("/tacos.json")
53
- end
54
-
55
- it "should write the response body" do
56
- subject.should include('meat')
57
- end
58
-
59
- describe "when accepting JSON as input" do
60
- before do
61
- @doc = ApiDoc.new(TestApp) do
62
- post '/tacos.json' do
63
- desc "Make a new delicious taco"
64
- accept :json
65
- params do
66
- JSON.generate({ meat: 'beef', lettuce: true })
67
- end
68
- end
69
- end
70
-
71
- @doc.run
72
- end
73
-
74
- subject do
75
- stream = StringIO.new
76
- ApiDoc::HtmlWriter.new(@doc).write(stream)
77
- stream.read
78
- end
79
-
80
- it "should pretty-print the JSON data in the IN section" do
81
- subject.should include("{\n &quot;meat&quot;: &quot;beef&quot;,\n &quot;lettuce&quot;: true\n}")
82
- end
83
- end
84
-
85
- describe "when setting the default accept and content type" do
86
- before do
87
- @doc = ApiDoc.new(TestApp) do
88
- accept :json
89
- content_type :json
90
-
91
- post '/tacos.json' do
92
- desc "Make a new delicious taco"
93
- params do
94
- JSON.generate({ meat: 'beef', lettuce: true })
95
- end
96
- end
97
- end
98
-
99
- @doc.run
100
- end
101
-
102
- subject { @doc }
103
-
104
- it "should propagate the accept type to its resources" do
105
- @doc.resources.first.accept.should == :json
106
- end
107
-
108
- it "should propagate the content type to its resources" do
109
- @doc.resources.first.content_type.should == :json
110
- end
111
- end
112
-
113
- describe "when providing JSON as output" do
114
- before do
115
- @doc = ApiDoc.new(TestApp) do
116
- post '/tacos.json' do
117
- desc "Make a new delicious taco"
118
- content_type :json
119
- params do
120
- JSON.generate({ meat: 'beef', lettuce: true })
121
- end
122
- end
123
- end
124
-
125
- @doc.run
126
- end
127
-
128
- subject do
129
- stream = StringIO.new
130
- ApiDoc::HtmlWriter.new(@doc).write(stream)
131
- stream.read
132
- end
133
-
134
- it "should pretty-print the JSON data in the OUT section" do
135
- subject.should include("{\n &quot;meat&quot;: &quot;beef&quot;,\n &quot;lettuce&quot;: true\n}")
136
- end
137
- end
138
-
139
- end
140
-
141
29
  describe "different request methods" do
142
30
  before do
143
- class RequestMethodsApp < Sinatra::Base
144
- get '/get.json' do
145
- JSON.generate({ method: 'GET' })
146
- end
147
- post '/post.json' do
148
- JSON.generate({ method: 'POST' })
149
- end
150
- put '/put.json' do
151
- JSON.generate({ method: 'PUT' })
152
- end
153
- delete '/delete.json' do
154
- JSON.generate({ method: 'DELETE' })
155
- end
156
- options '/options.json' do
157
- JSON.generate({ method: 'OPTIONS' })
158
- end
159
- end
160
-
161
31
  @doc = ApiDoc.new(RequestMethodsApp) do
162
32
  get '/get.json' do
163
33
  desc "a get request"
@@ -191,9 +61,11 @@ describe ApiDoc do
191
61
  before do
192
62
  @doc = ApiDoc.new(TestApp) do
193
63
  post '/tacos.json' do
64
+ accept :json
65
+ content_type :json
194
66
  desc "Make a new delicious taco"
195
67
  params do
196
- JSON.generate({ meat: 'beef', lettuce: true })
68
+ { meat: 'beef', lettuce: true }
197
69
  end
198
70
  end
199
71
  end
@@ -203,208 +75,32 @@ describe ApiDoc do
203
75
 
204
76
  subject { @doc.resources.first }
205
77
 
206
- its(:params) { should == JSON.generate({ meat: 'beef', lettuce: true }) }
207
- its(:response_body) { should == JSON.generate({ meat: 'beef', lettuce: true }) }
208
- end
209
-
210
- describe "when specifying a global before block" do
211
- before do
212
- $var = 0
213
- @doc = ApiDoc.new(TestApp) do
214
- before do
215
- $var += 1
216
- end
217
-
218
- get '/tacos.json' do
219
- desc "Get all tacos as JSON"
220
- end
221
-
222
- get '/tacos.xml' do
223
- desc "Get all tacos as XML"
224
- end
225
- end
226
-
227
- @doc.run
228
- end
229
-
230
- it "should have run the before block twice, once for each resource" do
231
- $var.should == 2
232
- end
78
+ its(:params) { should == { meat: 'beef', lettuce: true } }
79
+ its(:response_body) { should == JSON.pretty_generate({ meat: 'beef', lettuce: true }) }
233
80
  end
234
81
 
235
- describe "when specifying a global before each block" do
82
+ describe "when specifying a request with an interpolated path" do
236
83
  before do
237
- $var = 0
238
84
  @doc = ApiDoc.new(TestApp) do
239
- before :each do
240
- $var += 1
241
- end
242
-
243
- get '/tacos.json' do
244
- desc "Get all tacos as JSON"
245
- end
246
-
247
- get '/tacos.xml' do
248
- desc "Get all tacos as XML"
249
- end
250
- end
251
-
252
- @doc.run
253
- end
254
-
255
- it "should have run the before block twice, once for each resource" do
256
- $var.should == 2
257
- end
258
- end
259
-
260
- describe "when specifying a global before all block" do
261
- before do
262
- $var = 0
263
- @doc = ApiDoc.new(TestApp) do
264
- before :all do
265
- $var += 1
266
- end
267
-
268
- get '/tacos.json' do
269
- desc "Get all tacos as JSON"
270
- end
271
-
272
- get '/tacos.xml' do
273
- desc "Get all tacos as XML"
274
- end
275
- end
276
-
277
- @doc.run
278
- end
279
-
280
- it "should have run the before block once and only once" do
281
- $var.should == 1
282
- end
283
- end
284
-
285
- describe "when specifying a global after block" do
286
- before do
287
- $var = 0
288
- @doc = ApiDoc.new(TestApp) do
289
- after do
290
- $var += 1
291
- end
292
-
293
- get '/tacos.json' do
294
- desc "Get all tacos as JSON"
295
- end
296
-
297
- get '/tacos.xml' do
298
- desc "Get all tacos as XML"
299
- end
300
- end
301
-
302
- @doc.run
303
- end
304
-
305
- it "should have run the after block twice, once for each resource" do
306
- $var.should == 2
307
- end
308
- end
309
-
310
- describe "when specifying a global after each block" do
311
- before do
312
- $var = 0
313
- @doc = ApiDoc.new(TestApp) do
314
- after :each do
315
- $var += 1
316
- end
317
-
318
- get '/tacos.json' do
319
- desc "Get all tacos as JSON"
320
- end
321
-
322
- get '/tacos.xml' do
323
- desc "Get all tacos as XML"
324
- end
325
- end
326
-
327
- @doc.run
328
- end
329
-
330
- it "should have run the after block twice, once for each resource" do
331
- $var.should == 2
332
- end
333
- end
334
-
335
- describe "when specifying a global after all block" do
336
- before do
337
- $var = 0
338
- @doc = ApiDoc.new(TestApp) do
339
- after :all do
340
- $var += 1
341
- end
342
-
343
- get '/tacos.json' do
344
- desc "Get all tacos as JSON"
345
- end
346
-
347
- get '/tacos.xml' do
348
- desc "Get all tacos as XML"
349
- end
350
- end
351
-
352
- @doc.run
353
- end
354
-
355
- it "should have run the after block once and only once" do
356
- $var.should == 1
357
- end
358
- end
359
-
360
- describe "when specifying a resource-specific before block" do
361
- before do
362
- $var = 0
363
- @doc = ApiDoc.new(TestApp) do
364
- get '/tacos.json' do
365
- desc "Get all tacos as JSON"
366
- before do
367
- $var += 1
85
+ get '/tacos/:id.json' do
86
+ accept :json
87
+ content_type :json
88
+ desc "Get a specific taco by its ID"
89
+ params do
90
+ { id: 123 }
368
91
  end
369
92
  end
370
-
371
- get '/tacos.xml' do
372
- desc "Get all tacos as XML"
373
- end
374
93
  end
375
94
 
376
95
  @doc.run
377
96
  end
378
97
 
379
- it "should only run the before block for its resource" do
380
- $var.should == 1
381
- end
382
- end
383
-
384
- describe "when specifying a resource-specific after block" do
385
- before do
386
- $var = 0
387
- @doc = ApiDoc.new(TestApp) do
388
- get '/tacos.json' do
389
- desc "Get all tacos as JSON"
390
- after do
391
- $var += 1
392
- end
393
- end
394
-
395
- get '/tacos.xml' do
396
- desc "Get all tacos as XML"
397
- end
398
- end
399
-
400
- @doc.run
401
- end
98
+ subject { @doc.resources.first }
402
99
 
403
- it "should only run the before block for its resource" do
404
- $var.should == 1
405
- end
100
+ its(:path) { should == '/tacos/:id.json' }
101
+ its(:params) { should == { id: 123 } }
102
+ its(:response_body) { should == JSON.generate({ meat: 'beef', id: '123' }) }
406
103
  end
407
104
 
408
-
409
105
  end
410
106
 
@@ -0,0 +1,203 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe "before and after blocks" do
4
+
5
+ describe "when specifying a global before block" do
6
+ before do
7
+ $var = 0
8
+ @doc = ApiDoc.new(TestApp) do
9
+ before do
10
+ $var += 1
11
+ end
12
+
13
+ get '/tacos.json' do
14
+ desc "Get all tacos as JSON"
15
+ end
16
+
17
+ get '/tacos.xml' do
18
+ desc "Get all tacos as XML"
19
+ end
20
+ end
21
+
22
+ @doc.run
23
+ end
24
+
25
+ it "should have run the before block twice, once for each resource" do
26
+ $var.should == 2
27
+ end
28
+ end
29
+
30
+ describe "when specifying a global before each block" do
31
+ before do
32
+ $var = 0
33
+ @doc = ApiDoc.new(TestApp) do
34
+ before :each do
35
+ $var += 1
36
+ end
37
+
38
+ get '/tacos.json' do
39
+ desc "Get all tacos as JSON"
40
+ end
41
+
42
+ get '/tacos.xml' do
43
+ desc "Get all tacos as XML"
44
+ end
45
+ end
46
+
47
+ @doc.run
48
+ end
49
+
50
+ it "should have run the before block twice, once for each resource" do
51
+ $var.should == 2
52
+ end
53
+ end
54
+
55
+ describe "when specifying a global before all block" do
56
+ before do
57
+ $var = 0
58
+ @doc = ApiDoc.new(TestApp) do
59
+ before :all do
60
+ $var += 1
61
+ end
62
+
63
+ get '/tacos.json' do
64
+ desc "Get all tacos as JSON"
65
+ end
66
+
67
+ get '/tacos.xml' do
68
+ desc "Get all tacos as XML"
69
+ end
70
+ end
71
+
72
+ @doc.run
73
+ end
74
+
75
+ it "should have run the before block once and only once" do
76
+ $var.should == 1
77
+ end
78
+ end
79
+
80
+ describe "when specifying a global after block" do
81
+ before do
82
+ $var = 0
83
+ @doc = ApiDoc.new(TestApp) do
84
+ after do
85
+ $var += 1
86
+ end
87
+
88
+ get '/tacos.json' do
89
+ desc "Get all tacos as JSON"
90
+ end
91
+
92
+ get '/tacos.xml' do
93
+ desc "Get all tacos as XML"
94
+ end
95
+ end
96
+
97
+ @doc.run
98
+ end
99
+
100
+ it "should have run the after block twice, once for each resource" do
101
+ $var.should == 2
102
+ end
103
+ end
104
+
105
+ describe "when specifying a global after each block" do
106
+ before do
107
+ $var = 0
108
+ @doc = ApiDoc.new(TestApp) do
109
+ after :each do
110
+ $var += 1
111
+ end
112
+
113
+ get '/tacos.json' do
114
+ desc "Get all tacos as JSON"
115
+ end
116
+
117
+ get '/tacos.xml' do
118
+ desc "Get all tacos as XML"
119
+ end
120
+ end
121
+
122
+ @doc.run
123
+ end
124
+
125
+ it "should have run the after block twice, once for each resource" do
126
+ $var.should == 2
127
+ end
128
+ end
129
+
130
+ describe "when specifying a global after all block" do
131
+ before do
132
+ $var = 0
133
+ @doc = ApiDoc.new(TestApp) do
134
+ after :all do
135
+ $var += 1
136
+ end
137
+
138
+ get '/tacos.json' do
139
+ desc "Get all tacos as JSON"
140
+ end
141
+
142
+ get '/tacos.xml' do
143
+ desc "Get all tacos as XML"
144
+ end
145
+ end
146
+
147
+ @doc.run
148
+ end
149
+
150
+ it "should have run the after block once and only once" do
151
+ $var.should == 1
152
+ end
153
+ end
154
+
155
+ describe "when specifying a resource-specific before block" do
156
+ before do
157
+ $var = 0
158
+ @doc = ApiDoc.new(TestApp) do
159
+ get '/tacos.json' do
160
+ desc "Get all tacos as JSON"
161
+ before do
162
+ $var += 1
163
+ end
164
+ end
165
+
166
+ get '/tacos.xml' do
167
+ desc "Get all tacos as XML"
168
+ end
169
+ end
170
+
171
+ @doc.run
172
+ end
173
+
174
+ it "should only run the before block for its resource" do
175
+ $var.should == 1
176
+ end
177
+ end
178
+
179
+ describe "when specifying a resource-specific after block" do
180
+ before do
181
+ $var = 0
182
+ @doc = ApiDoc.new(TestApp) do
183
+ get '/tacos.json' do
184
+ desc "Get all tacos as JSON"
185
+ after do
186
+ $var += 1
187
+ end
188
+ end
189
+
190
+ get '/tacos.xml' do
191
+ desc "Get all tacos as XML"
192
+ end
193
+ end
194
+
195
+ @doc.run
196
+ end
197
+
198
+ it "should only run the before block for its resource" do
199
+ $var.should == 1
200
+ end
201
+ end
202
+
203
+ end