mirage 1.1.0 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -10,7 +10,7 @@ I hope you find it useful,
10
10
 
11
11
  Leon
12
12
 
13
- P.s. Currently Mirage only runs on Linux; Rubies 1.8, 1.9 and JRuby. I plan to add support for Windows and MacOsX very soon.
13
+ P.s. Mirage runs on Linux,MacOSX and Windows; Rubies 1.8, 1.9 and JRuby.
14
14
 
15
15
  Installation
16
16
  ------------
data/bin/mirage CHANGED
@@ -2,28 +2,46 @@
2
2
  require 'rubygems'
3
3
  $LOAD_PATH.unshift("#{File.dirname(__FILE__)}/../lib")
4
4
  require 'mirage'
5
+ require 'childprocess'
5
6
  include Mirage::Util
6
- RUBY_CMD = RUBY_PLATFORM == 'JAVA' ? 'jruby' : 'ruby'
7
+ RUBY_CMD = RUBY_PLATFORM == 'java' ? 'jruby' : 'ruby'
7
8
 
8
9
 
9
10
  def start_mirage(args, mirage)
10
11
  puts "Starting Mirage"
11
- system "(#{RUBY_CMD} #{File.dirname(__FILE__)}/../lib/start_mirage.rb #{args.join(' ')}) > /dev/null 2>&1 &"
12
+
13
+ if windows?
14
+ command = ["cmd", "/C", "start", "\"mirage server\"", RUBY_CMD, "#{File.dirname(__FILE__)}/../lib/start_mirage.rb"]
15
+ else
16
+ command = [RUBY_CMD, "#{File.dirname(__FILE__)}/../lib/start_mirage.rb"]
17
+ end
18
+ ChildProcess.build(*(command.concat(args))).start
19
+
12
20
  wait_until do
13
21
  mirage.running?
14
22
  end
15
23
  end
16
24
 
17
- def mirage_process_ids
18
- process_ids = []
19
- process_ids << `ps aux | grep "Mirage Server" | grep -v grep`.split(' ')[1]
20
- process_ids << `ps aux | grep "start_mirage" | grep -v grep`.split(' ')[1]
21
- return process_ids.compact
25
+
26
+ def stop_mirage
27
+ if windows?
28
+ process_id = `tasklist /V | findstr "mirage\\ server"`.split(' ')[1]
29
+ `taskkill /F /T /PID #{process_id}` if process_id
30
+ else
31
+ begin
32
+ ["Mirage Server", 'mirage start', 'start_mirage'].each do |process_name|
33
+ process_id = `ps aux | grep "#{process_name}" | grep -v grep`.split(' ')[1]
34
+ system "kill -9 #{process_id}" if process_id
35
+ end
36
+ rescue
37
+ puts 'Mirage is not running'
38
+ end
39
+ end
22
40
  end
23
41
 
24
42
  if ARGV.include?('start')
25
43
 
26
- options = parse_options(ARGV)
44
+ options = parse_options(ARGV)
27
45
  mirage_client = Mirage::Client.new "http://localhost:#{options[:port]}/mirage"
28
46
 
29
47
  if mirage_client.running?
@@ -38,17 +56,9 @@ if ARGV.include?('start')
38
56
  puts "WARN: #{e.message}"
39
57
  end
40
58
 
41
-
42
59
  elsif ARGV.include?('stop')
43
- puts "Stoping Mirage"
44
- begin
45
- mirage_process_ids.each do |id|
46
- puts "killing #{id}"
47
- `kill -9 #{id}`
48
- end
49
- rescue
50
- puts 'Mirage is not running'
51
- end
60
+ puts "Stopping Mirage"
61
+ stop_mirage
52
62
  else
53
63
  parse_options ['--help']
54
64
  exit 1
@@ -73,7 +73,7 @@ Feature: the Mirage client provides methods for setting responses and loading de
73
73
  Scenario: Setting a file as a response
