mirage 2.2.3 → 2.3.0

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.
Files changed (36) hide show
  1. data/Gemfile +2 -0
  2. data/Gemfile.lock +11 -2
  3. data/HISTORY +1 -0
  4. data/VERSION +1 -1
  5. data/bin/mirage +6 -8
  6. data/features/client/clear.feature +8 -8
  7. data/features/client/{response.feature → preview_responses.feature} +0 -0
  8. data/features/client/put.feature +9 -1
  9. data/features/client/{request.feature → requests.feature} +0 -0
  10. data/features/client/running.feature +49 -0
  11. data/features/client/{command_line_interface.feature → start.feature} +0 -12
  12. data/features/client/stop.feature +85 -0
  13. data/features/server/commandline_interface/help.feature +16 -0
  14. data/features/server/commandline_interface/start.feature +30 -0
  15. data/features/server/commandline_interface/stop.feature +42 -0
  16. data/features/server/prime.feature +8 -7
  17. data/features/step_definitions/my_steps.rb +20 -16
  18. data/features/support/command_line.rb +22 -0
  19. data/features/support/env.rb +11 -121
  20. data/features/support/hooks.rb +30 -0
  21. data/features/support/mirage.rb +8 -0
  22. data/lib/mirage/client/client.rb +124 -0
  23. data/lib/mirage/client/error.rb +22 -0
  24. data/lib/mirage/client/response.rb +29 -0
  25. data/lib/mirage/client/runner.rb +142 -0
  26. data/lib/mirage/client.rb +4 -206
  27. data/mirage.gemspec +25 -8
  28. data/mirage_server.rb +15 -10
  29. data/rakefile +7 -1
  30. data/spec/running_via_api_spec.rb +147 -0
  31. data/spec/running_via_api_windows_spec.rb +187 -0
  32. data/test.rb +21 -4
  33. metadata +66 -33
  34. data/features/client/mirage_client.feature +0 -36
  35. data/features/server/command_line_iterface.feature +0 -45
  36. data/lib/mirage/cli.rb +0 -69
data/Gemfile CHANGED
@@ -1,8 +1,10 @@
1
1
  source :rubygems
2
2
 
3
3
  gem 'sinatra'
4
+ gem "eventmachine", "1.0.0.rc.4"
4
5
  gem 'childprocess'
5
6
  gem "waitforit"
7
+ gem "thor"
6
8
 
7
9
  group :development do
8
10
  gem "thin"
data/Gemfile.lock CHANGED
@@ -16,14 +16,17 @@ GEM
16
16
  diff-lcs (1.1.3)
17
17
  domain_name (0.5.1)
18
18
  unf (~> 0.0.3)
19
- eventmachine (0.12.10)
20
- eventmachine (0.12.10-java)
19
+ eventmachine (1.0.0.rc.4)
20
+ eventmachine (1.0.0.rc.4-java)
21
+ eventmachine (1.0.0.rc.4-x86-mingw32)
21
22
  ffi (1.0.11)
22
23
  ffi (1.0.11-java)
23
24
  gherkin (2.7.2)
24
25
  json (>= 1.4.6)
25
26
  gherkin (2.7.2-java)
26
27
  json (>= 1.4.6)
28
+ gherkin (2.7.2-x86-mingw32)
29
+ json (>= 1.4.6)
27
30
  git (1.2.5)
28
31
  jeweler (1.6.4)
29
32
  bundler (~> 1.0)
@@ -44,6 +47,7 @@ GEM
44
47
  net-http-persistent (2.3.3)
45
48
  nokogiri (1.5.0)
46
49
  nokogiri (1.5.0-java)
50
+ nokogiri (1.5.0-x86-mingw32)
47
51
  ntlm-http (0.1.1)
48
52
  rack (1.4.0)
49
53
  rack-protection (1.2.0)
@@ -75,10 +79,12 @@ GEM
75
79
  daemons (>= 1.0.9)
76
80
  eventmachine (>= 0.12.6)
77
81
  rack (>= 1.0.0)
82
+ thor (0.15.4)
78
83
  tilt (1.3.3)
79
84
  unf (0.0.4)
80
85
  unf_ext
81
86
  unf_ext (0.0.4)
87
+ unf_ext (0.0.4-x86-mingw32)
82
88
  waitforit (0.0.1)
83
89
  webrobots (0.0.12)
84
90
  nokogiri (>= 1.4.4)
@@ -86,10 +92,12 @@ GEM
86
92
  PLATFORMS
87
93
  java
88
94
  ruby
95
+ x86-mingw32
89
96
 
90
97
  DEPENDENCIES
91
98
  childprocess
92
99
  cucumber
100
+ eventmachine (= 1.0.0.rc.4)
93
101
  jeweler
94
102
  jruby-openssl
95
103
  mechanize
@@ -99,4 +107,5 @@ DEPENDENCIES
99
107
  sinatra
100
108
  sinatra-contrib
101
109
  thin
110
+ thor
102
111
  waitforit
data/HISTORY CHANGED
@@ -1,3 +1,4 @@
1
+ V2.3.0 - Enhanced commandline/programmatic api
1
2
  V2.2.3 - fix bug where mirage is already running a client calls exit.
2
3
  V2.2.1/2 - Code tidy up
3
4
  V2.2.0 - Api for starting and stopping Mirage locally
data/VERSION CHANGED
@@ -1 +1 @@
1
- 2.2.3
1
+ 2.3.0
data/bin/mirage CHANGED
@@ -1,16 +1,14 @@
1
1
  #!/usr/bin/env ruby
2
2
  require 'rubygems'
3
3
 
4
+
4
5
  $LOAD_PATH.unshift("#{File.dirname(__FILE__)}/../lib")
5
6
  require 'mirage/client'
6
7
 
7
-
8
-
9
- if ARGV.include?('start')
10
- Mirage.start ARGV
11
- elsif ARGV.include?('stop')
12
- Mirage.stop
13
- else
14
- Mirage::CLI.parse_options ['--help']
8
+ begin
9
+ Mirage::Runner.start
10
+ rescue Mirage::ClientError => e
11
+ puts e.message
15
12
  exit 1
16
13
  end
14
+
@@ -1,4 +1,4 @@
1
- Feature: The client can be used for clearing responses from Mirage
1
+ Feature: The client can be used for clearing response templates from Mirage
2
2
 
3
3
  Background:
4
4
  Given the following gems are required to run the Mirage client test code:
@@ -7,22 +7,22 @@ Feature: The client can be used for clearing responses from Mirage
7
7
  require 'rspec'
8
8
  require 'mirage/client'
9
9
  """
10
-
10
+
11
11
  And I send PUT to 'http://localhost:7001/mirage/templates/greeting' with request entity
12
12
  """
13
13
  Hello
14
14
  """
15
15
  And I send GET to 'http://localhost:7001/mirage/responses/greeting' with parameters:
16
16
  | message | hello there |
17
-
17
+
18
18
  And I send PUT to 'http://localhost:7001/mirage/templates/leaving' with request entity
19
19
  """
20
20
  Goodbye
21
21
  """
22
22
  And I send GET to 'http://localhost:7001/mirage/responses/greeting' with parameters:
23
- | message | I'm going |
23
+ | message | I'm going |
24
+
24
25
 
25
-
26
26
  Scenario: Clearing everything
27
27
  When I run
28
28
  """
@@ -51,12 +51,12 @@ Feature: The client can be used for clearing responses from Mirage
51
51
 
52
52
  When I send GET to 'http://localhost:7001/mirage/requests/2'
53
53
  Then a 404 should be returned
54
-
54
+
55
55
  When I send GET to 'http://localhost:7001/mirage/responses/greeting'
56
56
  Then a 200 should be returned
57
57
  When I send GET to 'http://localhost:7001/mirage/responses/leaving'
58
58
  Then a 200 should be returned
59
-
59
+
60
60
 
61
61
  Scenario: Clearning a response
62
62
  Given I run
@@ -71,7 +71,7 @@ Feature: The client can be used for clearing responses from Mirage
71
71
  Then a 200 should be returned
72
72
  When I send GET to 'http://localhost:7001/mirage/requests/2'
73
73
  Then a 200 should be returned
74
-
74
+
75
75
 
76
76
  Scenario: Clearning a request
77
77
  Given I run
@@ -66,7 +66,6 @@ Feature: the Mirage client provides methods for setting responses and loading de
66
66
  Then 'default greeting' should be returned
