faraday_cage 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .rspec
6
+ .yardoc
7
+ Gemfile.lock
8
+ InstalledFiles
9
+ _yardoc
10
+ coverage
11
+ doc/
12
+ lib/bundler/man
13
+ pkg
14
+ rdoc
15
+ spec/reports
16
+ test/tmp
17
+ test/version_tmp
18
+ tmp
data/.pryrc ADDED
@@ -0,0 +1,11 @@
1
+ #!/usr/bin/env ruby
2
+ # -*- mode: ruby -*-
3
+ # vi: set ft=ruby :
4
+
5
+ require 'pathname'
6
+ $LOAD_PATH.unshift(Pathname.getwd.join('lib').to_s)
7
+ require 'faraday_cage'
8
+
9
+ def reload!
10
+ Dir["#{Dir.pwd}/lib/**/*.rb"].each { |f| load f }
11
+ end
data/.travis.yml ADDED
@@ -0,0 +1,18 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.3
4
+ - jruby-19mode
5
+ - rbx-19mode
6
+ - 2.0.0
7
+ branches:
8
+ only:
9
+ - master
10
+ notifications:
11
+ email:
12
+ recipients:
13
+ - dev@tabeso.com
14
+ matrix:
15
+ allow_failures:
16
+ - rvm: jruby-19mode
17
+ - rvm: rbx-19mode
18
+ - rvm: 2.0.0
data/.yardopts ADDED
@@ -0,0 +1,3 @@
1
+ --title 'FaradayCage Documentation'
2
+ --charset utf-8
3
+ --markup markdown
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in faraday_cage.gemspec
4
+ gemspec
data/Guardfile ADDED
@@ -0,0 +1,15 @@
1
+ guard 'bundler' do
2
+ watch('Gemfile')
3
+ watch('faraday_cage.gemspec')
4
+ end
5
+
6
+ guard 'rspec' do
7
+ watch(%r{^spec/.+_spec\.rb$})
8
+ watch(%r{^lib/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
9
+ watch('spec/spec_helper.rb') { 'spec' }
10
+ end
11
+
12
+ guard 'yard' do
13
+ watch(%r{app/.+\.rb})
14
+ watch(%r{lib/.+\.rb})
15
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Tabeso, LLC.
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,88 @@
1
+ # Faraday Cage
2
+
3
+ * Code: [github.com/tabeso/faraday_cage](https://github.com/tabeso/faraday_cage)
4
+
5
+ ## Description
6
+
7
+ Faraday Cage allows you to use [Faraday](https://github.com/lostisland/faraday)
8
+ for making requests to your APIs in integration testing, minus the boilerplate
9
+ code and repeated parsing and encoding of requests and responses.
10
+
11
+ ## Installation
12
+
13
+ Add this line to your application's Gemfile:
14
+
15
+ ```ruby
16
+ gem 'faraday_cage'
17
+ ```
18
+
19
+ And then execute:
20
+
21
+ $ bundle
22
+
23
+ Or install it yourself as:
24
+
25
+ $ gem install faraday_cage
26
+
27
+ ## Usage with RSpec
28
+
29
+ Load RSpec support by adding the following to your `spec_helper.rb`:
30
+
31
+ ```ruby
32
+ require 'faraday_cage/rspec'
33
+ ```
34
+
35
+ Configure the middleware you would like to use (see
36
+ [Faraday](https://github.com/lostisland/faraday) and
37
+ [faraday_middleware](https://github.com/lostisland/faraday_middleware) for more
38
+ information):
39
+
40
+ ```ruby
41
+ FaradayCage.middleware do |conn|
42
+ conn.request :json
43
+ conn.response :mashify
44
+ conn.response :json, content_type: /\bjson$/
45
+
46
+ conn.options[:preserve_raw] = true
47
+ end
48
+ ```
49
+
50
+ You will now be able to test your app like so:
51
+
52
+ ```ruby
53
+ describe 'creating a gist' do
54
+
55
+ before do
56
+ header :accept, 'application/vnd.github.v3'
57
+ basic_auth 'username', 'password'
58
+ end
59
+
60
+ let(:gist) do
61
+ {
62
+ description: 'This is my gist, there are many like it but this one is mine.',
63
+ public: true,
64
+ files: [
65
+ 'file1.txt' => {
66
+ content: 'cool beans'
67
+ }
68
+ ]
69
+ }
70
+ end
71
+
72
+ subject { post 'gists', gist }
73
+
74
+ its(:status) { should be_created }
75
+
76
+ it 'references the location of the created gist' do
77
+ body[:url].should =~ %r{^https://api.github.com/gists/[a-f0-9]+$}
78
+ end
79
+ end
80
+ ```
81
+
82
+ ## Contributing
83
+
84
+ 1. Fork it
85
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
86
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
87
+ 4. Push to the branch (`git push origin my-new-feature`)
88
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,18 @@
1
+ require 'bundler/gem_tasks'
2
+
3
+ require 'rspec/core/rake_task'
4
+ RSpec::Core::RakeTask.new(:spec) do |spec|
5
+ spec.pattern = FileList['spec/**/*_spec.rb']
6
+ end
7
+
8
+ task :default => :spec
9
+
10
+ require 'yard'
11
+ YARD::Rake::YardocTask.new
12
+
13
+ desc 'Start Pry with runtime dependencies loaded'
14
+ task :console, :script do |t, args|
15
+ command = 'bundle exec pry'
16
+ command += "-r #{args[:script]}" if args[:script]
17
+ sh command
18
+ end
@@ -0,0 +1,40 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'faraday_cage/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = 'faraday_cage'
8
+ gem.version = FaradayCage::VERSION
9
+ gem.authors = ['Gabriel Evans']
10
+ gem.email = ['gabe@tabeso.com']
11
+ gem.description = %q{Faraday Cage allows you to use Faraday for making requests to your REST APIs in integration testing, minus the boilerplate code and crufty parsing and encoding of requests and responses.}
12
+ gem.summary = %q{Use Faraday to integration test your REST applications.}
13
+ gem.homepage = 'https://github.com/tabeso/faraday_cage'
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ['lib']
19
+
20
+ gem.add_dependency 'rack', '>= 1.0'
21
+ gem.add_dependency 'rack-test', '>= 0.5.4'
22
+ gem.add_dependency 'faraday_middleware', '>= 0.8', '< 1.0'
23
+ gem.add_dependency 'activesupport'
24
+
25
+ gem.add_development_dependency 'rake'
26
+ gem.add_development_dependency 'pry'
27
+ gem.add_development_dependency 'yard'
28
+ gem.add_development_dependency 'redcarpet'
29
+
30
+ gem.add_development_dependency 'rspec'
31
+ gem.add_development_dependency 'simplecov'
32
+
33
+ gem.add_development_dependency 'guard'
34
+ gem.add_development_dependency 'guard-bundler'
35
+ gem.add_development_dependency 'guard-rspec'
36
+ gem.add_development_dependency 'guard-yard'
37
+ gem.add_development_dependency 'rb-fsevent'
38
+ gem.add_development_dependency 'rb-inotify'
39
+ gem.add_development_dependency 'growl'
40
+ end
@@ -0,0 +1,38 @@
1
+ require 'active_support/core_ext/module/delegation'
2
+ require 'active_support/concern'
3
+
4
+ require 'faraday_middleware'
5
+
6
+ require 'faraday_cage/version'
7
+ require 'faraday_cage/errors'
8
+ require 'faraday_cage/config'
9
+ require 'faraday_cage/helpers'
10
+ require 'faraday_cage/response'
11
+ require 'faraday_cage/server'
12
+ require 'faraday_cage/status'
13
+
14
+ module FaradayCage
15
+ extend self
16
+
17
+ def configure
18
+ yield(config)
19
+ end
20
+
21
+ def config
22
+ @config ||= Config.new
23
+ end
24
+
25
+ def server
26
+ @server ||= FaradayCage::Server.new
27
+ end
28
+
29
+ def connection
30
+ server.connection
31
+ end
32
+
33
+ def reset!
34
+ @server = nil
35
+ end
36
+
37
+ delegate(*Config.public_instance_methods(false), to: :config)
38
+ end # FaradayCage
@@ -0,0 +1,38 @@
1
+ module FaradayCage
2
+ ##
3
+ # Handles configuration of Faraday connection and server options.
4
+ class Config
5
+
6
+ attr_accessor :default_host
7
+
8
+ attr_writer :app
9
+
10
+ def initialize
11
+ self.default_host = 'http://example.com'
12
+ end
13
+
14
+ def app
15
+ @app ||= begin
16
+ default_rails_app if defined?(Rails)
17
+ end
18
+ end
19
+
20
+ def middleware(&block)
21
+ if block_given?
22
+ @middleware = block
23
+ else
24
+ @middleware
25
+ end
26
+ end
27
+
28
+ private
29
+
30
+ def default_rails_app
31
+ Rack::Builder.new do
32
+ map '/' do
33
+ run Rails.application
34
+ end
35
+ end.to_app
36
+ end
37
+ end # Config
38
+ end # FaradayCage
@@ -0,0 +1,3 @@
1
+ module FaradayCage
2
+ class Error < StandardError; end
3
+ end
@@ -0,0 +1,55 @@
1
+ module FaradayCage
2
+ module Helpers
3
+
4
+ %w(get head post put patch delete).each do |method|
5
+ class_eval <<-RUBY, __FILE__, __LINE__ + 1
6
+ def #{method}(uri = nil, params_or_body = nil, headers = nil, &block)
7
+ @last_response = FaradayCage.connection.#{method}(uri, params_or_body, headers, &block).tap do |res|
8
+ res.extend(FaradayCage::Response)
9
+ end
10
+ end
11
+ RUBY
12
+ end
13
+
14
+ def params(hash)
15
+ FaradayCage.connection.params = hash
16
+ end
17
+
18
+ def headers(hash)
19
+ FaradayCage.connection.headers = hash
20
+ end
21
+
22
+ def header(name, value)
23
+ FaradayCage.connection.headers[name] = value
24
+ end
25
+
26
+ def basic_auth(login, pass = nil)
27
+ FaradayCage.connection.basic_auth(login, pass)
28
+ end
29
+
30
+ def token_auth(login, options = nil)
31
+ FaradayCage.connection.token_auth(token, options)
32
+ end
33
+
34
+ def last_response
35
+ @last_response || raise(FaradayCage::Error , 'No response yet. Request a page first.')
36
+ end
37
+
38
+ def response_headers
39
+ last_response.headers
40
+ end
41
+
42
+ def body
43
+ last_response.body
44
+ end
45
+
46
+ def source
47
+ last_response.env[:raw_body] || last_response.body
48
+ end
49
+
50
+ def reset_faraday_cage!
51
+ FaradayCage.reset!
52
+ @last_response = nil
53
+ end
54
+ end # Helpers
55
+ end # FaradayCage
@@ -0,0 +1,26 @@
1
+ module FaradayCage
2
+ ##
3
+ # Wraps Rack apps to allow identification when running multiple.
4
+ class RackMiddleware
5
+
6
+ attr_reader :app
7
+
8
+ def initialize(app)
9
+ @app = app
10
+ end
11
+
12
+ def call(env)
13
+ identity_request?(env) ? identity : app.call(env)
14
+ end
15
+
16
+ protected
17
+
18
+ def identity_request?(env)
19
+ env['PATH_INFO'] == '/__identify__'
20
+ end
21
+
22
+ def identity
23
+ [200, { 'Content-Type' => 'text/plain' }, [@app.object_id.to_s]]
24
+ end
25
+ end # Middleware
26
+ end # FaradayCage
@@ -0,0 +1,13 @@
1
+ module FaradayCage
2
+ module Response
3
+
4
+ def self.extended(base)
5
+ base.instance_eval do
6
+ alias :original_status :status
7
+ def status
8
+ finished? ? Status.new(env[:status]) : nil
9
+ end
10
+ end
11
+ end
12
+ end # Response
13
+ end # FaradayCage
@@ -0,0 +1,14 @@
1
+ require 'faraday_cage'
2
+ require 'rspec/core'
3
+
4
+ RSpec.configure do |config|
5
+ [:feature, :request].each do |group|
6
+ config.include FaradayCage::Helpers, type: group
7
+ end
8
+
9
+ config.after do
10
+ if self.class.include?(FaradayCage::Helpers)
11
+ FaradayCage.reset!
12
+ end
13
+ end
14
+ end