mirage 2.0.7 → 2.0.8

Sign up to get free protection for your applications and to get access to all the features.
data/HISTORY CHANGED
@@ -1,2 +1,4 @@
1
+ V2.0.8 - tidy up code, still a lot to do :)
2
+ V2.0.7 - fix missing dependency on waitforit gem
1
3
  V2.0.1/2/3 - tidying up code
2
4
  V2.0.0 - Converted to restful interface
data/VERSION CHANGED
@@ -1 +1 @@
1
- 2.0.7
1
+ 2.0.8
@@ -24,7 +24,7 @@ Feature: The Mirage client can be used to snaphsot and rollback the Mirage serve
24
24
  Goodbye
25
25
  """
26
26
 
27
- And I send PUT to 'http://localhost:7001/mirage/set/greeting' with request entity
27
+ And I send PUT to 'http://localhost:7001/mirage/templates/greeting' with request entity
28
28
  """
29
29
  Changed
30
30
  """
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "mirage"
8
- s.version = "2.0.7"
8
+ s.version = "2.0.8"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Leon Davis"]
12
- s.date = "2011-12-13"
12
+ s.date = "2011-12-14"
13
13
  s.description = "Mirage aids testing of your applications by hosting mock responses so that your applications do not have to talk to real endpoints. Its accessible via HTTP and has a RESTful interface."
14
14
  s.executables = ["mirage"]
15
15
  s.extra_rdoc_files = [
@@ -53,8 +53,8 @@ Gem::Specification.new do |s|
53
53
  "rakefile",
54
54
  "server/extensions/object.rb",
55
55
  "server/mock_response.rb",
56
- "server/mock_responses.rb",
57
56
  "server/util.rb",
57
+ "test.rb",
58
58
  "views/index.erb"
59
59
  ]
60
60
  s.homepage = "https://github.com/lashd/mirage"
@@ -8,7 +8,6 @@ require 'sinatra/base'
8
8
 
9
9
  require 'extensions/object'
10
10
  require 'mock_response'
11
- require 'mock_responses'
12
11
  require 'util'
13
12
 
14
13
  require 'mirage/client'
@@ -44,13 +43,14 @@ module Mirage
44
43
 
45
44
  pattern = headers['HTTP_X_MIRAGE_PATTERN'] ? /#{headers['HTTP_X_MIRAGE_PATTERN']}/ : :basic
46
45
 
47
- MockResponses << MockResponse.new(name, response, headers['CONTENT_TYPE'], http_method, pattern, headers['HTTP_X_MIRAGE_DELAY'].to_f, headers['HTTP_X_MIRAGE_DEFAULT'], headers['HTTP_X_MIRAGE_FILE'])
46
+ mock_response = MockResponse.new(name, response, headers['CONTENT_TYPE'], http_method, pattern, headers['HTTP_X_MIRAGE_DELAY'].to_f, headers['HTTP_X_MIRAGE_DEFAULT'], headers['HTTP_X_MIRAGE_FILE'])
47
+ mock_response.response_id.to_s
48
48
  end
49
49
 
50
50
  ['get', 'post', 'delete', 'put'].each do |http_method|
51
51
  send(http_method, '/mirage/responses/*') do |name|
52
- body, query_string = Rack::Utils.unescape(request.body.read.to_s), request.env['QUERY_STRING']
53
- record = MockResponses.get_response(name, http_method, body, query_string)
52
+ body, query_string = Rack::Utils.unescape(request.body.read.to_s), request.query_string
53
+ record = MockResponse.get_response(name, http_method, body, query_string)
54
54
 
55
55
  return 404 unless record
56
56
  REQUESTS[record.response_id] = body.empty? ? query_string : body
@@ -61,31 +61,30 @@ module Mirage
61
61
  end
62
62
 
63
63
  delete '/mirage/templates/:id' do
64
- MockResponses.delete(response_id)
64
+ MockResponse.delete(response_id)
65
65
  REQUESTS.delete(response_id)
66
- ""
66
+ 200
67
67
  end
68
68
 
69
69
  delete '/mirage/requests' do
70
70
  REQUESTS.clear
71
- ""
71
+ 200
72
72
  end
73
73
 
74
74
  delete '/mirage/requests/:id' do
75
75
  REQUESTS.delete(response_id)
76
- ""
76
+ 200
77
77
  end
78
78
 
79
79
 
80
80
  delete '/mirage/templates' do
81
81
  [REQUESTS].each { |map| map.clear }
82
- MockResponses.clear
83
- MockResponse.reset_count
84
- ""
82
+ MockResponse.clear
83
+ 200
85
84
  end
86
85
 
87
86
  get '/mirage/templates/:id' do
88
- response = MockResponses.find(response_id)
87
+ response = MockResponse.find(response_id)
89
88
  return 404 if response.is_a? Array
90
89
  send_response(response)
91
90
  end
@@ -98,7 +97,7 @@ module Mirage
98
97
  get '/mirage' do
99
98
  @responses = {}
100
99
 
101
- MockResponses.all.each do |response|
100
+ MockResponse.all.each do |response|
102
101
  pattern = response.pattern.is_a?(Regexp) ? "pattern = #{response.pattern.source}" : ''
103
102
  delay = response.delay > 0 ? "delay = #{response.delay}" : ''
104
103
  pattern << ' ,' unless pattern.empty? || delay.empty?
@@ -107,12 +106,9 @@ module Mirage
107
106
  erb :index
108
107
  end
109
108
 
110
- error do
111
- erb request.env['sinatra.error'].message
112
- end
113
109
 
114
110
  put '/mirage/defaults' do
115
- MockResponses.clear
111
+ MockResponse.clear
116
112
 
117
113
  Dir["#{settings.defaults_directory}/**/*.rb"].each do |default|
118
114
  begin
@@ -120,21 +116,24 @@ module Mirage
120
116
  rescue Exception => e
121
117
  raise "Unable to load default responses from: #{default}"
122
118
  end
123
-
124
119
  end
120
+ 200
125
121
  end
126
122
  #
127
123
  put '/mirage/backup' do
128
- MockResponses.backup
129
- ""
124
+ MockResponse.backup
125
+ 200
130
126
  end
131
127
 
132
128
 
133
129
  put '/mirage' do
134
- MockResponses.revert
135
- ""
130
+ MockResponse.revert
131
+ 200
136
132
  end
137
133
 
134
+ error do
135
+ erb request.env['sinatra.error'].message
136
+ end
138
137
 
139
138
  helpers do
140
139
 
@@ -1,15 +1,117 @@
1
1
  module Mirage
2
2
  class MockResponse
3
- @@id_count = 0
3
+ class << self
4
+
5
+ def add response
6
+ stored_response_sets = responses[response.name]||={}
7
+ stored_response_set = (stored_response_sets[response.pattern] ||= {})
8
+
9
+ old_response = stored_response_set.delete(response.http_method)
10
+ stored_response_set[response.http_method] = response
11
+
12
+ response.response_id = old_response ? old_response.response_id : next_id
13
+ end
14
+
15
+
16
+ def get_response name, http_method, body, query_string
17
+ find_response(body, query_string, responses[name], http_method) || default_response(body, http_method, name, query_string)
18
+ end
19
+
20
+ def find id
21
+ responses.values.each do |response_sets|
22
+ response_sets.values.each do |response_set|
23
+ response_set.values.each do |response|
24
+ return response if response.response_id == id
25
+ end
26
+ end
27
+ end
28
+ end
29
+
30
+ def delete(response_id)
31
+ responses.values.each do |response_sets|
32
+ response_sets.values.each do |response_set|
33
+ response_set.each do |method, response|
34
+ response_set.delete(method) if response.response_id == response_id
35
+ end
36
+ end
37
+ end
38
+ end
39
+
40
+ def clear
41
+ responses.clear
42
+ @next_id = 0
43
+ end
44
+
45
+ def backup
46
+ snapshot.clear and snapshot.replace(responses.deep_clone)
47
+ end
48
+
49
+ def revert
50
+ responses.clear and responses.replace(snapshot.deep_clone)
51
+ end
52
+
53
+ def all
54
+ all_responses = []
55
+ responses.values.each do |response_sets|
56
+ response_sets.values.each do |response_set|
57
+ response_set.values.each do |response|
58
+ all_responses << response
59
+ end
60
+ end
61
+ end
62
+ all_responses
63
+ end
64
+
65
+ def default_response(body, http_method, name, query_string)
66
+ default_response_sets = find_default_responses(name)
67
+
68
+ until default_response_sets.empty?
69
+ record = find_response(body, query_string, default_response_sets.delete_at(0), http_method)
70
+ return record if record && record.default?
71
+ end
72
+ end
73
+
74
+ private
75
+ def find_response(body, query_string, response_set, http_method)
76
+ return unless response_set
77
+ http_method = http_method.upcase
78
+ pattern_match = response_set.keys.find_all { |pattern| pattern != :basic }.find { |pattern| (body =~ pattern || query_string =~ pattern) }
79
+
80
+ if pattern_match
81
+ record = response_set[pattern_match][http_method]
82
+ else
83
+ record = response_set[:basic]
84
+ record = record[http_method] if record
85
+ end
86
+ record
87
+ end
88
+
89
+ def find_default_responses(name)
90
+ matches = responses.keys.find_all { |key| name.index(key) == 0 }.sort { |a, b| b.length <=> a.length }
91
+ matches.collect { |key| responses[key] }
92
+ end
93
+
94
+ def responses
95
+ @responses ||={}
96
+ end
97
+
98
+ def snapshot
99
+ @snapshot ||={}
100
+ end
101
+
102
+ def next_id
103
+ @next_id||= 0
104
+ @next_id+=1
105
+ end
106
+
107
+ end
108
+
4
109
  attr_reader :response_id, :delay, :name, :pattern, :http_method, :content_type
5
110
  attr_accessor :response_id
6
111
 
7
112
  def initialize name, value, content_type, http_method, pattern=nil, delay=0, default=false, file=false
8
- @name, @value,@content_type, @http_method, @pattern, @response_id, @delay, @default, @file = name, value, content_type, http_method, pattern, @@id_count+=1, delay, default, file
9
- end
10
-
11
- def self.reset_count
12
- @@id_count = 0
113
+ @name, @value, @content_type, @http_method, @pattern, @delay, @default, @file = name, value, content_type, http_method.to_s.upcase, pattern, delay, default, file
114
+ MockResponse.add self
13
115
  end
14
116
 
15
117
  def default?
@@ -17,7 +119,7 @@ module Mirage
17
119
  end
18
120
 
19
121
  def file?
20
- @file == 'true'
122
+ 'true' == @file
21
123
  end
22
124
 
23
125
 
@@ -46,4 +148,4 @@ module Mirage
46
148
  string.scan(/#{regex}/).flatten.first
47
149
  end
48
150
  end
49
- end
151
+ end
data/test.rb ADDED
@@ -0,0 +1,8 @@
1
+ hash = Hash.new do |hash, key|
2
+ hash[key] = Hash.new do |patterns_hash, pattern|
3
+ patterns_hash[pattern] = {:blah => 'foo'}
4
+ end
5
+ end
6
+ hash = hash['thing']['whatever']
7
+
8
+ puts hash
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mirage
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.7
4
+ version: 2.0.8
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-12-13 00:00:00.000000000Z
12
+ date: 2011-12-14 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: sinatra
16
- requirement: &19381300 !ruby/object:Gem::Requirement
16
+ requirement: &21161500 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *19381300
24
+ version_requirements: *21161500
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: childprocess
27
- requirement: &19380760 !ruby/object:Gem::Requirement
27
+ requirement: &21160920 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ~>
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '0.1'
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *19380760
35
+ version_requirements: *21160920
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: waitforit
38
- requirement: &19380120 !ruby/object:Gem::Requirement
38
+ requirement: &21160360 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: '0'
44
44
  type: :runtime
45
45
  prerelease: false
46
- version_requirements: *19380120
46
+ version_requirements: *21160360
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: rake
49
- requirement: &19379560 !ruby/object:Gem::Requirement
49
+ requirement: &21159720 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ! '>='
@@ -54,10 +54,10 @@ dependencies:
54
54
  version: '0'
55
55
  type: :development
56
56
  prerelease: false
57
- version_requirements: *19379560
57
+ version_requirements: *21159720
58
58
  - !ruby/object:Gem::Dependency
59
59
  name: cucumber
60
- requirement: &19378960 !ruby/object:Gem::Requirement
60
+ requirement: &21159100 !ruby/object:Gem::Requirement
61
61
  none: false
62
62
  requirements:
63
63
  - - ! '>='
@@ -65,10 +65,10 @@ dependencies:
65
65
  version: '0'
66
66
  type: :development
67
67
  prerelease: false
68
- version_requirements: *19378960
68
+ version_requirements: *21159100
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: rspec
71
- requirement: &19378180 !ruby/object:Gem::Requirement
71
+ requirement: &21158480 !ruby/object:Gem::Requirement
72
72
  none: false
73
73
  requirements:
74
74
  - - =
@@ -76,10 +76,10 @@ dependencies:
76
76
  version: 2.6.0
77
77
  type: :development
78
78
  prerelease: false
79
- version_requirements: *19378180
79
+ version_requirements: *21158480
80
80
  - !ruby/object:Gem::Dependency
81
81
  name: jeweler
82
- requirement: &19377440 !ruby/object:Gem::Requirement
82
+ requirement: &21157860 !ruby/object:Gem::Requirement
83
83
  none: false
84
84
  requirements:
85
85
  - - ~>
@@ -87,10 +87,10 @@ dependencies:
87
87
  version: 1.5.2
88
88
  type: :development
89
89
  prerelease: false
90
- version_requirements: *19377440
90
+ version_requirements: *21157860
91
91
  - !ruby/object:Gem::Dependency
92
92
  name: sinatra-contrib
93
- requirement: &19376420 !ruby/object:Gem::Requirement
93
+ requirement: &21154260 !ruby/object:Gem::Requirement
94
94
  none: false
95
95
  requirements:
96
96
  - - ! '>='
@@ -98,10 +98,10 @@ dependencies:
98
98
  version: '0'
99
99
  type: :development
100
100
  prerelease: false
101
- version_requirements: *19376420
101
+ version_requirements: *21154260
102
102
  - !ruby/object:Gem::Dependency
103
103
  name: mechanize
104
- requirement: &19372420 !ruby/object:Gem::Requirement
104
+ requirement: &21153660 !ruby/object:Gem::Requirement
105
105
  none: false
106
106
  requirements:
107
107
  - - =
@@ -109,10 +109,10 @@ dependencies:
109
109
  version: 1.0.0
110
110
  type: :development
111
111
  prerelease: false
112
- version_requirements: *19372420
112
+ version_requirements: *21153660
113
113
  - !ruby/object:Gem::Dependency
114
114
  name: gemcutter
115
- requirement: &19371840 !ruby/object:Gem::Requirement
115
+ requirement: &21153000 !ruby/object:Gem::Requirement
116
116
  none: false
117
117
  requirements:
118
118
  - - ! '>='
@@ -120,10 +120,10 @@ dependencies:
120
120
  version: '0'
121
121
  type: :development
122
122
  prerelease: false
123
- version_requirements: *19371840
123
+ version_requirements: *21153000
124
124
  - !ruby/object:Gem::Dependency
125
125
  name: nokogiri
126
- requirement: &19371180 !ruby/object:Gem::Requirement
126
+ requirement: &21152460 !ruby/object:Gem::Requirement
127
127
  none: false
128
128
  requirements:
129
129
  - - =
@@ -131,7 +131,7 @@ dependencies:
131
131
  version: 1.4.7
132
132
  type: :development
133
133
  prerelease: false
134
- version_requirements: *19371180
134
+ version_requirements: *21152460
135
135
  description: Mirage aids testing of your applications by hosting mock responses so
136
136
  that your applications do not have to talk to real endpoints. Its accessible via
137
137
  HTTP and has a RESTful interface.
@@ -179,8 +179,8 @@ files:
179
179
  - rakefile
180
180
  - server/extensions/object.rb
181
181
  - server/mock_response.rb
182
- - server/mock_responses.rb
183
182
  - server/util.rb
183
+ - test.rb
184
184
  - views/index.erb
185
185
  homepage: https://github.com/lashd/mirage
186
186
  licenses:
@@ -199,7 +199,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
199
199
  version: '0'
200
200
  segments:
201
201
  - 0
202
- hash: 79940138610282887
202
+ hash: 2115174176244193400
203
203
  required_rubygems_version: !ruby/object:Gem::Requirement
204
204
  none: false
205
205
  requirements:
@@ -1,119 +0,0 @@
1
- module Mirage
2
- class MockResponses
3
-
4
- class << self
5
-
6
- def << response
7
-
8
-
9
- stored_responses = responses[response.name]||={}
10
-
11
- stored_responses[response.pattern] ||= {}
12
- old_response = stored_responses[response.pattern].delete(response.http_method.upcase)
13
- stored_responses[response.pattern][response.http_method.upcase] = response
14
-
15
-
16
- # Right not an the main id count goes up by one even if the id is not used because the old id is reused from another response
17
- response.response_id = old_response.response_id if old_response
18
- response.response_id.to_s
19
-
20
- end
21
-
22
- def get_response name, http_method, body, query_string
23
- stored_responses = responses[name]
24
- record = nil
25
-
26
- record = find_response(body, query_string, stored_responses, http_method) if stored_responses
27
-
28
-
29
- unless record
30
- default_responses, record = find_default_responses(name), nil
31
-
32
- until record || default_responses.empty?
33
- record = find_response(body, query_string, default_responses.delete_at(0), http_method)
34
- if record
35
- record = record.default? ? record : nil
36
- end
37
-
38
- end
39
- end
40
-
41
- record
42
- end
43
-
44
- def find id
45
- responses.values.each do |response_sets|
46
- response_sets.values.each do |response_set|
47
- response_set.values.each do |response|
48
- return response if response.response_id == id
49
- end
50
- end
51
- end
52
-
53
- end
54
-
55
- def delete(response_id)
56
- responses.values.each do |response_sets|
57
- response_sets.values.each do |response_set|
58
- response_set.each do |method, response|
59
- response_set.delete(method) if response.response_id == response_id
60
- end
61
- end
62
- end
63
- end
64
-
65
- def clear
66
- responses.clear
67
- end
68
-
69
- def backup
70
- snapshot.clear and snapshot.replace(responses.deep_clone)
71
- end
72
-
73
- def revert
74
- responses.clear and responses.replace(snapshot.deep_clone)
75
- end
76
-
77
- def all
78
- all_responses = []
79
- responses.values.each do |response_sets|
80
- response_sets.each do |pattern, response_set|
81
- response_set.values.each do |response|
82
- all_responses << response
83
- end
84
- end
85
- end
86
- all_responses
87
- end
88
-
89
- private
90
- def find_response(body, query_string, stored_responses, http_method)
91
- http_method = http_method.upcase
92
- pattern_match = stored_responses.keys.find_all { |pattern| pattern != :basic }.find { |pattern| (body =~ pattern || query_string =~ pattern) }
93
-
94
- if pattern_match
95
- record = stored_responses[pattern_match][http_method]
96
- else
97
- record = stored_responses[:basic]
98
- record = record[http_method] if record
99
- end
100
- record
101
- end
102
-
103
- def find_default_responses(name)
104
- matches = responses.keys.find_all { |key| name.index(key) == 0 }.sort { |a, b| b.length <=> a.length }
105
- matches.collect { |key| responses[key] }
106
- end
107
-
108
- private
109
- def responses
110
- @responses ||={}
111
- end
112
-
113
- def snapshot
114
- @snapshot ||={}
115
- end
116
- end
117
- end
118
-
119
- end