wrest 0.0.8-java → 0.0.9-java
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.rdoc +24 -17
- data/Rakefile +6 -6
- data/VERSION.yml +1 -1
- data/examples/delicious.rb +7 -3
- data/examples/redirection.rb +27 -0
- data/examples/twitter.rb +78 -0
- data/{spec/wrest/http/request_spec.rb → examples/twitter_public_timeline.rb} +15 -12
- data/examples/wow_realm_status.rb +7 -3
- data/lib/wrest/components/attributes_container.rb +23 -5
- data/lib/wrest/components/attributes_container/alias_accessors.rb +66 -0
- data/lib/wrest/components/attributes_container/typecaster.rb +44 -38
- data/lib/wrest/components/mutators/base.rb +1 -1
- data/lib/wrest/components/translators.rb +1 -1
- data/lib/wrest/core_ext/string/conversions.rb +1 -1
- data/lib/wrest/exceptions.rb +14 -2
- data/lib/wrest/http.rb +1 -0
- data/lib/wrest/http/get.rb +2 -0
- data/lib/wrest/http/redirection.rb +35 -0
- data/lib/wrest/http/request.rb +37 -13
- data/lib/wrest/http/response.rb +17 -0
- data/lib/wrest/resource/base.rb +1 -2
- data/lib/wrest/uri.rb +2 -0
- data/lib/wrest/version.rb +1 -1
- data/spec/functional/sample_rails_app/README +3 -0
- data/spec/functional/sample_rails_app/Rakefile +10 -0
- data/spec/functional/sample_rails_app/app/controllers/application_controller.rb +10 -0
- data/spec/functional/sample_rails_app/app/controllers/lead_bottles_controller.rb +7 -0
- data/spec/functional/sample_rails_app/app/helpers/application_helper.rb +3 -0
- data/spec/functional/sample_rails_app/app/models/bottle.rb +3 -0
- data/spec/functional/sample_rails_app/app/models/glass_bottle.rb +3 -0
- data/spec/functional/sample_rails_app/app/models/lead_bottle.rb +3 -0
- data/spec/functional/sample_rails_app/config/boot.rb +110 -0
- data/spec/functional/sample_rails_app/config/database.yml +16 -0
- data/spec/functional/sample_rails_app/config/environment.rb +42 -0
- data/spec/functional/sample_rails_app/config/environments/development.rb +17 -0
- data/spec/functional/sample_rails_app/config/environments/production.rb +28 -0
- data/spec/functional/sample_rails_app/config/environments/test.rb +28 -0
- data/spec/functional/sample_rails_app/config/initializers/backtrace_silencers.rb +7 -0
- data/spec/functional/sample_rails_app/config/initializers/inflections.rb +10 -0
- data/spec/functional/sample_rails_app/config/initializers/mime_types.rb +5 -0
- data/spec/functional/sample_rails_app/config/initializers/new_rails_defaults.rb +19 -0
- data/spec/functional/sample_rails_app/config/initializers/session_store.rb +15 -0
- data/spec/functional/sample_rails_app/config/locales/en.yml +5 -0
- data/spec/functional/sample_rails_app/config/routes.rb +3 -0
- data/spec/functional/sample_rails_app/db/development.sqlite3 +0 -0
- data/spec/functional/sample_rails_app/db/migrate/20090319115628_create_bottle.rb +13 -0
- data/spec/functional/sample_rails_app/db/schema.rb +20 -0
- data/spec/functional/sample_rails_app/db/test.sqlite3 +0 -0
- data/spec/functional/sample_rails_app/log/development.log +1 -0
- data/spec/functional/sample_rails_app/public/404.html +30 -0
- data/spec/functional/sample_rails_app/public/422.html +30 -0
- data/spec/functional/sample_rails_app/public/500.html +30 -0
- data/spec/functional/sample_rails_app/public/favicon.ico +0 -0
- data/spec/functional/sample_rails_app/public/images/rails.png +0 -0
- data/spec/functional/sample_rails_app/public/index.html +275 -0
- data/spec/functional/sample_rails_app/public/robots.txt +5 -0
- data/spec/functional/sample_rails_app/script/about +4 -0
- data/spec/functional/sample_rails_app/script/autospec +6 -0
- data/spec/functional/sample_rails_app/script/console +3 -0
- data/spec/functional/sample_rails_app/script/dbconsole +3 -0
- data/spec/functional/sample_rails_app/script/destroy +3 -0
- data/spec/functional/sample_rails_app/script/generate +3 -0
- data/spec/functional/sample_rails_app/script/performance/benchmarker +3 -0
- data/spec/functional/sample_rails_app/script/performance/profiler +3 -0
- data/spec/functional/sample_rails_app/script/plugin +3 -0
- data/spec/functional/sample_rails_app/script/runner +3 -0
- data/spec/functional/sample_rails_app/script/server +3 -0
- data/spec/functional/sample_rails_app/script/spec +10 -0
- data/spec/functional/sample_rails_app/script/spec_server +9 -0
- data/spec/functional/sample_rails_app/test/performance/browsing_test.rb +9 -0
- data/spec/functional/sample_rails_app/test/test_helper.rb +38 -0
- data/spec/functional/sample_rails_app/tmtags +2559 -0
- data/spec/functional/sample_rails_app/vendor/plugins/resource_full/MIT-LICENSE +20 -0
- data/spec/functional/sample_rails_app/vendor/plugins/resource_full/README.rdoc +100 -0
- data/spec/functional/sample_rails_app/vendor/plugins/resource_full/Rakefile +18 -0
- data/spec/functional/sample_rails_app/vendor/plugins/resource_full/init.rb +5 -0
- data/spec/functional/sample_rails_app/vendor/plugins/resource_full/install.rb +1 -0
- data/spec/functional/sample_rails_app/vendor/plugins/resource_full/lib/resource_full.rb +14 -0
- data/spec/functional/sample_rails_app/vendor/plugins/resource_full/lib/resource_full/base.rb +140 -0
- data/spec/functional/sample_rails_app/vendor/plugins/resource_full/lib/resource_full/controllers/resources.rb +16 -0
- data/spec/functional/sample_rails_app/vendor/plugins/resource_full/lib/resource_full/controllers/resources_controller.rb +26 -0
- data/spec/functional/sample_rails_app/vendor/plugins/resource_full/lib/resource_full/controllers/routes_controller.rb +16 -0
- data/spec/functional/sample_rails_app/vendor/plugins/resource_full/lib/resource_full/core_extensions/api.rb +26 -0
- data/spec/functional/sample_rails_app/vendor/plugins/resource_full/lib/resource_full/core_extensions/exception.rb +23 -0
- data/spec/functional/sample_rails_app/vendor/plugins/resource_full/lib/resource_full/core_extensions/from_json.rb +15 -0
- data/spec/functional/sample_rails_app/vendor/plugins/resource_full/lib/resource_full/dispatch.rb +235 -0
- data/spec/functional/sample_rails_app/vendor/plugins/resource_full/lib/resource_full/models/resourced_route.rb +84 -0
- data/spec/functional/sample_rails_app/vendor/plugins/resource_full/lib/resource_full/query.rb +337 -0
- data/spec/functional/sample_rails_app/vendor/plugins/resource_full/lib/resource_full/render.rb +63 -0
- data/spec/functional/sample_rails_app/vendor/plugins/resource_full/lib/resource_full/render/html.rb +50 -0
- data/spec/functional/sample_rails_app/vendor/plugins/resource_full/lib/resource_full/render/json.rb +75 -0
- data/spec/functional/sample_rails_app/vendor/plugins/resource_full/lib/resource_full/render/xml.rb +65 -0
- data/spec/functional/sample_rails_app/vendor/plugins/resource_full/lib/resource_full/retrieve.rb +74 -0
- data/spec/functional/sample_rails_app/vendor/plugins/resource_full/lib/resource_full/version.rb +9 -0
- data/spec/functional/sample_rails_app/vendor/plugins/resource_full/spec/resource_full/base_spec.rb +88 -0
- data/spec/functional/sample_rails_app/vendor/plugins/resource_full/spec/resource_full/controllers/resources_spec.rb +29 -0
- data/spec/functional/sample_rails_app/vendor/plugins/resource_full/spec/resource_full/dispatch_spec.rb +262 -0
- data/spec/functional/sample_rails_app/vendor/plugins/resource_full/spec/resource_full/models/resourced_route_spec.rb +62 -0
- data/spec/functional/sample_rails_app/vendor/plugins/resource_full/spec/resource_full/query/parameter_spec.rb +57 -0
- data/spec/functional/sample_rails_app/vendor/plugins/resource_full/spec/resource_full/query_spec.rb +462 -0
- data/spec/functional/sample_rails_app/vendor/plugins/resource_full/spec/resource_full/render/html_spec.rb +4 -0
- data/spec/functional/sample_rails_app/vendor/plugins/resource_full/spec/resource_full/render/json_spec.rb +107 -0
- data/spec/functional/sample_rails_app/vendor/plugins/resource_full/spec/resource_full/render/xml_spec.rb +98 -0
- data/spec/functional/sample_rails_app/vendor/plugins/resource_full/spec/resource_full/render_spec.rb +5 -0
- data/spec/functional/sample_rails_app/vendor/plugins/resource_full/spec/resource_full/retrieve_spec.rb +173 -0
- data/spec/functional/sample_rails_app/vendor/plugins/resource_full/spec/spec_helper.rb +98 -0
- data/spec/functional/sample_rails_app/vendor/plugins/resource_full/uninstall.rb +1 -0
- data/spec/functional/spec_helper.rb +0 -0
- data/spec/{spec_helper.rb → unit/spec_helper.rb} +2 -2
- data/spec/unit/wrest/components/attributes_container/alias_accessors_spec.rb +49 -0
- data/spec/{wrest → unit/wrest}/components/attributes_container/typecaster_spec.rb +28 -8
- data/spec/{wrest → unit/wrest}/components/attributes_container_spec.rb +42 -15
- data/spec/{wrest → unit/wrest}/components/mutators/base_spec.rb +1 -1
- data/spec/{wrest → unit/wrest}/components/mutators/camel_to_snake_spec.rb +0 -0
- data/spec/{wrest → unit/wrest}/components/mutators/xml_mini_type_caster_spec.rb +0 -0
- data/spec/{wrest → unit/wrest}/components/mutators/xml_simple_type_caster_spec.rb +0 -0
- data/spec/{wrest → unit/wrest}/components/mutators_spec.rb +0 -0
- data/spec/{wrest → unit/wrest}/components/translators/xml_spec.rb +0 -0
- data/spec/{wrest → unit/wrest}/components/translators_spec.rb +1 -1
- data/spec/{wrest → unit/wrest}/core_ext/hash/conversions_spec.rb +0 -0
- data/spec/{wrest → unit/wrest}/core_ext/string/conversions_spec.rb +14 -0
- data/spec/unit/wrest/http/redirection_spec.rb +42 -0
- data/spec/unit/wrest/http/request_spec.rb +70 -0
- data/spec/unit/wrest/http/response_spec.rb +45 -0
- data/spec/{wrest → unit/wrest}/resource/base_spec.rb +5 -4
- data/spec/{wrest → unit/wrest}/uri_spec.rb +68 -67
- data/spec/{wrest → unit/wrest}/uri_template_spec.rb +0 -0
- metadata +188 -39
- data/lib/wrest/exceptions/method_not_overridden_exception.rb +0 -17
- data/lib/wrest/exceptions/unsupported_content_type_exception.rb +0 -17
- data/spec/wrest/http/response_spec.rb +0 -21
data/spec/functional/sample_rails_app/vendor/plugins/resource_full/lib/resource_full/render/html.rb
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
module ResourceFull
|
|
2
|
+
module Render
|
|
3
|
+
module HTML
|
|
4
|
+
protected
|
|
5
|
+
|
|
6
|
+
def show_html
|
|
7
|
+
self.model_object = send("find_#{model_name}")
|
|
8
|
+
rescue ActiveRecord::RecordNotFound => e
|
|
9
|
+
flash[:error] = e.message
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
def index_html
|
|
13
|
+
self.model_objects = send("find_all_#{model_name.pluralize}")
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def create_html
|
|
17
|
+
self.model_object = send("create_#{model_name}")
|
|
18
|
+
if model_object.errors.empty?
|
|
19
|
+
flash[:info] = "Successfully created #{model_name.humanize} with ID of #{model_object.id}."
|
|
20
|
+
redirect_to :action => :index, :format => :html
|
|
21
|
+
else
|
|
22
|
+
render :action => "new"
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def update_html
|
|
27
|
+
self.model_object = send("update_#{model_name}")
|
|
28
|
+
if model_object.errors.empty?
|
|
29
|
+
flash[:info] = "Successfully updated #{model_name.humanize} with ID of #{model_object.id}."
|
|
30
|
+
redirect_to :action => :index, :format => :html
|
|
31
|
+
else
|
|
32
|
+
render :action => "edit"
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def destroy_html
|
|
37
|
+
self.model_object = send("destroy_#{model_name}")
|
|
38
|
+
flash[:info] = "Successfully destroyed #{model_name.humanize} with ID of #{params[:id]}."
|
|
39
|
+
redirect_to :action => :index, :format => :html
|
|
40
|
+
rescue ActiveRecord::RecordNotFound => e
|
|
41
|
+
flash[:error] = e.message
|
|
42
|
+
redirect_to :back
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
def new_html
|
|
46
|
+
self.model_object = send("new_#{model_name}")
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
end
|
data/spec/functional/sample_rails_app/vendor/plugins/resource_full/lib/resource_full/render/json.rb
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
module ResourceFull
|
|
2
|
+
module Render
|
|
3
|
+
module JSON
|
|
4
|
+
protected
|
|
5
|
+
|
|
6
|
+
def json_class_name(obj)
|
|
7
|
+
obj.class.name.demodulize.underscore
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
def show_json_options
|
|
11
|
+
{}
|
|
12
|
+
end
|
|
13
|
+
def show_json
|
|
14
|
+
self.model_object = send("find_#{model_name}")
|
|
15
|
+
render :json => model_object.to_json(show_json_options)
|
|
16
|
+
rescue ActiveRecord::RecordNotFound => e
|
|
17
|
+
render :json => e.to_json , :status => :not_found
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def index_json_options
|
|
21
|
+
{}
|
|
22
|
+
end
|
|
23
|
+
def index_json
|
|
24
|
+
self.model_objects = send("find_all_#{model_name.pluralize}")
|
|
25
|
+
render :json => model_objects.to_json(index_json_options)
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def create_json_options
|
|
29
|
+
{}
|
|
30
|
+
end
|
|
31
|
+
def create_json
|
|
32
|
+
self.model_object = send("create_#{model_name}")
|
|
33
|
+
if model_object.errors.empty?
|
|
34
|
+
render :json => model_object.to_json(create_json_options), :status => :created, :location => send("#{model_name}_url", model_object.id)
|
|
35
|
+
else
|
|
36
|
+
json_data = model_object.attributes
|
|
37
|
+
json_data[:errors] = {:list => model_object.errors,
|
|
38
|
+
:full_messages => model_object.errors.full_messages}
|
|
39
|
+
render :json => {json_class_name(model_object) => json_data}.to_json , :status => status_for(model_object.errors)
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
def update_json_options
|
|
44
|
+
{}
|
|
45
|
+
end
|
|
46
|
+
def update_json
|
|
47
|
+
self.model_object = send("update_#{model_name}")
|
|
48
|
+
if model_object.errors.empty?
|
|
49
|
+
render :json => model_object.to_json(update_json_options)
|
|
50
|
+
else
|
|
51
|
+
json_data = model_object.attributes
|
|
52
|
+
json_data[:errors] = {:list => model_object.errors,
|
|
53
|
+
:full_messages => model_object.errors.full_messages}
|
|
54
|
+
render :json => {json_class_name(model_object) => json_data}.to_json , :status => status_for(model_object.errors)
|
|
55
|
+
end
|
|
56
|
+
rescue ActiveRecord::RecordNotFound => e
|
|
57
|
+
render :json => e.to_json , :status => :not_found
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
def destroy_json
|
|
61
|
+
self.model_object = send("destroy_#{model_name}")
|
|
62
|
+
head :ok
|
|
63
|
+
rescue ActiveRecord::RecordNotFound => e
|
|
64
|
+
render :json => e.to_json , :status => :not_found
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
def new_json_options
|
|
68
|
+
{}
|
|
69
|
+
end
|
|
70
|
+
def new_json
|
|
71
|
+
render :json => send("new_#{model_name}").to_json(new_json_options)
|
|
72
|
+
end
|
|
73
|
+
end
|
|
74
|
+
end
|
|
75
|
+
end
|
data/spec/functional/sample_rails_app/vendor/plugins/resource_full/lib/resource_full/render/xml.rb
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
module ResourceFull
|
|
2
|
+
module Render
|
|
3
|
+
module XML
|
|
4
|
+
protected
|
|
5
|
+
|
|
6
|
+
def show_xml_options
|
|
7
|
+
{}
|
|
8
|
+
end
|
|
9
|
+
def show_xml
|
|
10
|
+
self.model_object = send("find_#{model_name}")
|
|
11
|
+
render :xml => model_object.to_xml(show_xml_options)
|
|
12
|
+
rescue ActiveRecord::RecordNotFound => e
|
|
13
|
+
render :xml => e.to_xml, :status => :not_found
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def index_xml_options
|
|
17
|
+
{}
|
|
18
|
+
end
|
|
19
|
+
def index_xml
|
|
20
|
+
self.model_objects = send("find_all_#{model_name.pluralize}")
|
|
21
|
+
render :xml => model_objects.to_xml(index_xml_options)
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def create_xml_options
|
|
25
|
+
{}
|
|
26
|
+
end
|
|
27
|
+
def create_xml
|
|
28
|
+
self.model_object = send("create_#{model_name}")
|
|
29
|
+
if model_object.errors.empty?
|
|
30
|
+
render :xml => model_object.to_xml(create_xml_options), :status => :created, :location => send("#{model_name}_url", model_object.id)
|
|
31
|
+
else
|
|
32
|
+
render :xml => model_object.errors.to_xml, :status => status_for(model_object.errors)
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def update_xml_options
|
|
37
|
+
{}
|
|
38
|
+
end
|
|
39
|
+
def update_xml
|
|
40
|
+
self.model_object = send("update_#{model_name}")
|
|
41
|
+
if model_object.errors.empty?
|
|
42
|
+
render :xml => model_object.to_xml(update_xml_options)
|
|
43
|
+
else
|
|
44
|
+
render :xml => model_object.errors.to_xml, :status => status_for(model_object.errors)
|
|
45
|
+
end
|
|
46
|
+
rescue ActiveRecord::RecordNotFound => e
|
|
47
|
+
render :xml => e.to_xml, :status => :not_found
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
def destroy_xml
|
|
51
|
+
self.model_object = send("destroy_#{model_name}")
|
|
52
|
+
head :ok
|
|
53
|
+
rescue ActiveRecord::RecordNotFound => e
|
|
54
|
+
render :xml => e.to_xml, :status => :not_found
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
def new_xml_options
|
|
58
|
+
{}
|
|
59
|
+
end
|
|
60
|
+
def new_xml
|
|
61
|
+
render :xml => send("new_#{model_name}").to_xml(new_xml_options)
|
|
62
|
+
end
|
|
63
|
+
end
|
|
64
|
+
end
|
|
65
|
+
end
|
data/spec/functional/sample_rails_app/vendor/plugins/resource_full/lib/resource_full/retrieve.rb
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
module ResourceFull
|
|
2
|
+
module Retrieve
|
|
3
|
+
class << self
|
|
4
|
+
def included(base)
|
|
5
|
+
super(base)
|
|
6
|
+
# Define new_person, update_person, etc.
|
|
7
|
+
base.before_filter :move_queryable_params_into_model_params_on_create, :only => [:create]
|
|
8
|
+
end
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
# Override this to provide custom find conditions. This is automatically merged at query
|
|
12
|
+
# time with the queried conditions extracted from params.
|
|
13
|
+
def find_options; {}; end
|
|
14
|
+
|
|
15
|
+
protected
|
|
16
|
+
|
|
17
|
+
def find_model_object
|
|
18
|
+
# TODO I am not sure what the correct behavior should be here, but I'm artifically
|
|
19
|
+
# generating the exception in order to avoid altering the render methods for the time being.
|
|
20
|
+
returning(model_class.find(:first, :conditions => { resource_identifier => params[:id]})) do |o|
|
|
21
|
+
raise ActiveRecord::RecordNotFound, "not found: #{params[:id]}" if o.nil?
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def new_model_object
|
|
26
|
+
model_class.new
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def update_model_object
|
|
30
|
+
returning(find_model_object) do |object|
|
|
31
|
+
object.update_attributes params[model_name]
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def create_model_object
|
|
36
|
+
model_class.create(params[model_name])
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def destroy_model_object
|
|
40
|
+
model_class.destroy_all(resource_identifier => params[:id])
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
def find_all_model_objects
|
|
44
|
+
completed_query.find(:all)
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
def count_all_model_objects
|
|
48
|
+
completed_query.count
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
def move_queryable_params_into_model_params_on_create
|
|
52
|
+
params.except(model_name).each do |param_name, value|
|
|
53
|
+
if self.class.queryable_params.collect(&:name).include?(param_name.to_sym)
|
|
54
|
+
params[model_name][param_name] = params.delete(param_name)
|
|
55
|
+
end
|
|
56
|
+
end
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
private
|
|
60
|
+
|
|
61
|
+
def completed_query
|
|
62
|
+
self.class.queryable_params.inject(model_class) do |finder, queryer|
|
|
63
|
+
queryer.find finder, params
|
|
64
|
+
end
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
def resource_identifier
|
|
68
|
+
returning(self.class.resource_identifier) do |column|
|
|
69
|
+
return column.call(params[:id]) if column.is_a?(Proc)
|
|
70
|
+
end
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
end
|
|
74
|
+
end
|
data/spec/functional/sample_rails_app/vendor/plugins/resource_full/spec/resource_full/base_spec.rb
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
require File.dirname(__FILE__) + '/../spec_helper'
|
|
2
|
+
|
|
3
|
+
describe ResourceFull::Base, :type => :controller do
|
|
4
|
+
controller_name "resource_full_mocks"
|
|
5
|
+
|
|
6
|
+
it "infers the name of its resource model from its class name" do
|
|
7
|
+
controller.model_name.should == "resource_full_mock"
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
it "infers the class of its resource model from its class name" do
|
|
11
|
+
controller.model_class.should == ResourceFullMock
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
class ResourceFullFake; end
|
|
15
|
+
|
|
16
|
+
it "exposes a particular resource model given a symbol" do
|
|
17
|
+
controller.class.exposes :resource_full_fake
|
|
18
|
+
controller.model_class.should == ResourceFullFake
|
|
19
|
+
controller.class.exposes :resource_full_mock # cleanup
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
it "exposes a particular resource model given a pluralized symbol" do
|
|
23
|
+
controller.class.exposes :resource_full_fakes
|
|
24
|
+
controller.model_class.should == ResourceFullFake
|
|
25
|
+
controller.class.exposes :resource_full_mock # cleanup
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
it "exposes a particular resource model given a class" do
|
|
29
|
+
controller.class.exposes ResourceFullFake
|
|
30
|
+
controller.model_class.should == ResourceFullFake
|
|
31
|
+
controller.class.exposes ResourceFullMock # cleanup
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
it "renders two formats by default" do
|
|
35
|
+
controller.class.allowed_formats.should include(:xml, :html)
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
it "allows you to specify what formats to render" do
|
|
39
|
+
controller.class.responds_to :xml, :json
|
|
40
|
+
controller.class.allowed_formats.should include(:xml, :json)
|
|
41
|
+
controller.class.allowed_formats.should_not include(:html)
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
class NonResourcesController < ActionController::Base; end
|
|
45
|
+
class ResourcesController < ResourceFull::Base; end
|
|
46
|
+
|
|
47
|
+
it "knows about all controller subclasses of itself" do
|
|
48
|
+
ActionController::Routing.expects(:possible_controllers).at_least_once.returns %w{resources non_resources}
|
|
49
|
+
ResourceFull::Base.all_resources.should include(ResourcesController)
|
|
50
|
+
ResourceFull::Base.all_resources.should_not include(NonResourcesController)
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
it "serializes the notion of a resource controller as XML" do
|
|
54
|
+
ResourceFullMockUsersController.clear_queryable_params!
|
|
55
|
+
ResourceFullMockUsersController.queryable_with :first_name
|
|
56
|
+
xml = Hash.from_xml(ResourceFullMockUsersController.to_xml)
|
|
57
|
+
xml["resource"]["name"].should == "resource_full_mock_users"
|
|
58
|
+
xml["resource"]["parameters"].first["name"].should == "first_name"
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
it "has a default value of :id for the resource identifier column" do
|
|
62
|
+
ResourceFullMockUsersController.resource_identifier.should == :id
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
it "allows you to set the resource_identifier field" do
|
|
66
|
+
controller.class.identified_by :first_name
|
|
67
|
+
controller.class.resource_identifier.should == :first_name
|
|
68
|
+
controller.class.resource_identifier = :id # cleanup
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
it "is paginatable by default" do
|
|
72
|
+
controller.class.should be_paginatable
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
it "translates a model name into a controller constant" do
|
|
76
|
+
ResourceFull::Base.controller_for("resource_full_mock_users").should == ResourceFullMockUsersController
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
it "raises ResourceNotFound if it cannot constantize the requested controller" do
|
|
80
|
+
lambda do
|
|
81
|
+
ResourceFull::Base.controller_for("nonsense")
|
|
82
|
+
end.should raise_error(ResourceFull::ResourceNotFound, "not found: nonsense")
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
it "returns the controller it's been given if it receives a Class object" do
|
|
86
|
+
ResourceFull::Base.controller_for(ResourceFullMockUsersController).should == ResourceFullMockUsersController
|
|
87
|
+
end
|
|
88
|
+
end
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
require File.dirname(__FILE__) + '/../../spec_helper'
|
|
2
|
+
|
|
3
|
+
module ResourceFull
|
|
4
|
+
module Controllers
|
|
5
|
+
describe ResourcesController, :type => :controller do
|
|
6
|
+
|
|
7
|
+
it "finds all resources" do
|
|
8
|
+
get :index, :format => 'xml'
|
|
9
|
+
response.should have_tag('resources>resource') do
|
|
10
|
+
with_tag('name', 'resource_full_mock_users')
|
|
11
|
+
with_tag('name', 'resource_full_mock_addresses')
|
|
12
|
+
end
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
it "finds a specific resource" do
|
|
16
|
+
get :show, :format => 'xml', :id => 'resource_full_mock_users'
|
|
17
|
+
response.should have_tag('resource>name', 'resource_full_mock_users')
|
|
18
|
+
response.should_not have_tag('resource>name', 'resource_full_mock_addresses')
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
it "returns a 404 response when the requested resource is not found" do
|
|
22
|
+
get :show, :format => 'xml', :id => 'foo'
|
|
23
|
+
response.body.should have_tag("errors") { with_tag("error", "ResourceFull::ResourceNotFound: not found: foo") }
|
|
24
|
+
response.code.should == '404'
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
end
|
|
29
|
+
end
|
|
@@ -0,0 +1,262 @@
|
|
|
1
|
+
require File.dirname(__FILE__) + '/../spec_helper'
|
|
2
|
+
|
|
3
|
+
describe "ResourceFull::Dispatch", :type => :controller do
|
|
4
|
+
controller_name "resource_full_mocks"
|
|
5
|
+
|
|
6
|
+
before(:each) do
|
|
7
|
+
ResourceFullMock.stubs(:find).returns stub(:id => 1)
|
|
8
|
+
# controller.stubs :render
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
it "exposes a method for skipping format and method protection"
|
|
12
|
+
|
|
13
|
+
describe "based on request format" do
|
|
14
|
+
controller_name "resource_full_mocks"
|
|
15
|
+
|
|
16
|
+
after :each do
|
|
17
|
+
controller.class.responds_to :defaults
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
it "dispatches to index_xml render method if xml is requested" do
|
|
21
|
+
controller.expects(:index_xml)
|
|
22
|
+
get :index, :format => 'xml'
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
it "dispatches to index_json render method if json is requested" do
|
|
26
|
+
controller.expects(:index_json)
|
|
27
|
+
get :index, :format => 'json'
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
it "dispatches to index_html render method if html is requested" do
|
|
31
|
+
controller.expects(:index_html)
|
|
32
|
+
controller.stubs(:render)
|
|
33
|
+
get :index, :format => 'html'
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
it "raises a 406 error if it does not respond to a format for which no methods are included" do
|
|
37
|
+
get :index, :format => 'txt'
|
|
38
|
+
response.code.should == '406'
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
it "raises a 406 error if it does not respond to a format which has been explicitly removed" do
|
|
42
|
+
controller.class.responds_to :xml
|
|
43
|
+
get :index, :format => 'html'
|
|
44
|
+
response.code.should == '406'
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
it "includes an appropriate error message if it does not respond to a format which has been explicitly removed" do
|
|
48
|
+
controller.class.responds_to :xml
|
|
49
|
+
get :index, :format => 'html'
|
|
50
|
+
response.body.should =~ /Resource does not have a representation in text\/html format/
|
|
51
|
+
end
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
describe "based on request action" do
|
|
55
|
+
controller_name "resource_full_mocks"
|
|
56
|
+
|
|
57
|
+
after :each do
|
|
58
|
+
controller.class.responds_to :defaults
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
it "claims to respond to create, read, update, delete, and count by default" do
|
|
62
|
+
controller.class.responds_to :defaults
|
|
63
|
+
controller.class.allowed_methods.should include(:create, :read, :update, :delete)
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
it "lists all the standard Rails methods plus count as its possible actions" do
|
|
67
|
+
controller.class.possible_actions.should include(:create, :new, :show, :index, :count, :update, :edit, :destroy)
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
it "claims to not respond to any methods for an unsupported format" do
|
|
71
|
+
controller.class.responds_to :xml
|
|
72
|
+
controller.class.allowed_methods(:html).should be_empty
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
it "claims to respond to default methods for a requested format if no explicit methods are given" do
|
|
76
|
+
controller.class.responds_to :xml
|
|
77
|
+
controller.class.allowed_methods(:xml).should include(:create, :read, :update, :delete)
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
it "claims to respond to only methods given a single value with the :only option" do
|
|
81
|
+
controller.class.responds_to :xml, :only => :read
|
|
82
|
+
controller.class.allowed_methods(:xml).should == [:read]
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
it "claims to respond to only methods given multiple values with the :only option" do
|
|
86
|
+
controller.class.responds_to :xml, :only => [:read, :delete]
|
|
87
|
+
controller.class.allowed_methods(:xml).should == [:read, :delete]
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
it "responds successfully to supported methods" do
|
|
91
|
+
controller.class.responds_to :xml, :only => :read
|
|
92
|
+
controller.stubs(:index)
|
|
93
|
+
get :index, :format => "xml"
|
|
94
|
+
response.should be_success
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
it "disallows unsupported methods with code 405" do
|
|
98
|
+
controller.class.responds_to :html, :only => :read
|
|
99
|
+
controller.stubs(:destroy)
|
|
100
|
+
delete :destroy, :id => 1
|
|
101
|
+
response.code.should == '405'
|
|
102
|
+
response.body.should =~ /Resource does not allow destroy action/
|
|
103
|
+
end
|
|
104
|
+
|
|
105
|
+
it "ignores and does not verify custom methods" do
|
|
106
|
+
controller.class.responds_to :xml, :only => [:delete]
|
|
107
|
+
|
|
108
|
+
get :foo, :format => 'xml'
|
|
109
|
+
response.body.should have_tag("foo", "bar")
|
|
110
|
+
response.code.should == '200'
|
|
111
|
+
end
|
|
112
|
+
|
|
113
|
+
it "allows you to specify the appropriate CRUD semantics of a custom method"
|
|
114
|
+
end
|
|
115
|
+
|
|
116
|
+
describe "GET index" do
|
|
117
|
+
controller_name "resource_full_mocks"
|
|
118
|
+
|
|
119
|
+
before :each do
|
|
120
|
+
controller.stubs(:render)
|
|
121
|
+
end
|
|
122
|
+
|
|
123
|
+
it "sets an @mocks instance variable based on the default finder" do
|
|
124
|
+
ResourceFullMock.stubs(:find).returns "a list of mocks"
|
|
125
|
+
get :index, :format => 'html'
|
|
126
|
+
assigns(:resource_full_mocks).should == "a list of mocks"
|
|
127
|
+
end
|
|
128
|
+
|
|
129
|
+
it "sets an @mocks instance variable appropriately if the default finder is overridden" do
|
|
130
|
+
begin
|
|
131
|
+
controller.class.class_eval do
|
|
132
|
+
def find_all_resource_full_mocks; "another list of mocks"; end
|
|
133
|
+
end
|
|
134
|
+
get :index, :format => 'html'
|
|
135
|
+
assigns(:resource_full_mocks).should == "another list of mocks"
|
|
136
|
+
ensure
|
|
137
|
+
controller.class.class_eval do
|
|
138
|
+
undef :find_all_resource_full_mocks
|
|
139
|
+
end
|
|
140
|
+
end
|
|
141
|
+
end
|
|
142
|
+
end
|
|
143
|
+
|
|
144
|
+
describe "GET show" do
|
|
145
|
+
controller_name "resource_full_mocks"
|
|
146
|
+
|
|
147
|
+
before :each do
|
|
148
|
+
controller.stubs(:render)
|
|
149
|
+
end
|
|
150
|
+
|
|
151
|
+
it "sets a @mock instance variable based on the default finder" do
|
|
152
|
+
ResourceFullMock.stubs(:find).returns "a mock"
|
|
153
|
+
get :show, :id => 1, :format => 'html'
|
|
154
|
+
assigns(:resource_full_mock).should == "a mock"
|
|
155
|
+
end
|
|
156
|
+
|
|
157
|
+
it "sets a @mock instance variable appropriately if the default finder is overridden" do
|
|
158
|
+
controller.class.class_eval do
|
|
159
|
+
def find_resource_full_mock; "another mock"; end
|
|
160
|
+
end
|
|
161
|
+
get :show, :id => 1, :format => 'html'
|
|
162
|
+
assigns(:resource_full_mock).should == "another mock"
|
|
163
|
+
end
|
|
164
|
+
end
|
|
165
|
+
|
|
166
|
+
describe "POST create" do
|
|
167
|
+
controller_name "resource_full_mocks"
|
|
168
|
+
|
|
169
|
+
before :each do
|
|
170
|
+
controller.stubs :render
|
|
171
|
+
end
|
|
172
|
+
|
|
173
|
+
it "sets a @mock instance variable based on the default creator" do
|
|
174
|
+
ResourceFullMock.stubs(:create).returns stub(:errors => stub_everything, :id => :mock)
|
|
175
|
+
post :create, :format => 'html'
|
|
176
|
+
assigns(:resource_full_mock).id.should == :mock
|
|
177
|
+
end
|
|
178
|
+
|
|
179
|
+
it "sets a @mock instance variable appropriately if the default creator is overridden" do
|
|
180
|
+
ResourceFullMock.stubs(:super_create).returns stub(:errors => stub_everything, :id => :super_mock)
|
|
181
|
+
controller.class.class_eval do
|
|
182
|
+
def create_resource_full_mock; ResourceFullMock.super_create; end
|
|
183
|
+
end
|
|
184
|
+
post :create, :format => 'html'
|
|
185
|
+
assigns(:resource_full_mock).id.should == :super_mock
|
|
186
|
+
end
|
|
187
|
+
end
|
|
188
|
+
|
|
189
|
+
describe "PUT update" do
|
|
190
|
+
controller_name "resource_full_mocks"
|
|
191
|
+
|
|
192
|
+
before :each do
|
|
193
|
+
controller.stubs :render
|
|
194
|
+
end
|
|
195
|
+
|
|
196
|
+
it "sets a @mock instance variable based on the default updater" do
|
|
197
|
+
ResourceFullMock.stubs(:find).returns stub(:id => 1, :update_attributes => true, :errors => stub_everything)
|
|
198
|
+
put :update, :id => 1, :format => 'html'
|
|
199
|
+
assigns(:resource_full_mock).id.should == 1
|
|
200
|
+
end
|
|
201
|
+
|
|
202
|
+
it "sets a @mock instance variable appropriately if the default updater is overridden" do
|
|
203
|
+
ResourceFullMock.stubs(:super_update).returns stub(:errors => stub_everything, :id => :super_mock)
|
|
204
|
+
controller.class.class_eval do
|
|
205
|
+
def update_resource_full_mock; ResourceFullMock.super_update; end
|
|
206
|
+
end
|
|
207
|
+
put :update, :id => 1, :format => 'html'
|
|
208
|
+
assigns(:resource_full_mock).id.should == :super_mock
|
|
209
|
+
end
|
|
210
|
+
end
|
|
211
|
+
|
|
212
|
+
describe "when the user agent is IE7" do
|
|
213
|
+
before :each do
|
|
214
|
+
request.env["HTTP_USER_AGENT"] = "MSIE 7.0"
|
|
215
|
+
controller.stubs(:find_all_resource_full_mocks).returns([])
|
|
216
|
+
end
|
|
217
|
+
|
|
218
|
+
it "should set the request format to json when the incoming request format looks like json" do
|
|
219
|
+
get :index, :format => 'json-test'
|
|
220
|
+
response.content_type.should == "application/json"
|
|
221
|
+
end
|
|
222
|
+
|
|
223
|
+
it "should set the request format to json when the incoming request format looks like javascript" do
|
|
224
|
+
get :index, :format => 'javascript-test'
|
|
225
|
+
response.content_type.should == "application/json"
|
|
226
|
+
end
|
|
227
|
+
|
|
228
|
+
it "should set the request format to json when the incoming request uri looks like json" do
|
|
229
|
+
request.env["REQUEST_URI"] = "/resource_full_mocks.json?foo=bar"
|
|
230
|
+
get :index
|
|
231
|
+
response.content_type.should == "application/json"
|
|
232
|
+
end
|
|
233
|
+
|
|
234
|
+
it "should set the request format to xml when the incoming request format looks like xml" do
|
|
235
|
+
get :index, :format => 'xml-test'
|
|
236
|
+
response.content_type.should == "application/xml"
|
|
237
|
+
end
|
|
238
|
+
|
|
239
|
+
it "should set the request format to json when the incoming request uri looks like xml" do
|
|
240
|
+
request.env["REQUEST_URI"] = "/resource_full_mocks.xml?foo=bar"
|
|
241
|
+
get :index
|
|
242
|
+
response.content_type.should == "application/xml"
|
|
243
|
+
end
|
|
244
|
+
|
|
245
|
+
# Dirk assures me that the following functionality is necessary due to the way Rails handles IE7
|
|
246
|
+
# request formats, or perhaps the way IE7 sends its request content-type.
|
|
247
|
+
# TODO Find a better criterion than 'else, use text/html'.
|
|
248
|
+
it "should default the request format to text/html when the incoming request uri is not supported" do
|
|
249
|
+
controller.stubs :render
|
|
250
|
+
request.env["REQUEST_URI"] = "/resource_full_mocks.png"
|
|
251
|
+
get :index
|
|
252
|
+
response.content_type.should == "text/html"
|
|
253
|
+
end
|
|
254
|
+
|
|
255
|
+
# See above.
|
|
256
|
+
it "should default the request format to text/html when the incoming request format is not supported" do
|
|
257
|
+
controller.stubs :render
|
|
258
|
+
get :index, :format => "image/png"
|
|
259
|
+
response.content_type.should == "text/html"
|
|
260
|
+
end
|
|
261
|
+
end
|
|
262
|
+
end
|