weasel_diesel 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile +4 -0
- data/Gemfile.lock +38 -0
- data/LICENSE +23 -0
- data/README.md +231 -0
- data/Rakefile +12 -0
- data/lib/documentation.rb +151 -0
- data/lib/framework_ext/sinatra.rb +30 -0
- data/lib/framework_ext/sinatra_controller.rb +80 -0
- data/lib/inflection.rb +460 -0
- data/lib/json_response_verification.rb +109 -0
- data/lib/params.rb +374 -0
- data/lib/params_verification.rb +268 -0
- data/lib/response.rb +457 -0
- data/lib/weasel_diesel.rb +415 -0
- data/lib/weasel_diesel/version.rb +3 -0
- data/lib/ws_list.rb +58 -0
- data/spec/hello_world_controller.rb +5 -0
- data/spec/hello_world_service.rb +20 -0
- data/spec/json_response_description_spec.rb +124 -0
- data/spec/json_response_verification_spec.rb +225 -0
- data/spec/params_verification_spec.rb +102 -0
- data/spec/preferences_service.rb +10 -0
- data/spec/spec_helper.rb +20 -0
- data/spec/test_services.rb +102 -0
- data/spec/wsdsl_sinatra_ext_spec.rb +26 -0
- data/spec/wsdsl_spec.rb +314 -0
- data/weasel_diesel.gemspec +30 -0
- metadata +127 -0
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
if RUBY_VERSION =~ /1.8/
|
2
|
+
require 'rubygems'
|
3
|
+
require 'backports'
|
4
|
+
require 'json'
|
5
|
+
end
|
6
|
+
|
7
|
+
require 'rspec'
|
8
|
+
require 'rack/test'
|
9
|
+
require 'sinatra'
|
10
|
+
|
11
|
+
require_relative "../lib/weasel_diesel"
|
12
|
+
require_relative 'test_services'
|
13
|
+
require_relative 'preferences_service'
|
14
|
+
require_relative "../lib/framework_ext/sinatra_controller"
|
15
|
+
|
16
|
+
ENV["RACK_ENV"] = 'test'
|
17
|
+
|
18
|
+
RSpec.configure do |conf|
|
19
|
+
conf.include Rack::Test::Methods
|
20
|
+
end
|
@@ -0,0 +1,102 @@
|
|
1
|
+
WeaselDieselSpecOptions = ['RSpec', 'Bacon'] # usually pulled from a model
|
2
|
+
|
3
|
+
describe_service "services/test.xml" do |service|
|
4
|
+
service.formats :xml, :json
|
5
|
+
service.http_verb :get
|
6
|
+
|
7
|
+
service.params do |p|
|
8
|
+
p.string :framework, :in => WeaselDieselSpecOptions, :null => false, :required => true
|
9
|
+
|
10
|
+
p.datetime :timestamp, :default => Time.now
|
11
|
+
p.string :alpha, :in => ['a', 'b', 'c']
|
12
|
+
p.string :version, :null => false
|
13
|
+
p.integer :num, :minvalue => 42
|
14
|
+
|
15
|
+
end
|
16
|
+
|
17
|
+
# service.param :delta, :optional => true, :type => 'float'
|
18
|
+
# # if the optional flag isn't passed, the param is considered required.
|
19
|
+
# service.param :epsilon, :type => 'string'
|
20
|
+
|
21
|
+
service.params.namespace :user do |user|
|
22
|
+
user.integer :id, :required => :true
|
23
|
+
user.string :sex, :in => %Q{female, male}
|
24
|
+
user.boolean :mailing_list, :default => true
|
25
|
+
end
|
26
|
+
|
27
|
+
# the response contains a list of player creation ratings each object in the list
|
28
|
+
|
29
|
+
=begin
|
30
|
+
#Format not supported by Ruby 1.8 due to hash insertion order not being maintained.
|
31
|
+
service.response do |response|
|
32
|
+
response.element(:name => "player_creation_ratings") do |e|
|
33
|
+
e.attribute :id => :integer, :doc => "id doc"
|
34
|
+
e.attribute :is_accepted => :boolean, :doc => "is accepted doc"
|
35
|
+
e.attribute :name => :string, :doc => "name doc"
|
36
|
+
|
37
|
+
e.array :player_creation_rating, 'PlayerCreationRating' do |a|
|
38
|
+
a.attribute :comments => :string, :doc => "comments doc"
|
39
|
+
a.attribute :player_id => :integer, :doc => "player_id doc"
|
40
|
+
a.attribute :rating => :integer, :doc => "rating doc"
|
41
|
+
a.attribute :username => :string, :doc => "username doc"
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
=end
|
46
|
+
|
47
|
+
service.response do |response|
|
48
|
+
response.element(:name => "player_creation_ratings") do |e|
|
49
|
+
e.integer :id, :doc => "id doc"
|
50
|
+
e.boolean :is_accepted, :doc => "is accepted doc"
|
51
|
+
e.string :name, :doc => "name doc"
|
52
|
+
|
53
|
+
e.array :player_creation_rating, 'PlayerCreationRating' do |a|
|
54
|
+
a.string :comments, :doc => "comments doc"
|
55
|
+
a.integer :player_id, :doc => "player_id doc"
|
56
|
+
a.integer :rating, :doc => "rating doc"
|
57
|
+
a.string :username, :doc => "username doc"
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
|
63
|
+
service.documentation do |doc|
|
64
|
+
# doc.overall <markdown description text>
|
65
|
+
doc.overall <<-DOC
|
66
|
+
This is a test service used to test the framework.
|
67
|
+
DOC
|
68
|
+
|
69
|
+
# doc.params <name>, <definition>
|
70
|
+
doc.param :framework, "The test framework used, could be one of the two following: #{WeaselDieselSpecOptions.join(", ")}."
|
71
|
+
doc.param :version, "The version of the framework to use."
|
72
|
+
|
73
|
+
# doc.example <markdown text>
|
74
|
+
doc.example <<-DOC
|
75
|
+
The most common way to use this service looks like that:
|
76
|
+
http://example.com/services/test.xml?framework=rspec&version=2.0.0
|
77
|
+
DOC
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
|
82
|
+
describe_service "services/test_no_params.xml" do |service|
|
83
|
+
service.formats :xml
|
84
|
+
service.http_verb :get
|
85
|
+
service.accept_no_params!
|
86
|
+
end
|
87
|
+
|
88
|
+
describe_service "services.xml" do |service|
|
89
|
+
service.formats :xml
|
90
|
+
service.http_verb :put
|
91
|
+
|
92
|
+
end
|
93
|
+
|
94
|
+
describe_service "services/array_param.xml" do |s|
|
95
|
+
s.formats :xml
|
96
|
+
s.http_verb :post
|
97
|
+
|
98
|
+
s.params do |p|
|
99
|
+
p.array :seq, :required => true
|
100
|
+
end
|
101
|
+
|
102
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
require File.expand_path("spec_helper", File.dirname(__FILE__))
|
2
|
+
require 'sinatra'
|
3
|
+
require File.expand_path("./../lib/framework_ext/sinatra.rb", File.dirname(__FILE__))
|
4
|
+
WeaselDiesel.send(:include, WeaselDieselSinatraExtension)
|
5
|
+
|
6
|
+
describe "Hello World example" do
|
7
|
+
require_relative "hello_world_service"
|
8
|
+
require_relative "hello_world_controller"
|
9
|
+
|
10
|
+
def app
|
11
|
+
Sinatra::Application
|
12
|
+
end
|
13
|
+
|
14
|
+
before(:all) do
|
15
|
+
service = WSList.all.find{|s| s.url == 'hello_world.xml'}
|
16
|
+
service.should_not be_nil
|
17
|
+
service.load_sinatra_route
|
18
|
+
end
|
19
|
+
|
20
|
+
it "should dispatch the hello world service properly" do
|
21
|
+
get "/hello_world.xml"
|
22
|
+
last_response.body.should include("Hello World")
|
23
|
+
end
|
24
|
+
|
25
|
+
end
|
26
|
+
|
data/spec/wsdsl_spec.rb
ADDED
@@ -0,0 +1,314 @@
|
|
1
|
+
require File.expand_path("spec_helper", File.dirname(__FILE__))
|
2
|
+
|
3
|
+
describe WeaselDiesel do
|
4
|
+
|
5
|
+
before :all do
|
6
|
+
@service = WSList.all.find{|s| s.url == 'services/test.xml'}
|
7
|
+
@service.should_not be_nil
|
8
|
+
end
|
9
|
+
|
10
|
+
it "should have an url" do
|
11
|
+
# dummy test since that's how we found the service, but oh well
|
12
|
+
@service.url.should == 'services/test.xml'
|
13
|
+
end
|
14
|
+
|
15
|
+
it "should have some http verbs defined" do
|
16
|
+
@service.verb.should == :get
|
17
|
+
end
|
18
|
+
|
19
|
+
it "should have supported formats defined" do
|
20
|
+
@service.formats.should == [:xml, :json]
|
21
|
+
end
|
22
|
+
|
23
|
+
it "should have params info" do
|
24
|
+
@service.params.should be_an_instance_of(WeaselDiesel::Params)
|
25
|
+
end
|
26
|
+
|
27
|
+
it "should have direct access to the required params" do
|
28
|
+
@service.required_rules.should == @service.params.list_required
|
29
|
+
end
|
30
|
+
|
31
|
+
it "should have direct access to the optional params" do
|
32
|
+
@service.optional_rules.should == @service.params.list_optional
|
33
|
+
end
|
34
|
+
|
35
|
+
it "should have direct access to the nested params" do
|
36
|
+
@service.nested_params.should == @service.params.namespaced_params
|
37
|
+
end
|
38
|
+
|
39
|
+
describe "#controller_dispatch" do
|
40
|
+
|
41
|
+
class ProjectsController
|
42
|
+
def initialize(app, service)
|
43
|
+
@app = app
|
44
|
+
@service = service.name
|
45
|
+
end
|
46
|
+
|
47
|
+
def send(action)
|
48
|
+
[@app, @service, action]
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
module Projects
|
53
|
+
class TasksController < ProjectsController
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
module Projects
|
58
|
+
module Tasks
|
59
|
+
class ItemsController < ProjectsController
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
before :all do
|
65
|
+
@original_use_controller_dispatch = WeaselDiesel.use_controller_dispatch
|
66
|
+
WeaselDiesel.use_controller_dispatch = true
|
67
|
+
@original_services = WSList.all.dup
|
68
|
+
WSList.all.clear
|
69
|
+
end
|
70
|
+
|
71
|
+
after :all do
|
72
|
+
WeaselDiesel.use_controller_dispatch = @original_use_controller_dispatch
|
73
|
+
WSList.all.replace @original_services
|
74
|
+
end
|
75
|
+
|
76
|
+
it "should be able to dispatch controller" do
|
77
|
+
describe_service("projects.xml") { |s| }
|
78
|
+
service = WSList["projects.xml"]
|
79
|
+
service.controller_dispatch("application").
|
80
|
+
should == ["application", "projects", "list"]
|
81
|
+
end
|
82
|
+
|
83
|
+
it "should be able to dispatch namespaced controller" do
|
84
|
+
describe_service("project/:project_id/tasks.xml") do |service|
|
85
|
+
service.controller_name = "Projects::TasksController"
|
86
|
+
service.action = "list"
|
87
|
+
end
|
88
|
+
|
89
|
+
describe_service("project/:project_id/task/:task_id/items.xml") do |service|
|
90
|
+
service.controller_name = "Projects::Tasks::ItemsController"
|
91
|
+
service.action = "list"
|
92
|
+
end
|
93
|
+
|
94
|
+
service = WSList["project/:project_id/tasks.xml"]
|
95
|
+
service.controller_dispatch("application").should == ["application", "project", "list"]
|
96
|
+
|
97
|
+
service = WSList["project/:project_id/task/:task_id/items.xml"]
|
98
|
+
service.controller_dispatch("application").should == ["application", "project", "list"]
|
99
|
+
end
|
100
|
+
|
101
|
+
it "should raise exception when controller class is not found" do
|
102
|
+
describe_service("unknown.xml") do |service|
|
103
|
+
service.controller_name = "UnknownController"
|
104
|
+
service.action = "list"
|
105
|
+
end
|
106
|
+
service = WSList["unknown.xml"]
|
107
|
+
lambda { service.controller_dispatch("application") }.
|
108
|
+
should raise_error("The UnknownController class was not found")
|
109
|
+
end
|
110
|
+
|
111
|
+
end
|
112
|
+
|
113
|
+
describe "With controller dispatch on" do
|
114
|
+
before :all do
|
115
|
+
@original_services = WSList.all.dup
|
116
|
+
WSList.all.clear
|
117
|
+
WeaselDiesel.use_controller_dispatch = true
|
118
|
+
load File.expand_path('test_services.rb', File.dirname(__FILE__))
|
119
|
+
@c_service = WSList.all.find{|s| s.url == 'services/test.xml'}
|
120
|
+
@c_service.should_not be_nil
|
121
|
+
end
|
122
|
+
after :all do
|
123
|
+
WeaselDiesel.use_controller_dispatch = false
|
124
|
+
WSList.all.replace @original_services
|
125
|
+
end
|
126
|
+
|
127
|
+
it "should set the controller accordingly" do
|
128
|
+
@c_service.controller_name.should_not be_nil
|
129
|
+
@c_service.controller_name.should == 'ServicesController'
|
130
|
+
service = WeaselDiesel.new("preferences.xml")
|
131
|
+
service.name.should == 'preferences'
|
132
|
+
ExtlibCopy.classify('preferences').should == 'Preferences'
|
133
|
+
service.controller_name.should == 'PreferencesController'
|
134
|
+
end
|
135
|
+
|
136
|
+
it "should set the action accordingly" do
|
137
|
+
@c_service.action.should_not be_nil
|
138
|
+
@c_service.action.should == 'test'
|
139
|
+
end
|
140
|
+
|
141
|
+
it "should support restful routes based on the HTTP verb" do
|
142
|
+
service = WSList.all.find{|s| s.url == "services.xml"}
|
143
|
+
service.should_not be_nil
|
144
|
+
service.http_verb.should == :put
|
145
|
+
service.action.should_not be_nil
|
146
|
+
service.controller_name.should == 'ServicesController'
|
147
|
+
service.action.should == 'update'
|
148
|
+
end
|
149
|
+
|
150
|
+
it "should have a default action" do
|
151
|
+
service = WeaselDiesel.new('spec_test.xml')
|
152
|
+
service.action.should == 'list'
|
153
|
+
end
|
154
|
+
|
155
|
+
it "should route to show when an id is the last passed param" do
|
156
|
+
service = WeaselDiesel.new("players/:id.xml")
|
157
|
+
service.action.should == 'show'
|
158
|
+
end
|
159
|
+
|
160
|
+
it "should support some extra attributes" do
|
161
|
+
service = WeaselDiesel.new("players/:id.xml")
|
162
|
+
service.extra[:custom_name] = 'fooBar'
|
163
|
+
service.extra[:custom_name].should == 'fooBar'
|
164
|
+
end
|
165
|
+
|
166
|
+
it "should respect the global controller pluralization flag" do
|
167
|
+
WeaselDiesel.use_pluralized_controllers = true
|
168
|
+
service = WeaselDiesel.new("player/:id.xml")
|
169
|
+
service.controller_name.should == "PlayersController"
|
170
|
+
service = WeaselDiesel.new("players/:id.xml")
|
171
|
+
service.controller_name.should == "PlayersController"
|
172
|
+
WeaselDiesel.use_pluralized_controllers = false
|
173
|
+
service = WeaselDiesel.new("player/:id.xml")
|
174
|
+
service.controller_name.should == "PlayerController"
|
175
|
+
end
|
176
|
+
|
177
|
+
|
178
|
+
it "should let overwrite the controller name and action after initialization" do
|
179
|
+
describe_service "players/:id.xml" do |service|
|
180
|
+
service.controller_name = "CustomController"
|
181
|
+
service.action = "foo"
|
182
|
+
end
|
183
|
+
service = WSList.all.find{|s| s.url == "players/:id.xml"}
|
184
|
+
service.controller_name.should == "CustomController"
|
185
|
+
service.action.should == "foo"
|
186
|
+
end
|
187
|
+
|
188
|
+
end
|
189
|
+
|
190
|
+
|
191
|
+
describe WeaselDiesel::Params do
|
192
|
+
|
193
|
+
before(:all) do
|
194
|
+
@sparams = @service.params
|
195
|
+
end
|
196
|
+
|
197
|
+
it "should have the possibility to have a space name" do
|
198
|
+
@sparams.should respond_to(:space_name)
|
199
|
+
service_params = WeaselDiesel::Params.new(:space_name => 'spec_test')
|
200
|
+
service_params.space_name.should == 'spec_test'
|
201
|
+
end
|
202
|
+
|
203
|
+
it "should have a list of required param rules" do
|
204
|
+
@sparams.list_required.should be_an_instance_of(Array)
|
205
|
+
@sparams.list_required.length.should == 1
|
206
|
+
end
|
207
|
+
|
208
|
+
it "should have a list of optional param rules" do
|
209
|
+
@sparams.list_optional.should be_an_instance_of(Array)
|
210
|
+
@sparams.list_optional.length.should == 4
|
211
|
+
end
|
212
|
+
|
213
|
+
it "should have a list of namespaced param rules" do
|
214
|
+
@sparams.namespaced_params.should be_an_instance_of(Array)
|
215
|
+
@sparams.namespaced_params.length.should == 1
|
216
|
+
@sparams.namespaced_params.first.space_name.should == :user
|
217
|
+
end
|
218
|
+
|
219
|
+
describe WeaselDiesel::Params::Rule do
|
220
|
+
before :all do
|
221
|
+
@rule = @sparams.list_required.first
|
222
|
+
@rule.should_not be_nil
|
223
|
+
end
|
224
|
+
|
225
|
+
it "should have a name" do
|
226
|
+
@rule.name.should == :framework
|
227
|
+
end
|
228
|
+
|
229
|
+
it "should have options" do
|
230
|
+
@rule.options[:type].should == :string
|
231
|
+
@rule.options[:in].should == WeaselDieselSpecOptions
|
232
|
+
@rule.options[:null].should be_false
|
233
|
+
end
|
234
|
+
end
|
235
|
+
|
236
|
+
end
|
237
|
+
|
238
|
+
it "should have some documentation" do
|
239
|
+
@service.doc.should be_an_instance_of(WeaselDiesel::Documentation)
|
240
|
+
end
|
241
|
+
|
242
|
+
describe WeaselDiesel::Documentation do
|
243
|
+
before(:all) do
|
244
|
+
@doc = @service.doc
|
245
|
+
@doc.should_not be_nil
|
246
|
+
end
|
247
|
+
|
248
|
+
it "should have an overall description" do
|
249
|
+
@doc.desc.strip.should == "This is a test service used to test the framework."
|
250
|
+
end
|
251
|
+
|
252
|
+
it "should have a list of params doc" do
|
253
|
+
@doc.params_doc.should be_an_instance_of(Hash)
|
254
|
+
@doc.params_doc.keys.sort.should == [:framework, :version]
|
255
|
+
@doc.params_doc[:framework].should == "The test framework used, could be one of the two following: #{WeaselDieselSpecOptions.join(", ")}."
|
256
|
+
end
|
257
|
+
|
258
|
+
it "should allow to define namespaced params doc" do
|
259
|
+
service = WSList.all.find{|s| s.url == "services.xml"}
|
260
|
+
service.documentation do |doc|
|
261
|
+
doc.namespace :preference do |ns|
|
262
|
+
ns.param :id, "Ze id."
|
263
|
+
end
|
264
|
+
end
|
265
|
+
service.doc.namespaced_params.should_not be_empty
|
266
|
+
ns = service.doc.namespaced_params.find{|ns| ns.name == :preference}
|
267
|
+
ns.should_not be_nil
|
268
|
+
ns.params[:id].should == "Ze id."
|
269
|
+
end
|
270
|
+
|
271
|
+
it "should have an optional list of examples" do
|
272
|
+
@doc.examples.should be_an_instance_of(Array)
|
273
|
+
@doc.examples.first.should == <<-DOC
|
274
|
+
The most common way to use this service looks like that:
|
275
|
+
http://example.com/services/test.xml?framework=rspec&version=2.0.0
|
276
|
+
DOC
|
277
|
+
end
|
278
|
+
|
279
|
+
it "should have the service response documented" do
|
280
|
+
@doc.response.should_not be_nil
|
281
|
+
end
|
282
|
+
|
283
|
+
it "should have documentation for the response elements via the response itself" do
|
284
|
+
@service.response.elements.first.should_not be_nil
|
285
|
+
@service.response.elements.first.doc.should_not be_nil
|
286
|
+
@service.response.elements.first.doc.name.should == "player_creation_ratings"
|
287
|
+
end
|
288
|
+
|
289
|
+
it "should have documentation for a response element attribute" do
|
290
|
+
p @service.response.elements.first.doc.inspect
|
291
|
+
@service.response.elements.first.doc.attributes.should_not be_empty
|
292
|
+
@service.response.elements.first.doc.attributes[:id].should == "id doc"
|
293
|
+
end
|
294
|
+
|
295
|
+
it "should have documentation for a response element array" do
|
296
|
+
element = @service.response.elements.first
|
297
|
+
element.arrays.should_not be_empty
|
298
|
+
element.arrays.first.name.should == :player_creation_rating
|
299
|
+
element.arrays.first.type.should == "PlayerCreationRating"
|
300
|
+
element.arrays.first.attributes.should_not be_empty
|
301
|
+
end
|
302
|
+
|
303
|
+
it "should have documentation for the attributes of an response element array" do
|
304
|
+
element = @service.response.elements.first
|
305
|
+
array = element.arrays.first
|
306
|
+
attribute = array.attributes.find{|att| att.name == :comments }
|
307
|
+
attribute.should_not be_nil
|
308
|
+
attribute.name.should == :comments # just in case we change the way to find the attribute
|
309
|
+
attribute.doc.should == "comments doc"
|
310
|
+
end
|
311
|
+
|
312
|
+
end
|
313
|
+
|
314
|
+
end
|