mock_server 0.4.2 → 0.4.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -8,116 +8,118 @@ unless defined? MockServer::Store
8
8
  require 'mock_server/store/global'
9
9
  end
10
10
 
11
- class MockServer::Playback
12
- include MockServer::Utils
13
- include MockServer::Store
11
+ module MockServer
12
+ class Playback
13
+ include MockServer::Utils
14
+ include MockServer::Store
15
+
16
+ def initialize(app, opt = {})
17
+ @app = app
18
+ @options = mock_server_options_merge(opt)
19
+ end
14
20
 
15
- def initialize(app, opt = {})
16
- @app = app
17
- @options = mock_server_options_merge(opt)
18
- end
21
+ def call(env)
22
+ @options = self.mock_server_options_read
19
23
 
20
- def call(env)
21
- @options = self.mock_server_options_read
24
+ verbose(env) if @options[:verbose]
25
+ return @app.call(env) unless matchable_request?(env)
22
26
 
23
- verbose(env) if @options[:verbose]
24
- return @app.call(env) unless matchable_request?(env)
27
+ @request = Rack::Request.new(env)
28
+ @options[:requests_stack] << @request.path
29
+ @data = load_data
25
30
 
26
- @request = Rack::Request.new(env)
27
- @options[:requests_stack] << @request.path
28
- @data = load_data
31
+ response = build_response
32
+ self.mock_server_options_write(@options)
33
+ response
34
+ end
29
35
 
30
- response = build_response
31
- self.mock_server_options_write(@options)
32
- response
33
- end
36
+ private
34
37
 
35
- private
38
+ def build_response
39
+ if record = match_request
40
+ return_record(record)
41
+ else
42
+ return_error
43
+ end
44
+ end
36
45
 
37
- def build_response
38
- if record = match_request
39
- return_record(record)
40
- else
41
- return_error
46
+ def return_record(record)
47
+ @options[:success_stack] << @request.path
48
+ @options[:matcher_exceptions].clear
49
+ response = record[:response]
50
+ [response[:status], response[:headers], [response[:body]]]
42
51
  end
43
- end
44
52
 
45
- def return_record(record)
46
- @options[:success_stack] << @request.path
47
- @options[:matcher_exceptions].clear
48
- response = record[:response]
49
- [response[:status], response[:headers], [response[:body]]]
50
- end
53
+ def return_error
54
+ error = { @request.path => "Couldn't match #{@request.request_method} #{@request.path}" }
55
+ @options[:errors_stack] << error
56
+ [404, {}, ['RECORD NOT FOUND!']]
57
+ end
51
58
 
52
- def return_error
53
- error = { @request.path => "Couldn't match #{@request.request_method} #{@request.path}" }
54
- @options[:errors_stack] << error
55
- [404, {}, ['RECORD NOT FOUND!']]
56
- end
59
+ def match_request
60
+ request = Hashie::Mash.new hashified_request
57
61
 
58
- def match_request
59
- request = Hashie::Mash.new hashified_request
62
+ # Filter out data records by path and method
63
+ records = filter_records(request)
60
64
 
61
- # Filter out data records by path and method
62
- records = filter_records(request)
65
+ matchers = filter_matchers(request)
63
66
 