67
67
 
68
68
 
69
-
70
69
  Scenario: Setting the content type
71
70
  Given I run
72
71
  """
@@ -132,3 +131,12 @@ Feature: the Mirage client provides methods for setting responses and loading de
132
131
  """
133
132
  When I send GET to 'http://localhost:7001/mirage/responses/greeting'
134
133
  Then a 203 should be returned
134
+
135
+
136
+ Scenario: Setting a response with a delay
137
+ Given I run
138
+ """
139
+ Mirage::Client.new.put('greeting', 'hello'){|response| response.delay = 2}
140
+ """
141
+ When I send GET to 'http://localhost:7001/mirage/responses/greeting'
142
+ Then it should take at least '2' seconds
@@ -0,0 +1,49 @@
1
+ @command_line
2
+ Feature: Use the mirage client api to check if Mirage is running either on the local machine or on a remote host.
3
+
4
+
5
+ Background:
6
+ Given the following gems are required to run the Mirage client test code:
7
+ """
8
+ require 'rubygems'
9
+ require 'rspec'
10
+ require 'mirage/client'
11
+ """
12
+
13
+
14
+ Scenario: Check if mirage is running on the local machine on the default port
15
+ Given Mirage is not running
16
+ Then I run
17
+ """
18
+ Mirage.running?.should == false
19
+ """
20
+ Given Mirage is running
21
+ Then I run
22
+ """
23
+ Mirage.running?.should == true
24
+ """
25
+
26
+
27
+ Scenario: Check if mirage is running on the local machine on a non standard port
28
+ Given I run 'mirage start -p 9001'
29
+ Then I run
30
+ """
31
+ Mirage.running?(:port => 9001).should == true
32
+ """
33
+
34
+
35
+ Scenario: Checking if mirage is running on remote machine
36
+ Given I run 'mirage start -p 9001'
37
+ Then I run
38
+ """
39
+ Mirage.running? "http://localhost:9001/mirage"
40
+ """
41
+
42
+
43
+ Scenario: Check if mirage is running using the client directly
44
+ Given I run 'mirage start -p 9001'
45
+ Then I run
46
+ """
47
+ client = Mirage::Client.new "http://localhost:9001/mirage"
48
+ client.running?.should == true
49
+ """
@@ -1,4 +1,3 @@
1
- @command_line
2
1
  Feature: The Mirage client provides a programmatic interface equivalent to the command line interface. This gives an
3
2
  easy method for bringing a local instance of Mirage in situ inside a test suite.
4
3
 
@@ -14,7 +13,6 @@ Feature: The Mirage client provides a programmatic interface equivalent to the c
14
13
 
15
14
 
16
15
  Scenario: Starting mirage with defaults
17
- Given Mirage is not running
18
16
  When I run
