summaryrefslogtreecommitdiffstats
path: root/spec
diff options
context:
space:
mode:
authorNick Lewis <nick@puppetlabs.com>2011-04-12 17:08:36 -0700
committerNick Lewis <nick@puppetlabs.com>2011-04-12 17:08:36 -0700
commit46721411066926aff3a7d5bb6470d3b8aec1b47d (patch)
tree5f844a68b7fe541b3699762b635cf1d8c40312f5 /spec
parent8778307ca33a637fe10b601ee737628f2e5f9fbf (diff)
downloadpuppet-46721411066926aff3a7d5bb6470d3b8aec1b47d.tar.gz
puppet-46721411066926aff3a7d5bb6470d3b8aec1b47d.tar.xz
puppet-46721411066926aff3a7d5bb6470d3b8aec1b47d.zip
(#6117) Add POST support to indirector requests
POST with a singular indirection is turned into a find in the indirector. When making a large find request from a REST terminus, POST is used, and for small requests, GET is used for backward compatibility. Paired-With: Jesse Wolfe
Diffstat (limited to 'spec')
-rwxr-xr-xspec/unit/indirector/rest_spec.rb33
-rwxr-xr-xspec/unit/network/http/api/v1_spec.rb26
2 files changed, 51 insertions, 8 deletions
diff --git a/spec/unit/indirector/rest_spec.rb b/spec/unit/indirector/rest_spec.rb
index d9c3068e8..29d00f1eb 100755
--- a/spec/unit/indirector/rest_spec.rb
+++ b/spec/unit/indirector/rest_spec.rb
@@ -224,13 +224,32 @@ describe Puppet::Indirector::REST do
@searcher.stubs(:network).returns(@connection) # neuter the network connection
# Use a key with spaces, so we can test escaping
- @request = Puppet::Indirector::Request.new(:foo, :find, "foo bar")
+ @request = Puppet::Indirector::Request.new(:foo, :find, "foo bar", :environment => "myenv")
end
- it "should call the GET http method on a network connection" do
- @searcher.expects(:network).returns @connection
- @connection.expects(:get).returns @response
- @searcher.find(@request)
+ describe "with a large body" do
+ it "should use the POST http method" do
+ params = {}
+ 'aa'.upto('zz') do |s|
+ params[s] = 'foo'
+ end
+
+ @request = Puppet::Indirector::Request.new(:foo, :find, "foo bar", params.merge(:environment => "myenv"))
+
+ @connection.expects(:post).with do |uri, body|
+ uri == "/myenv/foo/foo%20bar" and body.split("&").sort == params.map {|key,value| "#{key}=#{value}"}.sort
+ end.returns(@response)
+
+ @searcher.find(@request)
+ end
+ end
+
+ describe "with a small body" do
+ it "should use the GET http method" do
+ @searcher.expects(:network).returns @connection
+ @connection.expects(:get).returns @response
+ @searcher.find(@request)
+ end
end
it "should deserialize and return the http response, setting name" do
@@ -252,10 +271,8 @@ describe Puppet::Indirector::REST do
@searcher.find(@request).should == instance
end
-
it "should use the URI generated by the Handler module" do
- @searcher.expects(:indirection2uri).with(@request).returns "/my/uri"
- @connection.expects(:get).with { |path, args| path == "/my/uri" }.returns(@response)
+ @connection.expects(:get).with { |path, args| path == "/myenv/foo/foo%20bar?" }.returns(@response)
@searcher.find(@request)
end
diff --git a/spec/unit/network/http/api/v1_spec.rb b/spec/unit/network/http/api/v1_spec.rb
index 3da8cbfae..d16ff122a 100755
--- a/spec/unit/network/http/api/v1_spec.rb
+++ b/spec/unit/network/http/api/v1_spec.rb
@@ -68,6 +68,10 @@ describe Puppet::Network::HTTP::API::V1 do
@tester.uri2indirection("GET", "/env/foo/bar", {})[1].should == :find
end
+ it "should choose 'find' as the indirection method if the http method is a POST and the indirection name is singular" do
+ @tester.uri2indirection("POST", "/env/foo/bar", {})[1].should == :find
+ end
+
it "should choose 'head' as the indirection method if the http method is a HEAD and the indirection name is singular" do
@tester.uri2indirection("HEAD", "/env/foo/bar", {})[1].should == :head
end
@@ -164,4 +168,26 @@ describe Puppet::Network::HTTP::API::V1 do
end
end
+ describe "when converting a request into a URI with body" do
+ before :each do
+ @request = Puppet::Indirector::Request.new(:foo, :find, "with spaces", :foo => :bar, :environment => "myenv")
+ end
+
+ it "should use the environment as the first field of the URI" do
+ @tester.request_to_uri_and_body(@request).first.split("/")[1].should == "myenv"
+ end
+
+ it "should use the indirection as the second field of the URI" do
+ @tester.request_to_uri_and_body(@request).first.split("/")[2].should == "foo"
+ end
+
+ it "should use the escaped key as the remainder of the URI" do
+ escaped = URI.escape("with spaces")
+ @tester.request_to_uri_and_body(@request).first.split("/")[3].sub(/\?.+/, '').should == escaped
+ end
+
+ it "should return the URI and body separately" do
+ @tester.request_to_uri_and_body(@request).should == ["/myenv/foo/with%20spaces", "foo=bar"]
+ end
+ end
end