mirage 3.0.0.alpha.9 → 3.0.0.alpha.10
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.
- data/README.md +2 -1
- data/VERSION +1 -1
- data/lib/mirage/client/client.rb +3 -1
- data/lib/mirage/client/template/configuration.rb +10 -1
- data/lib/mirage/client/template/model/instance_methods.rb +12 -0
- data/lib/mirage/client/templates.rb +12 -2
- data/mirage.gemspec +2 -2
- data/server/mock_response.rb +2 -1
- data/spec/client/client_spec.rb +12 -0
- data/spec/client/template/configuration_spec.rb +5 -5
- data/spec/client/template/model/instance_methods_spec.rb +5 -5
- data/spec/client/template_spec.rb +17 -3
- data/spec/client/templates_spec.rb +76 -12
- data/spec/server/mock_response_spec.rb +1 -0
- data/test.rb +14 -13
- metadata +3 -3
data/README.md
CHANGED
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
3.0.0.alpha.
|
1
|
+
3.0.0.alpha.10
|
data/lib/mirage/client/client.rb
CHANGED
@@ -9,7 +9,7 @@ module Mirage
|
|
9
9
|
|
10
10
|
attr_reader :url
|
11
11
|
|
12
|
-
def initialize options={:url => "http://localhost:7001/mirage"}
|
12
|
+
def initialize options={:url => "http://localhost:7001/mirage"}, &block
|
13
13
|
if options.is_a?(String) && options =~ URI.regexp
|
14
14
|
warn("Client.new(url): Deprecated usage, please use :url => url | :port => port")
|
15
15
|
@url = options
|
@@ -20,6 +20,8 @@ module Mirage
|
|
20
20
|
else
|
21
21
|
raise "specify a valid URL or port"
|
22
22
|
end
|
23
|
+
|
24
|
+
templates.default_config &block if block
|
23
25
|
end
|
24
26
|
|
25
27
|
def templates id=nil
|
@@ -1,7 +1,11 @@
|
|
1
|
+
require 'client/helpers/method_builder'
|
2
|
+
|
1
3
|
module Mirage
|
2
4
|
class Template
|
3
5
|
class Configuration
|
4
|
-
|
6
|
+
extend Helpers::MethodBuilder
|
7
|
+
builder_methods :http_method, :status, :delay, :content_type, :default
|
8
|
+
attr_accessor :caller_binding
|
5
9
|
DEFAULT_HTTP_METHOD=:get
|
6
10
|
DEFAULT_STATUS=200
|
7
11
|
DEFAULT_DELAY=0
|
@@ -20,6 +24,11 @@ module Mirage
|
|
20
24
|
@default = DEFAULT_DEFAULT
|
21
25
|
end
|
22
26
|
|
27
|
+
|
28
|
+
def method_missing(method, *args, &block)
|
29
|
+
@caller_binding.send method, *args, &block if @caller_binding
|
30
|
+
end
|
31
|
+
|
23
32
|
end
|
24
33
|
end
|
25
34
|
end
|
@@ -5,6 +5,8 @@ module Mirage
|
|
5
5
|
extend Helpers::MethodBuilder
|
6
6
|
include CommonMethods
|
7
7
|
|
8
|
+
attr_accessor :caller_binding
|
9
|
+
|
8
10
|
def initialize *args
|
9
11
|
if args.last.is_a?(Template::Configuration)
|
10
12
|
default_config = args.delete_at(-1)
|
@@ -77,6 +79,16 @@ module Mirage
|
|
77
79
|
def encode(value)
|
78
80
|
value.is_a?(Regexp) ? "%r{#{value.source}}" : value
|
79
81
|
end
|
82
|
+
|
83
|
+
def method_missing(method, *args, &block)
|
84
|
+
|
85
|
+
if @caller_binding
|
86
|
+
@caller_binding.send method, *args, &block
|
87
|
+
else
|
88
|
+
super method, *args, &block
|
89
|
+
end
|
90
|
+
|
91
|
+
end
|
80
92
|
end
|
81
93
|
|
82
94
|
end
|
@@ -9,7 +9,10 @@ module Mirage
|
|
9
9
|
|
10
10
|
def default_config &block
|
11
11
|
return @default_config unless block_given?
|
12
|
-
|
12
|
+
calling_instance = eval "self", block.binding
|
13
|
+
@default_config.caller_binding = calling_instance
|
14
|
+
@default_config.instance_eval &block
|
15
|
+
@default_config.caller_binding = nil
|
13
16
|
end
|
14
17
|
|
15
18
|
def delete_all
|
@@ -25,7 +28,14 @@ module Mirage
|
|
25
28
|
endpoint, response = args
|
26
29
|
template = Mirage::Template.new "#{@url}/#{endpoint}", response, @default_config
|
27
30
|
end
|
28
|
-
|
31
|
+
|
32
|
+
if block
|
33
|
+
calling_instance = eval "self", block.binding
|
34
|
+
template.caller_binding = calling_instance
|
35
|
+
template.instance_exec(template,&block)
|
36
|
+
template.caller_binding = nil
|
37
|
+
end
|
38
|
+
|
29
39
|
template.create
|
30
40
|
end
|
31
41
|
end
|
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.
|
8
|
+
s.version = "3.0.0.alpha.10"
|
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-
|
12
|
+
s.date = "2013-05-02"
|
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 = [
|
data/server/mock_response.rb
CHANGED
@@ -69,7 +69,8 @@ module Mirage
|
|
69
69
|
def add(new_response)
|
70
70
|
response_set = responses_for_endpoint(new_response)
|
71
71
|
method_specific_responses = response_set[new_response.request_spec['http_method'].upcase]||=[]
|
72
|
-
|
72
|
+
duplicate_response_location = method_specific_responses.index{|response| response.request_spec == new_response.request_spec}
|
73
|
+
old_response = method_specific_responses.delete_at(duplicate_response_location) if duplicate_response_location
|
73
74
|
if old_response
|
74
75
|
new_response.response_id = old_response.response_id
|
75
76
|
else
|
data/spec/client/client_spec.rb
CHANGED
@@ -29,6 +29,18 @@ describe Mirage::Client do
|
|
29
29
|
expect { Client.new({}) }.to raise_error()
|
30
30
|
expect { Client.new("rubbish") }.to raise_error()
|
31
31
|
end
|
32
|
+
|
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
|
39
|
+
end
|
40
|
+
Client.new &config
|
41
|
+
end
|
42
|
+
|
43
|
+
|
32
44
|
end
|
33
45
|
|
34
46
|
it 'should clear mirage' do
|
@@ -12,11 +12,11 @@ describe Template::Configuration do
|
|
12
12
|
|
13
13
|
it 'should be reset' do
|
14
14
|
configuration = Template::Configuration.new
|
15
|
-
configuration.http_method
|
16
|
-
configuration.status
|
17
|
-
configuration.delay
|
18
|
-
configuration.default
|
19
|
-
configuration.content_type
|
15
|
+
configuration.http_method :post
|
16
|
+
configuration.status 202
|
17
|
+
configuration.delay 3
|
18
|
+
configuration.default true
|
19
|
+
configuration.content_type "text/xml"
|
20
20
|
|
21
21
|
configuration.reset
|
22
22
|
assert_defaults configuration
|
@@ -26,15 +26,15 @@ describe Template::Model::InstanceMethods do
|
|
26
26
|
endpoint, value = 'endpoint', 'value'
|
27
27
|
instance = model.new endpoint, value
|
28
28
|
instance.endpoint.should == endpoint
|
29
|
-
instance.
|
29
|
+
instance.body.should == value
|
30
30
|
end
|
31
31
|
|
32
32
|
it 'can use configuration for all http related config' do
|
33
33
|
config = Mirage::Template::Configuration.new
|
34
|
-
config.content_type
|
35
|
-
config.http_method
|
36
|
-
config.status
|
37
|
-
config.default
|
34
|
+
config.content_type 'content_type'
|
35
|
+
config.http_method 'method'
|
36
|
+
config.status 'status'
|
37
|
+
config.default true
|
38
38
|
|
39
39
|
instance = model.new 'endpoint', 'value', config
|
40
40
|
instance.content_type.should == config.content_type
|
@@ -9,7 +9,7 @@ describe Mirage::Template do
|
|
9
9
|
endpoint = "endpoint"
|
10
10
|
id = 1
|
11
11
|
requests_url = 'request_url'
|
12
|
-
|
12
|
+
body = "Hello"
|
13
13
|
default = false
|
14
14
|
delay = 1.2
|
15
15
|
content_type = "application/json"
|
@@ -28,7 +28,7 @@ describe Mirage::Template do
|
|
28
28
|
requests_url: requests_url,
|
29
29
|
response:{
|
30
30
|
default: default,
|
31
|
-
body:
|
31
|
+
body: body,
|
32
32
|
delay: delay,
|
33
33
|
content_type: content_type,
|
34
34
|
status: status,
|
@@ -46,7 +46,7 @@ describe Mirage::Template do
|
|
46
46
|
Template.should_receive(:backedup_get).with(template_url, :format => :json).and_return(template_json)
|
47
47
|
|
48
48
|
template = Template.get(template_url)
|
49
|
-
template.
|
49
|
+
template.body.should == body
|
50
50
|
template.endpoint.should == endpoint
|
51
51
|
template.id.should == id
|
52
52
|
|
@@ -122,4 +122,18 @@ describe Mirage::Template do
|
|
122
122
|
end
|
123
123
|
|
124
124
|
end
|
125
|
+
|
126
|
+
describe 'method missing' do
|
127
|
+
it 'should delagate to the caller if it is set' do
|
128
|
+
caller = Object.new
|
129
|
+
caller.should_receive(:some_method)
|
130
|
+
template = Template.new('endpoint')
|
131
|
+
template.caller_binding = caller
|
132
|
+
template.some_method
|
133
|
+
end
|
134
|
+
|
135
|
+
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)
|
137
|
+
end
|
138
|
+
end
|
125
139
|
end
|
@@ -18,6 +18,7 @@ describe 'templates' do
|
|
18
18
|
end
|
19
19
|
|
20
20
|
describe 'setting default config' do
|
21
|
+
|
21
22
|
it 'should preset configuration for templates' do
|
22
23
|
Template.stub(:put).and_return(convert_keys_to_strings({:id => 1}))
|
23
24
|
templates = Templates.new "base_url"
|
@@ -28,12 +29,12 @@ describe 'templates' do
|
|
28
29
|
delay = 2
|
29
30
|
content_type = "text/xml"
|
30
31
|
|
31
|
-
templates.default_config do
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
32
|
+
templates.default_config do
|
33
|
+
http_method http_method
|
34
|
+
status status
|
35
|
+
default default
|
36
|
+
delay delay
|
37
|
+
content_type content_type
|
37
38
|
end
|
38
39
|
|
39
40
|
template = templates.put('greeting', 'hello')
|
@@ -44,6 +45,37 @@ describe 'templates' do
|
|
44
45
|
template.delay.should == delay
|
45
46
|
template.content_type.should == content_type
|
46
47
|
end
|
48
|
+
|
49
|
+
it 'should fall over to methods on the caller if the method does not exist on the configuration object' do
|
50
|
+
templates_wrapper = Class.new do
|
51
|
+
def initialize
|
52
|
+
@templates = Templates.new "base_url"
|
53
|
+
@outer_method_called = false
|
54
|
+
end
|
55
|
+
|
56
|
+
def outer_method_call
|
57
|
+
@outer_method_called = true
|
58
|
+
end
|
59
|
+
|
60
|
+
|
61
|
+
def outer_method_called?
|
62
|
+
@outer_method_called
|
63
|
+
end
|
64
|
+
|
65
|
+
def test
|
66
|
+
@templates.default_config do
|
67
|
+
outer_method_call
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
|
73
|
+
wrapper = templates_wrapper.new
|
74
|
+
|
75
|
+
wrapper.test
|
76
|
+
wrapper.outer_method_called?.should == true
|
77
|
+
|
78
|
+
end
|
47
79
|
end
|
48
80
|
|
49
81
|
describe 'putting templates' do
|
@@ -51,14 +83,12 @@ describe 'templates' do
|
|
51
83
|
|
52
84
|
endpoint = "greeting"
|
53
85
|
value = "hello"
|
54
|
-
let(:base_url){ "base_url"}
|
55
|
-
let!(:templates){Templates.new(base_url)}
|
56
|
-
|
57
|
-
|
86
|
+
let(:base_url) { "base_url" }
|
87
|
+
let!(:templates) { Templates.new(base_url) }
|
58
88
|
|
59
89
|
|
60
90
|
context 'model as parameter' do
|
61
|
-
let!(:endpoint){'endpoint'}
|
91
|
+
let!(:endpoint) { 'endpoint' }
|
62
92
|
let!(:model_class) do
|
63
93
|
Class.new do
|
64
94
|
extend Template::Model
|
@@ -76,6 +106,40 @@ describe 'templates' do
|
|
76
106
|
template.endpoint.should == "#{@base_url}/templates/#{endpoint}"
|
77
107
|
end
|
78
108
|
|
109
|
+
it 'should fall over to methods on the caller if the method does not exist on the template object' do
|
110
|
+
template_wrapper = Class.new do
|
111
|
+
def initialize
|
112
|
+
@templates = Templates.new "base_url"
|
113
|
+
@outer_method_called = false
|
114
|
+
end
|
115
|
+
|
116
|
+
def outer_method_call
|
117
|
+
@outer_method_called = true
|
118
|
+
end
|
119
|
+
|
120
|
+
def outer_method_called?
|
121
|
+
@outer_method_called
|
122
|
+
end
|
123
|
+
|
124
|
+
def test
|
125
|
+
template = Template.new 'endpoint'
|
126
|
+
Template.should_receive(:new).and_return template
|
127
|
+
template.stub(:create).and_return(template)
|
128
|
+
@templates.put('endpoint', 'value') do |response|
|
129
|
+
puts response
|
130
|
+
outer_method_call
|
131
|
+
end
|
132
|
+
end
|
133
|
+
end
|
134
|
+
|
135
|
+
wrapper = template_wrapper.new
|
136
|
+
|
137
|
+
template = wrapper.test
|
138
|
+
wrapper.outer_method_called?.should == true
|
139
|
+
template.caller_binding.should == nil
|
140
|
+
|
141
|
+
end
|
142
|
+
|
79
143
|
|
80
144
|
end
|
81
145
|
|
@@ -98,7 +162,7 @@ describe 'templates' do
|
|
98
162
|
|
99
163
|
describe 'block parameter that can be used for template customisation' do
|
100
164
|
it 'it is called in the context of the template' do
|
101
|
-
template = Template.new('','')
|
165
|
+
template = Template.new('', '')
|
102
166
|
template.stub(:create)
|
103
167
|
Template.should_receive(:new).and_return(template)
|
104
168
|
templates.put(endpoint, value) do
|
@@ -470,6 +470,7 @@ describe Mirage::MockResponse do
|
|
470
470
|
}
|
471
471
|
})
|
472
472
|
response1 = MockResponse.new("greeting", response_spec)
|
473
|
+
response_spec['response']['body'] = 'response2'
|
473
474
|
response2 = MockResponse.new("greeting", response_spec)
|
474
475
|
|
475
476
|
response1.response_id.should_not == nil
|
data/test.rb
CHANGED
@@ -1,19 +1,20 @@
|
|
1
1
|
require './lib/mirage/client'
|
2
|
-
class UserProfile
|
3
|
-
extend Mirage::Template::Model
|
4
2
|
|
5
|
-
endpoint '/users'
|
6
|
-
http_method :get
|
7
|
-
status 200
|
8
|
-
content_type 'application/json'
|
9
|
-
required_body_content %w(leon davis)
|
10
3
|
|
11
|
-
|
4
|
+
Mirage.stop
|
5
|
+
mirage = Mirage.start
|
6
|
+
|
12
7
|
|
13
|
-
|
14
|
-
|
15
|
-
|
8
|
+
mirage.put('FindCis.do', 'value1') do
|
9
|
+
http_method :post
|
10
|
+
content_type "text/xml"
|
11
|
+
required_body_content ['value']
|
12
|
+
status 200
|
16
13
|
end
|
17
14
|
|
18
|
-
mirage
|
19
|
-
|
15
|
+
mirage.put('FindCis.do', 'value2') do
|
16
|
+
http_method :post
|
17
|
+
content_type "text/xml"
|
18
|
+
required_body_content ['value']
|
19
|
+
status 200
|
20
|
+
end
|
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: 3.0.0.alpha.
|
4
|
+
version: 3.0.0.alpha.10
|
5
5
|
prerelease: 6
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-
|
12
|
+
date: 2013-05-02 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: sinatra
|
@@ -349,7 +349,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
349
349
|
version: '0'
|
350
350
|
segments:
|
351
351
|
- 0
|
352
|
-
hash:
|
352
|
+
hash: 3670135109747540376
|
353
353
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
354
354
|
none: false
|
355
355
|
requirements:
|