74
74
  Given I run
75
75
  """
76
- Mirage::Client.new.set('download', File.open('features/resources/test.zip'))
76
+ Mirage::Client.new.set('download', File.open('README.md'))
77
77
  """
78
78
  When I hit 'http://localhost:7001/mirage/get/download'
79
- Then the response should be a file the same as 'features/resources/test.zip'
79
+ Then the response should be a file the same as 'README.md'
@@ -28,7 +28,7 @@ Feature: Mirage is started from the command line.
28
28
 
29
29
 
30
30
  Scenario: Stopping Mirage
31
- Given I run 'mirage start'
31
+ Given Mirage is running
32
32
  When I run 'mirage stop'
33
33
  Then Connection should be refused to 'http://localhost:7001/mirage'
34
34
 
@@ -1,9 +1,8 @@
1
1
  Feature: Mirage can also be used to host files.
2
2
 
3
- #TODO - generate zip file or move resources directory
4
3
  Scenario: A file is set as a response
5
4
  Given I hit 'http://localhost:7001/mirage/set/some/location/download' with parameters:
6
- | response | features/resources/test.zip |
5
+ | response | README.md |
7
6
 
8
7
  When I hit 'http://localhost:7001/mirage/get/some/location/download'
9
- Then the response should be a file the same as 'features/resources/test.zip'
8
+ Then the response should be a file the same as 'README.md'
@@ -4,4 +4,4 @@ Feature: Output from Mirage is stored in mirage.log.
4
4
  Scenario: response is set.
5
5
  Given I post to 'http://localhost:7001/mirage/set/greeting' with parameters:
6
6
  | response | Hello |
7
- Then 'mirage.log' should contain '/mirage/set/greeting?response=Hello'
7
+ Then mirage.log should contain '/mirage/set/greeting?response=Hello'
@@ -12,9 +12,10 @@ Feature: If you want to see the content of a particular response without trigger
12
12
 
13
13
  Scenario: Peeking a file based response
14
14
  Given I hit 'http://localhost:7001/mirage/set/download' with parameters:
15
- | response | features/resources/test.zip |
15
+ | response | README.md |
16
+
16
17
  When I hit 'http://localhost:7001/mirage/peek/1'
17
- Then the response should be a file the same as 'features/resources/test.zip'
18
+ Then the response should be a file the same as 'README.md'
18
19
 
19
20
 
20
21
  Scenario: Peeking a response that does not exist
@@ -37,7 +37,7 @@ end
37
37
 
38
38
  Given /^I run '(.*)'$/ do |command|
39
39
  path = ENV['mode'] == 'regression' ? '' : "../bin/"
40
- @commandline_output = normalise(IO.popen("export RUBYOPT='' && cd #{SCRATCH} && #{path}#{command}").read)
40
+ @commandline_output = normalise(run("#{path}#{command}"))
41
41
  end
42
42
 
43
43
  Given /^Mirage (is|is not) running$/ do |running|
@@ -62,9 +62,9 @@ Given /^the file '(.*)' contains:$/ do |file_path, content|
62
62
  file_path = "#{SCRATCH}/#{file_path}" unless file_path =~ /^\//
63
63
 
64
64
  FileUtils.rm_rf(file_path) if File.exists?(file_path)
65
- directory = File.dirname(file_path)
66
- FileUtils.mkdir_p(directory)
67
- file = File.new("#{directory}/#{File.basename(file_path)}", 'w')
65
+ FileUtils.mkdir_p(File.dirname(file_path))
66
+
67
+ file = File.new("#{file_path}", 'w')
68
68
  file.write(content)
69
69
  file.close
70
70
  end
@@ -94,7 +94,7 @@ When /^I (hit|get|post to) '(http:\/\/localhost:7001\/mirage\/(.*?))' with param
94
94
  parameters = {}
95
95
  table.raw.each do |row|
96
96
  parameter, value = row[0].to_sym, row[1]
