Skip to content

Commit

Permalink
before all is now before every tests
Browse files Browse the repository at this point in the history
  • Loading branch information
guilhermesilveira committed Oct 19, 2010
1 parent 89d017c commit e990484
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 61 deletions.
2 changes: 1 addition & 1 deletion lib/restfulie.rb
Expand Up @@ -11,9 +11,9 @@ module Restfulie
# creates a new entry point for executing requests
def self.at(uri)
Restfulie.using {
request_marshaller
recipe
follow_link
request_marshaller
verb_request
}.at(uri)
end
Expand Down
42 changes: 38 additions & 4 deletions lib/restfulie/client/http/request_adapter.rb
Expand Up @@ -16,6 +16,37 @@ class RequestAdapter
attr_accessor :cookies, :response_handler
attr_writer :default_headers

class EnhanceResponse
def initialize(requester)
@requester = requester
end

def parse(host, path, http_request, request, response)
resp = @requester.parse(host, path, http_request, request, response)
resp.extend(ResponseHolder)
resp.response = response
resp
end
end

class IgnoreError

def initialize(requester)
@requester = requester
end

def parse(host, path, http_request, request, response)
begin
@requester.parse(host, path, http_request, request, response)
rescue Error::RESTError => se
if response.code==409
debugger
end
se
end
end

end
class CatchAndThrow
def parse(host, path, http_request, request, response)
case response.code
Expand All @@ -36,6 +67,7 @@ def parse(host, path, http_request, request, response)
when 407
raise Error::ProxyAuthenticationRequired.new(request, response)
when 409
debugger
raise Error::Conflict.new(request, response)
when 410
raise Error::Gone.new(request, response)
Expand Down Expand Up @@ -119,9 +151,11 @@ def headers
# * <tt>path: '/posts'</tt>
# * <tt>args: payload: 'some text' and/or headers: {'Accept' => '*/*', 'Content-Type' => 'application/atom+xml'}</tt>
def request(method, path, *args)
if path=="/test/409"
debugger
end
@response_handler = IgnoreError.new(@response_handler)
request!(method, path, *args)
rescue Error::RESTError => se
[[@host, path], nil, se.response]
end

# Executes a request against your server and return a response instance.
Expand All @@ -143,15 +177,15 @@ def request!(method, path, *args)
# if response.has_cookie?
# default_headers << response.cookies
# end
return [[@host, path], http_request, response] if response
return response if response
response = http_request.send(method, path, *args)
response = ResponseHandler.handle(method, path, response)
rescue Exception => e
Restfulie::Common::Logger.logger.error(e)
raise Error::ServerNotAvailableError.new(self, Response.new(method, path, 503, nil, {}), e )
end

response_handler.parse(@host, path, http_request, self, response)
EnhanceResponse.new(response_handler).parse(@host, path, http_request, self, response)

end

Expand Down
14 changes: 3 additions & 11 deletions lib/restfulie/client/http/request_marshaller.rb
Expand Up @@ -35,17 +35,9 @@ def parse(host, path, http_request, request, response)
response
elsif (!response.body.nil?) && !response.body.empty?
representation = RequestMarshaller.content_type_for(response.headers['content-type']) || Restfulie::Common::Representation::Generic.new
representation.unmarshal(response.body).tap do |u|
u.extend(ResponseHolder)
u.response = response
u
end
representation.unmarshal(response.body)
else
response.tap do |resp|
resp.extend(ResponseHolder)
resp.response = response
resp
end
response
end
end
end
Expand Down Expand Up @@ -112,7 +104,7 @@ def request!(method, path, *args)
args = add_representation_headers(method, path, unmarshaller, *args)
end

delegate(:request, method, path, *args)
delegate(:request!, method, path, *args)

end

Expand Down
4 changes: 0 additions & 4 deletions lib/restfulie/client/http/request_wrappers.rb
Expand Up @@ -15,10 +15,6 @@ def initialize(host)
class RequestHistoryExecutor < MasterDelegator
def initialize(host)
@requester = Restfulie.using {
# recipe
# follow_link
# request_marshaller
# verb_request
request_history
verb_request
}.at(host)
Expand Down
113 changes: 73 additions & 40 deletions tests/spec/requests/http/adapter_spec.rb
Expand Up @@ -165,7 +165,7 @@
it "should remember last requests" do
@builder.at('/test').accepts('application/atom+xml').with('Accept-Language' => 'en').get.should respond_with_status(200)
@builder.at('/test').accepts('text/html').with('Accept-Language' => 'pt-BR').head.should respond_with_status(200)
@builder.at('/test').as('application/xml').with('Accept-Language' => 'en').post('test').should respond_with_status(201)
@builder.at('/test').as('application/xml').with('Accept-Language' => 'en').post('test').response.should respond_with_status(201)
@builder.at('/test/500').accepts('application/xml').with('Accept-Language' => 'en').get.should respond_with_status(500)

