restrack 0.0.1

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.
Files changed (60) hide show
  1. data/Gemfile +4 -0
  2. data/README.rdoc +71 -0
  3. data/Rakefile +32 -0
  4. data/bin/restrack +23 -0
  5. data/config/constants.yaml +8 -0
  6. data/lib/restrack/generator/constants.yaml.erb +24 -0
  7. data/lib/restrack/generator/controller.rb.erb +35 -0
  8. data/lib/restrack/generator/loader.rb.erb +21 -0
  9. data/lib/restrack/generator.rb +93 -0
  10. data/lib/restrack/http_status.rb +10 -0
  11. data/lib/restrack/resource_controller.rb +192 -0
  12. data/lib/restrack/resource_request.rb +135 -0
  13. data/lib/restrack/support.rb +56 -0
  14. data/lib/restrack/version.rb +3 -0
  15. data/lib/restrack/web_service.rb +66 -0
  16. data/lib/restrack.rb +24 -0
  17. data/restrack.gemspec +28 -0
  18. data/test/sample_app_1/config/constants.yaml +25 -0
  19. data/test/sample_app_1/controllers/bat_controller.rb +9 -0
  20. data/test/sample_app_1/controllers/baz_controller.rb +9 -0
  21. data/test/sample_app_1/controllers/baza_controller.rb +11 -0
  22. data/test/sample_app_1/controllers/bazu_controller.rb +16 -0
  23. data/test/sample_app_1/controllers/foo_bar_controller.rb +62 -0
  24. data/test/sample_app_1/loader.rb +31 -0
  25. data/test/sample_app_1/test/test_controller_actions.rb +122 -0
  26. data/test/sample_app_1/test/test_controller_modifiers.rb +153 -0
  27. data/test/sample_app_1/test/test_formats.rb +119 -0
  28. data/test/sample_app_1/test/test_resource_request.rb +160 -0
  29. data/test/sample_app_1/test/test_web_service.rb +27 -0
  30. data/test/sample_app_1/views/foo_bar/show.xml.builder +4 -0
  31. data/test/sample_app_2/config/constants.yaml +24 -0
  32. data/test/sample_app_2/controllers/bat_controller.rb +9 -0
  33. data/test/sample_app_2/controllers/baz_controller.rb +9 -0
  34. data/test/sample_app_2/controllers/baza_controller.rb +11 -0
  35. data/test/sample_app_2/controllers/bazu_controller.rb +8 -0
  36. data/test/sample_app_2/controllers/foo_bar_controller.rb +59 -0
  37. data/test/sample_app_2/loader.rb +31 -0
  38. data/test/sample_app_2/test/test_controller_modifiers.rb +121 -0
  39. data/test/sample_app_2/test/test_resource_request.rb +71 -0
  40. data/test/sample_app_2/views/foo_bar/show.xml.builder +4 -0
  41. data/test/sample_app_3/config/constants.yaml +24 -0
  42. data/test/sample_app_3/controllers/bat_controller.rb +9 -0
  43. data/test/sample_app_3/controllers/baz_controller.rb +9 -0
  44. data/test/sample_app_3/controllers/baza_controller.rb +11 -0
  45. data/test/sample_app_3/controllers/bazu_controller.rb +8 -0
  46. data/test/sample_app_3/controllers/foo_bar_controller.rb +59 -0
  47. data/test/sample_app_3/loader.rb +31 -0
  48. data/test/sample_app_3/test/test_resource_request.rb +42 -0
  49. data/test/sample_app_3/views/foo_bar/show.xml.builder +4 -0
  50. data/test/sample_app_4/config/constants.yaml +24 -0
  51. data/test/sample_app_4/controllers/bar_controller.rb +11 -0
  52. data/test/sample_app_4/controllers/baz_controller.rb +15 -0
  53. data/test/sample_app_4/controllers/foo_controller.rb +21 -0
  54. data/test/sample_app_4/loader.rb +31 -0
  55. data/test/sample_app_4/test/test_controller_modifiers.rb +28 -0
  56. data/test/sample_app_4/test/test_formats.rb +49 -0
  57. data/test/sample_app_4/views/alphatest.png +0 -0
  58. data/test/test_support.rb +20 -0
  59. data/test/test_web_service.rb +31 -0
  60. metadata +238 -0