19
17
  """
20
18
  Mirage.start
@@ -22,16 +20,6 @@ Feature: The Mirage client provides a programmatic interface equivalent to the c
22
20
  Then mirage should be running on 'http://localhost:7001/mirage'
23
21
 
24
22
 
25
- Scenario: Stopping Mirage
26
- Given Mirage is not running
27
- When I run
28
- """
29
- Mirage.start
30
- Mirage.stop
31
- """
32
- Then Connection should be refused to 'http://localhost:7001/mirage'
33
-
34
-
35
23
  Scenario: Starting Mirage on a custom port
36
24
  Given Mirage is not running
37
25
  And the file './custom_responses_location/default_greetings.rb' contains:
@@ -0,0 +1,85 @@
1
+ Feature: The Mirage client provides a programmatic interface equivalent to the command line interface. This gives an
2
+ easy method for bringing a local instance of Mirage in situ inside a test suite.
3
+
4
+ The client can only be used to stop Mirage if it is was used to start the running instance.
5
+
6
+ Background:
7
+ Given the following gems are required to run the Mirage client test code:
8
+ """
9
+ require 'rubygems'
10
+ require 'rspec'
11
+ require 'mirage/client'
12
+ """
13
+
14
+
15
+ Scenario: Stopping Mirage
16
+ Given Mirage is not running
17
+ When I run
18
+ """
19
+ Mirage.start
20
+ Mirage.stop :port => 7001
21
+ """
22
+ Then Connection should be refused to 'http://localhost:7001/mirage'
23
+
24
+ Scenario: Stopping Mirage on custom port
25
+ Given Mirage is not running
26
+ And I run 'mirage start -p 7001'
27
+ And I run 'mirage start -p 9001'
28
+ When I run
29
+ """
30
+ Mirage.stop :port => 9001
31
+ """
32
+ Then mirage should be running on 'http://localhost:7001/mirage'
33
+ Then mirage should not be running on 'http://localhost:9001/mirage'
34
+
35
+ Scenario: Stopping multiple instances of Mirage
36
+ Given Mirage is not running
37
+ And I run 'mirage start -p 7001'
38
+ And I run 'mirage start -p 9001'
39
+ And I run 'mirage start -p 10001'
40
+ When I run
41
+ """
42
+ Mirage.stop :port => [9001,10001]
43
+ """
44
+ Then mirage should be running on 'http://localhost:7001/mirage'
45
+ Then mirage should not be running on 'http://localhost:9001/mirage'
46
+ Then mirage should not be running on 'http://localhost:10001/mirage'
47
+
48
+ Scenario: Stopping all instances of Mirage
49
+ Given Mirage is not running
50
+ And I run 'mirage start -p 7001'
51
+ And I run 'mirage start -p 9001'
52
+ And I run 'mirage start -p 10001'
53
+ When I run
54
+ """
55
+ Mirage.stop :all
56
+ """
57
+ Then mirage should not be running on 'http://localhost:7001/mirage'
58
+ Then mirage should not be running on 'http://localhost:9001/mirage'
59
+ Then mirage should not be running on 'http://localhost:10001/mirage'
60
+
61
+
62
+
63
+ Scenario: Using client to stop mirage
64
+ Given Mirage is not running
65
+ And I run 'mirage start -p 7001'
66
+ And I run 'mirage start -p 9001'
67
+ When I run
68
+ """
69
+ begin
70
+ Mirage.stop
71
+ raise "should have errored"
72
+ rescue Mirage::ClientError => ce
73
+ end
74
+ """
75
+ Then mirage should be running on 'http://localhost:7001/mirage'
76
+ And mirage should be running on 'http://localhost:9001/mirage'
77
+
78
+ Scenario: Stopping Mirage without specifying the port when more than one instance of Mirage is running
79
+ Given Mirage is not running
80
+ When I run
81
+ """
82
+ client = Mirage.start
83
+ client.stop
84
+ """
85
+ Then Connection should be refused to 'http://localhost:7001/mirage'
@@ -0,0 +1,16 @@
1
+ @command_line
2
+ Feature: Mirage is started from the command line.
3
+ Mirage logs to mirage.log at the path where Mirage is started from
4
+
5
+
6
+ Background: Mirage usage
7
+ Given usage information:
8
+ | Tasks: |
9
+ | mirage help [TASK] # Describe available tasks or one specific task |
10
+ # | mirage start # Starts mirage |
11
+ # | mirage stop -p, --port=[port_1 port_2\|all] # stops mirage |
12
+
13
+
14
+ Scenario: Starting with help option
15
+ Given I run 'mirage help'
16
+ Then the usage information should be displayed
@@ -0,0 +1,30 @@
1
+ @command_line
2
+ Feature: Mirage is started from the command line.
3
+ more than instance of Mirage can be started on different ports at the same time.
4
+
5
+
6
+ Scenario: Starting mirage
7
+ Given Mirage is not running
8
+ When I run 'mirage start'
9
+ Then mirage should be running on 'http://localhost:7001/mirage'
10
+ And 'mirage.log' should exist
11
+
12
+
13
+ Scenario: Starting Mirage on a custom port
14
+ Given Mirage is not running
15
+ When I run 'mirage start -p 9001'
16
+ Then mirage should be running on 'http://localhost:9001/mirage'
17
+
18
+
19
+ Scenario: Starting multiple instances of Mirage
20
+ Given Mirage is not running
21
+ When I run 'mirage start -p 7001'
22
+ And I run 'mirage start -p 9001'
23
+ Then mirage should be running on 'http://localhost:7001/mirage'
24
+ And mirage should be running on 'http://localhost:9001/mirage'
25
+
26
+
27
+ Scenario: Starting Mirage when it is already running
28
+ Given Mirage is running
29
+ When I run 'mirage start -p 7001'
30
+ Then I should see 'Mirage is already running' on the command line
@@ -0,0 +1,42 @@
1
+ @command_line
2
+ Feature: stop
3
+
4
+ Scenario: Calling stop without specifying the port when only one instance is running
5
+ Given I run 'mirage start -p 7001'
6
+ When I run 'mirage stop'
7
+ Then mirage should not be running on 'http://localhost:7001/mirage'
8
+
9
+
10
+ Scenario: Calling stop without specifying the port when more than one instance is running
11
+ Given I run 'mirage start -p 7001'
12
+ Given I run 'mirage start -p 9001'
13
+ When I run 'mirage stop'
14
+ Then I should see 'Mirage is running on ports 7001, 9001. Please run mirage stop -p [PORT(s)] instead' on the command line
15
+ And mirage should be running on 'http://localhost:7001/mirage'
16
+ And mirage should be running on 'http://localhost:9001/mirage'
17
+
18
+
19
+ Scenario: stopping on a single instance
20
+ Given I run 'mirage start -p 7001'
21
+ And I run 'mirage start -p 9001'
22
+ When I run 'mirage stop -p 7001'
23
+ Then mirage should be running on 'http://localhost:9001/mirage'
24
+ Then mirage should not be running on 'http://localhost:7001/mirage'
25
+
26
+ Scenario: stop more than one instance
27
+ Given I run 'mirage start -p 7001'
28
+ And I run 'mirage start -p 9001'
29
+ And I run 'mirage start -p 10001'
30
+ When I run 'mirage stop -p 7001 9001'
31
+ Then mirage should be running on 'http://localhost:10001/mirage'
32
+ Then mirage should not be running on 'http://localhost:7001/mirage'
33
+ Then mirage should not be running on 'http://localhost:9001/mirage'
34
+
35
+ Scenario: stop all instances
36
+ Given I run 'mirage start -p 7001'
37
+ And I run 'mirage start -p 9001'
38
+ When I run 'mirage stop -p all'
39
+ Then mirage should not be running on 'http://localhost:10001/mirage'
40
+ Then mirage should not be running on 'http://localhost:7001/mirage'
41
+ Then mirage should not be running on 'http://localhost:9001/mirage'
42
+
@@ -4,7 +4,7 @@ Feature: Mirage can be primed with a set of responses.
4
4
  of your choosing
5
5
 
6
6
  Responses can be added to the responses directory and used to prime Mirage after Mirage has been started.
7
-
7
+
8
8
  Priming mirage causes any modifications to its state to be lost
9
9
 
10
10
 
@@ -36,13 +36,14 @@ Feature: Mirage can be primed with a set of responses.
36
36
 
37
37
 
38
38
  Scenario: Mirage is started pointing with a full path for the responses
39
- Given the file '/tmp/responses/default_greetings.rb' contains:
39
+ Given Mirage is not running
40
+ And the file '/tmp/responses/default_greetings.rb' contains:
40
41
  """
