api_fixtures 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,36 @@
1
+ require 'api_fixtures/fixtures'
2
+
3
+ module ApiFixtures
4
+ module DSL
5
+ module InstanceDSL
6
+ def api
7
+ ApiFixtures::Fixtures
8
+ end
9
+ end
10
+
11
+ def self.extended(base)
12
+ base.class_attribute :api_fixture_paths
13
+ base.send(:include, InstanceDSL)
14
+
15
+ before_hook = [:before, :setup].find {|hook| base.respond_to?(hook) }
16
+ after_hook = [:after, :teardown].find {|hook| base.respond_to?(hook) }
17
+
18
+ base.send(before_hook) do
19
+ ApiFixtures::Fixtures.clear
20
+ (api_fixture_paths || []).each do |path|
21
+ ApiFixtures::Fixtures.fixture('GET', path)
22
+ end
23
+ end
24
+
25
+ base.send(after_hook) do
26
+ ApiFixtures::Fixtures.assert_expected_calls(self)
27
+ end
28
+ end
29
+
30
+ def api_fixtures(*paths)
31
+ self.api_fixture_paths ||= []
32
+ self.api_fixture_paths += paths
33
+ self.api_fixture_paths.uniq!
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,82 @@
1
+ module ApiFixtures
2
+ class Fixtures
3
+ FIXTURES = {
4
+ :get => {},
5
+ :put => {},
6
+ :post => {},
7
+ :delete => {}
8
+ }
9
+ EXPECTATIONS = []
10
+ CALLS = []
11
+
12
+ def self.normalize_method(method)
13
+ method.downcase.to_sym
14
+ end
15
+
16
+ def self.normalize_path(path)
17
+ path.gsub(/\.\w+$/, '')
18
+ end
19
+
20
+ def self.fixtures_folder
21
+ unless @fixtures_folder
22
+ if defined?(Rails)
23
+ @fixtures_folder = (Rails.root + 'test/api_fixtures')
24
+ end
25
+ end
26
+ @fixtures_folder
27
+ end
28
+
29
+ def self.fixtures_folder=(folder)
30
+ @fixtures_folder = folder
31
+ end
32
+
33
+ def self.fixture(method, path, response = nil)
34
+ ApiFixtures::Middleware.must_be_in_stack!
35
+
36
+ case response
37
+ when Hash
38
+ response = [200, {'Content-Type' => 'application/json; charset=utf-8'}, [response.to_json]]
39
+ when Array
40
+ case response[2]
41
+ when String
42
+ response[2] = [response[2]]
43
+ end
44
+ when nil
45
+ file_name = fixtures_folder + ('.' + path + '.json')
46
+ response = [200, {'Content-Type' => 'application/json; charset=utf-8'}, [File.read(file_name.to_s)]]
47
+ end
48
+
49
+ FIXTURES[normalize_method(method)][path] = response
50
+ end
51
+
52
+ def self.expect(method, path, options = {})
53
+ if options[:response]
54
+ fixture(method, path, options[:response])
55
+ end
56
+ EXPECTATIONS << [normalize_method(method), path]
57
+ end
58
+
59
+ def self.lookup(method, path)
60
+ method = normalize_method(method)
61
+ path = normalize_path(path)
62
+ CALLS << [method, path]
63
+ FIXTURES[method][path]
64
+ end
65
+
66
+ def self.clear
67
+ FIXTURES[:get].clear
68
+ FIXTURES[:put].clear
69
+ FIXTURES[:post].clear
70
+ FIXTURES[:delete].clear
71
+
72
+ EXPECTATIONS.clear
73
+ CALLS.clear
74
+ end
75
+
76
+ def self.assert_expected_calls(test)
77
+ (EXPECTATIONS - CALLS).each do | method, path|
78
+ test.flunk("Expected API call: #{method.to_s.upcase} #{path}")
79
+ end
80
+ end
81
+ end
82
+ end
@@ -0,0 +1,23 @@
1
+ module ApiFixtures
2
+ class Middleware
3
+ def initialize(app)
4
+ @app = app
5
+ end
6
+
7
+ def call(env)
8
+ request = Rack::Request.new(env)
9
+ ApiFixtures::Fixtures.lookup(request.request_method, request.path) || @app.call(env)
10
+ end
11
+
12
+ def self.must_be_in_stack!
13
+ if defined?(Rails::Application)
14
+ unless Rails.application.middleware.include?(self)
15
+ raise "ApiFixtures::Middleware needs to be in the middleware stack"
16
+ end
17
+ true
18
+ else
19
+ false
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,7 @@
1
+ require 'api_fixtures/fixtures'
2
+ require 'api_fixtures/middleware'
3
+ require 'api_fixtures/dsl'
4
+
5
+ module ApiFixtures
6
+ VERSION = '0.0.1'
7
+ end
metadata ADDED
@@ -0,0 +1,74 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: api_fixtures
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Mick Staugaard
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-11-10 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ requirement: &70290369209240 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: *70290369209240
25
+ - !ruby/object:Gem::Dependency
26
+ name: activesupport
27
+ requirement: &70290369208700 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: 2.3.9
33
+ - - <
34
+ - !ruby/object:Gem::Version
35
+ version: 3.1.1
36
+ type: :runtime
37
+ prerelease: false
38
+ version_requirements: *70290369208700
39
+ description:
40
+ email:
41
+ - mick@staugaard.com
42
+ executables: []
43
+ extensions: []
44
+ extra_rdoc_files: []
45
+ files:
46
+ - lib/api_fixtures/dsl.rb
47
+ - lib/api_fixtures/fixtures.rb
48
+ - lib/api_fixtures/middleware.rb
49
+ - lib/api_fixtures.rb
50
+ homepage: ''
51
+ licenses: []
52
+ post_install_message:
53
+ rdoc_options: []
54
+ require_paths:
55
+ - lib
56
+ required_ruby_version: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ required_rubygems_version: !ruby/object:Gem::Requirement
63
+ none: false
64
+ requirements:
65
+ - - ! '>='
66
+ - !ruby/object:Gem::Version
67
+ version: '0'
68
+ requirements: []
69
+ rubyforge_project:
70
+ rubygems_version: 1.8.10
71
+ signing_key:
72
+ specification_version: 3
73
+ summary: An easy way to setup JSON API fixtures for your integration tests
74
+ test_files: []