@builder.history(0).request.should respond_with_status(200)
Expand Down Expand Up @@ -204,14 +204,14 @@ class FakeResponse < ::Restfulie::Client::HTTP::Response
end
::Restfulie::Client::HTTP::ResponseHandler.register(701,FakeResponse)

let(:client) { ::Restfulie::Client::HTTP::RequestExecutor.new(::URI.parse("http://localhost:4567")) }
let(:client) { Restfulie.at("http://localhost:4567") }

it 'should have FakeResponder as Response Handler to 201' do
::Restfulie::Client::HTTP::ResponseHandler.handlers(701).should equal FakeResponse
end

it 'should respond FakeResponse' do
client.get('/test/701')[2].class.should equal FakeResponse
client.get('/test/701').should equal FakeResponse
end

it 'should respond default Response' do
Expand All @@ -236,89 +236,122 @@ class FakeResponse < ::Restfulie::Client::HTTP::Response

context "error conditions" do

before(:all) do
before do
@host = "http://localhost:4567"
@client = Restfulie.at(@host)
end

it "receives error when 300..399 code is returned" do
@client.at("/test/302").get.should respond_with_status(302)
end
it "raise Error::Redirection error when 300..399 code is returned" do
@client.get("/test/302").should respond_with_status(302)
lambda { @client.get!("/test/302") }.should raise_exception ::Restfulie::Client::HTTP::Error::Redirection
lambda { @client.at("/test/302").get! }.should raise_exception ::Restfulie::Client::HTTP::Error::Redirection
end

it "raise Error::BadRequest error when 400 code is returned" do
@client.get("/test/400").should respond_with_status(400)
lambda { @client.get!("/test/400") }.should raise_exception ::Restfulie::Client::HTTP::Error::BadRequest
@client.at("/test/400").get.should respond_with_status(400)
end
it "receives error when 400 code is returned" do
lambda { @client.at("/test/400").get! }.should raise_exception ::Restfulie::Client::HTTP::Error::BadRequest
end

it "raise Error::Unauthorized error when 401 code is returned" do
@client.get("/test/401").should respond_with_status(401)
lambda { @client.get!("/test/401") }.should raise_exception ::Restfulie::Client::HTTP::Error::Unauthorized
@client.at("/test/401").get.should respond_with_status(401)
end
it "receives error when 401 code is returned" do
lambda { @client.at("/test/401").get! }.should raise_exception ::Restfulie::Client::HTTP::Error::Unauthorized
end

it "raise Error::Forbidden error when 403 code is returned" do
@client.get("/test/403").should respond_with_status(403)
lambda { @client.get!("/test/403") }.should raise_exception ::Restfulie::Client::HTTP::Error::Forbidden
@client.at("/test/403").get.should respond_with_status(403)
end
it "receives error when 403 code is returned" do
lambda { @client.at!("/test/403").get! }.should raise_exception ::Restfulie::Client::HTTP::Error::Forbidden
end

it "raise Error::NotFound error when 404 code is returned" do
@client.get("/test/404").should respond_with_status(404)
lambda { @client.get!("/test/404") }.should raise_exception ::Restfulie::Client::HTTP::Error::NotFound
@client.at("/test/404").get.should respond_with_status(404)
end
it "receives error when 404 code is returned" do
lambda { @client.at("/test/404").get! }.should raise_exception ::Restfulie::Client::HTTP::Error::NotFound
end

it "raise Error::MethodNotAllowed error when 405 code is returned" do
@client.get("/test/405").should respond_with_status(405)
lambda { @client.get!("/test/405") }.should raise_exception ::Restfulie::Client::HTTP::Error::MethodNotAllowed
@client.at("/test/405").get.should respond_with_status(405)
end
it "receives error when 405 code is returned" do
lambda { @client.at("/test/405").get! }.should raise_exception ::Restfulie::Client::HTTP::Error::MethodNotAllowed
end

it "raise Error::ProxyAuthenticationRequired error when 407 code is returned" do
@client.get("/test/407").should respond_with_status(407)
lambda { @client.get!("/test/407") }.should raise_exception ::Restfulie::Client::HTTP::Error::ProxyAuthenticationRequired
@client.at("/test/407").get.should respond_with_status(407)
end
it "receives error when 407 code is returned" do
lambda { @client.at("/test/407").get! }.should raise_exception ::Restfulie::Client::HTTP::Error::ProxyAuthenticationRequired
end

it "receives error when 409 code is returned" do
@client.at("/test/409").get.should respond_with_status(409)
end