97
- value = File.exists?(value) ? File.open(value) : value
97
+ value = File.exists?(value) ? File.open(value, 'rb') : value
98
98
  parameters[parameter]=value
99
99
  end
100
100
 
@@ -113,15 +113,19 @@ Then /^'(.*)' should exist$/ do |path|
113
113
  File.exists?("#{SCRATCH}/#{path}").should == true
114
114
  end
115
115
 
116
- Then /^'(.*)' should contain '(.*)'$/ do |file, content|
117
- fail("#{content} not found in: #{File.read(file)}") unless File.read("#{SCRATCH}/#{file}").index(content)
116
+ Then /^mirage.log should contain '(.*)'$/ do |content|
117
+ log_file_content = @mirage_log_file.readlines.to_s
118
+ fail("#{content} not found in mirage.log: #{log_file_content}") unless log_file_content.index(content)
118
119
  end
120
+
119
121
  Given /^I goto '(.*)'$/ do |url|
120
122
  @page = Mechanize.new.get url
121
123
  end
124
+
122
125
  Then /^I should see '(.*)'$/ do |text|
123
126
  @page.body.index(text).should_not == nil
124
127
  end
128
+
125
129
  When /^I click '(.*)'$/ do |thing|
126
- @page = @page.links.find{|link| link.attributes['id'] == thing}.click
130
+ @page = @page.links.find { |link| link.attributes['id'] == thing }.click
127
131
  end
@@ -1,7 +1,5 @@
1
1
  $LOAD_PATH.unshift("#{File.dirname(__FILE__)}/../../lib")
2
2
  require 'rubygems'
3
- #require 'bundler/setup'
4
- #Bundler.setup(:test)
5
3
  require 'mirage'
6
4
  require 'cucumber'
7
5
  require 'rspec'
@@ -9,6 +7,16 @@ require 'mechanize'
9
7
 
10
8
  SCRATCH = './scratch'
11
9
  RUBY_CMD = RUBY_PLATFORM == 'JAVA' ? 'jruby' : 'ruby'
10
+ $log_file_marker = 0
11
+
12
+
13
+ module CommandLine
14
+ def execute command
15
+ command_line_output_path = "#{SCRATCH}/commandline_output.txt"
16
+ system "cd #{SCRATCH} && #{command} > #{File.basename(command_line_output_path)}"
17
+ File.read(command_line_output_path)
18
+ end
19
+ end
12
20
 
13
21
 
14
22
  module Web
@@ -22,7 +30,7 @@ module Web
22
30
 
23
31
  def hit_mirage(url, parameters={})
24
32
  start_time = Time.now
25
- file = parameters.values.find{|value| value.is_a?(File)}
33
+ file = parameters.values.find { |value| value.is_a?(File) }
26
34
  response = (file ? http_post(url, parameters) : http_get(url, parameters))
27
35
  @response_time = Time.now - start_time
28
36
  response
@@ -35,34 +43,36 @@ end
35
43
 
36
44
 
37
45
  module Regression
46
+ include CommandLine
47
+
38
48
  def stop_mirage
39
- system "export RUBYOPT='' && cd #{SCRATCH} && mirage stop"
49
+ system "cd #{SCRATCH} && mirage stop"
40
50
  end
41
51
 
42
52
  def start_mirage
43
- system "truncate mirage.log --size 0"
44
- system "export RUBYOPT='' && cd #{SCRATCH} && mirage start"
53
+ system "cd #{SCRATCH} && mirage start"
54
+ end
55
+
56
+ def run command
57
+ execute(command)
45
58
  end
46
59
  end
47
60
 
48
61
  module IntelliJ
62
+ include CommandLine
49
63
  include Mirage::Util
50
64
 
51
65
  def stop_mirage
52
- system "cd #{SCRATCH} && ../bin/mirage stop"
53
- wait_until do
54
- !$mirage.running?
55
- end
66
+ system "cd #{SCRATCH} && #{RUBY_CMD} ../bin/mirage stop"
56
67
  end
