lib/rapidash/test_client.rb in rapidash-0.2.2 vs lib/rapidash/test_client.rb in rapidash-0.3.0.beta
- old
+ new
@@ -1,13 +1,62 @@
+# Rapidash::TestClient
+# A dummy client for testing with. Create a new test
+# client by including this module and initializing
+# with a set of responses.
+# Example:
+#
+# class TesterClient
+# include Rapidash::TestClient
+# end
+#
+# responses = {
+# get: { "foo" => "bar" },
+# post: { "baz" => "data" }
+# }
+#
+# client = TesterClient.new(responses)
+#
+# Example with JSON support:
+#
+# responses = {
+# get: { "foo" => '{"some": 123, "json": 456}' }
+# }
+#
+# client = TesterClient.new(responses, json: true)
module Rapidash
module TestClient
- attr_accessor :responses
+ attr_reader :responses, :stubs, :json
- def initialize(options = {})
- @responses = options.delete(:responses)
+ def initialize(responses, options = {})
+ @json = options[:json] || false
+ @responses = responses
+ build_stubs
end
def request(verb, url, options = {})
- Response.new(responses[verb][url])
+ connection.send(verb, url, options).body
+ end
+
+ private
+
+ def build_stubs
+ @stubs = Faraday::Adapter::Test::Stubs.new do |stub|
+ responses.each_pair do |verb, req|
+ req.each_pair do |url, body|
+ stub.send(verb, url) { [200, {}, body] }
+ end
+ end
+ end
+ end
+
+ def connection
+ @connection ||= Faraday.new do |builder|
+ builder.adapter :test, stubs
+ builder.use FaradayMiddleware::Mashify
+
+ if json
+ builder.use FaradayMiddleware::ParseJson
+ end
+ end
end
end
end