btakita-screw-unit-server 0.3.0 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (141) hide show
  1. data/CHANGES +44 -1
  2. data/README.markdown +17 -5
  3. data/Rakefile +28 -55
  4. data/VERSION.yml +5 -0
  5. data/bin/screw_unit_server +8 -4
  6. data/core/CHANGES +10 -0
  7. data/core/EXAMPLE.html +4 -4
  8. data/core/README.markdown +7 -7
  9. data/core/example/models/man.js +2 -2
  10. data/core/example/spec/suite.html +1 -1
  11. data/core/lib/jquery-1.3.2.js +4376 -0
  12. data/core/lib/jquery.fn.js +2 -1
  13. data/core/lib/jquery.print.js +13 -12
  14. data/core/lib/screw.behaviors.js +11 -9
  15. data/core/lib/screw.builder.js +26 -11
  16. data/core/lib/screw.css +2 -2
  17. data/core/lib/screw.events.js +11 -4
  18. data/core/lib/screw.matchers.js +192 -13
  19. data/core/spec/behaviors_spec.js +11 -1
  20. data/core/spec/matchers_spec.js +288 -9
  21. data/core/spec/print_spec.js +47 -8
  22. data/core/spec/suite.html +2 -1
  23. data/core/spec/with_screw_context_spec.js +9 -0
  24. data/lib/screw_unit.rb +12 -17
  25. data/spec/functional/functional_spec.rb +15 -4
  26. data/spec/functional/functional_spec_helper.rb +22 -35
  27. data/spec/functional/functional_spec_server_starter.rb +68 -0
  28. data/spec/spec_suite.rb +0 -1
  29. data/standalone.ru +3 -0
  30. data/vendor/js-test-core/CHANGES +22 -0
  31. data/vendor/js-test-core/README +4 -10
  32. data/vendor/js-test-core/Rakefile +2 -1
  33. data/vendor/js-test-core/{spec/example_core/JsTestCore.css → bin/js-test-client} +0 -0
  34. data/vendor/js-test-core/bin/js-test-server +6 -0
  35. data/vendor/js-test-core/lib/js_test_core.rb +27 -19
  36. data/vendor/js-test-core/lib/js_test_core/app.rb +11 -0
  37. data/vendor/js-test-core/lib/js_test_core/client.rb +125 -36
  38. data/vendor/js-test-core/lib/js_test_core/configuration.rb +65 -0
  39. data/vendor/js-test-core/lib/js_test_core/representations.rb +13 -0
  40. data/vendor/js-test-core/lib/js_test_core/representations/dir.html.rb +25 -0
  41. data/vendor/js-test-core/lib/js_test_core/representations/frameworks.rb +3 -0
  42. data/vendor/js-test-core/lib/js_test_core/representations/not_found.html.rb +16 -0
  43. data/vendor/js-test-core/lib/js_test_core/representations/page.html.rb +41 -0
  44. data/vendor/js-test-core/lib/js_test_core/representations/suite.html.rb +57 -0
  45. data/vendor/js-test-core/lib/js_test_core/representations/suites.rb +3 -0
  46. data/vendor/js-test-core/lib/js_test_core/representations/suites/screw_unit.html.rb +81 -0
  47. data/vendor/js-test-core/lib/js_test_core/resources.rb +12 -8
  48. data/vendor/js-test-core/lib/js_test_core/resources/core_file.rb +19 -0
  49. data/vendor/js-test-core/lib/js_test_core/resources/file.rb +43 -13
  50. data/vendor/js-test-core/lib/js_test_core/resources/implementations_deprecation.rb +12 -0
  51. data/vendor/js-test-core/lib/js_test_core/resources/not_found.rb +28 -0
  52. data/vendor/js-test-core/lib/js_test_core/resources/resource.rb +16 -0
  53. data/vendor/js-test-core/lib/js_test_core/resources/spec_file.rb +51 -0
  54. data/vendor/js-test-core/lib/js_test_core/resources/web_root.rb +7 -54
  55. data/vendor/js-test-core/lib/js_test_core/server.rb +65 -92
  56. data/vendor/js-test-core/public/js_test_server.js +541 -0
  57. data/vendor/js-test-core/spec/{example_core/JsTestCore.js → example_framework/JsTestCore.css} +0 -0
  58. data/vendor/js-test-core/spec/{example_public/favicon.ico → example_framework/JsTestCore.js} +0 -0
  59. data/vendor/js-test-core/spec/{example_public/robots.txt → example_framework/subdir/SubDirFile.js} +0 -0
  60. data/vendor/js-test-core/spec/example_root/favicon.ico +0 -0
  61. data/vendor/js-test-core/spec/{example_public → example_root}/javascripts/foo.js +1 -1
  62. data/vendor/js-test-core/spec/example_root/javascripts/large_file.js +59 -0
  63. data/vendor/js-test-core/spec/example_root/javascripts/subdir/bar.js +1 -0
  64. data/vendor/js-test-core/spec/example_root/robots.txt +0 -0
  65. data/vendor/js-test-core/spec/{example_public → example_root}/stylesheets/example.css +0 -0
  66. data/vendor/js-test-core/spec/example_spec/custom_dir_and_suite/passing_spec.js +8 -0
  67. data/vendor/js-test-core/spec/example_spec/custom_suite.html +8 -0
  68. data/vendor/js-test-core/spec/example_spec/failing_spec.js +7 -0
  69. data/vendor/js-test-core/spec/example_spec/foo/failing_spec.js +8 -0
  70. data/vendor/js-test-core/spec/example_spec/foo/passing_spec.js +8 -0
  71. data/vendor/js-test-core/spec/example_spec/passing_spec.js +8 -0
  72. data/vendor/js-test-core/spec/functional/functional_spec.rb +25 -0
  73. data/vendor/js-test-core/spec/functional/functional_spec_helper.rb +42 -0
  74. data/vendor/js-test-core/spec/functional/functional_spec_server_starter.rb +77 -0
  75. data/vendor/js-test-core/spec/functional_suite.rb +10 -0
  76. data/vendor/js-test-core/spec/spec_helpers/be_http.rb +32 -0
  77. data/vendor/js-test-core/spec/spec_helpers/example_group.rb +36 -0
  78. data/vendor/js-test-core/spec/spec_helpers/fake_selenium_driver.rb +16 -0
  79. data/vendor/js-test-core/spec/spec_helpers/show_test_exceptions.rb +22 -0
  80. data/vendor/js-test-core/spec/spec_suite.rb +2 -1
  81. data/vendor/js-test-core/spec/unit/js_test_core/client_spec.rb +153 -94
  82. data/vendor/js-test-core/spec/unit/js_test_core/configuration_spec.rb +50 -0
  83. data/vendor/js-test-core/spec/unit/js_test_core/resources/core_file_spec.rb +60 -0
  84. data/vendor/js-test-core/spec/unit/js_test_core/resources/file_spec.rb +61 -68
  85. data/vendor/js-test-core/spec/unit/js_test_core/resources/implementations_deprecation_spec.rb +18 -0
  86. data/vendor/js-test-core/spec/unit/js_test_core/resources/not_found_spec.rb +51 -0
  87. data/vendor/js-test-core/spec/unit/js_test_core/resources/spec_file_spec.rb +149 -0
  88. data/vendor/js-test-core/spec/unit/js_test_core/resources/web_root_spec.rb +14 -89
  89. data/vendor/js-test-core/spec/unit/js_test_core/server_spec.rb +41 -106
  90. data/vendor/js-test-core/spec/unit/unit_spec_helper.rb +19 -114
  91. data/vendor/js-test-core/standalone.ru +1 -0
  92. data/vendor/js-test-core/vendor/lucky-luciano/README.markdown +77 -0
  93. data/vendor/js-test-core/vendor/lucky-luciano/lib/lucky_luciano.rb +5 -0
  94. data/vendor/js-test-core/vendor/lucky-luciano/lib/lucky_luciano/resource.rb +142 -0
  95. data/vendor/js-test-core/vendor/lucky-luciano/lib/lucky_luciano/resource/path.rb +24 -0
  96. data/vendor/js-test-core/vendor/lucky-luciano/lib/lucky_luciano/rspec.rb +4 -0
  97. data/vendor/js-test-core/vendor/lucky-luciano/lib/lucky_luciano/rspec/be_http.rb +32 -0
  98. data/vendor/js-test-core/vendor/lucky-luciano/spec/lucky_luciano/resource_spec.rb +276 -0
  99. data/vendor/js-test-core/vendor/lucky-luciano/spec/spec_helper.rb +48 -0
  100. data/vendor/js-test-core/vendor/lucky-luciano/spec/spec_suite.rb +4 -0
  101. data/vendor/js-test-core/vendor/lucky-luciano/syntax_ideas.txt +34 -0
  102. metadata +145 -130
  103. data/core/TODO +0 -2
  104. data/core/lib/jquery-1.2.3.js +0 -3408
  105. data/core/lib/screw.assets.js +0 -36
  106. data/core/lib/screw.server.js +0 -21
  107. data/lib/screw_unit/resources.rb +0 -2
  108. data/lib/screw_unit/resources/spec.rb +0 -37
  109. data/spec/unit/js_test_core/specs/spec_dir_spec.rb +0 -43
  110. data/spec/unit/js_test_core/specs/spec_file_spec.rb +0 -43
  111. data/spec/unit/unit_spec_helper.rb +0 -120
  112. data/spec/unit_suite.rb +0 -10
  113. data/vendor/js-test-core/lib/js_test_core/rack.rb +0 -2
  114. data/vendor/js-test-core/lib/js_test_core/rack/commonlogger.rb +0 -5
  115. data/vendor/js-test-core/lib/js_test_core/rails_server.rb +0 -22
  116. data/vendor/js-test-core/lib/js_test_core/resources/dir.rb +0 -52
  117. data/vendor/js-test-core/lib/js_test_core/resources/file_not_found.rb +0 -15
  118. data/vendor/js-test-core/lib/js_test_core/resources/runners.rb +0 -15
  119. data/vendor/js-test-core/lib/js_test_core/resources/runners/firefox_runner.rb +0 -75
  120. data/vendor/js-test-core/lib/js_test_core/resources/specs/spec_dir.rb +0 -50
  121. data/vendor/js-test-core/lib/js_test_core/resources/specs/spec_file.rb +0 -17
  122. data/vendor/js-test-core/lib/js_test_core/resources/suite.rb +0 -24
  123. data/vendor/js-test-core/lib/js_test_core/resources/suite_finish.rb +0 -19
  124. data/vendor/js-test-core/lib/js_test_core/selenium.rb +0 -2
  125. data/vendor/js-test-core/lib/js_test_core/selenium/selenium_driver.rb +0 -5
  126. data/vendor/js-test-core/lib/js_test_core/thin.rb +0 -3
  127. data/vendor/js-test-core/lib/js_test_core/thin/backends/js_test_core_server.rb +0 -9
  128. data/vendor/js-test-core/lib/js_test_core/thin/js_test_core_connection.rb +0 -42
  129. data/vendor/js-test-core/spec/example_specs/failing_spec.js +0 -5
  130. data/vendor/js-test-core/spec/example_specs/foo/failing_spec.js +0 -6
  131. data/vendor/js-test-core/spec/example_specs/foo/passing_spec.js +0 -6
  132. data/vendor/js-test-core/spec/unit/js_test_core/rails_server_spec.rb +0 -45
  133. data/vendor/js-test-core/spec/unit/js_test_core/resources/dir_spec.rb +0 -42
  134. data/vendor/js-test-core/spec/unit/js_test_core/resources/file_not_found_spec.rb +0 -26
  135. data/vendor/js-test-core/spec/unit/js_test_core/resources/runner_spec.rb +0 -24
  136. data/vendor/js-test-core/spec/unit/js_test_core/resources/runners/firefox_runner_spec.rb +0 -197
  137. data/vendor/js-test-core/spec/unit/js_test_core/resources/specs/spec_dir_spec.rb +0 -79
  138. data/vendor/js-test-core/spec/unit/js_test_core/resources/specs/spec_file_spec.rb +0 -42
  139. data/vendor/js-test-core/spec/unit/js_test_core/resources/suite_finish_spec.rb +0 -94
  140. data/vendor/js-test-core/spec/unit/js_test_core/resources/suite_spec.rb +0 -44
  141. data/vendor/js-test-core/spec/unit/thin/js_test_core_connection_spec.rb +0 -92
@@ -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