41
42
  prime do |mirage|
42
43
  mirage.put('greeting', 'hello')
43
44
  end
44
45
  """
45
- And I run 'mirage start -d /tmp/responses'
46
+ And I run 'mirage start --defaults /tmp/responses'
46
47
  When I send PUT to 'http://localhost:7001/mirage/defaults'
47
48
  And I send GET to 'http://localhost:7001/mirage/responses/greeting'
48
49
  Then 'hello' should be returned
@@ -58,9 +59,9 @@ Feature: Mirage can be primed with a set of responses.
58
59
  And I run 'mirage start'
59
60
  And I send DELETE to 'http://localhost:7001/mirage/templates'
60
61
  And I send PUT to 'http://localhost:7001/mirage/templates/a_new_response' with request entity
61
- """
62
- new response
63
- """
62
+ """
63
+ new response
64
+ """
64
65
 
65
66
  When I send PUT to 'http://localhost:7001/mirage/defaults'
66
67
  When I send GET to 'http://localhost:7001/mirage/responses/greeting'
@@ -68,7 +69,7 @@ Feature: Mirage can be primed with a set of responses.
68
69
  When I send GET to 'http://localhost:7001/mirage/responses/a_new_response'
69
70
  Then a 404 should be returned
70
71
 
71
-
72
+ @command_line
72
73
  Scenario: Mirage is started with a bad file
73
74
  Given the file 'responses/default_greetings.rb' contains:
74
75
  """