57
68
 
58
69
  def start_mirage
59
70
  puts "starting mirage"
60
- system "truncate mirage.log --size 0"
61
- system "cd #{SCRATCH} && ../bin/mirage start"
71
+ system "cd #{SCRATCH} && #{RUBY_CMD} ../bin/mirage start"
72
+ end
62
73
 
63
- wait_until do
64
- $mirage.running?
65
- end
74
+ def run command
75
+ execute "#{RUBY_CMD} #{command}"
66
76
  end
67
77
  end
68
78
 
@@ -74,17 +84,21 @@ World(Web)
74
84
  Before do
75
85
  FileUtils.mkdir_p(SCRATCH)
76
86
  $mirage = Mirage::Client.new
77
-
78
87
  if $mirage.running?
79
88
  $mirage.clear
80
89
  else
81
90
  start_mirage
82
91
  end
83
-
84
- system "cd #{SCRATCH}/ && ls | grep -v mirage.log | xargs rm -rf"
85
- system "truncate -s 0 #{SCRATCH}/mirage.log"
92
+
93
+ Dir["#{SCRATCH}/*"].each do |file|
94
+ FileUtils.rm_rf(file) unless file == "#{SCRATCH}/mirage.log"
95
+ end
96
+
97
+ @mirage_log_file = File.open("#{SCRATCH}/mirage.log")
98
+ @mirage_log_file.seek(0,IO::SEEK_END)
86
99
  end
87
100
 
101
+
88
102
  at_exit do
89
- stop_mirage
90
- end
103
+ stop_mirage if $mirage.running?
104
+ end
data/lib/mirage/client.rb CHANGED
@@ -22,6 +22,7 @@ module Mirage
22
22
 
23
23
  class Client
24
24
  include ::Mirage::Web
25
+ attr_reader :url
25
26
 
26
27
  # Creates an instance of the MIrage client that can be used to interact with the Mirage Server
27
28
  #
@@ -58,7 +59,7 @@ module Mirage
58
59
  # Client.set('greeting', 'hello', :pattern => 'regex or plain text':)
59
60
  # Client.set('greeting', 'hello', :delay => 5) # number of seconds
60
61
  def set endpoint, response, params={}
61
- params[:response] = response
62
+ params[:response] = response.is_a?(File) ? File.open(response.path, 'rb') : response
62
63
  response(http_post("#{@url}/set/#{endpoint}", params))
63
64
  end
64
65
 
data/lib/mirage/core.rb CHANGED
@@ -91,6 +91,7 @@ module Mirage
91
91
  pattern = request['pattern'] ? /#{request['pattern']}/ : :basic
92
92
  name = args.join('/')
93
93
  is_default = request['default'] == 'true'
94
+ the_request = request
94
95
 
95
96
  response = MockResponse.new(name, response_value, pattern, delay.to_f, is_default)
96
97
 
@@ -196,7 +197,8 @@ module Mirage
196
197
  def send_response(response, body='', request={}, query_string='')
197
198
  if response.file?
198
199
  tempfile, filename, type = response.value.values_at(:tempfile, :filename, :type)
199
- send_file(tempfile.path, type, "Content-Disposition: attachment; filename=#{filename}")
200
+ tempfile.binmode
201
+ send_file(tempfile.path, type, "Content-Length: #{tempfile.size}; Content-Disposition: attachment; filename=#{filename}")
200
202
  else
201
203
  response.value(body, request, query_string)
202
204
  end
data/lib/mirage/util.rb CHANGED
@@ -2,7 +2,7 @@ require 'optparse'
2
2
  module Mirage
3
3
  module Util
4
4
 
5
- def wait_until time=30
5
+ def wait_until time=45
6
6
  start_time = Time.now
7
7
  until Time.now >= start_time + time
8
8
  sleep 0.1
