diff --git a/lib/webmachine/decision/flow.rb b/lib/webmachine/decision/flow.rb index 6354dd79..cfd3b9b3 100644 --- a/lib/webmachine/decision/flow.rb +++ b/lib/webmachine/decision/flow.rb @@ -131,7 +131,7 @@ def b4 # OPTIONS? def b3 - if request.method == "OPTIONS" + if request.options? response.headers.merge!(resource.options) 200 else @@ -282,7 +282,7 @@ def i4 # PUT? def i7 - request.method == "PUT" ? :i4 : :k7 + request.put? ? :i4 : :k7 end # If-none-match exists? @@ -297,7 +297,7 @@ def i13 # GET or HEAD? def j18 - %w{GET HEAD}.include?(request.method) ? 304 : 412 + (request.get? || request.head?) ? 304 : 412 end # Moved permanently? @@ -339,7 +339,7 @@ def l5 # POST? def l7 - request.method == "POST" ? :m7 : 404 + request.post? ? :m7 : 404 end # If-Modified-Since exists? @@ -369,7 +369,7 @@ def l17 # POST? def m5 - request.method == "POST" ? :n5 : 410 + request.post? ? :n5 : 410 end # Server allows POST to missing resource? @@ -379,7 +379,7 @@ def m7 # DELETE? def m16 - request.method == "DELETE" ? :m20 : :n16 + request.delete? ? :m20 : :n16 end # DELETE enacted immediately? (Also where DELETE is forced.) @@ -435,7 +435,7 @@ def n11 # POST? def n16 - request.method == "POST" ? :n11 : :o16 + request.post? ? :n11 : :o16 end # Conflict? @@ -450,13 +450,13 @@ def o14 # PUT? def o16 - request.method == "PUT" ? :o14 : :o18 + request.put? ? :o14 : :o18 end # Multiple representations? # Also where body generation for GET and HEAD is done. def o18 - if request.method =~ /^(GET|HEAD)$/ + if request.get? || request.head? add_caching_headers content_type = metadata['Content-Type'] handler = resource.content_types_provided.find {|ct, _| content_type.type_matches?(MediaType.parse(ct)) }.last diff --git a/lib/webmachine/request.rb b/lib/webmachine/request.rb index 0c96e1fd..dd3b39fe 100644 --- a/lib/webmachine/request.rb +++ b/lib/webmachine/request.rb @@ -9,6 +9,8 @@ class Request attr_reader :method, :uri, :headers, :body attr_accessor :disp_path, :path_info, :path_tokens + STANDARD_HTTP_METHODS = %w[GET HEAD POST PUT DELETE TRACE CONNECT OPTIONS] + # @param [String] method the HTTP request method # @param [URI] uri the requested URI, including host, scheme and # port @@ -62,5 +64,78 @@ def query end @query end + + # Is this an HTTPS request? + # + # @return [Boolean] + # true if this request was made via HTTPS + def https? + uri.scheme == "https" + end + + # Is this a GET request? + # + # @return [Boolean] + # true if this request was made with the GET method + def get? + method == "GET" + end + + # Is this a HEAD request? + # + # @return [Boolean] + # true if this request was made with the HEAD method + def head? + method == "HEAD" + end + + # Is this a POST request? + # + # @return [Boolean] + # true if this request was made with the GET method + def post? + method == "POST" + end + + # Is this a PUT request? + # + # @return [Boolean] + # true if this request was made with the PUT method + def put? + method == "PUT" + end + + # Is this a DELETE request? + # + # @return [Boolean] + # true if this request was made with the DELETE method + def delete? + method == "DELETE" + end + + # Is this a TRACE request? + # + # @return [Boolean] + # true if this request was made with the TRACE method + def trace? + method == "TRACE" + end + + # Is this a CONNECT request? + # + # @return [Boolean] + # true if this request was made with the CONNECT method + def connect? + method == "CONNECT" + end + + # Is this an OPTIONS request? + # + # @return [Boolean] + # true if this request was made with the OPTIONS method + def options? + method == "OPTIONS" + end + end # class Request end # module Webmachine diff --git a/spec/webmachine/request_spec.rb b/spec/webmachine/request_spec.rb index b59d24e6..67a1a496 100644 --- a/spec/webmachine/request_spec.rb +++ b/spec/webmachine/request_spec.rb @@ -1,7 +1,14 @@ require 'spec_helper' describe Webmachine::Request do - subject { Webmachine::Request.new("GET", URI.parse("http://localhost:8080/some/resource"), Webmachine::Headers.new, "") } + subject { request } + + let(:uri) { URI.parse("http://localhost:8080/some/resource") } + let(:http_method) { "GET" } + let(:headers) { Webmachine::Headers.new } + let(:body) { "" } + let(:request) { Webmachine::Request.new(http_method, uri, headers, body) } + it "should provide access to the headers via brackets" do subject.headers['Accept'] = "*/*" subject["accept"].should == "*/*" @@ -31,4 +38,149 @@ subject.uri.query = nil subject.query.should == {} end + + describe '#https?' do + subject { request.https? } + + context "when the request was issued via HTTPS" do + let(:uri) { URI.parse("https://localhost.com:8080/some/resource") } + + it { should be_true } + end + + context "when the request was not issued via HTTPS" do + let(:uri) { URI.parse("http://localhost.com:8080/some/resource") } + + it { should be_false } + end + end + + describe '#get?' do + subject { request.get? } + + context "when the request method is GET" do + let(:http_method) { "GET" } + + it { should be_true } + end + + context "when the request method is not GET" do + let(:http_method) { "POST" } + + it { should be_false } + end + end + + describe '#head?' do + subject { request.head? } + + context "when the request method is HEAD" do + let(:http_method) { "HEAD" } + + it { should be_true } + end + + context "when the request method is not HEAD" do + let(:http_method) { "GET" } + + it { should be_false } + end + end + + describe '#post?' do + subject { request.post? } + + context "when the request method is POST" do + let(:http_method) { "POST" } + + it { should be_true } + end + + context "when the request method is not POST" do + let(:http_method) { "GET" } + + it { should be_false } + end + end + + describe '#put?' do + subject { request.put? } + + context "when the request method is PUT" do + let(:http_method) { "PUT" } + + it { should be_true } + end + + context "when the request method is not PUT" do + let(:http_method) { "GET" } + + it { should be_false } + end + end + + describe '#delete?' do + subject { request.delete? } + + context "when the request method is DELETE" do + let(:http_method) { "DELETE" } + + it { should be_true } + end + + context "when the request method is not DELETE" do + let(:http_method) { "GET" } + + it { should be_false } + end + end + + describe '#trace?' do + subject { request.trace? } + + context "when the request method is TRACE" do + let(:http_method) { "TRACE" } + + it { should be_true } + end + + context "when the request method is not TRACE" do + let(:http_method) { "GET" } + + it { should be_false } + end + end + + describe '#connect?' do + subject { request.connect? } + + context "when the request method is CONNECT" do + let(:http_method) { "CONNECT" } + + it { should be_true } + end + + context "when the request method is not CONNECT" do + let(:http_method) { "GET" } + + it { should be_false } + end + end + + describe '#options?' do + subject { request.options? } + + context "when the request method is OPTIONS" do + let(:http_method) { "OPTIONS" } + + it { should be_true } + end + + context "when the request method is not OPTIONS" do + let(:http_method) { "GET" } + + it { should be_false } + end + end + end