4
require 'puppet/network/http/api/v1'
7
include Puppet::Network::HTTP::API::V1
10
describe Puppet::Network::HTTP::API::V1 do
12
@tester = V1RestApiTester.new
15
it "should be able to convert a URI into a request" do
16
@tester.should respond_to(:uri2indirection)
19
it "should be able to convert a request into a URI" do
20
@tester.should respond_to(:indirection2uri)
23
describe "when converting a URI into a request" do
25
@tester.stubs(:handler).returns "foo"
28
it "should require the http method, the URI, and the query parameters" do
29
# Not a terribly useful test, but an important statement for the spec
30
lambda { @tester.uri2indirection("/foo") }.should raise_error(ArgumentError)
33
it "should use the first field of the URI as the environment" do
34
@tester.uri2indirection("GET", "/env/foo/bar", {})[3][:environment].to_s.should == "env"
37
it "should fail if the environment is not alphanumeric" do
38
lambda { @tester.uri2indirection("GET", "/env ness/foo/bar", {}) }.should raise_error(ArgumentError)
41
it "should use the environment from the URI even if one is specified in the parameters" do
42
@tester.uri2indirection("GET", "/env/foo/bar", {:environment => "otherenv"})[3][:environment].to_s.should == "env"
45
it "should return the environment as a Puppet::Node::Environment" do
46
@tester.uri2indirection("GET", "/env/foo/bar", {})[3][:environment].should be_a Puppet::Node::Environment
49
it "should use the second field of the URI as the indirection name" do
50
@tester.uri2indirection("GET", "/env/foo/bar", {})[0].should == "foo"
53
it "should fail if the indirection name is not alphanumeric" do
54
lambda { @tester.uri2indirection("GET", "/env/foo ness/bar", {}) }.should raise_error(ArgumentError)
57
it "should use the remainder of the URI as the indirection key" do
58
@tester.uri2indirection("GET", "/env/foo/bar", {})[2].should == "bar"
61
it "should support the indirection key being a /-separated file path" do
62
@tester.uri2indirection("GET", "/env/foo/bee/baz/bomb", {})[2].should == "bee/baz/bomb"
65
it "should fail if no indirection key is specified" do
66
lambda { @tester.uri2indirection("GET", "/env/foo/", {}) }.should raise_error(ArgumentError)
67
lambda { @tester.uri2indirection("GET", "/env/foo", {}) }.should raise_error(ArgumentError)
70
it "should choose 'find' as the indirection method if the http method is a GET and the indirection name is singular" do
71
@tester.uri2indirection("GET", "/env/foo/bar", {})[1].should == :find
74
it "should choose 'find' as the indirection method if the http method is a POST and the indirection name is singular" do
75
@tester.uri2indirection("POST", "/env/foo/bar", {})[1].should == :find
78
it "should choose 'head' as the indirection method if the http method is a HEAD and the indirection name is singular" do
79
@tester.uri2indirection("HEAD", "/env/foo/bar", {})[1].should == :head
82
it "should choose 'search' as the indirection method if the http method is a GET and the indirection name is plural" do
83
@tester.uri2indirection("GET", "/env/foos/bar", {})[1].should == :search
86
it "should choose 'find' as the indirection method if the http method is a GET and the indirection name is facts" do
87
@tester.uri2indirection("GET", "/env/facts/bar", {})[1].should == :find
90
it "should choose 'save' as the indirection method if the http method is a PUT and the indirection name is facts" do
91
@tester.uri2indirection("PUT", "/env/facts/bar", {})[1].should == :save
94
it "should choose 'search' as the indirection method if the http method is a GET and the indirection name is inventory" do
95
@tester.uri2indirection("GET", "/env/inventory/search", {})[1].should == :search
98
it "should choose 'find' as the indirection method if the http method is a GET and the indirection name is facts" do
99
@tester.uri2indirection("GET", "/env/facts/bar", {})[1].should == :find
102
it "should choose 'save' as the indirection method if the http method is a PUT and the indirection name is facts" do
103
@tester.uri2indirection("PUT", "/env/facts/bar", {})[1].should == :save
106
it "should choose 'search' as the indirection method if the http method is a GET and the indirection name is inventory" do
107
@tester.uri2indirection("GET", "/env/inventory/search", {})[1].should == :search
110
it "should choose 'search' as the indirection method if the http method is a GET and the indirection name is facts_search" do
111
@tester.uri2indirection("GET", "/env/facts_search/bar", {})[1].should == :search
114
it "should change indirection name to 'facts' if the http method is a GET and the indirection name is facts_search" do
115
@tester.uri2indirection("GET", "/env/facts_search/bar", {})[0].should == 'facts'
118
it "should not change indirection name from 'facts' if the http method is a GET and the indirection name is facts" do
119
@tester.uri2indirection("GET", "/env/facts/bar", {})[0].should == 'facts'
122
it "should change indirection name to 'status' if the http method is a GET and the indirection name is statuses" do
123
@tester.uri2indirection("GET", "/env/statuses/bar", {})[0].should == 'status'
126
it "should change indirection name to 'probe' if the http method is a GET and the indirection name is probes" do
127
@tester.uri2indirection("GET", "/env/probes/bar", {})[0].should == 'probe'
130
it "should choose 'delete' as the indirection method if the http method is a DELETE and the indirection name is singular" do
131
@tester.uri2indirection("DELETE", "/env/foo/bar", {})[1].should == :destroy
134
it "should choose 'save' as the indirection method if the http method is a PUT and the indirection name is singular" do
135
@tester.uri2indirection("PUT", "/env/foo/bar", {})[1].should == :save
138
it "should fail if an indirection method cannot be picked" do
139
lambda { @tester.uri2indirection("UPDATE", "/env/foo/bar", {}) }.should raise_error(ArgumentError)
142
it "should URI unescape the indirection key" do
143
escaped = URI.escape("foo bar")
144
indirection_name, method, key, params = @tester.uri2indirection("GET", "/env/foo/#{escaped}", {})
145
key.should == "foo bar"
149
describe "when converting a request into a URI" do
151
@request = Puppet::Indirector::Request.new(:foo, :find, "with spaces", :foo => :bar, :environment => "myenv")
154
it "should use the environment as the first field of the URI" do
155
@tester.indirection2uri(@request).split("/")[1].should == "myenv"
158
it "should use the indirection as the second field of the URI" do
159
@tester.indirection2uri(@request).split("/")[2].should == "foo"
162
it "should pluralize the indirection name if the method is 'search'" do
163
@request.stubs(:method).returns :search
164
@tester.indirection2uri(@request).split("/")[2].should == "foos"
167
it "should use the escaped key as the remainder of the URI" do
168
escaped = URI.escape("with spaces")
169
@tester.indirection2uri(@request).split("/")[3].sub(/\?.+/, '').should == escaped
172
it "should add the query string to the URI" do
173
@request.expects(:query_string).returns "?query"
174
@tester.indirection2uri(@request).should =~ /\?query$/
178
describe "when converting a request into a URI with body" do
180
@request = Puppet::Indirector::Request.new(:foo, :find, "with spaces", :foo => :bar, :environment => "myenv")
183
it "should use the environment as the first field of the URI" do
184
@tester.request_to_uri_and_body(@request).first.split("/")[1].should == "myenv"
187
it "should use the indirection as the second field of the URI" do
188
@tester.request_to_uri_and_body(@request).first.split("/")[2].should == "foo"
191
it "should use the escaped key as the remainder of the URI" do
192
escaped = URI.escape("with spaces")
193
@tester.request_to_uri_and_body(@request).first.split("/")[3].sub(/\?.+/, '').should == escaped
196
it "should return the URI and body separately" do
197
@tester.request_to_uri_and_body(@request).should == ["/myenv/foo/with%20spaces", "foo=bar"]