mirage 3.0.0.alpha.11 → 3.0.0.alpha.12

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 (56) hide show
  1. data/Gemfile +1 -1
  2. data/Gemfile.lock +1 -1
  3. data/VERSION +1 -1
  4. data/features/.nav +19 -0
  5. data/features/client/clear.feature +15 -23
  6. data/features/client/configure.feature +24 -28
  7. data/features/client/preview_responses.feature +4 -4
  8. data/features/client/put.feature +49 -62
  9. data/features/client/requests.feature +1 -1
  10. data/features/client/running.feature +2 -2
  11. data/features/client/save_and_revert.feature +5 -8
  12. data/features/client/start.feature +3 -3
  13. data/features/client/stop.feature +8 -8
  14. data/features/commandline_interface/help.feature +17 -0
  15. data/features/commandline_interface/readme.md +1 -0
  16. data/features/{server/commandline_interface → commandline_interface}/start.feature +4 -4
  17. data/features/{server/commandline_interface → commandline_interface}/stop.feature +22 -24
  18. data/features/{server/logging.feature → logging.feature} +2 -2
  19. data/features/{server/prime.feature → prime.feature} +4 -3
  20. data/features/readme.md +7 -0
  21. data/features/requests/delete.feature +48 -0
  22. data/features/{server/requests → requests}/get.feature +6 -5
  23. data/features/{server/save_and_revert.feature → save_and_revert.feature} +5 -5
  24. data/features/step_definitions/my_steps.rb +47 -12
  25. data/features/support/command_line.rb +1 -1
  26. data/features/templates/delete.feature +45 -0
  27. data/features/{server/templates → templates}/get.feature +9 -5
  28. data/features/templates/put.feature +77 -0
  29. data/features/{server/templates/put → templates}/put_with_substitutions.feature +4 -3
  30. data/features/templates/readme.md +7 -0
  31. data/features/{server/templates/put → templates}/required_content.feature +27 -35
  32. data/features/{server/web_user_interface.feature → web_user_interface.feature} +8 -7
  33. data/lib/mirage/client/cli_bridge.rb +1 -1
  34. data/lib/mirage/client/client.rb +16 -3
  35. data/lib/mirage/client/error.rb +1 -1
  36. data/lib/mirage/client/runner.rb +1 -1
  37. data/lib/mirage/client/template.rb +4 -2
  38. data/lib/mirage/client/template/configuration.rb +10 -0
  39. data/lib/mirage/client/template/model/instance_methods.rb +3 -4
  40. data/lib/mirage/client/templates.rb +1 -1
  41. data/mirage.gemspec +21 -17
  42. data/mirage_server.rb +1 -1
  43. data/server/mock_response.rb +18 -17
  44. data/server/server.rb +15 -15
  45. data/spec/client/client_spec.rb +44 -10
  46. data/spec/client/template/model/common_methods_spec.rb +1 -1
  47. data/spec/client/template/model/instance_methods_spec.rb +2 -2
  48. data/spec/client/template_spec.rb +11 -4
  49. data/spec/client/templates_spec.rb +10 -4
  50. data/spec/server/server_spec.rb +23 -23
  51. data/test.rb +2 -19
  52. metadata +22 -18
  53. data/features/server/commandline_interface/help.feature +0 -16
  54. data/features/server/requests/delete.feature +0 -47
  55. data/features/server/templates/delete.feature +0 -44
  56. data/features/server/templates/put/put.feature +0 -62
@@ -23,7 +23,7 @@ module Mirage
23
23
  def put *args, &block
24
24
  if args.first.class.is_a?(Template::Model)
25
25
  template = args.first
26
- template.endpoint "#{@url}/#{template.endpoint}"
26
+ template.endpoint "#{@url}/#{template.endpoint}" unless template.endpoint.to_s.start_with?(@url)
27
27
  else
28
28
  endpoint, response = args
29
29
  template = Mirage::Template.new "#{@url}/#{endpoint}", response, @default_config
