honkster-screw-unit-server 0.6.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (114) hide show
  1. data/CHANGES +48 -0
  2. data/README.markdown +359 -0
  3. data/Rakefile +43 -0
  4. data/VERSION.yml +5 -0
  5. data/bin/screw_unit +6 -0
  6. data/bin/screw_unit_server +14 -0
  7. data/core/CHANGES +10 -0
  8. data/core/EXAMPLE.html +68 -0
  9. data/core/LICENSE +22 -0
  10. data/core/README.markdown +307 -0
  11. data/core/example/models/cat.js +5 -0
  12. data/core/example/models/man.js +17 -0
  13. data/core/example/spec/matchers/have.js +8 -0
  14. data/core/example/spec/models/cat_spec.js +31 -0
  15. data/core/example/spec/models/man_spec.js +34 -0
  16. data/core/example/spec/spec_helper.js +5 -0
  17. data/core/example/spec/suite.html +25 -0
  18. data/core/lib/jquery-1.3.2.js +4376 -0
  19. data/core/lib/jquery.fn.js +30 -0
  20. data/core/lib/jquery.print.js +109 -0
  21. data/core/lib/screw.behaviors.js +93 -0
  22. data/core/lib/screw.builder.js +95 -0
  23. data/core/lib/screw.css +90 -0
  24. data/core/lib/screw.events.js +45 -0
  25. data/core/lib/screw.matchers.js +244 -0
  26. data/core/spec/behaviors_spec.js +166 -0
  27. data/core/spec/matchers_spec.js +372 -0
  28. data/core/spec/print_spec.js +158 -0
  29. data/core/spec/spec_helper.js +0 -0
  30. data/core/spec/suite.html +19 -0
  31. data/core/spec/with_screw_context_spec.js +9 -0
  32. data/init.rb +0 -0
  33. data/lib/screw_unit.rb +21 -0
  34. data/spec/functional/functional_spec.rb +25 -0
  35. data/spec/functional/functional_spec_helper.rb +42 -0
  36. data/spec/functional/functional_spec_server_starter.rb +68 -0
  37. data/spec/functional_suite.rb +10 -0
  38. data/spec/spec_suite.rb +2 -0
  39. data/standalone.ru +3 -0
  40. data/vendor/js-test-core/CHANGES +31 -0
  41. data/vendor/js-test-core/README +6 -0
  42. data/vendor/js-test-core/Rakefile +73 -0
  43. data/vendor/js-test-core/bin/js-test-client +0 -0
  44. data/vendor/js-test-core/bin/js-test-server +6 -0
  45. data/vendor/js-test-core/lib/js_test_core.rb +38 -0
  46. data/vendor/js-test-core/lib/js_test_core/app.rb +11 -0
  47. data/vendor/js-test-core/lib/js_test_core/client.rb +139 -0
  48. data/vendor/js-test-core/lib/js_test_core/configuration.rb +65 -0
  49. data/vendor/js-test-core/lib/js_test_core/representations.rb +13 -0
  50. data/vendor/js-test-core/lib/js_test_core/representations/dir.html.rb +25 -0
  51. data/vendor/js-test-core/lib/js_test_core/representations/frameworks.rb +3 -0
  52. data/vendor/js-test-core/lib/js_test_core/representations/not_found.html.rb +16 -0
  53. data/vendor/js-test-core/lib/js_test_core/representations/page.html.rb +41 -0
  54. data/vendor/js-test-core/lib/js_test_core/representations/suite.html.rb +57 -0
  55. data/vendor/js-test-core/lib/js_test_core/representations/suites.rb +3 -0
  56. data/vendor/js-test-core/lib/js_test_core/representations/suites/screw_unit.html.rb +81 -0
  57. data/vendor/js-test-core/lib/js_test_core/resources.rb +15 -0
  58. data/vendor/js-test-core/lib/js_test_core/resources/core_file.rb +19 -0
  59. data/vendor/js-test-core/lib/js_test_core/resources/file.rb +62 -0
  60. data/vendor/js-test-core/lib/js_test_core/resources/implementations_deprecation.rb +12 -0
  61. data/vendor/js-test-core/lib/js_test_core/resources/not_found.rb +28 -0
  62. data/vendor/js-test-core/lib/js_test_core/resources/resource.rb +16 -0
  63. data/vendor/js-test-core/lib/js_test_core/resources/spec_file.rb +51 -0
  64. data/vendor/js-test-core/lib/js_test_core/resources/web_root.rb +15 -0
  65. data/vendor/js-test-core/lib/js_test_core/server.rb +84 -0
  66. data/vendor/js-test-core/public/js_test_server.js +541 -0
  67. data/vendor/js-test-core/spec/example_framework/JsTestCore.css +0 -0
  68. data/vendor/js-test-core/spec/example_framework/JsTestCore.js +0 -0
  69. data/vendor/js-test-core/spec/example_framework/subdir/SubDirFile.js +0 -0
  70. data/vendor/js-test-core/spec/example_root/favicon.ico +0 -0
  71. data/vendor/js-test-core/spec/example_root/javascripts/foo.js +3 -0
  72. data/vendor/js-test-core/spec/example_root/javascripts/large_file.js +59 -0
  73. data/vendor/js-test-core/spec/example_root/javascripts/subdir/bar.js +1 -0
  74. data/vendor/js-test-core/spec/example_root/robots.txt +0 -0
  75. data/vendor/js-test-core/spec/example_root/stylesheets/example.css +3 -0
  76. data/vendor/js-test-core/spec/example_spec/custom_dir_and_suite/passing_spec.js +8 -0
  77. data/vendor/js-test-core/spec/example_spec/custom_suite.html +8 -0
  78. data/vendor/js-test-core/spec/example_spec/failing_spec.js +7 -0
  79. data/vendor/js-test-core/spec/example_spec/foo/failing_spec.js +8 -0
  80. data/vendor/js-test-core/spec/example_spec/foo/passing_spec.js +8 -0
  81. data/vendor/js-test-core/spec/example_spec/passing_spec.js +8 -0
  82. data/vendor/js-test-core/spec/functional/functional_spec.rb +25 -0
  83. data/vendor/js-test-core/spec/functional/functional_spec_helper.rb +42 -0
  84. data/vendor/js-test-core/spec/functional/functional_spec_server_starter.rb +77 -0
  85. data/vendor/js-test-core/spec/functional_suite.rb +10 -0
  86. data/vendor/js-test-core/spec/spec_helpers/be_http.rb +32 -0
  87. data/vendor/js-test-core/spec/spec_helpers/example_group.rb +36 -0
  88. data/vendor/js-test-core/spec/spec_helpers/fake_selenium_driver.rb +16 -0
  89. data/vendor/js-test-core/spec/spec_helpers/show_test_exceptions.rb +22 -0
  90. data/vendor/js-test-core/spec/spec_suite.rb +3 -0
  91. data/vendor/js-test-core/spec/specs/failing_spec.js +0 -0
  92. data/vendor/js-test-core/spec/unit/js_test_core/client_spec.rb +196 -0
  93. data/vendor/js-test-core/spec/unit/js_test_core/configuration_spec.rb +50 -0
  94. data/vendor/js-test-core/spec/unit/js_test_core/resources/core_file_spec.rb +60 -0
  95. data/vendor/js-test-core/spec/unit/js_test_core/resources/file_spec.rb +81 -0
  96. data/vendor/js-test-core/spec/unit/js_test_core/resources/implementations_deprecation_spec.rb +18 -0
  97. data/vendor/js-test-core/spec/unit/js_test_core/resources/not_found_spec.rb +51 -0
  98. data/vendor/js-test-core/spec/unit/js_test_core/resources/spec_file_spec.rb +149 -0
  99. data/vendor/js-test-core/spec/unit/js_test_core/resources/web_root_spec.rb +28 -0
  100. data/vendor/js-test-core/spec/unit/js_test_core/server_spec.rb +66 -0
  101. data/vendor/js-test-core/spec/unit/unit_spec_helper.rb +30 -0
  102. data/vendor/js-test-core/spec/unit_suite.rb +10 -0
  103. data/vendor/js-test-core/standalone.ru +1 -0
  104. data/vendor/js-test-core/vendor/lucky-luciano/README.markdown +77 -0
  105. data/vendor/js-test-core/vendor/lucky-luciano/lib/lucky_luciano.rb +5 -0
  106. data/vendor/js-test-core/vendor/lucky-luciano/lib/lucky_luciano/resource.rb +142 -0
  107. data/vendor/js-test-core/vendor/lucky-luciano/lib/lucky_luciano/resource/path.rb +24 -0
  108. data/vendor/js-test-core/vendor/lucky-luciano/lib/lucky_luciano/rspec.rb +4 -0
  109. data/vendor/js-test-core/vendor/lucky-luciano/lib/lucky_luciano/rspec/be_http.rb +32 -0
  110. data/vendor/js-test-core/vendor/lucky-luciano/spec/lucky_luciano/resource_spec.rb +276 -0
  111. data/vendor/js-test-core/vendor/lucky-luciano/spec/spec_helper.rb +48 -0
  112. data/vendor/js-test-core/vendor/lucky-luciano/spec/spec_suite.rb +4 -0
  113. data/vendor/js-test-core/vendor/lucky-luciano/syntax_ideas.txt +34 -0
  114. metadata +201 -0
