mirage 2.0.1 → 2.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 2.0.1
1
+ 2.0.2
data/bin/mirage CHANGED
@@ -11,9 +11,9 @@ def start_mirage(args)
11
11
  puts "Starting Mirage"
12
12
 
13
13
  if windows?
14
- command = ["cmd", "/C", "start", "\"mirage server\"", RUBY_CMD, "#{File.dirname(__FILE__)}/../lib/start_mirage.rb"]
14
+ command = ["cmd", "/C", "start", "\"mirage server\"", RUBY_CMD, "#{File.dirname(__FILE__)}/../mirage_server.rb"]
15
15
  else
16
- command = [RUBY_CMD, "#{File.dirname(__FILE__)}/../lib/start_mirage.rb"]
16
+ command = [RUBY_CMD, "#{File.dirname(__FILE__)}/../mirage_server.rb"]
17
17
  end
18
18
  ChildProcess.build(*(command.concat(args))).start
19
19
  Mirage::Client.new "http://localhost:#{parse_options(ARGV)[:port]}/mirage"
@@ -24,7 +24,7 @@ def mirage_process_ids
24
24
  if windows?
25
25
  [`tasklist /V | findstr "mirage\\ server"`.split(' ')[1]].compact
26
26
  else
27
- ["Mirage Server", 'start_mirage'].collect do |process_name|
27
+ ["Mirage Server", 'mirage_server'].collect do |process_name|
28
28
  `ps aux | grep "#{process_name}" | grep -v grep`.split(' ')[1]
29
29
  end.find_all { |process_id| process_id != $$.to_s }.compact
30
30
  end
@@ -83,7 +83,7 @@ Feature: the Mirage client provides methods for setting responses and loading de
83
83
 
84
84
  When the file 'responses/default_greetings.rb' contains:
85
85
  """
86
- Mirage.prime do |mirage|
86
+ prime do |mirage|
87
87
  mirage.put('greeting', 'hello')
88
88
  mirage.put('leaving', 'goodbye')
89
89
  end
@@ -11,7 +11,7 @@ Feature: Mirage can be primed with a set of responses.
11
11
  Scenario: Mirage is started with the responses to be used for priming located in ./responses
12
12
  Given the file 'responses/default_greetings.rb' contains:
13
13
  """
14
- Mirage.prime do |mirage|
14
+ prime do |mirage|
15
15
  mirage.put('greeting', 'hello')
16
16
  mirage.put('leaving', 'goodbye')
17
17
  end
@@ -26,7 +26,7 @@ Feature: Mirage can be primed with a set of responses.
26
26
  Scenario: Mirage is started pointing with a relative path given for the responses directory
27
27
  Given the file './custom_responses_location/default_greetings.rb' contains:
28
28
  """
29
- Mirage.prime do |mirage|
29
+ prime do |mirage|
30
30
  mirage.put('greeting', 'hello')
31
31
  end
32
32
  """
@@ -38,7 +38,7 @@ Feature: Mirage can be primed with a set of responses.
38
38
  Scenario: Mirage is started pointing with a full path for the responses
39
39
  Given the file '/tmp/responses/default_greetings.rb' contains:
40
40
  """
41
- Mirage.prime do |mirage|
41
+ prime do |mirage|
42
42
  mirage.put('greeting', 'hello')
43
43
  end
44
44
  """
@@ -51,7 +51,7 @@ Feature: Mirage can be primed with a set of responses.
51
51
  Scenario: Priming mirage after its state has been modified
52
52
  Given the file 'responses/default_greetings.rb' contains:
53
53
  """
54
- Mirage.prime do |mirage|
54
+ prime do |mirage|
55
55
  mirage.put('greeting', 'hello')
56
56
  end
57
57
  """
@@ -85,11 +85,4 @@ Feature: Mirage can be primed with a set of responses.
85
85
  A file with a mistake in it