it "raise Error::Conflict error when 409 code is returned" do
@client.get("/test/409").should respond_with_status(409)
lambda { @client.get!("/test/409") }.should raise_exception ::Restfulie::Client::HTTP::Error::Conflict
lambda { debugger; @client.at("/test/409").get! }.should raise_exception ::Restfulie::Client::HTTP::Error::Conflict
end

it "raise Error::Gone error when 410 code is returned" do
@client.get("/test/410").should respond_with_status(410)
lambda { @client.get!("/test/410") }.should raise_exception ::Restfulie::Client::HTTP::Error::Gone
@client.at("/test/410").get.should respond_with_status(410)
end
it "receives error when 410 code is returned" do
lambda { @client.at("/test/410").get! }.should raise_exception ::Restfulie::Client::HTTP::Error::Gone
end

it "raise Error::PreconditionFailed error when 412 code is returned" do
@client.get("/test/412").should respond_with_status(412)
lambda { @client.get!("/test/412") }.should raise_exception ::Restfulie::Client::HTTP::Error::PreconditionFailed
@client.at("/test/412").get.should respond_with_status(412)
end
it "receives error when 412 code is returned" do
lambda { @client.at("/test/412").get! }.should raise_exception ::Restfulie::Client::HTTP::Error::PreconditionFailed
end

it "raise Error::ClientError error when 413 code is returned" do
@client.get("/test/413").should respond_with_status(413)
lambda { @client.get!("/test/413") }.should raise_exception ::Restfulie::Client::HTTP::Error::ClientError
it "receives error when 413 code is returned" do
@client.at("/test/413").get.should respond_with_status(413)
end
it "raise Error::ClientError error when 413 code is returned" do
lambda { @client.at("/test/413").get! }.should raise_exception ::Restfulie::Client::HTTP::Error::ClientError
end

it "raise Error::NotImplemented error when 501 code is returned" do
@client.get("/test/501").should respond_with_status(501)
lambda { @client.get!("/test/501") }.should raise_exception ::Restfulie::Client::HTTP::Error::NotImplemented
it "receives error when 501 code is returned" do
@client.at("/test/501").get.should respond_with_status(501)
end
it "raise Error::NotImplemented error when 501 code is returned" do
lambda { @client.at("/test/501").get! }.should raise_exception ::Restfulie::Client::HTTP::Error::NotImplemented
end

it "raise Error::ServerError error when 500 code is returned" do
@client.get("/test/500").should respond_with_status(500)
lambda { @client.get!("/test/500") }.should raise_exception ::Restfulie::Client::HTTP::Error::ServerError
it "receives error when 500 code is returned" do
@client.at("/test/500").get.should respond_with_status(500)
end
it "raise Error::ServerError error when 500 code is returned" do
lambda { @client.at("/test/500").get! }.should raise_exception ::Restfulie::Client::HTTP::Error::ServerError
end

it "raise Error::ServerNotAvailableError error when 503 code is returned" do
@client.get(nil,nil).should respond_with_status(503)
lambda { @client.get!(nil,nil) }.should raise_exception ::Restfulie::Client::HTTP::Error::ServerNotAvailableError
it "receives error when 503 code is returned" do
@client.at(nil).get.should respond_with_status(503)
end
it "raise Error::ServerNotAvailableError error when 503 code is returned" do
lambda { @client.at(nil,nil).get! }.should raise_exception ::Restfulie::Client::HTTP::Error::ServerNotAvailableError
end

it "receives error when 502..599 code is returned" do
@client.at("/test/502").get.should respond_with_status(502)
end
it "raise Error::ServerError error when 502..599 code is returned" do
@client.get("/test/502").should respond_with_status(502)
lambda { @client.get!("/test/502") }.should raise_exception ::Restfulie::Client::HTTP::Error::ServerError
lambda { @client.at("/test/502").get! }.should raise_exception ::Restfulie::Client::HTTP::Error::ServerError
end

it "receives error when 600 or bigger code is returned" do
@client.at("/test/600").get.should respond_with_status(600)
end
it "raise Error::UnknownError error when 600 or bigger code is returned" do
@client.get("/test/600").should respond_with_status(600)
lambda { @client.get!("/test/600") }.should raise_exception ::Restfulie::Client::HTTP::Error::UnknownError
lambda { @client.at("/test/600").get! }.should raise_exception ::Restfulie::Client::HTTP::Error::UnknownError
end

end
Expand Down
2 changes: 1 addition & 1 deletion tests/spec/spec_helper.rb
Expand Up @@ -17,7 +17,7 @@ def initialize(response_code)
end

def matches?(response_header)
@actual_code = response_header.code
@actual_code = response_header.response.code
@expected_code == @actual_code
end

Expand Down

0 comments on commit e990484

Please sign in to comment.