Navigation Menu

Skip to content

Commit

Permalink
Merge pull request #27 from emmanuel/request_verb_queries
Browse files Browse the repository at this point in the history
Add Request verb queries for standard HTTP verbs
  • Loading branch information
emmanuel committed Jan 16, 2012
2 parents 67b9d97 + eaa560b commit b7ec78d
Show file tree
Hide file tree
Showing 3 changed files with 237 additions and 10 deletions.
18 changes: 9 additions & 9 deletions lib/webmachine/decision/flow.rb
Expand Up @@ -131,7 +131,7 @@ def b4

# OPTIONS?
def b3
if request.method == "OPTIONS"
if request.options?
response.headers.merge!(resource.options)
200
else
Expand Down Expand Up @@ -282,7 +282,7 @@ def i4

# PUT?
def i7
request.method == "PUT" ? :i4 : :k7
request.put? ? :i4 : :k7
end

# If-none-match exists?
Expand All @@ -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?
Expand Down Expand Up @@ -339,7 +339,7 @@ def l5

# POST?
def l7
request.method == "POST" ? :m7 : 404
request.post? ? :m7 : 404
end

# If-Modified-Since exists?
Expand Down Expand Up @@ -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?
Expand All @@ -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.)
Expand Down Expand Up @@ -435,7 +435,7 @@ def n11

# POST?
def n16
request.method == "POST" ? :n11 : :o16
request.post? ? :n11 : :o16
end

# Conflict?
Expand All @@ -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
Expand Down
75 changes: 75 additions & 0 deletions lib/webmachine/request.rb
Expand Up @@ -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
Expand Down Expand Up @@ -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
154 changes: 153 additions & 1 deletion 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 == "*/*"
Expand Down Expand Up @@ -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

0 comments on commit b7ec78d

Please sign in to comment.