data/mirage.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "mirage"
8
- s.version = "3.0.0.alpha.11"
8
+ s.version = "3.0.0.alpha.12"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new("> 1.3.1") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Leon Davis"]
12
- s.date = "2013-05-07"
12
+ s.date = "2013-08-20"
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 = [
@@ -24,6 +24,7 @@ Gem::Specification.new do |s|
24
24
  "README.md",
25
25
  "VERSION",
26
26
  "bin/mirage",
27
+ "features/.nav",
27
28
  "features/client/clear.feature",
28
29
  "features/client/configure.feature",
29
30
  "features/client/preview_responses.feature",
@@ -33,26 +34,29 @@ Gem::Specification.new do |s|
33
34
  "features/client/save_and_revert.feature",
34
35
  "features/client/start.feature",
35
36
  "features/client/stop.feature",
36
- "features/server/commandline_interface/help.feature",
37
- "features/server/commandline_interface/start.feature",
38
- "features/server/commandline_interface/stop.feature",
39
- "features/server/logging.feature",
40
- "features/server/prime.feature",
41
- "features/server/requests/delete.feature",
42
- "features/server/requests/get.feature",
43
- "features/server/save_and_revert.feature",
44
- "features/server/templates/delete.feature",
45
- "features/server/templates/get.feature",
46
- "features/server/templates/put/put.feature",
47
- "features/server/templates/put/put_with_substitutions.feature",
48
- "features/server/templates/put/required_content.feature",
49
- "features/server/web_user_interface.feature",
37
+ "features/commandline_interface/help.feature",
38
+ "features/commandline_interface/readme.md",
39
+ "features/commandline_interface/start.feature",
40
+ "features/commandline_interface/stop.feature",
41
+ "features/logging.feature",
42
+ "features/prime.feature",
43
+ "features/readme.md",
44
+ "features/requests/delete.feature",
45
+ "features/requests/get.feature",
46
+ "features/save_and_revert.feature",
50
47
  "features/step_definitions/my_steps.rb",
51
48
  "features/support/command_line.rb",
52
49
  "features/support/env.rb",
53
50
  "features/support/hooks.rb",
54
51
  "features/support/mirage.rb",
55
52
  "features/support/web.rb",
53
+ "features/templates/delete.feature",
54
+ "features/templates/get.feature",
55
+ "features/templates/put.feature",
56
+ "features/templates/put_with_substitutions.feature",
57
+ "features/templates/readme.md",
58
+ "features/templates/required_content.feature",
59
+ "features/web_user_interface.feature",
56
60
  "full_build.sh",
57
61
  "lib/mirage/client.rb",
58
62
  "lib/mirage/client/cli_bridge.rb",
@@ -108,7 +112,7 @@ Gem::Specification.new do |s|
108
112
  s.licenses = ["MIT"]
109
113
  s.post_install_message = "\n===============================================================================\nThanks you for installing mirage. \n\nRun Mirage with:\n\nmirage start \n\nFor more information go to: https://github.com/lashd/mirage/wiki\n===============================================================================\n"
110
114
  s.require_paths = ["lib"]
111
- s.rubygems_version = "1.8.25"
115
+ s.rubygems_version = "1.8.24"
112
116
  s.summary = "Mirage is a easy mock server for testing your applications"
113
117
 
114
118
  if s.respond_to? :specification_version then
data/mirage_server.rb CHANGED
@@ -5,8 +5,8 @@ $LOAD_PATH.unshift("#{ROOT_DIR}/server")
5
5
 
6
6
  require 'sinatra/base'
7
7
  require 'extensions/object'
8
- require 'extensions/hash'
9
8
  require 'app'
9
+ require 'extensions/hash'
10
10
  require 'mirage/client'
11
11
 
12
12
  module Mirage
@@ -13,12 +13,12 @@ module Mirage
13
13
  class << self
14
14
 
15
15
  def find_by_id(id)
16
- all.find{|response| response.response_id == id} || raise(ServerResponseNotFound)
16
+ all.find { |response| response.response_id == id } || raise(ServerResponseNotFound)
17
17
  end
18
18
 
19
19
  def delete(id)
20
20
  responses.values.each do |set|
21
- set.values.each{|responses| responses.delete_if{|response|response.response_id == id}}
21
+ set.values.each { |responses| responses.delete_if { |response| response.response_id == id } }
22
22
  end
23
23
  end
24
24
 
@@ -37,20 +37,21 @@ module Mirage
37
37
  end
38
38
 
39
39
  def all
40
- responses.values.collect do|response_set|
40
+ responses.values.collect do |response_set|
41
41
  response_set.values
42
42
  end.flatten
43
43
  end
44
44
 
45
45
  def find_default(options)
46
- http_method = options[:http_method].upcase!
46
+ options[:http_method].upcase!
47
+ http_method = options[:http_method]
47
48
  default_responses = subdomains(options[:endpoint]).collect do |domain|
48
- if(responses_for_domain = responses[domain])
49
- responses_for_domain[http_method].find_all{|response| response.default?} if responses_for_domain[http_method]
49
+ if (responses_for_domain = responses[domain])
50
+ responses_for_domain[http_method].find_all { |response| response.default? } if responses_for_domain[http_method]
50
51
  end
51
52
  end.flatten.compact
52
53
 
53
- default_responses.find{|response| match?(options,response)} || raise(ServerResponseNotFound)
54
+ default_responses.find { |response| match?(options, response) } || raise(ServerResponseNotFound)
54
55
  end
55
56
 
56
57
  def subdomains(name)
@@ -69,7 +70,7 @@ module Mirage
69
70
  def add(new_response)
70
71
  response_set = responses_for_endpoint(new_response)
71
72
  method_specific_responses = response_set[new_response.request_spec['http_method'].upcase]||=[]
72
- duplicate_response_location = method_specific_responses.index{|response| response.request_spec == new_response.request_spec}
73
+ duplicate_response_location = method_specific_responses.index { |response| response.request_spec == new_response.request_spec }
73
74
  old_response = method_specific_responses.delete_at(duplicate_response_location) if duplicate_response_location
74
75
  if old_response
75
76
  new_response.response_id = old_response.response_id
@@ -90,13 +91,13 @@ module Mirage
90
91
  match?(options, stored_response)
91
92
  end
92
93
 
93
- responses.sort{|a, b| b.score <=> a.score}.first
94
+ responses.sort { |a, b| b.score <=> a.score }.first
94
95
 
95
96
  end
96
97
 
97
- def match?(options,stored_response)
98
+ def match?(options, stored_response)
98
99
  parameters = options[:params]
99
- headers = Hash[options[:headers].collect{|key, value| [key.downcase, value]}]
100
+ headers = Hash[options[:headers].collect { |key, value| [key.downcase, value] }]
100
101
 
101
102
  request_spec = stored_response.request_spec
102
103
 
@@ -156,7 +157,7 @@ module Mirage
156
157
  request_defaults = JSON.parse({:parameters => {},
157
158
  :body_content => [],
158
159
  :http_method => 'get',
159
- :headers => {}}.to_json)
160
+ :headers => {}}.to_json)
160
161
  response_defaults = JSON.parse({:default => false,
161
162
  :body => Base64.encode64(''),
162
163
  :delay => 0,
@@ -169,9 +170,9 @@ module Mirage
169
170
  @request_spec = Hashie::Mash.new request_defaults.merge(spec['request']||{})
170
171
  @response_spec = Hashie::Mash.new response_defaults.merge(spec['response']||{})
171
172
 
172
- @request_spec['headers'] = Hash[@request_spec['headers'].collect{|key, value| [key.downcase, value.to_s]}]
173
- @request_spec['parameters'] = Hash[@request_spec['parameters'].collect{|key, value| [key, value.to_s]}]
174
- @request_spec['body_content'] = @request_spec['body_content'].collect{|value|value.to_s}
173
+ @request_spec['headers'] = Hash[@request_spec['headers'].collect { |key, value| [key.downcase, value.to_s] }]
174
+ @request_spec['parameters'] = Hash[@request_spec['parameters'].collect { |key, value| [key, value.to_s] }]
175
+ @request_spec['body_content'] = @request_spec['body_content'].collect { |value| value.to_s }
175
176
  @binary = BinaryDataChecker.contains_binary_data? @response_spec['body']
176
177
 
177
178
  MockResponse.add self
@@ -187,7 +188,7 @@ module Mirage
187
188
 
188
189
  def score
189
190
  [@request_spec['headers'].values, @request_spec['parameters'].values, @request_spec['body_content']].inject(0) do |score, matchers|
190
- matchers.inject(score){|matcher_score, value| interpret_value(value).is_a?(Regexp) ? matcher_score+=1 : matcher_score+=2}
191
+ matchers.inject(score) { |matcher_score, value| interpret_value(value).is_a?(Regexp) ? matcher_score+=1 : matcher_score+=2 }
191
192
  end
192
193
  end
193
194
 
@@ -217,7 +218,7 @@ module Mirage
217
218
  end
218
219
 
219
220
  def raw
220
- {:id =>response_id, :endpoint => @name, :requests_url => requests_url, :response => @response_spec, :request => @request_spec}.to_json
221
+ {:id => response_id, :endpoint => @name, :requests_url => requests_url, :response => @response_spec, :request => @request_spec}.to_json
221
222
  end
222
223
 
223
224
  def binary?
data/server/server.rb CHANGED
@@ -10,15 +10,15 @@ module Mirage
10
10
 
11
11
  helpers Mirage::Server::Helpers
12
12
 
13
- put '/mirage/templates/*' do |name|
13
+ put '/templates/*' do |name|
14
14
  content_type :json
15
15
  mock_response = MockResponse.new(name, JSON.parse(request.body.read))
16
- mock_response.requests_url = request.url.gsub("/mirage/templates/#{name}", "/mirage/requests/#{mock_response.response_id}")
16
+ mock_response.requests_url = request.url.gsub("/templates/#{name}", "/requests/#{mock_response.response_id}")
17
17
  {:id => mock_response.response_id}.to_json
18
18
  end
19
19
 
20
20
  %w(get post delete put).each do |http_method|
21
- send(http_method, '/mirage/responses/*') do |name|
21
+ send(http_method, '/responses/*') do |name|
22
22
  body, query_string = Rack::Utils.unescape(request.body.read.to_s), request.query_string
23
23
 
24
24
  options = {:body => body,
@@ -38,33 +38,33 @@ module Mirage
38
38
  end
39
39
  end
40
40
 
41
- delete '/mirage/templates/:id' do
41
+ delete '/templates/:id' do
42
42
  MockResponse.delete(response_id)
43
43
  REQUESTS.delete(response_id)
44
44
  200
45
45
  end
46
46
 
47
- delete '/mirage/requests' do
47
+ delete '/requests' do
48
48
  REQUESTS.clear
49
49
  200
50
50
  end
51
51
 
52
- delete '/mirage/requests/:id' do
52
+ delete '/requests/:id' do
53
53
  REQUESTS.delete(response_id)
54
54
  200
55
55
  end
56
56
 
57
- delete '/mirage/templates' do
57
+ delete '/templates' do
58
58
  REQUESTS.clear
59
59
  MockResponse.delete_all
60
60
  200
61
61
  end
62
62
 
63
- get '/mirage/templates/:id' do
63
+ get '/templates/:id' do
64
64
  MockResponse.find_by_id(response_id).raw
65
65
  end
66
66
 
67
- get '/mirage/requests/:id' do
67
+ get '/requests/:id' do
68
68
  content_type :json
69
69
  tracked_request = REQUESTS[response_id]
70
70
  if tracked_request
@@ -84,12 +84,12 @@ module Mirage
84
84
  end
85
85
  end
86
86
 
87
- get '/mirage' do
87
+ get '/' do
88
88
  haml :index
89
89
  end
90
90
 
91
91
 
92
- put '/mirage/defaults' do
92
+ put '/defaults' do
93
93
  MockResponse.delete_all
94
94
  if File.directory?(settings.defaults.to_s)
95
95
  Dir["#{settings.defaults}/**/*.rb"].each do |default|
@@ -103,18 +103,18 @@ module Mirage
103
103
  200
104
104
  end
105
105
  #
106
- put '/mirage/backup' do
106
+ put '/backup' do
107
107
  MockResponse.backup
108
108
  200
109
109
  end
110
110
 
111
111
 
112
- put '/mirage' do
112
+ put '/' do
113
113
  MockResponse.revert
114
114
  200
115
115
  end
116
116
 
117
- get '/mirage/pid' do
117
+ get '/pid' do
118
118
  "#{$$}"
119
119
  end
120
120
 
@@ -133,7 +133,7 @@ module Mirage
133
133
  end
134
134
 
135
135
  def prime &block
136
- block.call Mirage::Client.new "http://localhost:#{settings.port}/mirage"
136
+ block.call Mirage::Client.new "http://localhost:#{settings.port}"
137
137
  end
138
138
 
139
139
  def send_response(mock_response, body='', request={}, query_string='')
@@ -10,7 +10,7 @@ describe Mirage::Client do
10
10
 
11
11
  describe 'configuration' do
12
12
  it 'is configured to connect to local host port 7001 by default' do
13
- Client.new.url.should == "http://localhost:7001/mirage"
13
+ Client.new.url.should == "http://localhost:7001"
14
14
  end
15
15
 
16
16
  it 'can be configured with a url pointing to Mirage' do
@@ -22,7 +22,7 @@ describe Mirage::Client do
22
22
 
23
23
  it 'can be configured with a port refering to which port Mirage is running on on localhost' do
24
24
  port = 9001
25
- Client.new(:port => port).url.should == "http://localhost:#{port}/mirage"
25
+ Client.new(:port => port).url.should == "http://localhost:#{port}"
26
26
  end
27
27
 
28
28
  it 'raises an error if neither a port or url specified in the argument' do
@@ -31,16 +31,35 @@ describe Mirage::Client do
31
31
  end
32
32
 
33
33
 
34
- it 'can be configured with template defaults' do
35
- templates, config = Templates.new("url"), proc{}
36
- Templates.should_receive(:new).and_return(templates)
37
- templates.should_receive(:default_config) do |&block|
38
- block.should == config
34
+ describe 'defaults' do
35
+ it 'can be configured with template defaults on initialize' do
36
+ templates, config = Templates.new("url"), proc {}
37
+ Templates.should_receive(:new).and_return(templates)
38
+ templates.should_receive(:default_config) do |&block|
39
+ block.should == config
40
+ end
41
+ Client.new &config
42
+ end
43
+
44
+ it 'can be configured with template defaults on after initalize' do
45
+ templates, config = Templates.new("url"), proc {}
46
+ Templates.should_receive(:new).and_return(templates)
47
+ templates.should_receive(:default_config) do |&block|
48
+ block.should == config
49
+ end
50
+ Client.new.configure &config
39
51
  end
40
- Client.new &config
41
- end
42
52
 
53
+ it 'can be reset' do
54
+ client = Client.new do
55
+ http_method :post
56
+ end
43
57
 
58
+ client.reset
59
+ client.templates.default_config.should == Template::Configuration.new
60
+ end
61
+
62
+ end
44
63
  end
45
64
 
46
65
  it 'should clear mirage' do
@@ -55,7 +74,7 @@ describe Mirage::Client do
55
74
 
56
75
  it 'should prime mirage' do
57
76
  Client.should_receive(:put) do |url|
58
- url.should == "http://localhost:7001/mirage/defaults"
77
+ url.should == "http://localhost:7001/defaults"
59
78
  end
60
79
  Client.new.prime
61
80
  end
@@ -66,6 +85,11 @@ describe Mirage::Client do
66
85
  mirage.templates.instance_of?(Templates).should == true
67
86
  end
68
87
 
88
+ it 'The templates instance should be the one created on construction otherwise the defaults passed in will get lost' do
89
+ mirage = Client.new
90
+ mirage.templates.should == mirage.templates
91
+ end
92
+
69
93
  it 'should find a template' do
70
94
  id = 1
71
95
  mirage = Client.new
@@ -74,6 +98,7 @@ describe Mirage::Client do
74
98
  mirage.templates(1).should == mock_template
75
99
  end
76
100
 
101
+
77
102
  describe 'put' do
78
103
  it "should put a response on mirage by passing args on to template's put method " do
79
104
  endpoint, value, block = 'greeting', 'hello', Proc.new{}
@@ -119,6 +144,15 @@ describe Mirage::Client do
119
144
  end
120
145
  end
121
146
 
147
+ describe 'running?' do
148
+ it 'should check if mirage is runing' do
149
+ url = 'http://some_url'
150
+
151
+ Mirage.should_receive(:running?).with url
152
+ Client.new(url).running?
153
+ end
154
+ end
155
+
122
156
 
123
157
  describe 'interface to mirage' do
124
158
 
@@ -7,7 +7,7 @@ describe 'CommonMethods' do
7
7
  end
8
8
 
9
9
  let!(:instance) do
10
- model.new '', ''
10
+ model.new
11
11
  end
12
12
  it 'should provide methods for customising the model' do
13
13
  instance.methods.should include(:content_type,
@@ -101,13 +101,13 @@ describe Template::Model::InstanceMethods do
101
101
 
102
102
  describe 'delay' do
103
103
  it 'should default to 0' do
104
- JSON.parse(instance.to_json)["delay"].should == 0
104
+ JSON.parse(instance.to_json)["response"]["delay"].should == 0
105
105
  end
106
106
 
107
107
  it 'should set the delay' do
108
108
  delay = 5
109
109
  instance.delay delay
110
- JSON.parse(instance.to_json)["delay"].should == delay
110
+ JSON.parse(instance.to_json)["response"]["delay"].should == delay
111
111
  end
112
112
  end
113
113
 
@@ -10,7 +10,7 @@ describe Mirage::Template do
10
10
  id = 1
11
11
  requests_url = 'request_url'
12
12
  body = "Hello"
13
- default = false
13
+ default = true
14
14
  delay = 1.2
15
15
  content_type = "application/json"
16
16
  status = 201
@@ -28,7 +28,7 @@ describe Mirage::Template do
28
28
  requests_url: requests_url,
29
29
  response:{
30
30
  default: default,
31
- body: body,
31
+ body: Base64.encode64(body),
32
32
  delay: delay,
33
33
  content_type: content_type,
34
34
  status: status,
@@ -41,6 +41,7 @@ describe Mirage::Template do
41
41
  http_method: http_method
42
42
  }
43
43
  }
44
+ template_json.should_receive(:code).and_return 200
44
45
 
45
46
  template_url = "url"
46
47
  Template.should_receive(:backedup_get).with(template_url, :format => :json).and_return(template_json)
@@ -50,7 +51,6 @@ describe Mirage::Template do
50
51
  template.endpoint.should == endpoint
51
52
  template.id.should == id
52
53
 
53
- template.default.should == default
54
54
  template.default.should == default
55
55
  template.delay.should == delay
56
56
  template.content_type.should == content_type
@@ -64,6 +64,13 @@ describe Mirage::Template do
64
64
  template.url.should == template_url
65
65
  template.requests_url.should == requests_url
66
66
  end
67
+
68
+ it 'should raise an error if the template is not found' do
69
+ template_url = 'url'
70
+ response = mock(code: 404)
71
+ Template.should_receive(:backedup_get).with(template_url, :format => :json).and_return response
72
+ expect{Template.get(template_url)}.to raise_error Mirage::ResponseNotFound
73
+ end
67
74
  end
68
75
 
69
76
  describe 'initialize' do
@@ -133,7 +140,7 @@ describe Mirage::Template do
133
140
  end
134
141
 
135
142
  it 'should throw a standard method missing error if a caller binding is not set' do
136
- expect{Template.new('endpoint').some_method}.should raise_error(NameError)
143
+ expect{Template.new('endpoint').some_method}.to raise_error(NameError)
137
144
  end
138
145
  end
139
146
  end