@@ -1,11 +1,3 @@
1
- Before('@command_line') do
2
- stop_mirage
3
- end
4
-
5
- After('@command_line') do
6
- stop_mirage
7
- end
8
-
9
1
  Then /^'([^']*)' should be returned$/ do |expected_response|
10
2
  response_text = @response.body
11
3
  if response_text != expected_response
@@ -23,20 +15,31 @@ Then /^it should take at least '(.*)' seconds$/ do |time|
23
15
  end
24
16
 
25
17
 
26
- Then /^mirage should be running on '(.*)'$/ do |url|
27
- http_get(url).code.to_i.should == 200
18
+ Then /^mirage (should|should not) be running on '(.*)'$/ do |should, url|
19
+ running = false
20
+ begin
21
+ running = http_get(url).code.to_i.should == 200
22
+ rescue
23
+ end
24
+
25
+ should == "should" ? running.should == true : running.should == false
28
26
  end
29
27
 
30
28
  Given /^I run '(.*)'$/ do |command|
31
- path = ENV['mode'] == 'regression' ? '' : "../bin/"
29
+ if ENV['mode'] == 'regression' && ChildProcess.windows?
30
+ command.gsub!(/^mirage/, MIRAGE_CMD)
31
+ else
32
+ path = "#{RUBY_CMD} ../bin/"
33
+ end
34
+
32
35
  @commandline_output = normalise(run("#{path}#{command}"))
33
36
  end
34
37
 
35
38
  Given /^Mirage (is|is not) running$/ do |running|
36
39
  if running == 'is'
37
- start_mirage unless $mirage.running?
40
+ start_mirage_in_scratch_dir unless Mirage.running?
38
41
  else
39
- stop_mirage if $mirage.running?
42
+ Mirage.stop :all
40
43
  end
41
44
  end
42
45
 
@@ -61,8 +64,9 @@ Given /^the file '(.*)' contains:$/ do |file_path, content|
61
64
  end
62
65
 
63
66
  Then /^the usage information should be displayed$/ do
64
- @usage.each { |line| @commandline_output.should =~ /#{line}/ }
67
+ @usage.each { |line| @commandline_output.should include(line) }
65
68
  end
69
+
66
70
  Given /^usage information:$/ do |table|
67
71
  @usage = table.raw.flatten.collect { |line| normalise(line) }
68
72
  end
@@ -70,7 +74,7 @@ end
70
74
  Then /^I run$/ do |text|
71
75
  text.gsub!("\"", "\\\\\"")
72
76
  Dir.chdir SCRATCH do
73
- raise "run failed" unless system "#{RUBY_CMD} -e \"#{@code_snippet}\n#{text}\""
77
+ raise "run failed" unless system "#{RUBY_CMD} -I #{SOURCE_PATH} -e \"#{@code_snippet}\n#{text}\""
74
78
  end
75
79
  end
76
80
 
@@ -126,7 +130,7 @@ When /^I send PUT to '(http:\/\/localhost:7001\/mirage\/([^']*))' with body '([^
126
130
  end
127
131
 
128
132
  Then /^I should see '(.*?)' on the command line$/ do |content|
129
- @commandline_output.should =~/#{content}/
133
+ @commandline_output.should include(content)
130
134
  end
131
135
 
132
136
  Then /^'(.*)' should exist$/ do |path|
@@ -0,0 +1,22 @@
1
+ require 'tempfile'
2
+ module CommandLine
3
+ def run command
4
+ output = Tempfile.new("child")
5
+ Dir.chdir SCRATCH do
6
+ process = ChildProcess.build(*("#{command}".split(' ')))
7
+ process.detach
8
+ process.io.stdout = output
9
+ process.io.stderr = output
10
+ process.start
11
+ wait_until(:timeout_after => 30.seconds) { process.exited? }
12
+ end
13
+ File.read(output.path)
14
+ end
15
+
16
+ def normalise text
17
+ text.gsub(/[\n]/, ' ').gsub(/\s+/, ' ')
18
+ end
19
+ end
20
+
21
+ World CommandLine
22
+ include CommandLine