64
- matchers = filter_matchers(request)
67
+ record = false
68
+ matchers.detect { |matcher|
69
+ # Match the request with a record by validating against the matcher if any.
70
+ record = records.detect { |entry|
71
+ recorded_request = Hashie::Mash.new entry[:request]
72
+ recorded_response = entry[:response].dup
65
73
 
66
- record = false
67
- matchers.detect { |matcher|
68
- # Match the request with a record by validating against the matcher if any.
69
- record = records.detect { |entry|
70
- recorded_request = Hashie::Mash.new entry[:request]
71
- recorded_response = entry[:response].dup
74
+ recorded_response[:body] = JSON.parse(recorded_response[:body]) rescue recorded_response[:body]
75
+ recorded_response = Hashie::Mash.new recorded_response
72
76
 
73
- recorded_response[:body] = JSON.parse(recorded_response[:body]) rescue recorded_response[:body]
74
- recorded_response = Hashie::Mash.new recorded_response
77
+ test_request_and_matcher(matcher, request, recorded_request, recorded_response)
78
+ }
79
+ }
80
+ record
81
+ end
75
82
 
76
- test_request_and_matcher(matcher, request, recorded_request, recorded_response)
83
+ def filter_matchers(request)
84
+ @options[:matchers].select { |match|
85
+ request[:method].to_s.upcase == match[:method].to_s.upcase and request[:path] == match[:path]
77
86
  }
78
- }
79
- record
80
- end
87
+ end
81
88
 
82
- def filter_matchers(request)
83
- @options[:matchers].select { |match|
84
- request[:method].to_s.upcase == match[:method].to_s.upcase and request[:path] == match[:path]
85
- }
86
- end
89
+ def filter_records(request)
90
+ @data.select { |record|
91
+ record[:request][:path] == request[:path] and record[:request][:method] == request[:method]
92
+ }
93
+ end
87
94
 
88
- def filter_records(request)
89
- @data.select { |record|
90
- record[:request][:path] == request[:path] and record[:request][:method] == request[:method]
91
- }
92
- end
95
+ def test_request_and_matcher(matcher, request, recorded_request, recorded_response)
96
+ return true if matcher[:matcher].nil?
97
+ begin
98
+ matcher[:matcher].call(request, recorded_request, recorded_response) == true
99
+ rescue => matcher_err
100
+ store_matcher_exception(matcher_err)
101
+ false
102
+ end
103
+ end
93
104
 
94
- def test_request_and_matcher(matcher, request, recorded_request, recorded_response)
95
- return true if matcher[:matcher].nil?
96
- begin
97
- matcher[:matcher].call(request, recorded_request, recorded_response) == true
98
- rescue => matcher_err
99
- store_matcher_exception(matcher_err)
100
- false
105
+ def store_matcher_exception(exception)
106
+ @options[:matcher_exceptions] << exception
101
107
  end
102
- end
103
108
 
104
- def store_matcher_exception(exception)
105
- @options[:matcher_exceptions] << exception
106
- end
109
+ def load_data
110
+ FileUtils.mkdir_p(@options[:path]) unless File.exists? @options[:path]
107
111
 
108
- def load_data
109
- FileUtils.mkdir_p(@options[:path]) unless File.exists? @options[:path]
112
+ data = []
110
113
 
111
- data = []
114
+ @options[:record_filenames].map do |filename|
115
+ file_path = File.join( @options[:path], filename + '.yml' )
116
+ content = File.open(file_path).read
117
+ compiled = ERB.new(content).result
118
+ parsed = YAML.load(compiled)
119
+ data += parsed
120
+ end
112
121
 
113
- @options[:record_filenames].map do |filename|
114
- file_path = File.join( @options[:path], filename + '.yml' )
115
- content = File.open(file_path).read
116
- compiled = ERB.new(content).result
117
- parsed = YAML.load(compiled)
118
- data += parsed
122
+ data
119
123
  end
120
-
121
- data
122
124
  end
123
125
  end
@@ -5,59 +5,61 @@ unless defined? MockServer::Store
5
5
  require 'mock_server/store/global'
6
6
  end
7
7
 
8
- class MockServer::Record
9
- include MockServer::Utils
10
- include MockServer::Store
8
+ module MockServer
9
+ class Record
10
+ include MockServer::Utils
11
+ include MockServer::Store
11
12
 
12
- def initialize(app, opt = {})
13
- @app = app
14
- @options = mock_server_options_merge(opt)
15
- end
13
+ def initialize(app, opt = {})
14
+ @app = app
15
+ @options = mock_server_options_merge(opt)
16
+ end
16
17
 
17
- def call(env)
18
- @options = self.mock_server_options_read
18
+ def call(env)
19
+ @options = self.mock_server_options_read
19
20
 
20
- verbose(env) if @options[:verbose]
21
- return @app.call(env) unless matchable_request?(env)
21
+ verbose(env) if @options[:verbose]
22
+ return @app.call(env) unless matchable_request?(env)
22
23
 
23
- @request = Rack::Request.new(env)
24
- @data = load_data
24
+ @request = Rack::Request.new(env)
25
+ @data = load_data
25
26
 
26
- @app.call(env).tap do |status, header, response|
27
- record_response(status, header, response)
28
- self.mock_server_options_write(@options)
29
- response
27
+ @app.call(env).tap do |status, header, response|
28
+ record_response(status, header, response)
29
+ self.mock_server_options_write(@options)
30
+ response
31
+ end
30
32
  end
31
- end
32
33
 
33
- private
34
+ private
34
35
 
35
- def record_response(status, header, response)
36
- request = hashified_request
36
+ def record_response(status, header, response)
37
+ request = hashified_request
37
38
 
38
- @data << { :request => request, :response => hashify_response(status, header, response) }
39
- save_data(@data)
40
- end
39
+ @data << { :request => request, :response => hashify_response(status, header, response) }
40
+ save_data(@data)
41
+ end
41
42
 
42
- def records_path
43
- File.join( @options[:path], @options[:filename] + '.yml' )
44
- end
43
+ def records_path
44
+ File.join( @options[:path], @options[:filename] + '.yml' )
45
+ end
45
46
 
46
- def save_data(data)
47
- File.open(records_path, 'w') do |f|
48
- YAML.dump(data, f)
47
+ def save_data(data)
48
+ File.open(records_path, 'w') do |f|
49
+ YAML.dump(data, f)
50
+ end
49
51
  end
50
- end
51
52
 
52
- def load_data
53
- FileUtils.mkdir_p(@options[:path]) unless File.exists? @options[:path]
53
+ def load_data
54
+ FileUtils.mkdir_p(@options[:path]) unless File.exists? @options[:path]
54
55
 
55
- data = YAML.load_file(records_path) rescue []
56
+ data = YAML.load_file(records_path) rescue []
56
57
 
57
- if data.is_a? Array
58
- data
59
- else
60
- []
58
+ if data.is_a? Array
59
+ data
60
+ else
61
+ []
62
+ end
61
63
  end
62
64
  end
63
65
  end
@@ -2,7 +2,7 @@ unless defined? MockServer::Store
2
2
  require 'mock_server/store/global'
3
3
  end
4
4
 
5
- module MockServer::Spec
5
+ module MockServer; module Spec
6
6
  module Helpers
7
7
  include MockServer::Store
8
8
 
@@ -298,4 +298,4 @@ module MockServer::Spec
298
298
  end
299
299
 
300
300
  end
301
- end
301
+ end; end
@@ -1,30 +1,32 @@
1
1
  require 'hashie'
2
2
 
3
- class MockServer::State < Hashie::Dash
4
- property :path, :default => 'fixtures/records'
5
- property :filename, :default => 'record'
6
- property :routes, :default => []
7
- property :record_filenames, :default => []
8
- property :matchers, :default => []
9
- property :verbose, :default => false
10
- property :requests_stack, :default => []
11
- property :success_stack, :default => []
12
- property :errors_stack, :default => []
13
- property :requests_stack, :default => []
14
- property :matcher_exceptions, :default => []
3
+ module MockServer
4
+ class State < Hashie::Dash
5
+ property :path, :default => 'fixtures/records'
6
+ property :filename, :default => 'record'
7
+ property :routes, :default => []
8
+ property :record_filenames, :default => []
9
+ property :matchers, :default => []
10
+ property :verbose, :default => false
11
+ property :requests_stack, :default => []
12
+ property :success_stack, :default => []
13
+ property :errors_stack, :default => []
14
+ property :requests_stack, :default => []
15
+ property :matcher_exceptions, :default => []
15
16
 
16
- def merge(hash)
17
- new_hash = self
18
- hash.each do |k,v|
19
- new_hash[k] = v
17
+ def merge(hash)
18
+ new_hash = self
19
+ hash.each do |k,v|
20
+ new_hash[k] = v
21
+ end
22
+ new_hash
20
23
  end
21
- new_hash
22
- end
23
24
 
24
- def merge!(hash)
25
- hash.each do |k,v|
26
- self[k] = v
25
+ def merge!(hash)
26
+ hash.each do |k,v|
27
+ self[k] = v
28
+ end
29
+ self
27
30
  end
28
- self
29
31
  end
30
32
  end
@@ -1,18 +1,20 @@
1
1
  require 'hashie'
2
2
 
3
- module MockServer::Store
4
- def mock_server_options_merge(opt = {})
5
- $mock_server_options ||= MockServer::State.new(opt)
6
- $mock_server_options.merge!(opt)
7
- end
3
+ module MockServer
4
+ module Store
5
+ def mock_server_options_merge(opt = {})
6
+ $mock_server_options ||= MockServer::State.new(opt)
7
+ $mock_server_options.merge!(opt)
8
+ end
8
9
 
9
- def mock_server_options_read
10
- $mock_server_options ||= MockServer::State.new
11
- $mock_server_options
12
- end
10
+ def mock_server_options_read
11
+ $mock_server_options ||= MockServer::State.new
12
+ $mock_server_options
13
+ end
13
14
 
14
- def mock_server_options_write(value)
15
- $mock_server_options ||= MockServer::State.new
16
- $mock_server_options = value
15
+ def mock_server_options_write(value)
16
+ $mock_server_options ||= MockServer::State.new
17
+ $mock_server_options = value
18
+ end
17
19
  end
18
20
  end
@@ -1,60 +1,62 @@
1
1
  require 'yaml'
2
2
  require 'json'
3
3
 
4
- module MockServer::Utils
5
- private
6
-
7
- def verbose(env)
8
- interception = lazy_match(@options[:routes], env["PATH_INFO"]) ? "intercepted!" : "NOT intercepted."
9
- puts %([MockServer] #{env["PATH_INFO"]} was #{interception}"\n)
10
- end
11
-
12
- def matchable_request?(env)
13
- @options[:routes] and lazy_match @options[:routes], env["PATH_INFO"]
14
- end
15
-
16
- def lazy_match(strings, path)
17
- regexps = strings.map { |str|
18
- escaped = Regexp.escape(str)
19
- escaped.gsub!('\\*\\*', '[\w|.|\-|\/]+')
20
- escaped.gsub!('\\*', '[\w|.|\-]+')
21
- Regexp.new("^#{escaped}$")
22
- }
23
-
24
- regexps.any? { |regex| regex.match(path) }
25
- end
26
-
27
- def hashified_request
28
- #rewind to ensure we read from the start
29
- @request.body.rewind
30
-
31
- #read body
32
- body = @request.body.read
33
-
34
- #rewind in case upstream expects it rewound
35
- @request.body.rewind
36
-
37
- json = JSON.parse(body) rescue body
38
-
39
- {
40
- :method => @request.request_method,
41
- :path => @request.path,
42
- :query => @request.query_string,
43
- :body => json
44
- }
45
- end
46
-
47
- def hashify_response(status, header, response)
48
- {
49
- :method => @request.request_method,
50
- :path => @request.path,
51
- :status => status,
52
- :headers => header,
53
- :body => if response.respond_to? :body
54
- response.body
55
- else
56
- response.join
57
- end
58
- }
4
+ module MockServer
5
+ module Utils
6
+ private
7
+
8
+ def verbose(env)
9
+ interception = lazy_match(@options[:routes], env["PATH_INFO"]) ? "intercepted!" : "NOT intercepted."
10
+ puts %([MockServer] #{env["PATH_INFO"]} was #{interception}"\n)
11
+ end
12
+
13
+ def matchable_request?(env)
14
+ @options[:routes] and lazy_match @options[:routes], env["PATH_INFO"]
15
+ end
16
+
17
+ def lazy_match(strings, path)
18
+ regexps = strings.map { |str|
19
+ escaped = Regexp.escape(str)
20
+ escaped.gsub!('\\*\\*', '[\w|.|\-|\/]+')
21
+ escaped.gsub!('\\*', '[\w|.|\-]+')
22
+ Regexp.new("^#{escaped}$")
23
+ }
24
+
25
+ regexps.any? { |regex| regex.match(path) }
26
+ end
27
+
28
+ def hashified_request
29
+ #rewind to ensure we read from the start
30
+ @request.body.rewind
31
+
32
+ #read body
33
+ body = @request.body.read
34
+
35
+ #rewind in case upstream expects it rewound
36
+ @request.body.rewind
37
+
38
+ json = JSON.parse(body) rescue body
39
+
40
+ {
41
+ :method => @request.request_method,
42
+ :path => @request.path,
43
+ :query => @request.query_string,
44
+ :body => json
45
+ }
46
+ end
47
+
48
+ def hashify_response(status, header, response)
49
+ {
50
+ :method => @request.request_method,
51
+ :path => @request.path,
52
+ :status => status,
53
+ :headers => header,
54
+ :body => if response.respond_to? :body
55
+ response.body
56
+ else
57
+ response.join
58
+ end
59
+ }
60
+ end
59
61
  end
60
62
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mock_server
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.2
4
+ version: 0.4.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: