From 03d37a2d68c1940f65d5a65a51ae747955a5b075 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tobias=20L=C3=BCtke?= Date: Sun, 5 Mar 2006 18:59:58 +0000 Subject: Added new infrastructure support for REST webservices. By default application/xml posts are handled by creating a XmlNode object with the same name as the root element of the submitted xml. M$ ActionController::Base.param_parsers['application/atom+xml'] = Proc.new do |data| node = REXML::Document.new(post) { node.root.name => node.root } end XmlSimple and Yaml web services were retired, ActionController::Base.param_parsers carries an example which shows how to get this functio$ request.[formatted_post?, xml_post?, yaml_post? and post_format] were all deprecated in favor of request.content_type [Tobias Luetke] Closes #4081 git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@3777 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- actionpack/test/controller/webservice_test.rb | 146 ++++++++++++++++++++++++++ 1 file changed, 146 insertions(+) create mode 100644 actionpack/test/controller/webservice_test.rb (limited to 'actionpack/test/controller') diff --git a/actionpack/test/controller/webservice_test.rb b/actionpack/test/controller/webservice_test.rb new file mode 100644 index 0000000000..844ac723ba --- /dev/null +++ b/actionpack/test/controller/webservice_test.rb @@ -0,0 +1,146 @@ +require File.dirname(__FILE__) + '/../abstract_unit' +require 'stringio' + +class WebServiceTest < Test::Unit::TestCase + + class MockCGI < CGI #:nodoc: + attr_accessor :stdinput, :stdoutput, :env_table + + def initialize(env, data = '') + self.env_table = env + self.stdinput = StringIO.new(data) + self.stdoutput = StringIO.new + super() + end + end + + + class TestController < ActionController::Base + session :off + + def assign_parameters + render :text => (@params.keys - ['controller', 'action']).sort.join(", ") + end + + def rescue_action(e) raise end + end + + def setup + @controller = TestController.new + end + + def test_check_parameters + process('GET') + assert_equal '', @controller.response.body + end + + def test_post_xml + process('POST', 'application/xml', 'content...') + + assert_equal 'entry', @controller.response.body + assert @controller.params.has_key?(:entry) + assert_equal 'content...', @controller.params["entry"].summary.node_value + assert_equal 'true', @controller.params["entry"]['attributed'] + end + + def test_put_xml + process('PUT', 'application/xml', 'content...') + + assert_equal 'entry', @controller.response.body + assert @controller.params.has_key?(:entry) + assert_equal 'content...', @controller.params["entry"].summary.node_value + assert_equal 'true', @controller.params["entry"]['attributed'] + end + + def test_register_and_use_yaml + ActionController::Base.param_parsers['application/x-yaml'] = Proc.new { |d| YAML.load(d) } + process('POST', 'application/x-yaml', {"entry" => "loaded from yaml"}.to_yaml) + assert @controller.params.has_key?(:entry) + assert_equal 'loaded from yaml', @controller.params["entry"] + ensure + ActionController::Base.param_parsers['application/x-yaml'] = nil + end + + + def test_deprecated_request_methods + process('POST', 'application/x-yaml') + assert_equal 'application/x-yaml', @controller.request.content_type + assert_equal true, @controller.request.post? + assert_equal :yaml, @controller.request.post_format + assert_equal true, @controller.request.yaml_post? + assert_equal false, @controller.request.xml_post? + end + + + private + + def process(verb, content_type = 'application/x-www-form-urlencoded', data = '') + + cgi = MockCGI.new({ + 'REQUEST_METHOD' => verb, + 'CONTENT_TYPE' => content_type, + 'QUERY_STRING' => "action=assign_parameters&controller=webservicetest/test", + "REQUEST_URI" => "/", + "HTTP_HOST" => 'testdomain.com', + "CONTENT_LENGTH" => data.size, + "SERVER_PORT" => "80", + "HTTPS" => "off"}, data) + + @controller.send(:process, ActionController::CgiRequest.new(cgi, {}), ActionController::CgiResponse.new(cgi)) + end + +end + + +class XmlNodeTest < Test::Unit::TestCase + def test_all + xn = XmlNode.from_xml(%{ + + + With O'Reilly and Adaptive Path + + + Staying at the Savoy + + + + + + + + + } + ) + assert_equal 'UTF-8', xn.node.document.encoding + assert_equal '1.0', xn.node.document.version + assert_equal 'true', xn['success'] + assert_equal 'response', xn.node_name + assert_equal 'Ajax Summit', xn.page['title'] + assert_equal '1133', xn.page['id'] + assert_equal "With O'Reilly and Adaptive Path", xn.page.description.node_value + assert_equal nil, xn.nonexistent + assert_equal "Staying at the Savoy", xn.page.notes.note.node_value.strip + assert_equal 'Technology', xn.page.tags.tag[0]['name'] + assert_equal 'Travel', xn.page.tags.tag[1][:name] + matches = xn.xpath('//@id').map{ |id| id.to_i } + assert_equal [4, 5, 1020, 1133], matches.sort + matches = xn.xpath('//tag').map{ |tag| tag['name'] } + assert_equal ['Technology', 'Travel'], matches.sort + assert_equal "Ajax Summit", xn.page['title'] + xn.page['title'] = 'Ajax Summit V2' + assert_equal "Ajax Summit V2", xn.page['title'] + assert_equal "Staying at the Savoy", xn.page.notes.note.node_value.strip + xn.page.notes.note.node_value = "Staying at the Ritz" + assert_equal "Staying at the Ritz", xn.page.notes.note.node_value.strip + assert_equal '5', xn.page.tags.tag[1][:id] + xn.page.tags.tag[1]['id'] = '7' + assert_equal '7', xn.page.tags.tag[1]['id'] + end + + + def test_small_entry + node = XmlNode.from_xml('hi') + assert_equal 'hi', node.node_value + end + +end -- cgit v1.2.3