Permalink
Browse files

progress

  • Loading branch information...
1 parent c9b8c39 commit 3f4f4eb75e8ce48ac2d0746a0dbc234255d01501 @avalanche123 committed May 30, 2011
View
@@ -1,2 +1,5 @@
+module Frank
+end
+
require 'frank/dsl'
require 'frank/http'
View
@@ -1 +1,3 @@
+require 'frank/http/errors'
+require 'frank/http/request'
require 'frank/http/request_factory'
View
@@ -0,0 +1,4 @@
+module Frank::HTTP
+ class TraceRequestBody < ArgumentError
+ end
+end
View
@@ -0,0 +1,16 @@
+module Frank::HTTP
+ class Request
+ attr_reader :body
+
+ def initialize(method)
+ @method = method
+ end
+
+ def body=(body)
+ if @method === "TRACE"
+ raise TraceRequestBody unless body.nil?
+ end
+ @body = body
+ end
+ end
+end
@@ -1,5 +1,15 @@
module Frank::HTTP
- module RequestFactory
-
+ class RequestFactory
+ @allowed_methods ||= ["GET", "POST", "PUT", "DELETE", "HEAD", "PATCH", "OPTIONS", "TRACE"]
+
+ module ClassMethods
+ def create_request(method, &block)
+ raise ArgumentError unless block_given? and @allowed_methods.include?(method)
+ request = Request.new(method)
+ request.body = request.instance_eval(&block)
+ request
+ end
+ end
+ extend ClassMethods
end
end
@@ -0,0 +1,47 @@
+require 'spec_helper'
+
+module Frank::HTTP
+ describe RequestFactory do
+ ["GET", "POST", "PUT", "DELETE", "HEAD", "PATCH", "OPTIONS", "TRACE"].each do |method|
+ context "when request creation succedes" do
+ let (:request) { mock(Request).as_null_object }
+
+ before(:each) do
+ Request.should_receive(:new).with(method).and_return(request)
+ end
+
+ it "creates request with given method" do
+ block = lambda { |r| }
+ RequestFactory.create_request(method, &block)
+ end
+
+ it "evaluates block on request instance" do
+ request.should_receive(:accept).with('text/html').and_return(nil)
+ RequestFactory.create_request method, do
+ accept 'text/html'
+ end
+ end
+ end
+ end
+
+ ["POST", "PUT", "DELETE", "PATCH"].each do |method|
+ context "when method is non-safe" do
+ it "sets request body to block's returned string" do
+ request = RequestFactory.create_request method, do
+ 'foo=bar&baz=quiz'
+ end
+ request.body.should eq('foo=bar&baz=quiz')
+ end
+ end
+ end
+
+ it "raises if no block given" do
+ expect { RequestFactory.create_request(method) }.to raise_error(ArgumentError)
+ end
+
+ it "raises if invalid method given" do
+ method, block = "UNKNOWN", lambda {|a, b|}
+ expect { RequestFactory.create_request(method, &block) }.to raise_error(ArgumentError)
+ end
+ end
+end
@@ -0,0 +1,10 @@
+require 'spec_helper'
+
+module Frank::HTTP
+ describe Request do
+ it "raises if a trace request contains body" do
+ request = Request.new("TRACE")
+ expect { request.body = 'body' }.to raise_error(TraceRequestBody)
+ end
+ end
+end

0 comments on commit 3f4f4eb

Please sign in to comment.