@@ -28,13 +28,16 @@ module Mirage
28
28
  begin
29
29
  opt_parser.parse args
30
30
  rescue
31
- puts "mirage start|stop [OPTIONS]"
32
31
  puts opt_parser
33
32
  exit 1
34
33
  end
35
34
 
36
35
  options
37
36
  end
37
+
38
+ def windows?
39
+ ENV['OS'] == 'Windows_NT'
40
+ end
38
41
  end
39
42
 
40
43
  end
data/lib/start_mirage.rb CHANGED
@@ -1,9 +1,10 @@
1
1
  require 'rubygems'
2
2
  $0='Mirage Server'
3
- $LOAD_PATH.unshift("#{File.dirname(__FILE__)}/../lib")
3
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
4
4
  require 'mirage'
5
5
  include Mirage::Util
6
6
 
7
+ #puts "ARGV is " + ARGV
7
8
  options = parse_options(ARGV)
8
9
 
9
10
  DEFAULT_RESPONSES_DIR = "#{options[:defaults_directory]}"
data/mirage.gemspec CHANGED
@@ -1,6 +1,8 @@
1
1
  Gem::Specification.new do |s|
2
+
3
+ windows = ENV['OS'] == 'Windows_NT'
2
4
  s.name = 'mirage'
3
- s.version = '1.1.0'
5
+ s.version = '1.2.0'
4
6
  s.authors = ["Leon Davis"]
5
7
  s.homepage = 'https://github.com/lashd/mirage'
6
8
  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.'
@@ -22,9 +24,13 @@ For more information go to: https://github.com/lashd/mirage/wiki
22
24
 
23
25
  s.has_rdoc = 'true'
24
26
 
25
- s.add_dependency 'rack', "~> 1.1.0"
26
- s.add_dependency 'ramaze', ">= 2011.01.30"
27
- s.add_dependency "mechanize", ">= 1.0.0"
27
+ s.add_dependency 'rack', '~> 1.1.0'
28
+ s.add_dependency 'ramaze', '>= 2011.01.30'
29
+ s.add_dependency 'mechanize', '>= 1.0.0'
30
+ s.add_dependency 'childprocess', '~> 0.1'
31
+
32
+ s.add_dependency 'jruby-openssl' if RUBY_PLATFORM == 'java'
33
+
28
34
 
29
35
  s.add_development_dependency 'rake'
30
36
  s.add_development_dependency 'cucumber'
data/rakefile CHANGED
@@ -1,16 +1,11 @@
1
1
  $LOAD_PATH.unshift('lib')
2
2
  require 'rubygems'
3
- require 'bundler/setup'
4
-
5
- require 'mirage/client'
6
3
  require 'rake'
7
- require 'open-uri'
8
4
  require 'cucumber'
9
5
  require 'cucumber/rake/task'
10
- require 'mechanize'
11
6
 
12
7
  def run_command command
13
- system "export RUBYOPT='' && #{command}"
8
+ system "#{command}"
14
9
  end
15
10
 
16
11
  task :gem => :clean do
@@ -25,19 +20,7 @@ Cucumber::Rake::Task.new(:features) do |t|
25
20
  t.cucumber_opts = "mode=regression features --format pretty"
26
21
  end
27
22
 
28
- task :start_mirage => :stop_mirage do
29
- run_command "mirage start"
30
- task.reenable
31
- end
32
-
33
- task :stop_mirage do |task|
34
- run_command "mirage stop"
35
- task.reenable
36
- end
37
-
38
-
39
23
  task :clean do |task|
40
-
41
24
  if run_command "gem list -i mirage"
42
25
  puts "cleaning"
43
26
  run_command "gem uninstall -x mirage"
@@ -47,4 +30,4 @@ task :clean do |task|
47
30
  end
48
31
 
49
32
 