@@ -0,0 +1,119 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'rack/test'
4
+ require File.expand_path(File.join(File.dirname(__FILE__),'..','loader'))
5
+ require 'pp'
6
+
7
+ class SampleApp::TestFormats < Test::Unit::TestCase
8
+
9
+ def setup
10
+ @ws = SampleApp::WebService.new
11
+ end
12
+
13
+ def test_show_builder_xml
14
+ env = Rack::MockRequest.env_for('/foo_bar/144.xml', {
15
+ :method => 'GET'
16
+ })
17
+ output = ''
18
+ assert_nothing_raised do
19
+ output = @ws.call(env)
20
+ end
21
+ test_val = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><data><foo>bar</foo><baz>123</baz></data>"
22
+ assert_equal test_val, output[2]
23
+ end
24
+
25
+ def test_show_default_xml
26
+ env = Rack::MockRequest.env_for('/foo_bar/index.xml', {
27
+ :method => 'GET'
28
+ })
29
+ output = ''
30
+ assert_nothing_raised do
31
+ output = @ws.call(env)
32
+ end
33
+ test_val = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><data><foo>bar</foo><baz>123</baz></data>"
34
+ assert_equal test_val, output[2]
35
+
36
+ env = Rack::MockRequest.env_for('/foo_bar.xml', {
37
+ :method => 'GET'
38
+ })
39
+ output = ''
40
+ assert_nothing_raised do
41
+ output = @ws.call(env)
42
+ end
43
+ test_val = XmlSimple.xml_out([1,2,3,4,5,6,7])
44
+ assert_equal test_val, output[2]
45
+ end
46
+
47
+ def test_show_json
48
+ env = Rack::MockRequest.env_for('/foo_bar/144', {
49
+ :method => 'GET'
50
+ })
51
+ output = ''
52
+ assert_nothing_raised do
53
+ output = @ws.call(env)
54
+ end
55
+ test_val = { :foo => 'bar', :baz => 123 }.to_json
56
+ assert_equal test_val, output[2]
57
+ end
58
+ #
59
+ #def test_complex_data_structure
60
+ # env = Rack::MockRequest.env_for('/foo_bar/1234567890', {
61
+ # :method => 'GET'
62
+ # })
63
+ # output = ''
64
+ # assert_nothing_raised do
65
+ # output = @ws.call(env)
66
+ # end
67
+ # test_val = "{\"foo\":\"abc\",\"baz\":456,\"bar\":\"123\",\"more\":{\"two\":[1,2],\"three\":\"deep_fu\",\"one\":1}}"
68
+ # assert_equal test_val, output[2]
69
+ #
70
+ # env = Rack::MockRequest.env_for('/foo_bar/1234567890.xml', {
71
+ # :method => 'GET'
72
+ # })
73
+ # output = ''
74
+ # assert_nothing_raised do
75
+ # output = @ws.call(env)
76
+ # end
77
+ # test_val = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><data><foo>abc</foo><baz>456</baz><bar>123</bar><more><two></data>"
78
+ # assert_equal test_val, output[2]
79
+ #
80
+ # #env = Rack::MockRequest.env_for('/foo_bar/42', {
81
+ # # :method => 'GET'
82
+ # #})
83
+ # #output = ''
84
+ # #assert_nothing_raised do
85
+ # # output = @ws.call(env)
86
+ # #end
87
+ # ##test_val = {
88
+ # ## :foo => 'abc',
89
+ # ## :bar => 123,
90
+ # ## :baz => {
91
+ # ## 'one' => [1],
92
+ # ## 'two' => ['1','2'],
93
+ # ## 'three' => ['1', 2, {:three => 3}],
94
+ # ## 4 => :four
95
+ # ## }
96
+ # ##}.to_json
97
+ # #assert_equal test_val, output[2]
98
+ # #
99
+ # #env = Rack::MockRequest.env_for('/foo_bar/42.xml', {
100
+ # # :method => 'GET'
101
+ # #})
102
+ # #output = ''
103
+ # #assert_nothing_raised do
104
+ # # output = @ws.call(env)
105
+ # #end
106
+ # ##test_val = {
107
+ # ## :foo => 'abc',
108
+ # ## :bar => 123,
109
+ # ## :baz => {
110
+ # ## 'one' => [1],
111
+ # ## 'two' => ['1','2'],
112
+ # ## 'three' => ['1', 2, {:three => 3}],
113
+ # ## 4 => :four
114
+ # ## }
115
+ # ##}
116
+ # #assert_equal test_val, output[2]
117
+ #end
118
+
119
+ end
@@ -0,0 +1,160 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'rack/test'
4
+ require File.expand_path(File.join(File.dirname(__FILE__),'..','loader'))
5
+
6
+ class SampleApp::TestResourceRequest < Test::Unit::TestCase
7
+
8
+ def setup
9
+ @ws = SampleApp::WebService.new # init logs
10
+ end
11
+
12
+ def test_locate
13
+ env = Rack::MockRequest.env_for('/foo_bar', {
14
+ :method => 'POST',
15
+ :params => %Q|[
16
+ {
17
+ "bar": ""
18
+ }
19
+ ]|
20
+ })
21
+ request = Rack::Request.new(env)
22
+ assert_nothing_raised do
23
+ resource_request = RESTRack::ResourceRequest.new(:request => request)
24
+ resource_request.locate
25
+ end
26
+ end
27
+
28
+ def test_initialize
29
+ env = Rack::MockRequest.env_for('/foo_bar', {
30
+ :method => 'POST',
31
+ :params => %Q|[
32
+ {
33
+ "bar": "baz"
34
+ }
35
+ ]|
36
+ })
37
+ request = Rack::Request.new(env)
38
+ assert_nothing_raised do
39
+ resource_request = RESTRack::ResourceRequest.new(:request => request)
40
+ end
41
+ end
42
+
43
+ def test_root_resource_accept
44
+ # These are the resources which can be accessed from the root of your web service. If left empty, all resources are available at the root.
45
+ #:ROOT_RESOURCE_ACCEPT: [ 'foo_bar' ]
46
+
47
+ RESTRack::CONFIG[:ROOT_RESOURCE_ACCEPT] = [ 'foo_bar' ]
48
+ # This should not be allowed because it is not in ROOT_RESOURCE_ACCEPT
49
+ env = Rack::MockRequest.env_for('/bat/144', {
50
+ :method => 'GET'
51
+ })
52
+ output = ''
53
+ assert_nothing_raised do
54
+ output = @ws.call(env)
55
+ end
56
+ #test_val = [403, {"Content-Type"=>"text/plain"}, "HTTPStatus::HTTP403Forbidden\nYou are forbidden to access that resource."]
57
+ assert_equal 403, output[0]
58
+
59
+ # This should be allowed
60
+ env = Rack::MockRequest.env_for('/foo_bar/144', {
61
+ :method => 'GET'
62
+ })
63
+ output = ''
64
+ assert_nothing_raised do
65
+ output = @ws.call(env)
66
+ end
67
+ assert_equal 200, output[0]
68
+
69
+ RESTRack::CONFIG[:ROOT_RESOURCE_ACCEPT] = []
70
+ env = Rack::MockRequest.env_for('/foo_bar/144', {
71
+ :method => 'GET'
72
+ })
73
+ output = ''
74
+ assert_nothing_raised do
75
+ output = @ws.call(env)
76
+ end
77
+ assert_equal 200, output[0]
78
+
79
+ RESTRack::CONFIG[:ROOT_RESOURCE_ACCEPT] = nil
80
+ env = Rack::MockRequest.env_for('/foo_bar/144', {
81
+ :method => 'GET'
82
+ })
83
+ output = ''
84
+ assert_nothing_raised do
85
+ output = @ws.call(env)
86
+ end
87
+ assert_equal 200, output[0]
88
+
89
+ RESTRack::CONFIG[:ROOT_RESOURCE_ACCEPT] = [ 'foo_bar' ]
90
+ end
91
+
92
+
93
+ def test_root_resource_denied
94
+ ## These are the resources which cannot be accessed from the root of your web service. Use either this or ROOT_RESOURCE_ACCEPT as a blacklist or whitelist to establish routing (relationships defined in resource controllers define further routing).
95
+ #:ROOT_RESOURCE_DENY: [ 'baz' ]
96
+ env = Rack::MockRequest.env_for('/baz/144', {
97
+ :method => 'GET'
98
+ })
99
+ output = ''
100
+ assert_nothing_raised do
101
+ output = @ws.call(env)
102
+ end
103
+ #test_val = [403, {"Content-Type"=>"text/plain"}, "HTTPStatus::HTTP403Forbidden\nYou are forbidden to access that resource."]
104
+ assert_equal 403, output[0]
105
+
106
+ env = Rack::MockRequest.env_for('/bat/144', {
107
+ :method => 'GET'
108
+ })
109
+ assert_nothing_raised do
110
+ output = @ws.call(env)
111
+ end
112
+ assert_not_equal 200, output[0]
113
+
114
+ RESTRack::CONFIG[:ROOT_RESOURCE_DENY] = []
115
+ env = Rack::MockRequest.env_for('/foo_bar/144', {
116
+ :method => 'GET'
117
+ })
118
+ output = ''
119
+ assert_nothing_raised do
120
+ output = @ws.call(env)
121
+ end
122
+ assert_equal 200, output[0]
123
+
124
+ RESTRack::CONFIG[:ROOT_RESOURCE_DENY] = nil
125
+ env = Rack::MockRequest.env_for('/foo_bar/144', {
126
+ :method => 'GET'
127
+ })
128
+ output = ''
129
+ assert_nothing_raised do
130
+ output = @ws.call(env)
131
+ end
132
+ assert_equal 200, output[0]
133
+
134
+ RESTRack::CONFIG[:ROOT_RESOURCE_DENY] = ['']
135
+ # it should handle this, although it is incorrect
136
+ env = Rack::MockRequest.env_for('/foo_bar/144', {
137
+ :method => 'GET'
138
+ })
139
+ output = ''
140
+ assert_nothing_raised do
141
+ output = @ws.call(env)
142
+ end
143
+ assert_equal 200, output[0]
144
+
145
+ RESTRack::CONFIG[:ROOT_RESOURCE_DENY] = [ 'baz' ]
146
+ end
147
+
148
+ def test_default_resource
149
+ # This should be handled by bazu_controller
150
+ env = Rack::MockRequest.env_for('/', {
151
+ :method => 'GET'
152
+ })
153
+ output = ''
154
+ assert_nothing_raised do
155
+ output = @ws.call(env)
156
+ end
157
+ assert_equal 403, output[0]
158
+ end
159
+
160
+ end
@@ -0,0 +1,27 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'rack/test'
4
+ require File.expand_path(File.join(File.dirname(__FILE__),'..','loader'))
5
+
6
+ class SampleApp::TestWebService < Test::Unit::TestCase
7
+
8
+ def setup
9
+ @ws = SampleApp::WebService.new # init logs
10
+ end
11
+
12
+ def test_service_name
13
+ env = Rack::MockRequest.env_for('/foo_bar', {
14
+ :method => 'POST',
15
+ :params => %Q|[
16
+ {
17
+ "bar": "baz"
18
+ }
19
+ ]|
20
+ })
21
+ output = ''
22
+ output = @ws.call(env)
23
+ assert_nothing_raised do
24
+ RESTRack::CONFIG[:SERVICE_NAME].to_sym.to_s
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,4 @@
1
+ xml.data do
2
+ xml.foo data[:foo]
3
+ xml.baz data[:baz]
4
+ end
@@ -0,0 +1,24 @@
1
+ #GENERATOR-CONST# -DO NOT REMOVE OR CHANGE THIS LINE- Application-Namespace => SampleApp
2
+ #
3
+ # = constants.yaml
4
+ # This is where RESTRack applications define the constants relevant to their particular
5
+ # application that are used by the RESTRack base classes.
6
+
7
+ # Application log path definition
8
+ :LOG: '/var/log/restrack/sample_app.log'
9
+ # Request log path definition
10
+ :REQUEST_LOG: '/var/log/restrack/sample_app.request.log'
11
+
12
+ # Logger object levels
13
+ :LOG_LEVEL: :WARN # Logger object level
14
+ :REQUEST_LOG_LEVEL: :WARN # Logger object level
15
+
16
+ # Supported formats are :JSON, :XML, :YAML, :BIN, :TEXT
17
+ :DEFAULT_FORMAT: :JSON
18
+ # The resource which will handle root level requests where the name is not specified. Best for users of this not to implement method_missing in their default controller, unless they are checking for bad URI.
19
+ :DEFAULT_RESOURCE: 'bazu'
20
+
21
+ # These are the resources which can be accessed from the root of your web service. If left empty, all resources are available at the root.
22
+ #:ROOT_RESOURCE_ACCEPT: []
23
+ # These are the resources which cannot be accessed from the root of your web service. Use either this or ROOT_RESOURCE_ACCEPT as a blacklist or whitelist to establish routing (relationships defined in resource controllers define further routing).
24
+ :ROOT_RESOURCE_DENY: [ 'baz' ]
@@ -0,0 +1,9 @@
1
+ class SampleApp::BatController < RESTRack::ResourceController
2
+
3
+ def show(id)
4
+ return { :WOM => 'BAT!' } if id == 777
5
+ return { :WOM => 'NOBAT!' } if id == '777'
6
+ return { :SUHWING => 'BATTER' }
7
+ end
8
+
9
+ end
@@ -0,0 +1,9 @@
1
+ class SampleApp::BazController < RESTRack::ResourceController
2
+
3
+ def show(id)
4
+ return { :BAZ => 'HOLA!' } if id == 777
5
+ return { :BAZ => 'ALOHA!' } if id == '777'
6
+ return { :OTHER => 'YUP' }
7
+ end
8
+
9
+ end
@@ -0,0 +1,11 @@
1
+ class SampleApp::BazaController < RESTRack::ResourceController
2
+
3
+ keyed_with_type Fixnum
4
+
5
+ def show(id)
6
+ return { :BAZA => 'YESSIR' } if id == 1
7
+ return { :NOWAY => 'JOSE' } if id == 8
8
+ return { :NOTTODAY => 'JOSEPH' }
9
+ end
10
+
11
+ end
@@ -0,0 +1,8 @@
1
+ class SampleApp::BazuController < RESTRack::ResourceController
2
+
3
+ def show(id)
4
+ return 1 if id == 1
5
+ return 0
6
+ end
7
+
8
+ end
@@ -0,0 +1,59 @@
1
+ class SampleApp::FooBarController < RESTRack::ResourceController
2
+
3
+ has_direct_relationship_to( :baz ) do |id|
4
+ if id =='144'
5
+ output = '777'
6
+ else
7
+ output = '666'
8
+ end
9
+ output # You can't "return" from a Proc! It will do a "return" in the outer method. Remember a "Proc" is not a Method.
10
+ end
11
+
12
+ has_direct_relationship_to( :bat, :as => :slugger ) do |id|
13
+ if id =='144'
14
+ output = '777'
15
+ else
16
+ output = '666'
17
+ end
18
+ output # You can't "return" from a Proc! It will do a "return" in the outer method. Remember a "Proc" is not a Method.
19
+ end
20
+
21
+ has_direct_relationships_to( :baza, :as => :children ) do |id|
22
+ [1,2,3,4,5,6,7,8,9]
23
+ end
24
+
25
+ has_mapped_relationships_to( :bazu, :as => :maps ) do |id|
26
+ {
27
+ :first => 1,
28
+ :second => 2,
29
+ :third => 3
30
+ }
31
+ end
32
+
33
+ def index
34
+ [1,2,3,4,5,6,7]
35
+ end
36
+ def create
37
+ { :success => true }
38
+ end
39
+ def replace
40
+ { :success => true }
41
+ end
42
+ def drop
43
+ { :success => true }
44
+ end
45
+
46
+ def show(id)
47
+ { :foo => 'bar', :baz => 123 }
48
+ end
49
+ def update(id)
50
+ { :success => true }
51
+ end
52
+ def destroy(id)
53
+ { :success => true }
54
+ end
55
+ def add(id)
56
+ { :success => true }
57
+ end
58
+
59
+ end
@@ -0,0 +1,31 @@
1
+ # for development only
2
+ $:.unshift File.expand_path(File.join(File.dirname(__FILE__),'../../lib'))
3
+ #####
4
+ require 'restrack'
5
+
6
+ module SampleApp; end
7
+ class SampleApp::WebService < RESTRack::WebService; end
8
+
9
+ RESTRack::CONFIG = RESTRack::load_config(File.join(File.dirname(__FILE__), 'config/constants.yaml'))
10
+ RESTRack::CONFIG[:ROOT] = File.dirname(__FILE__)
11
+
12
+ # Dynamically load all controllers
13
+ Find.find( File.join(File.dirname(__FILE__), 'controllers') ) do |file|
14
+ next if File.extname(file) != '.rb'
15
+ require file
16
+ end
17
+
18
+ if File.directory?( File.join(File.dirname(__FILE__), 'models') )
19
+ # Dynamically load all models
20
+ Find.find( File.join(File.dirname(__FILE__), 'models') ) do |file|
21
+ next if File.extname(file) != '.rb'
22
+ require file
23
+ end
24
+ end
25
+
26
+ puts "sample_app_2 RESTRack::CONFIG:\n"
27
+ config = RESTRack::CONFIG.keys.map {|c| c.to_s }.sort
28
+ config.each do |key|
29
+ puts "\t" + key + ' => ' + RESTRack::CONFIG[key.to_sym].to_s
30
+ end
31
+ puts "\n"
@@ -0,0 +1,121 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'rack/test'
4
+ require File.expand_path(File.join(File.dirname(__FILE__),'..','loader'))
5
+ require 'pp'
6
+
7
+ class SampleApp::TestControllerModifiers < Test::Unit::TestCase
8
+
9
+ def setup
10
+ @ws = SampleApp::WebService.new
11
+ end
12
+
13
+ def test_has_direct_relationship_to
14
+ env = Rack::MockRequest.env_for('/foo_bar/144/baz', {
15
+ :method => 'GET'
16
+ })
17
+ output = ''
18
+ assert_nothing_raised do
19
+ output = @ws.call(env)
20
+ end
21
+ test_val = { :BAZ => 'HOLA!' }.to_json
22
+ assert_equal test_val, output[2]
23
+
24
+ env = Rack::MockRequest.env_for('/foo_bar/133/baz', {
25
+ :method => 'GET'
26
+ })
27
+ output = ''
28
+ assert_nothing_raised do
29
+ output = @ws.call(env)
30
+ end
31
+ test_val = { :OTHER => 'YUP' }.to_json
32
+ assert_equal test_val, output[2]
33
+
34
+ env = Rack::MockRequest.env_for('/foo_bar/144/baz/', {
35
+ :method => 'GET'
36
+ })
37
+ output = ''
38
+ assert_nothing_raised do
39
+ output = @ws.call(env)
40
+ end
41
+ test_val = { :BAZ => 'HOLA!' }.to_json
42
+ assert_equal test_val, output[2]
43
+ end
44
+
45
+ def test_has_direct_relationships_to
46
+ env = Rack::MockRequest.env_for('/foo_bar/133/children/1', {
47
+ :method => 'GET'
48
+ })
49
+ output = ''
50
+ assert_nothing_raised do
51
+ output = @ws.call(env)
52
+ end
53
+ test_val = { :BAZA => 'YESSIR' }.to_json
54
+ assert_equal test_val, output[2]
55
+
56
+ env = Rack::MockRequest.env_for('/foo_bar/133/children/8', {
57
+ :method => 'GET'
58
+ })
59
+ output = ''
60
+ assert_nothing_raised do
61
+ output = @ws.call(env)
62
+ end
63
+ test_val = { :NOWAY => 'JOSE' }.to_json
64
+ assert_equal test_val, output[2]
65
+
66
+ env = Rack::MockRequest.env_for('/foo_bar/133/children/11', {
67
+ :method => 'GET'
68
+ })
69
+ output = ''
70
+ assert_nothing_raised do
71
+ output = @ws.call(env)
72
+ end
73
+ assert_equal 404, output[0]
74
+ end
75
+
76
+ def test_has_mapped_relationships_to
77
+ env = Rack::MockRequest.env_for('/foo_bar/133/maps/first', {
78
+ :method => 'GET'
79
+ })
80
+ output = ''
81
+ assert_nothing_raised do
82
+ output = @ws.call(env)
83
+ end
84
+ test_val = '1'
85
+ assert_equal test_val, output[2]
86
+
87
+ env = Rack::MockRequest.env_for('/foo_bar/133/maps/second', {
88
+ :method => 'GET'
89
+ })
90
+ output = ''
91
+ assert_nothing_raised do
92
+ output = @ws.call(env)
93
+ end
94
+ test_val = '0'
95
+ assert_equal test_val, output[2]
96
+
97
+ env = Rack::MockRequest.env_for('/foo_bar/133/maps/third', {
98
+ :method => 'GET'
99
+ })
100
+ output = ''
101
+ assert_nothing_raised do
102
+ output = @ws.call(env)
103
+ end
104
+ test_val = '0'
105
+ assert_equal test_val, output[2]
106
+ end
107
+
108
+ def test_keyed_with_type
109
+ # baza controller exercises this option
110
+ env = Rack::MockRequest.env_for('/foo_bar/133/children/1', {
111
+ :method => 'GET'
112
+ })
113
+ output = ''
114
+ assert_nothing_raised do
115
+ output = @ws.call(env)
116
+ end
117
+ test_val = { :BAZA => 'YESSIR' }.to_json
118
+ assert_equal test_val, output[2]
119
+ end
120
+
121
+ end
@@ -0,0 +1,71 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'rack/test'
4
+ require File.expand_path(File.join(File.dirname(__FILE__),'..','loader'))
5
+
6
+ class SampleApp::TestResourceRequest < Test::Unit::TestCase
7
+
8
+ def setup
9
+ @ws = SampleApp::WebService.new # init logs
10
+ end
11
+
12
+ ## These are the resources which can be accessed from the root of your web service. If left empty, all resources are available at the root.
13
+ ##:ROOT_RESOURCE_ACCEPT: []
14
+ ## These are the resources which cannot be accessed from the root of your web service. Use either this or ROOT_RESOURCE_ACCEPT as a blacklist or whitelist to establish routing (relationships defined in resource controllers define further routing).
15
+ #:ROOT_RESOURCE_DENY: [ 'baz' ]
16
+ def test_root_resource_denied
17
+ env = Rack::MockRequest.env_for('/baz/144', {
18
+ :method => 'GET'
19
+ })
20
+ output = ''
21
+ assert_nothing_raised do
22
+ output = @ws.call(env)
23
+ end
24
+ #test_val = [403, {"Content-Type"=>"text/plain"}, "HTTPStatus::HTTP403Forbidden\nYou are forbidden to access that resource."]
25
+ assert_equal 403, output[0]
26
+
27
+ env = Rack::MockRequest.env_for('/bat/144', {
28
+ :method => 'GET'
29
+ })
30
+ assert_nothing_raised do
31
+ output = @ws.call(env)
32
+ end
33
+ #test_val = [403, {"Content-Type"=>"text/plain"}, "HTTPStatus::HTTP403Forbidden\nYou are forbidden to access that resource."]
34
+ assert_not_equal 403, output[0]
35
+ end
36
+
37
+ def test_root_resource_accept
38
+ env = Rack::MockRequest.env_for('/foo_bar/144', {
39
+ :method => 'GET'
40
+ })
41
+ output = ''
42
+ assert_nothing_raised do
43
+ output = @ws.call(env)
44
+ end
45
+ #test_val = [403, {"Content-Type"=>"text/plain"}, "HTTPStatus::HTTP403Forbidden\nYou are forbidden to access that resource."]
46
+ assert_not_equal 403, output[0]
47
+ end
48
+
49
+ def test_default_resource
50
+ # This should be handled by bazu_controller
51
+ env = Rack::MockRequest.env_for('/', {
52
+ :method => 'GET'
53
+ })
54
+ output = ''
55
+ assert_nothing_raised do
56
+ output = @ws.call(env)
57
+ end
58
+ # no index method defined/allowed
59
+ assert_equal 405, output[0]
60
+
61
+ # This should be handled by bazu_controller
62
+ env = Rack::MockRequest.env_for('/123', {
63
+ :method => 'GET'
64
+ })
65
+ output = ''
66
+ assert_nothing_raised do
67
+ output = @ws.call(env)
68
+ end
69
+ assert_equal 200, output[0]
70
+ end
71
+ end
@@ -0,0 +1,4 @@
1
+ xml.data do
2
+ xml.foo data[:foo]
3
+ xml.baz data[:baz]
4
+ end