diff --git a/lib/webmachine/decision/flow.rb b/lib/webmachine/decision/flow.rb index 6354dd79..3388efb6 100644 --- a/lib/webmachine/decision/flow.rb +++ b/lib/webmachine/decision/flow.rb @@ -401,17 +401,21 @@ def n5 def n11 # Stage1 if resource.post_is_create? - case uri = resource.create_path - when nil + uri = resource.create_path + + result = accept_helper + return result if Fixnum === result + + uri ||= response.headers['Location'] + + unless uri raise InvalidResource, t('create_path_nil', :class => resource.class) - when URI, String - base_uri = resource.base_uri || request.base_uri - new_uri = URI.join(base_uri.to_s, uri) - request.disp_path = new_uri.path - response.headers['Location'] = new_uri.to_s - result = accept_helper - return result if Fixnum === result end + + base_uri = resource.base_uri || request.base_uri + new_uri = URI.join(base_uri.to_s, uri) + request.disp_path = new_uri.path + response.headers['Location'] = new_uri.to_s else case result = resource.process_post when true diff --git a/spec/webmachine/decision/flow_spec.rb b/spec/webmachine/decision/flow_spec.rb index 00b51060..b29da692 100644 --- a/spec/webmachine/decision/flow_spec.rb +++ b/spec/webmachine/decision/flow_spec.rb @@ -838,27 +838,80 @@ def accept_text context "when the method is POST" do let(:method){ "POST" } + [true, false].each do |e| context "and the resource #{ e ? 'exists' : "does not exist"}" do before { resource.exist = e } - it "should reply with 201 when post_is_create is true and create_path returns a URI" do - resource.new_loc = created = "/foo/bar/baz" - resource.create = true - subject.run - response.code.should == 201 - response.headers['Location'].should == created - end - it "should reply with 500 when post_is_create is true and create_path returns nil" do - resource.create = true - subject.run - response.code.should == 500 - response.error.should_not be_nil + + context "when post_is_create is true" do + before do + resource.create = true + end + + context "and create_path returns a URI" do + let(:created) { "/foo/bar/baz" } + + before do + resource.new_loc = created + end + + it "should reply with 201" do + subject.run + response.code.should == 201 + response.headers['Location'].should eq \ + URI.join(request.base_uri.to_s, created).to_s + end + + context "but the content type is invalid" do + before do + request.headers['content-type'] = 'invalid' + end + + it "should reply with 415" do + subject.run + response.code.should eq 415 + end + + it "should not set the create path location" do + subject.run + response.headers['Location'].should be_nil + end + end + end + + context "and create_path is nil" do + it "should reply with 500" do + subject.run + response.code.should == 500 + response.error.should_not be_nil + end + + context "but the handler sets a location" do + before do + resource.stub(:accept_text) do + response.headers['Location'] = "/foo/bar/baz" + true + end + end + + it "should reply with 201" do + subject.run + response.code.should == 201 + response.headers['Location'].should eq \ + URI.join(request.base_uri.to_s, "/foo/bar/baz").to_s + end + end + end end - it "should not reply with 201 when post_is_create is false" do - resource.create = false - subject.run - response.code.should_not == 201 + + context "when post_is_create is false" do + it "should not reply with 201" do + resource.create = false + subject.run + response.code.should_not == 201 + end end + end end end