@@ -0,0 +1,30 @@
1
+ require "rubygems"
2
+ require "spec"
3
+ require "spec/autorun"
4
+ require "rack/test"
5
+ require "rr"
6
+ ARGV.push("-b")
7
+
8
+ dir = File.dirname(__FILE__)
9
+ LIBRARY_ROOT_DIR = File.expand_path("#{dir}/../..")
10
+ $LOAD_PATH.unshift File.expand_path("#{LIBRARY_ROOT_DIR}/lib")
11
+ require "js_test_core"
12
+ require "nokogiri"
13
+ require "guid"
14
+ require "thin"
15
+ require "#{LIBRARY_ROOT_DIR}/spec/spec_helpers/be_http"
16
+ require "#{LIBRARY_ROOT_DIR}/spec/spec_helpers/example_group"
17
+ require "#{LIBRARY_ROOT_DIR}/spec/spec_helpers/fake_selenium_driver"
18
+ require "#{LIBRARY_ROOT_DIR}/spec/spec_helpers/show_test_exceptions"
19
+
20
+ Spec::Runner.configure do |config|
21
+ config.mock_with RR::Adapters::Rspec
22
+ end
23
+
24
+ Sinatra::Application.use ShowTestExceptions
25
+ Sinatra::Application.set :raise_errors, true
26
+ Sinatra::Application.set :show_exceptions, false
27
+ JsTestCore::App.set :raise_errors, true
28
+ JsTestCore::App.set :show_exceptions, false
29
+
30
+ Sinatra::Application.use(JsTestCore::App)
@@ -0,0 +1,10 @@
1
+ class UnitSuite
2
+ def run
3
+ dir = File.dirname(__FILE__)
4
+ Dir["#{dir}/unit/**/*_spec.rb"].each do |file|
5
+ require file
6
+ end
7
+ end
8
+ end
9
+
10
+ UnitSuite.new.run
@@ -0,0 +1 @@
1
+ JsTestCore::Server.instance.standalone_rackup(self)
@@ -0,0 +1,77 @@
1
+ Lucky Luciano, is a library that makes it easy to organize your Sinatra routes into Restful Resources.
2
+
3
+ You can define Resources with a base route, and then map relative routes in the Resource.
4
+ The base route and the relative route are simply merged to create the full route.
5
+
6
+ module Resource
7
+ class Users < LuckyLuciano::Resource
8
+ map "/users"
9
+ include UserAuthentication
10
+
11
+ get "/" do
12
+ end
13
+
14
+ get "/:user_id" do
15
+ end
16
+
17
+ post "/" do
18
+ end
19
+
20
+ put "/:user_id" do
21
+ require_login
22
+ # ...
23
+ end
24
+
25
+ delete "/:user_id" do
26
+ require_login
27
+ # ...
28
+ end
29
+ end
30
+
31
+ class UserComments < LuckyLuciano::Resource
32
+ map "/users/:user_id/comments"
33
+ include UserAuthentication
34
+
35
+ get "/" do
36
+ end
37
+
38
+ get "/:comment_id" do
39
+ end
40
+
41
+ post "/" do
42
+ require_login
43
+ # ...
44
+ end
45
+ end
46
+ end
47
+
48
+
49
+ You can map the Resource to the Sinatra application by registering the route_handler.
50
+
51
+ class YourApp < Sinatra::Base
52
+ register(UsersResource.route_handler)
53
+ register(UserCommentsResource.route_handler)
54
+ end
55
+
56
+ Lucky Luciano also provides a named-route scheme, which is easy to search for in the code-base.
57
+ It is rather verbose and ideas are welcome to shorten it, while maintaining searchability and simplicity.
58
+
59
+ <a href="<%= Resource::Users.path("/:user_id", :user_id => @user.id, :additional_param => "param 1") %>">Bob</a>
60
+ a "Bob", :href => Resource::Users.path("/:user_id", :user_id => @user.id, :additional_param => "param 1")
61
+
62
+ <a href="<%= Resource::UserComments.path("/:comment_id", :user_id => @user.id, :comment_id => @comment.id, :additional_param => "param 1") %>">Bob's Comment</a>
63
+ a "Bob's Comment", :href => Resource::UserComments.path("/:comment_id", :user_id => @user.id, :comment_id => @comment.id, :additional_param => "param 1")
64
+
65
+ Compare with Rails named routes
66
+
67
+ <a href="<%= user_path(:user_id => @user, :additional_param => "param 1") %>">Bob</a>
68
+ a "Bob", :href => user_path(:user_id => @user, :additional_param => "param 1")
69
+
70
+ <a href="<%= user_comment_path(:user_id => @user.id, :comment_id => @comment.id, :additional_param => "param 1") %>">Bob's Comment</a>
71
+ a "Bob's Comment", :href => user_comment_path(@user, @comment)
72
+
73
+ ## Who was this Lucky Luciano person?
74
+
75
+ Lucky Luciano was a mob boss and an alleged friend of Frank Sinatra.
76
+
77
+ http://news.bbc.co.uk/2/hi/special_report/1998/05/98/sinatra/94360.stm
@@ -0,0 +1,5 @@
1
+ require "sinatra/base"
2
+
3
+ dir = File.dirname(__FILE__)
4
+ require "#{dir}/lucky_luciano/resource"
5
+ require "#{dir}/lucky_luciano/resource/path"
@@ -0,0 +1,142 @@
1
+ module LuckyLuciano
2
+ class Resource
3
+ class << self
4
+ attr_reader :base_path_definition
5
+
6
+ def map(base_path_definition)
7
+ @base_path_definition = base_path_definition
8
+ end
9
+
10
+ def recorded_http_handlers
11
+ @recorded_http_handlers ||= []
12
+ end
13
+
14
+ def route_handler
15
+ create_sinatra_handler
16
+ end
17
+
18
+ ["get", "put", "post", "delete"].each do |http_verb|
19
+ class_eval(<<-RUBY, __FILE__, __LINE__ + 1)
20
+ def #{http_verb}(relative_path, opts={}, &block)
21
+ recorded_http_handlers << [:#{http_verb}, relative_path, opts, block]
22
+ end
23
+ RUBY
24
+ end
25
+
26
+ def [](sub_path=nil)
27
+ Path.new(self, sub_path)
28
+ end
29
+
30
+ def path(*sub_paths)
31
+ params = sub_paths.last.is_a?(Hash) ? sub_paths.pop : {}
32
+
33
+ sub_path_definition = sub_paths.join("/")
34
+
35
+ full_path = normalize_seperators("#{base_path(params)}/#{path_from_definition(sub_path_definition, params)}").gsub(/\/$/, "")
36
+
37
+ base_path_param_keys = param_keys_from(base_path_definition)
38
+ query_params = params.delete_if do |key, value|
39
+ base_path_param_keys.include?(key)
40
+ end
41
+
42
+ sub_path_param_keys = param_keys_from(sub_path_definition)
43
+ query_params = params.delete_if do |key, value|
44
+ sub_path_param_keys.include?(key)
45
+ end
46
+
47
+ if query_params.empty?
48
+ full_path
49
+ else
50
+ query = build_query(query_params)
51
+ "#{full_path}?#{query}"
52
+ end
53
+ end
54
+
55
+ def base_path(params={})
56
+ path_from_definition(base_path_definition, params)
57
+ end
58
+
59
+ def normalize_seperators(url)
60
+ url.gsub(Regexp.new("//+"), '/')
61
+ end
62
+
63
+ protected
64
+
65
+ def path_from_definition(definition, params={})
66
+ param_keys = param_keys_from(definition)
67
+ if param_keys.empty?
68
+ definition.dup
69
+ else
70
+ param_keys.each do |base_path_param|
71
+ unless params.include?(base_path_param.to_sym)
72
+ raise ArgumentError, "Expected #{base_path_param.inspect} to have a value"
73
+ end
74
+ end
75
+ definition.split("/").map do |segment|
76
+ if param_key = segment_param_key(segment)
77
+ params[param_key]
78
+ else
79
+ segment
80
+ end
81
+ end.join("/")
82
+ end
83
+ end
84
+
85
+ def param_keys_from(definition)
86
+ definition.split("/").find_all do |segment|
87
+ segment_param_key(segment)
88
+ end.map do |param|
89
+ param[1..-1].to_sym
90
+ end
91
+ end
92
+
93
+ def segment_param_key(segment)
94
+ segment[0..0] == ':' ? segment[1..-1].to_sym : nil
95
+ end
96
+
97
+ def build_query(params)
98
+ params.to_a.inject([]) do |splatted_params, (key, value)|
99
+ [value].flatten.each do |value_in_param|
100
+ splatted_params << "#{URI.escape(key.to_s)}=#{URI.escape(value_in_param.to_s)}"
101
+ end
102
+ splatted_params
103
+ end.join("&")
104
+ end
105
+
106
+ def create_sinatra_handler
107
+ resource_class = self
108
+ Module.new do
109
+ (class << self; self; end).class_eval do
110
+ define_method(:registered) do |app|
111
+ resource_class.recorded_http_handlers.each do |handler|
112
+ verb, relative_path, opts, block = handler
113
+ full_path = resource_class.normalize_seperators(
114
+ "#{resource_class.base_path_definition}/#{relative_path}"
115
+ ).gsub(%r{^.+/$}) do |match|
116
+ match.gsub(%r{/$}, "")
117
+ end
118
+ app.send(verb, full_path, opts) do
119
+ resource_class.new(self).instance_eval(&block)
120
+ end
121
+ end
122
+ end
123
+ end
124
+ end
125
+ end
126
+ end
127
+
128
+ attr_reader :app
129
+
130
+ def initialize(app)
131
+ @app = app
132
+ end
133
+
134
+ def method_missing(method_name, *args, &block)
135
+ if app.respond_to?(method_name)
136
+ app.send(method_name, *args, &block)
137
+ else
138
+ super
139
+ end
140
+ end
141
+ end
142
+ end
@@ -0,0 +1,24 @@
1
+ class LuckyLuciano::Resource::Path
2
+ attr_reader :resource, :sub_paths
3
+ def initialize(resource, *sub_paths)
4
+ @resource = resource
5
+ @sub_paths = sub_paths
6
+ end
7
+
8
+ def =~(other)
9
+ to_s =~ other
10
+ end
11
+
12
+ def [](*additional_sub_paths)
13
+ sub_paths.push(*additional_sub_paths)
14
+ self
15
+ end
16
+
17
+ def to_s
18
+ resource.path(*sub_paths)
19
+ end
20
+
21
+ def ==(other)
22
+ to_s == other.to_s
23
+ end
24
+ end
@@ -0,0 +1,4 @@
1
+ dir = File.dirname(__FILE__)
2
+ Dir["#{dir}/rspec/**/*.rb"].each do |file|
3
+ require file
4
+ end
@@ -0,0 +1,32 @@
1
+ module BeHttp
2
+ include Spec::Matchers
3
+ def be_http(status, headers, body)
4
+ SimpleMatcher.new(nil) do |given, matcher|
5
+ description = (<<-DESC).gsub(/^ +/, "")
6
+ be an http of
7
+ expected status: #{status.inspect}
8
+ actual status : #{given.status.inspect}
9
+
10
+ expected headers containing: #{headers.inspect}
11
+ actual headers : #{given.headers.inspect}
12
+
13
+ expected body containing: #{body.inspect}
14
+ actual body : #{given.body.inspect}
15
+ DESC
16
+ matcher.failure_message = description
17
+ matcher.negative_failure_message = "not #{description}"
18
+
19
+ passed = true
20
+ unless given.status == status
21
+ passed = false
22
+ end
23
+ unless headers.all?{|k, v| given.headers[k] == headers[k]}
24
+ passed = false
25
+ end
26
+ unless body.is_a?(Regexp) ? given.body =~ body : given.body.include?(body)
27
+ passed = false
28
+ end
29
+ passed
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,276 @@
1
+ require File.expand_path("#{File.dirname(__FILE__)}/../spec_helper")
2
+
3
+ module LuckyLuciano
4
+ module ResourceSpec
5
+ class Root < Resource
6
+ map "/"
7
+ end
8
+
9
+ class ResourceFixture < Resource
10
+ map "/foobar"
11
+ end
12
+
13
+ class ResourceFixtureWithSubPaths < Resource
14
+ map "/foobar/"
15
+
16
+ get "/baz" do
17
+ "Response from /foobar/baz"
18
+ end
19
+
20
+ get "/users/:user_id" do
21
+ "User id is #{params[:user_id]}"
22
+ end
23
+ end
24
+
25
+ class User < Resource
26
+ map "/users/:user_id"
27
+
28
+ get "/friends" do
29
+ "User #{params['user_id']} friends"
30
+ end
31
+ end
32
+
33
+ describe Resource do
34
+ include ResourceSpec
35
+
36
+ before do
37
+ ResourceFixture.recorded_http_handlers.clear
38
+ end
39
+
40
+ macro("http verb") do |verb|
41
+ describe ".#{verb}" do
42
+ context "" do
43
+ before do
44
+ ResourceFixture.send(verb, "/") do
45
+ "He sleeps with the fishes"
46
+ end
47
+ end
48
+
49
+ it "creates a route to #{verb.upcase} the given path that executes the given block" do
50
+ app.register(ResourceFixture.route_handler)
51
+ response = send(verb, "/foobar")
52
+ response.should be_http( 200, {}, "He sleeps with the fishes" )
53
+ end
54
+ end
55
+
56
+ context "when the base path is blank and the relative path is blank" do
57
+ before do
58
+ Root.send(verb, "") do
59
+ "Response from /"
60
+ end
61
+ end
62
+
63
+ it "creates a route to /" do
64
+ app.register(Root.route_handler)
65
+ response = send(verb, "/")
66
+ response.should be_http( 200, {}, "Response from /" )
67
+ end
68
+ end
69
+
70
+ context "when the relative path does not have a leading slash" do
71
+ before do
72
+ ResourceFixtureWithSubPaths.send(verb, "no_leading_slash") do
73
+ "Response from /foobar/no_leading_slash"
74
+ end
75
+ end
76
+
77
+ it "creates a route to #{verb.upcase} the given path that executes the given block" do
78
+ app.register(ResourceFixtureWithSubPaths.route_handler)
79
+ response = send(verb, "/foobar/no_leading_slash")
80
+ response.should be_http( 200, {}, "Response from /foobar/no_leading_slash" )
81
+ end
82
+ end
83
+
84
+
85
+ it "does not respond to another type of http request" do
86
+ ResourceFixture.send(verb, "/") do
87
+ ""
88
+ end
89
+ app.register(ResourceFixture.route_handler)
90
+ get("/foobar").status.should == 404 unless verb == "get"
91
+ put("/foobar").status.should == 404 unless verb == "put"
92
+ post("/foobar").status.should == 404 unless verb == "post"
93
+ delete("/foobar").status.should == 404 unless verb == "delete"
94
+ end
95
+
96
+ describe "Instance behavior" do
97
+ attr_reader :evaluation_target
98
+ before do
99
+ evaluation_target = nil
100
+ ResourceFixture.send(verb, "/") do
101
+ evaluation_target = self
102
+ ""
103
+ end
104
+ app.register(ResourceFixture.route_handler)
105
+
106
+ send(verb, "/foobar")
107
+ @evaluation_target = evaluation_target
108
+ end
109
+
110
+ it "evaluates the block in as a Resource" do
111
+ evaluation_target.class.should == ResourceFixture
112
+ end
113
+
114
+ it "sets app to be the Sinatra context" do
115
+ evaluation_target.app.class.should == Sinatra::Application
116
+ end
117
+
118
+ it "delegates methods to the #app" do
119
+ return_value = nil
120
+ mock.proxy(evaluation_target.app).params {|val| return_value = val}
121
+ evaluation_target.params.should == return_value
122
+ end
123
+
124
+ context "when the #app does not respond to the method" do
125
+ it "raises a NoMethodError from the Resource's perspective" do
126
+ lambda do
127
+ evaluation_target.i_dont_exist
128
+ end.should raise_error(NoMethodError, /ResourceFixture/)
129
+ end
130
+ end
131
+ end
132
+
133
+ end
134
+ end
135
+
136
+ send("http verb", "get")
137
+ send("http verb", "put")
138
+ send("http verb", "post")
139
+ send("http verb", "delete")
140
+
141
+ describe ".[][]" do
142
+ context "when passed nothing" do
143
+ it "returns the base_path" do
144
+ ResourceFixture[].should == "/foobar"
145
+ ResourceFixture[][].should == "/foobar"
146
+ end
147
+ end
148
+
149
+ context "when passed a sub path" do
150
+ it "merges the base_path into the sub path, regardless of a / in front" do
151
+ ResourceFixtureWithSubPaths["/baz"].to_s.should == "/foobar/baz"
152
+ ResourceFixtureWithSubPaths["baz"].to_s.should == "/foobar/baz"
153
+ end
154
+
155
+ context "when passed a multiple sub paths" do
156
+ it "joins the sub paths with '/'" do
157
+ ResourceFixtureWithSubPaths["users"][99].should == "/foobar/users/99"
158
+ end
159
+ end
160
+
161
+ context "when passed a hash as the last argument" do
162
+ context "when using a single path argument" do
163
+ it "creates url params from the hash" do
164
+ path = ResourceFixtureWithSubPaths["/users/:user_id"][:user_id => 99, :single_value_param => "single_value_param_value", 'multiple_value_param[]' => [1,2,3]]
165
+ path.should be_url("/foobar/users/99", [
166
+ "multiple_value_param[]=1",
167
+ "multiple_value_param[]=2",
168
+ "multiple_value_param[]=3",
169
+ "single_value_param=single_value_param_value",
170
+ ])
171
+ end
172
+ end
173
+
174
+ context "when using multiple path arguments" do
175
+ it "creates url params from the hash" do
176
+ path = ResourceFixtureWithSubPaths["users"][99][:single_value_param => "single_value_param_value", 'multiple_value_param[]' => [1,2,3]]
177
+ path.should be_url("/foobar/users/99", [
178
+ "multiple_value_param[]=1",
179
+ "multiple_value_param[]=2",
180
+ "multiple_value_param[]=3",
181
+ "single_value_param=single_value_param_value",
182
+ ])
183
+ end
184
+ end
185
+
186
+ def be_url(expected_path, expected_query_parts)
187
+ Spec::Matchers::SimpleMatcher.new("match url") do |actual|
188
+ uri = URI.parse(actual.to_s)
189
+ uri.path.should == expected_path
190
+ query_parts = uri.query.split("&")
191
+ query_parts.should =~ expected_query_parts
192
+ end
193
+ end
194
+ end
195
+ end
196
+ end
197
+
198
+ describe ".path" do
199
+ context "when passed nothing" do
200
+ it "returns the base_path" do
201
+ ResourceFixture.path.should == "/foobar"
202
+ end
203
+ end
204
+
205
+ context "when passed a sub path" do
206
+ it "merges the base_path into the sub path, regardless of a / in front" do
207
+ ResourceFixtureWithSubPaths.path("/baz").should == "/foobar/baz"
208
+ ResourceFixtureWithSubPaths.path("baz").should == "/foobar/baz"
209
+ end
210
+
211
+ context "when passed a multiple sub paths" do
212
+ it "joins the sub paths with '/'" do
213
+ ResourceFixtureWithSubPaths.path("users", 99).should == "/foobar/users/99"
214
+ end
215
+ end
216
+
217
+ context "when passed a hash as the last argument" do
218
+ context "when using a single path argument" do
219
+ it "creates url params from the hash" do
220
+ path = ResourceFixtureWithSubPaths.path(
221
+ "/users/:user_id", {:user_id => 99, :single_value_param => "single_value_param_value", 'multiple_value_param[]' => [1,2,3]}
222
+ )
223
+ uri = URI.parse(path)
224
+ uri.path.should == "/foobar/users/99"
225
+ query_parts = uri.query.split("&")
226
+ query_parts.should =~ [
227
+ "multiple_value_param[]=1",
228
+ "multiple_value_param[]=2",
229
+ "multiple_value_param[]=3",
230
+ "single_value_param=single_value_param_value",
231
+ ]
232
+ end
233
+ end
234
+
235
+ context "when using multiple path arguments" do
236
+ it "creates url params from the hash" do
237
+ path = ResourceFixtureWithSubPaths.path(
238
+ "users", 99, {:single_value_param => "single_value_param_value", 'multiple_value_param[]' => [1,2,3]}
239
+ )
240
+ uri = URI.parse(path)
241
+ uri.path.should == "/foobar/users/99"
242
+ query_parts = uri.query.split("&")
243
+ query_parts.should =~ [
244
+ "multiple_value_param[]=1",
245
+ "multiple_value_param[]=2",
246
+ "multiple_value_param[]=3",
247
+ "single_value_param=single_value_param_value",
248
+ ]
249
+ end
250
+ end
251
+ end
252
+ end
253
+
254
+ context "when the base_path contains a parameter" do
255
+ context "when not passed" do
256
+ it "raises an ArgumentError" do
257
+ lambda do
258
+ User.path
259
+ end.should raise_error(
260
+ ArgumentError,
261
+ %r{Expected :user_id to have a value}
262
+ )
263
+ end
264
+ end
265
+
266
+ context "when first argument is a hash" do
267
+ it "returns the full path with the base path param value" do
268
+ User.path("friends", :user_id => 99).should == "/users/99/friends"
269
+ end
270
+ end
271
+ end
272
+ end
273
+ end
274
+ end
275
+
276
+ end