50
- task :default => [:install, :start_mirage, :features, :stop_mirage, :clean]
33
+ task :default => [:install,:features,:clean]
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: mirage
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 1.1.0
5
+ version: 1.2.0
6
6
  platform: ruby
7
7
  authors:
8
8
  - Leon Davis
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2011-03-30 00:00:00 +01:00
13
+ date: 2011-04-04 00:00:00 +01:00
14
14
  default_executable: mirage
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
@@ -47,18 +47,18 @@ dependencies:
47
47
  type: :runtime
48
48
  version_requirements: *id003
49
49
  - !ruby/object:Gem::Dependency
50
- name: rake
50
+ name: childprocess
51
51
  prerelease: false
52
52
  requirement: &id004 !ruby/object:Gem::Requirement
53
53
  none: false
54
54
  requirements:
55
- - - ">="
55
+ - - ~>
56
56
  - !ruby/object:Gem::Version
57
- version: "0"
58
- type: :development
57
+ version: "0.1"
58
+ type: :runtime
59
59
  version_requirements: *id004
60
60
  - !ruby/object:Gem::Dependency
61
- name: cucumber
61
+ name: rake
62
62
  prerelease: false
63
63
  requirement: &id005 !ruby/object:Gem::Requirement
64
64
  none: false
@@ -69,7 +69,7 @@ dependencies:
69
69
  type: :development
70
70
  version_requirements: *id005
71
71
  - !ruby/object:Gem::Dependency
72
- name: rspec
72
+ name: cucumber
73
73
  prerelease: false
74
74
  requirement: &id006 !ruby/object:Gem::Requirement
75
75
  none: false
@@ -80,7 +80,7 @@ dependencies:
80
80
  type: :development
81
81
  version_requirements: *id006
82
82
  - !ruby/object:Gem::Dependency
83
- name: bundler
83
+ name: rspec
84
84
  prerelease: false
85
85
  requirement: &id007 !ruby/object:Gem::Requirement
86
86
  none: false
@@ -90,6 +90,17 @@ dependencies:
90
90
  version: "0"
91
91
  type: :development
92
92
  version_requirements: *id007
93
+ - !ruby/object:Gem::Dependency
94
+ name: bundler
95
+ prerelease: false
96
+ requirement: &id008 !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - ">="
100
+ - !ruby/object:Gem::Version
101
+ version: "0"
102
+ type: :development
103
+ version_requirements: *id008
93
104
  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.
94
105
  email:
95
106
  executables:
@@ -110,7 +121,6 @@ files:
110
121
  - features/client/save_and_revert.feature
111
122
  - features/client/set.feature
112
123
  - features/client/track.feature
113
- - features/resources/test.zip
114
124
  - features/server/clear.feature
115
125
  - features/server/command_line_iterface.feature
116
126
  - features/server/file_responses.feature
@@ -144,7 +154,7 @@ licenses: []
144
154
 
145
155
  post_install_message: "\n\
146
156
  ===============================================================================\n\
147
- Thanks you for installing mirage-1.1.0. \n\n\
157
+ Thanks you for installing mirage-1.2.0. \n\n\
148
158
  Run Mirage with:\n\n\
149
159
  mirage start \n\n\
150
160
  For more information go to: https://github.com/lashd/mirage/wiki\n\
@@ -171,7 +181,7 @@ rubyforge_project:
171
181
  rubygems_version: 1.6.1
172
182
  signing_key:
173
183
  specification_version: 3
174
- summary: mirage-1.1.0
184
+ summary: mirage-1.2.0
175
185
  test_files:
176
186
  - features/client/clear.feature
177
187
  - features/client/get.feature
@@ -180,7 +190,6 @@ test_files:
180
190
  - features/client/save_and_revert.feature
181
191
  - features/client/set.feature
182
192
  - features/client/track.feature
183
- - features/resources/test.zip
184
193
  - features/server/clear.feature
185
194
  - features/server/command_line_iterface.feature
186
195
  - features/server/file_responses.feature
Binary file