86
86
  """
87
87
  And I send PUT to 'http://localhost:7001/mirage/defaults'
88
- Then a 500 should be returned
89
-
90
-
91
-
92
-
93
-
94
-
95
-
88
+ Then a 500 should be returned
@@ -1,110 +1,118 @@
1
1
  module Mirage
2
- class MockResponsesCollection
3
- def initialize
4
- @snapshot, @responses = {}, {}
5
- end
2
+ class MockResponses
6
3
 
7
- def << response
4
+ class << self
8
5
 
6
+ def << response
9
7
 
10
- stored_responses = @responses[response.name]||={}
11
8
 
12
- stored_responses[response.pattern] ||= {}
13
- old_response = stored_responses[response.pattern].delete(response.http_method.upcase)
14
- stored_responses[response.pattern][response.http_method.upcase] = response
9
+ stored_responses = responses[response.name]||={}
15
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
16
14
 
17
- # 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
18
- response.response_id = old_response.response_id if old_response
19
- response.response_id.to_s
20
15
 
21
- end
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
22
19
 
23
- def get_response name, http_method, body, query_string
24
- stored_responses = @responses[name]
25
- record = nil
20
+ end
26
21
 
27
- record = find_response(body, query_string, stored_responses, http_method) if stored_responses
22
+ def get_response name, http_method, body, query_string
23
+ stored_responses = responses[name]
24
+ record = nil
28
25
 
26
+ record = find_response(body, query_string, stored_responses, http_method) if stored_responses
29
27
 
30
- unless record
31
- default_responses, record = find_default_responses(name), nil
32
28
 
33
- until record || default_responses.empty?
34
- record = find_response(body, query_string, default_responses.delete_at(0), http_method)
35
- if record
36
- record = record.default? ? record : nil
37
- end
29
+ unless record
30
+ default_responses, record = find_default_responses(name), nil
38
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
39
  end
40
- end
41
40
 
42
- record
43
- end
41
+ record
42
+ end
44
43
 
45
- def find id
46
- @responses.values.each do |response_sets|
47
- response_sets.values.each do |response_set|
48
- response_set.values.each do |response|
49
- return response if response.response_id == id
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
50
  end
51
51
  end
52
52
  end
53
- end
54
53
 
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
54
+ def delete(response_id)
55
+ responses.values.each do |response_sets|
56
+ response_sets.values.each do |response_set|
57
+ response_set.each do |method, response|
58
+ response_set.delete(method) if response.response_id == response_id
59
+ end
60
60
  end
61
61
  end
62
62
  end
63
- end
64
63
 
65
- def clear
66
- @responses.clear
67
- end
64
+ def clear
65
+ responses.clear
66
+ end
68
67
 
69
- def backup
70
- @snapshot.clear and @snapshot.replace(@responses.deep_clone)
71
- end
68
+ def backup
69
+ snapshot.clear and snapshot.replace(responses.deep_clone)
70
+ end
72
71
 
73
- def revert
74
- @responses.clear and @responses.replace(@snapshot.deep_clone)
75
- end
72
+ def revert
73
+ responses.clear and responses.replace(snapshot.deep_clone)
74
+ end
76
75
 
77
- def all
78
- responses = []
79
- @responses.values.each do |response_sets|
80
- response_sets.each do |pattern, response_set|
81
- response_set.values.each do |response|
82
- responses << response
76
+ def all
77
+ all_responses = []
78
+ responses.values.each do |response_sets|
79
+ response_sets.each do |pattern, response_set|
80
+ response_set.values.each do |response|
81
+ all_responses << response
82
+ end
83
83
  end
84
84
  end
85
+ all_responses
85
86
  end
86
- responses
87
- end
88
87
 
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) }
88
+ private
89
+ def find_response(body, query_string, stored_responses, http_method)
90
+ http_method = http_method.upcase
91
+ pattern_match = stored_responses.keys.find_all { |pattern| pattern != :basic }.find { |pattern| (body =~ pattern || query_string =~ pattern) }
93
92
 
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
93
+ if pattern_match
94
+ record = stored_responses[pattern_match][http_method]
95
+ else
96
+ record = stored_responses[:basic]
97
+ record = record[http_method] if record
98
+ end
99
+ record
99
100
  end
100
- record
101
- end
102
101
 
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
102
+ def find_default_responses(name)
103
+ matches = responses.keys.find_all { |key| name.index(key) == 0 }.sort { |a, b| b.length <=> a.length }
104
+ matches.collect { |key| responses[key] }
105
+ end
106
+
107
+ private
108
+ def responses
109
+ @responses ||={}
110
+ end
107
111
 
112
+ def snapshot
113
+ @snapshot ||={}
114
+ end
115
+ end
108
116
  end
109
-
117
+
110
118
  end
data/mirage.gemspec CHANGED
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{mirage}
8
- s.version = "2.0.1"
8
+ s.version = "2.0.2"
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"]
@@ -46,17 +46,16 @@ Gem::Specification.new do |s|
46
46
  "features/support/env.rb",
47
47
  "full_build.sh",
48
48
  "lib/mirage/client.rb",
49
- "lib/mirage/core.rb",
50
49
  "lib/mirage/mock_response.rb",
51
50
  "lib/mirage/mock_responses_collection.rb",
52
51
  "lib/mirage/object.rb",
53
- "lib/mirage/server.rb",
54
52
  "lib/mirage/util.rb",
55
53
  "lib/mirage/web.rb",
56
- "lib/start_mirage.rb",
57
- "lib/views/index.erb",
58
54
  "mirage.gemspec",
59
- "rakefile"
55
+ "mirage_server.rb",
56
+ "rakefile",
57
+ "test.rb",
58
+ "views/index.erb"
60
59
  ]
61
60
  s.homepage = %q{https://github.com/lashd/mirage}
62
61
  s.licenses = ["MIT"]
@@ -1,15 +1,38 @@
1
+ require 'rubygems'
2
+ $0='Mirage Server'
3
+ ROOT_DIR = File.dirname(__FILE__)
4
+ $LOAD_PATH.unshift("#{ROOT_DIR}/lib/mirage")
5
+
6
+ require 'client'
1
7
  require 'sinatra/base'
2
- require 'sinatra/reloader'
8
+ require 'object'
9
+ require 'mock_response'
10
+ require 'mock_responses_collection'
3
11
 
12
+ include Mirage::Util
13
+
4
14
 
5
15
  module Mirage
6
16
 
7
17
  class Server < Sinatra::Base
18
+
19
+ configure do
20
+ options = parse_options(ARGV)
21
+ set :defaults_directory, options[:defaults_directory]
22
+ set :port, options[:port]
23
+ set :show_exceptions, false
24
+ set :logging, true
25
+ set :server, 'webrick'
26
+ set :views, "#{ROOT_DIR}/views"
27
+
28
+ log_file = File.open('mirage.log', 'a')
29
+ log_file.sync=true
30
+ use Rack::CommonLogger, log_file
31
+ enable :logging
32
+ end
8
33
 
9
34
  REQUESTS= {}
10
35
 
11
- MOCK_RESPONSES = MockResponsesCollection.new
12
-
13
36
  put '/mirage/templates/*' do |name|
14
37
  response = request.body.read
15
38
 
@@ -18,13 +41,13 @@ module Mirage
18
41
 
19
42
  pattern = headers['HTTP_X_MIRAGE_PATTERN'] ? /#{headers['HTTP_X_MIRAGE_PATTERN']}/ : :basic
20
43
 
21
- MOCK_RESPONSES << 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'])
44
+ 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'])
22
45
  end
23
46
 
24
47
  ['get', 'post', 'delete', 'put'].each do |http_method|
25
48
  send(http_method, '/mirage/responses/*') do |name|
26
49
  body, query_string = Rack::Utils.unescape(request.body.read.to_s), request.env['QUERY_STRING']
27
- record = MOCK_RESPONSES.get_response(name, http_method, body, query_string)
50
+ record = MockResponses.get_response(name, http_method, body, query_string)
28
51
 
29
52
  return 404 unless record
30
53
  REQUESTS[record.response_id] = body.empty? ? query_string : body
@@ -36,7 +59,7 @@ module Mirage
36
59
 
37
60
  delete '/mirage/templates/:id' do
38
61
  response_id = params[:id].to_i
39
- MOCK_RESPONSES.delete(response_id)
62
+ MockResponses.delete(response_id)
40
63
  REQUESTS.delete(response_id)
41
64
  end
42
65
 
@@ -51,12 +74,12 @@ module Mirage
51
74
 
52
75
  delete '/mirage/templates' do
53
76
  [REQUESTS].each { |map| map.clear }
54
- MOCK_RESPONSES.clear
77
+ MockResponses.clear
55
78
  MockResponse.reset_count
56
79
  end
57
80
 
58
81
  get '/mirage/templates/:id' do
59
- response = MOCK_RESPONSES.find(params[:id].to_i)
82
+ response = MockResponses.find(params[:id].to_i)
60
83
  return 404 if response.is_a? Array
61
84
  send_response(response)
62
85
  end
@@ -69,7 +92,7 @@ module Mirage
69
92
  get '/mirage' do
70
93
  @responses = {}
71
94
 
72
- MOCK_RESPONSES.all.each do |response|
95
+ MockResponses.all.each do |response|
73
96
  pattern = response.pattern.is_a?(Regexp) ? "pattern = #{response.pattern.source}" : ''
74
97
  delay = response.delay > 0 ? "delay = #{response.delay}" : ''
75
98
  pattern << ' ,' unless pattern.empty? || delay.empty?
@@ -83,12 +106,12 @@ module Mirage
83
106
  end
84
107
 
85
108
  put '/mirage/defaults' do
86
- MOCK_RESPONSES.clear
109
+ MockResponses.clear
87
110
 
88
111
  Dir["#{settings.defaults_directory}/**/*.rb"].each do |default|
89
112
  begin
90
- load default
91
- rescue Exception
113
+ eval File.read(default)
114
+ rescue Exception => e
92
115
  raise "Unable to load default responses from: #{default}"
93
116
  end
94
117
 
@@ -96,16 +119,20 @@ module Mirage
96
119
  end
97
120
  #
98
121
  put '/mirage/backup' do
99
- MOCK_RESPONSES.backup
122
+ MockResponses.backup
100
123
  end
101
124
 
102
125
 
103
126
  put '/mirage' do
104
- MOCK_RESPONSES.revert
127
+ MockResponses.revert
105
128
  end
106
129
 
107
130
 
108
131
  helpers do
132
+
133
+ def prime &block
134
+ yield Mirage::Client.new "http://localhost:#{settings.port}/mirage"
135
+ end
109
136
 
110
137
  def response_value
111
138
  return request['response'] unless request['response'].nil?
@@ -117,4 +144,7 @@ module Mirage
117
144
  end
118
145
  end
119
146
  end
120
- end
147
+ end
148
+
149
+
150
+ Mirage::Server.run!
data/test.rb ADDED
@@ -0,0 +1,9 @@
1
+ require 'rubygems'
2
+ require 'mirage/client'
3
+
4
+
5
+ client = Mirage::Client.new
6
+ client.put('greeting', 'hello')
7
+ client.save
8
+ client.put('leaving', 'goodbye')
9
+
File without changes
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: mirage
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 2.0.1
5
+ version: 2.0.2
6
6
  platform: ruby
7
7
  authors:
8
8
  - Leon Davis
@@ -161,17 +161,16 @@ files:
161
161
  - features/support/env.rb
162
162
  - full_build.sh
163
163
  - lib/mirage/client.rb
164
- - lib/mirage/core.rb
165
164
  - lib/mirage/mock_response.rb
166
165
  - lib/mirage/mock_responses_collection.rb
167
166
  - lib/mirage/object.rb
168
- - lib/mirage/server.rb
169
167
  - lib/mirage/util.rb
170
168
  - lib/mirage/web.rb
171
- - lib/start_mirage.rb
172
- - lib/views/index.erb
173
169
  - mirage.gemspec
170
+ - mirage_server.rb
174
171
  - rakefile
172
+ - test.rb
173
+ - views/index.erb
175
174
  has_rdoc: true
176
175
  homepage: https://github.com/lashd/mirage
177
176
  licenses:
@@ -192,7 +191,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
192
191
  requirements:
193
192
  - - ">="
194
193
  - !ruby/object:Gem::Version
195
- hash: -2984992030017044050
194
+ hash: -2214189456461539388
196
195
  segments:
197
196
  - 0
198
197
  version: "0"
data/lib/mirage/server.rb DELETED
@@ -1,17 +0,0 @@
1
- $LOAD_PATH.unshift "#{File.dirname(__FILE__)}"
2
- require 'object'
3
- require 'mock_response'
4
- require 'mock_responses_collection'
5
- require 'core'
6
-
7
- module Mirage
8
- class << self
9
- def prime
10
- yield @client
11
- end
12
-
13
- def client= client
14
- @client = client
15
- end
16
- end
17
- end
data/lib/start_mirage.rb DELETED
@@ -1,34 +0,0 @@
1
- require 'rubygems'
2
- $0='Mirage Server'
3
- ROOT_DIR = File.dirname(__FILE__)
4
- $LOAD_PATH.unshift(ROOT_DIR)
5
-
6
-
7
- require 'sinatra'
8
- require 'mirage/server'
9
- require 'mirage/client'
10
-
11
- include Mirage::Util
12
-
13
- module Mirage
14
- class Server < Sinatra::Base
15
- configure do
16
-
17
- set :defaults_directory, parse_options(ARGV)[:defaults_directory]
18
- require 'logger'
19
- enable :logging
20
- log_file = File.open('mirage.log', 'a')
21
- log_file.sync=true
22
- use Rack::CommonLogger, log_file
23
- set :views, "#{ROOT_DIR}/views"
24
- set :show_exception, false
25
- set :raise_errors, false
26
- end
27
- end
28
- end
29
-
30
- options = parse_options(ARGV)
31
- Mirage.client = Mirage::Client.new "http://localhost:#{options[:port]}/mirage"
32
- Mirage::Server.run! :port => options[:port], :show_exceptions => false, :logging => true, :server => 'webrick'
33
-
34
-