Skip to content

Commit

Permalink
add rough ActionDispatch adapter for testing Rails 3 APIs with Faraday
Browse files Browse the repository at this point in the history
  • Loading branch information
technoweenie committed Jun 7, 2010
1 parent 71248de commit b0941f3
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 9 deletions.
20 changes: 11 additions & 9 deletions lib/faraday.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,17 +59,19 @@ def all_loaded_constants
module Adapter
extend AutoloadHelper
autoload_all 'faraday/adapter',
:NetHttp => 'net_http',
:Typhoeus => 'typhoeus',
:Patron => 'patron',
:Test => 'test'
:ActionDispatch => 'action_dispatch',
:NetHttp => 'net_http',
:Typhoeus => 'typhoeus',
:Patron => 'patron',
:Test => 'test'

register_lookup_modules \
:test => :Test,
:net_http => :NetHttp,
:typhoeus => :Typhoeus,
:patron => :Patron,
:net_http => :NetHttp
:action_dispatch => :ActionDispatch,
:test => :Test,
:net_http => :NetHttp,
:typhoeus => :Typhoeus,
:patron => :Patron,
:net_http => :NetHttp
end
end

Expand Down
32 changes: 32 additions & 0 deletions lib/faraday/adapter/action_dispatch.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
module Faraday
module Adapter
class ActionDispatch < Middleware
attr_reader :session

# Initializes a new middleware instance for each request. Instead of
# initiating an HTTP request with a web server, this adapter calls
# a Rails 3 app using integration tests.
#
# app - The current Faraday request.
# session - An ActionDispatch::Integration::Session instance.
#
# Returns nothing.
def initialize(app, session)
super(app)
@session = session
end

def call(env)
process_body_for_request(env)
full_path = full_path_for(env[:url].path, env[:url].query, env[:url].fragment)
@session.__send__(env[:method], full_path, env[:body], env[:request_headers])
resp = @session.response
env.update \
:status => resp.status,
:response_headers => resp.headers,
:body => resp.body
@app.call env
end
end
end
end

0 comments on commit b0941f3

Please sign in to comment.