mirage 3.0.0.alpha.3 → 3.0.0.alpha.4

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/VERSION CHANGED
@@ -1 +1 @@
1
- 3.0.0.alpha.3
1
+ 3.0.0.alpha.4
data/lib/mirage/client.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  $LOAD_PATH.unshift "#{File.dirname(__FILE__)}"
2
- require 'client/template_configuration'
2
+ require 'client/template'
3
3
  require 'client/error'
4
4
  require 'client/cli_bridge'
5
5
  require 'client/runner'
@@ -0,0 +1,19 @@
1
+ module Mirage
2
+ module Helpers
3
+ module MethodBuilder
4
+ def builder_methods *method_names
5
+
6
+ method_names.each do |method_name|
7
+ method_name = method_name.to_sym
8
+ define_method method_name do |arg=nil|
9
+ return instance_variable_get("@#{method_name}".to_sym) unless arg
10
+ instance_variable_set("@#{method_name}".to_sym, arg)
11
+ self
12
+ end
13
+ end
14
+
15
+ end
16
+ alias builder_method builder_methods
17
+ end
18
+ end
19
+ end
@@ -3,10 +3,15 @@ require 'json'
3
3
  require 'httparty'
4
4
  require 'hashie/mash'
5
5
 
6
+ require 'client/template/configuration'
7
+ require 'client/template/model'
8
+
6
9
  module Mirage
7
10
 
8
11
  class Template
12
+
9
13
  include HTTParty
14
+ include Model::InstanceMethods
10
15
 
11
16
  class << self
12
17
  alias_method :backedup_get, :get
@@ -19,93 +24,22 @@ module Mirage
19
24
 
20
25
  template = new(response_hashie.endpoint, response_config.body)
21
26
 
22
- template.id = response_hashie.id
23
- template.default = response_config['default']
24
- template.delay = response_config.delay
25
- template.content_type = response_config.content_type
26
- template.status = response_config.status
27
- template.headers = response_config.headers
27
+ template.id response_hashie.id
28
+ template.default response_config['default']
29
+ template.delay response_config.delay
30
+ template.content_type response_config.content_type
31
+ template.status response_config.status
32
+ template.headers response_config.headers
28
33
 
29
- template.required_parameters = request_config.parameters
30
- template.required_body_content = request_config.body_content
31
- template.http_method = request_config.http_method
32
- template.url = url
33
- template.requests_url = response_hashie.requests_url
34
- template.required_headers = request_config.headers
34
+ template.required_parameters request_config.parameters
35
+ template.required_body_content request_config.body_content
36
+ template.http_method request_config.http_method
37
+ template.url url
38
+ template.requests_url response_hashie.requests_url
39
+ template.required_headers request_config.headers
35
40
 
36
41
  template
37
42
  end
38
43
  end
39
-
40
- format :json
41
-
42
- attr_accessor :content_type, :http_method, :default, :status, :delay, :required_parameters, :required_body_content, :required_headers, :endpoint, :id, :url, :requests_url, :headers
43
- attr_reader :value
44
-
45
-
46
- def initialize endpoint, response, default_config=TemplateConfiguration.new
47
-
48
- @endpoint = endpoint
49
- @content_type = default_config.content_type
50
- @value = response
51
- @http_method = default_config.http_method
52
- @status = default_config.status
53
- @delay = default_config.delay
54
- @required_parameters = {}
55
- @required_headers = {}
56
- @required_body_content = []
57
- @headers = {}
58
- @default = default_config.default
59
- end
60
-
61
- def create
62
- @id = self.class.put("#{@endpoint}", :body => self.to_json, :headers => {'content-type' => 'application/json'})['id']
63
- self
64
- end
65
-
66
- def delete
67
- self.class.delete(url)
68
- Request.delete requests_url
69
- end
70
-
71
-
72
- def to_json
73
- {
74
- :response => {
75
- :body => Base64.encode64(@value),
76
- :status => status,
77
- :default => default,
78
- :content_type => content_type,
79
- :headers => headers
80
-
81
- },
82
- :request => {
83
- :parameters => encode_regexs(required_parameters),
84
- :headers => encode_regexs(required_headers),
85
- :body_content => encode_regexs(required_body_content),
86
- :http_method => http_method,
87
- },
88
- :delay => delay
89
- }.to_json
90
- end
91
-
92
-
93
- def encode_regexs hash_or_array
94
- case hash_or_array
95
- when Array
96
- hash_or_array.collect { |value| encode(value) }
97
- else
98
- encoded = {}
99
- hash_or_array.each do |key, value|
100
- encoded[key] = encode(value)
101
- end
102
- encoded
103
- end
104
- end
105
-
106
- def encode(value)
107
- value.is_a?(Regexp) ? "%r{#{value.source}}" : value
108
- end
109
-
110
44
  end
111
45
  end
@@ -0,0 +1,25 @@
1
+ module Mirage
2
+ class Template
3
+ class Configuration
4
+ attr_accessor :http_method, :status, :delay, :content_type, :default
5
+ DEFAULT_HTTP_METHOD=:get
6
+ DEFAULT_STATUS=200
7
+ DEFAULT_DELAY=0
8
+ DEFAULT_CONTENT_TYPE="text/plain"
9
+ DEFAULT_DEFAULT=false
10
+
11
+ def initialize
12
+ reset
13
+ end
14
+
15
+ def reset
16
+ @http_method = DEFAULT_HTTP_METHOD
17
+ @status = DEFAULT_STATUS
18
+ @delay = DEFAULT_DELAY
19
+ @content_type = DEFAULT_CONTENT_TYPE
20
+ @default = DEFAULT_DEFAULT
21
+ end
22
+
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,30 @@
1
+
2
+ require 'client/helpers/method_builder'
3
+ require 'client/template/model/class_methods'
4
+ require 'client/template/model/instance_methods'
5
+
6
+ module Mirage
7
+ class Template
8
+ module Model
9
+
10
+ class << self
11
+ def extended clazz
12
+ clazz.extend(ClassMethods)
13
+ clazz.send(:include, HTTParty)
14
+ clazz.send(:include, InstanceMethods)
15
+
16
+ clazz.class_eval do
17
+ def initialize
18
+ super self.class.endpoint, ''
19
+ status self.class.status if self.class.status
20
+ end
21
+ end
22
+
23
+ clazz.format :json
24
+ clazz
25
+ end
26
+ end
27
+ end
28
+
29
+ end
30
+ end
@@ -0,0 +1,11 @@
1
+ module Mirage
2
+ class Template
3
+ module Model
4
+ module ClassMethods
5
+ extend Helpers::MethodBuilder
6
+ include Helpers::MethodBuilder
7
+ builder_method :endpoint, :status
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,88 @@
1
+ module Mirage
2
+ class Template
3
+ module Model
4
+ module InstanceMethods
5
+ extend Helpers::MethodBuilder
6
+
7
+ def initialize endpoint, response, default_config=Template::Configuration.new
8
+ @endpoint = endpoint
9
+ @content_type = default_config.content_type
10
+ @value = response
11
+ @http_method = default_config.http_method
12
+ @status = default_config.status
13
+ @delay = default_config.delay
14
+ @required_parameters = {}
15
+ @required_headers = {}
16
+ @required_body_content = []
17
+ @headers = {}
18
+ @default = default_config.default
19
+ end
20
+
21
+ builder_method :content_type,
22
+ :http_method,
23
+ :default,
24
+ :status,
25
+ :delay,
26
+ :required_parameters,
27
+ :required_body_content,
28
+ :required_headers,
29
+ :endpoint,
30
+ :id,
31
+ :url,
32
+ :requests_url,
33
+ :headers,
34
+ :value
35
+
36
+ def create
37
+ @id = self.class.put("#{@endpoint}", :body => self.to_json, :headers => {'content-type' => 'application/json'})['id']
38
+ self
39
+ end
40
+
41
+ def delete
42
+ self.class.delete(url)
43
+ Request.delete requests_url
44
+ end
45
+
46
+
47
+ def to_json
48
+ {
49
+ :response => {
50
+ :body => Base64.encode64(value),
51
+ :status => status,
52
+ :default => default,
53
+ :content_type => content_type,
54
+ :headers => headers
55
+
56
+ },
57
+ :request => {
58
+ :parameters => encode_regexs(required_parameters),
59
+ :headers => encode_regexs(required_headers),
60
+ :body_content => encode_regexs(required_body_content),
61
+ :http_method => http_method,
62
+ },
63
+ :delay => delay
64
+ }.to_json
65
+ end
66
+
67
+
68
+ def encode_regexs hash_or_array
69
+ case hash_or_array
70
+ when Array
71
+ hash_or_array.collect { |value| encode(value) }
72
+ else
73
+ encoded = {}
74
+ hash_or_array.each do |key, value|
75
+ encoded[key] = encode(value)
76
+ end
77
+ encoded
78
+ end
79
+ end
80
+
81
+ def encode(value)
82
+ value.is_a?(Regexp) ? "%r{#{value.source}}" : value
83
+ end
84
+ end
85
+
86
+ end
87
+ end
88
+ end
@@ -4,7 +4,7 @@ module Mirage
4
4
  def initialize base_url
5
5
  @url = "#{base_url}/templates"
6
6
  @requests = Requests.new(base_url)
7
- @default_config = TemplateConfiguration.new
7
+ @default_config = Template::Configuration.new
8
8
  end
9
9
 
10
10
  def default_config &block
@@ -18,9 +18,9 @@ module Mirage
18
18
  end
19
19
 
20
20
  def put *args
21
- if args.first.is_a?(Template)
21
+ if args.first.class.is_a?(Template::Model)
22
22
  template = args.first
23
- template.endpoint = "#{@url}/#{template.endpoint}"
23
+ template.endpoint "#{@url}/#{template.endpoint}"
24
24
  else
25
25
  endpoint, response = args
26
26
  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.3"
8
+ s.version = "3.0.0.alpha.4"
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-04-21"
12
+ s.date = "2013-04-24"
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 = [
@@ -58,14 +58,19 @@ Gem::Specification.new do |s|
58
58
  "lib/mirage/client/cli_bridge.rb",
59
59
  "lib/mirage/client/client.rb",
60
60
  "lib/mirage/client/error.rb",
61
+ "lib/mirage/client/helpers/method_builder.rb",
61
62
  "lib/mirage/client/request.rb",
62
63
  "lib/mirage/client/requests.rb",
63
64
  "lib/mirage/client/runner.rb",
64
65
  "lib/mirage/client/template.rb",
65
- "lib/mirage/client/template_configuration.rb",
66
+ "lib/mirage/client/template/configuration.rb",
67
+ "lib/mirage/client/template/model.rb",
68
+ "lib/mirage/client/template/model/class_methods.rb",
69
+ "lib/mirage/client/template/model/instance_methods.rb",
66
70
  "lib/mirage/client/templates.rb",
67
71
  "mirage.gemspec",
68
72
  "mirage_server.rb",
73
+ "profiles.rb",
69
74
  "rakefile",
70
75
  "responses/default_responses.rb",
71
76
  "server/app.rb",
@@ -77,10 +82,14 @@ Gem::Specification.new do |s|
77
82
  "server/server.rb",
78
83
  "spec/client/cli_bridge_spec.rb",
79
84
  "spec/client/client_spec.rb",
85
+ "spec/client/helpers/method_builder_spec.rb",
80
86
  "spec/client/request_spec.rb",
81
87
  "spec/client/requests_spec.rb",
82
88
  "spec/client/runner_spec.rb",
83
- "spec/client/template_configuration_spec.rb",
89
+ "spec/client/template/configuration_spec.rb",
90
+ "spec/client/template/model/class_methods_spec.rb",
91
+ "spec/client/template/model/instance_methods_spec.rb",
92
+ "spec/client/template/model_spec.rb",
84
93
  "spec/client/template_spec.rb",
85
94
  "spec/client/templates_spec.rb",
86
95
  "spec/resources/binary.file",
data/profiles.rb ADDED
@@ -0,0 +1 @@
1
+ Profile.create
@@ -0,0 +1,32 @@
1
+ require 'spec_helper'
2
+
3
+ describe Helpers::MethodBuilder do
4
+ it 'should give a builder_method builder method' do
5
+ model_class = Class.new do
6
+ extend Helpers::MethodBuilder
7
+
8
+ builder_method :name
9
+ builder_method :age
10
+ end
11
+ name, age = 'Joe', 25
12
+
13
+ person = model_class.new
14
+ person.name(name).age(age)
15
+ person.name.should == name
16
+ person.age.should == age
17
+ end
18
+
19
+ it 'should let you define more than one builder method at a time' do
20
+ model_class = Class.new do
21
+ extend Helpers::MethodBuilder
22
+
23
+ builder_methods :name, :age
24
+ end
25
+ name, age = 'Joe', 25
26
+
27
+ person = model_class.new
28
+ person.name(name).age(age)
29
+ person.name.should == name
30
+ person.age.should == age
31
+ end
32
+ end
@@ -3,15 +3,15 @@ require 'mirage/client'
3
3
 
4
4
 
5
5
 
6
- describe TemplateConfiguration do
6
+ describe Template::Configuration do
7
7
 
8
8
  it 'should have defaults' do
9
- configuration = TemplateConfiguration.new
9
+ configuration = Template::Configuration.new
10
10
  assert_defaults configuration
11
11
  end
12
12
 
13
13
  it 'should be reset' do
14
- configuration = TemplateConfiguration.new
14
+ configuration = Template::Configuration.new
15
15
  configuration.http_method = :post
16
16
  configuration.status = 202
17
17
  configuration.delay = 3
@@ -0,0 +1,19 @@
1
+ require 'spec_helper'
2
+
3
+ describe Template::Model::ClassMethods do
4
+ context 'class methods' do
5
+
6
+ it 'should provide a method for setting the endpoint for the class' do
7
+ endpoint = 'endpoint'
8
+ model_class = Class.new do
9
+ extend Template::Model::ClassMethods
10
+
11
+ endpoint endpoint
12
+ end
13
+
14
+ model_class.endpoint.should == endpoint
15
+ end
16
+
17
+ end
18
+
19
+ end
@@ -0,0 +1,172 @@
1
+ require 'spec_helper'
2
+
3
+ describe Template::Model::InstanceMethods do
4
+
5
+
6
+ let!(:model) do
7
+ Class.new do
8
+ include Template::Model::InstanceMethods
9
+ end
10
+ end
11
+
12
+ let!(:instance) do
13
+ model.new '', ''
14
+ end
15
+
16
+ it 'should provide methods for customising the model' do
17
+ instance.methods.should include(:content_type,
18
+ :http_method,
19
+ :default,
20
+ :status,
21
+ :delay,
22
+ :required_parameters,
23
+ :required_body_content,
24
+ :required_headers,
25
+ :endpoint,
26
+ :id,
27
+ :url,
28
+ :requests_url,
29
+ :headers,
30
+ :value)
31
+ end
32
+
33
+ context 'initialize' do
34
+ it 'requires an endpoint and value to be provided' do
35
+ endpoint, value = 'endpoint', 'value'
36
+ instance = model.new endpoint, value
37
+ instance.endpoint.should == endpoint
38
+ instance.value.should == value
39
+ end
40
+
41
+ it 'can use configuration for all http related config' do
42
+ config = Mirage::Template::Configuration.new
43
+ config.content_type='content_type'
44
+ config.http_method='method'
45
+ config.status='status'
46
+ config.default=true
47
+
48
+ instance = model.new 'endpoint', 'value', config
49
+ instance.content_type.should == config.content_type
50
+ instance.http_method.should == config.http_method
51
+ instance.status.should == config.status
52
+ instance.default.should == config.default
53
+ end
54
+ end
55
+
56
+ context 'to_json' do
57
+ describe 'response body' do
58
+ it 'should base64 encode response values' do
59
+ value = "value"
60
+ response = model.new "endpoint", value
61
+ JSON.parse(response.to_json)["response"]["body"].should == Base64.encode64(value)
62
+ end
63
+ end
64
+
65
+ describe 'required request parameters' do
66
+
67
+ it 'should contain expected request parameters' do
68
+ required_parameters = {:key => "value"}
69
+ instance.required_parameters required_parameters
70
+ JSON.parse(instance.to_json)["request"]["parameters"].should == convert_keys_to_strings(required_parameters)
71
+ end
72
+
73
+ it 'should encode parameter requirements that are regexs' do
74
+ instance.required_parameters({:key => /regex/})
75
+ JSON.parse(instance.to_json)["request"]["parameters"].should == convert_keys_to_strings({:key => "%r{regex}"})
76
+ end
77
+ end
78
+
79
+ describe 'required body content' do
80
+ it 'should contain expected body content' do
81
+ required_body_content = ["body content"]
82
+ instance.required_body_content required_body_content
83
+ JSON.parse(instance.to_json)["request"]["body_content"].should == required_body_content
84
+ end
85
+
86
+ it 'should encode body content requirements that are regexs' do
87
+ instance.required_body_content [/regex/]
88
+ JSON.parse(instance.to_json)["request"]["body_content"].should == %w(%r{regex})
89
+ end
90
+ end
91
+
92
+ describe 'required headers' do
93
+ it 'should contain expected headers' do
94
+ required_headers = {:header => "value"}
95
+ instance.required_headers required_headers
96
+ JSON.parse(instance.to_json)["request"]["headers"].should == convert_keys_to_strings(required_headers)
97
+ end
98
+
99
+ it 'should encode header requirements that are regexs' do
100
+ instance.required_headers({:header => /regex/})
101
+ JSON.parse(instance.to_json)["request"]["headers"].should == convert_keys_to_strings(:header => "%r{regex}")
102
+ end
103
+ end
104
+
105
+ describe 'delay' do
106
+ it 'should default to 0' do
107
+ JSON.parse(instance.to_json)["delay"].should == 0
108
+ end
109
+
110
+ it 'should set the delay' do
111
+ delay = 5
112
+ instance.delay delay
113
+ JSON.parse(instance.to_json)["delay"].should == delay
114
+ end
115
+ end
116
+
117
+ describe 'status code' do
118
+ it 'should default to 200' do
119
+ JSON.parse(instance.to_json)["response"]["status"].should == 200
120
+ end
121
+
122
+ it 'should set the status' do
123
+ status = 404
124
+ instance.status status
125
+ JSON.parse(instance.to_json)["response"]["status"].should == status
126
+ end
127
+ end
128
+
129
+ describe 'http method' do
130
+ it 'should default to get' do
131
+ JSON.parse(instance.to_json)["request"]["http_method"].should == "get"
132
+ end
133
+
134
+ it 'should set the http method' do
135
+ method = :post
136
+ instance.http_method(method)
137
+ JSON.parse(instance.to_json)["request"]["http_method"].should == "post"
138
+ end
139
+ end
140
+
141
+ describe 'response as default' do
142
+ it 'should be false by default' do
143
+ JSON.parse(instance.to_json)["response"]["default"].should == false
144
+ end
145
+
146
+ it 'should set the default value' do
147
+ default = true
148
+ instance.default(default)
149
+ JSON.parse(instance.to_json)["response"]["default"].should == default
150
+ end
151
+ end
152
+
153
+ describe 'content type' do
154
+ it 'should be text/plain by default' do
155
+ JSON.parse(instance.to_json)["response"]["content_type"].should == "text/plain"
156
+ end
157
+
158
+ it 'should set the default value' do
159
+ content_type = "application/json"
160
+ instance.content_type content_type
161
+ JSON.parse(instance.to_json)["response"]["content_type"].should == content_type
162
+ end
163
+ end
164
+
165
+ it 'should set headers' do
166
+ header, value = 'header', 'value'
167
+ instance.headers[header] = value
168
+ JSON.parse(instance.to_json)["response"]["headers"].should == {header => value}
169
+ end
170
+ end
171
+
172
+ end
@@ -0,0 +1,46 @@
1
+ require 'spec_helper'
2
+
3
+ describe Template::Model do
4
+ let!(:test_class) do
5
+ Class.new do
6
+ extend Template::Model
7
+ end
8
+ end
9
+ context 'class' do
10
+ it 'should extend ClassMethods and MethodBuilder' do
11
+ test_class.is_a?(Template::Model::ClassMethods).should == true
12
+ test_class.is_a?(Helpers::MethodBuilder).should == true
13
+ end
14
+ end
15
+
16
+ context 'instances' do
17
+ it 'should have instance methods included' do
18
+ test_class.ancestors.should include(Template::Model::InstanceMethods)
19
+ end
20
+
21
+
22
+ it 'should include httparty' do
23
+ test_class.ancestors.should include(HTTParty)
24
+ end
25
+
26
+ it 'provides a method for customising the endpoint of all instances' do
27
+ endpoint = 'endpoint'
28
+ test_class.endpoint endpoint
29
+ test_class.new.endpoint.should == endpoint
30
+ end
31
+
32
+ describe 'default status' do
33
+ it 'sets the status of instances if set' do
34
+ status = 404
35
+ test_class.status status
36
+ test_class.new.status.should == status
37
+ end
38
+
39
+ it 'status of instances retain the global default if status is not set at the class level' do
40
+ test_class.new.status.should == 200
41
+ end
42
+
43
+ end
44
+ end
45
+
46
+ end
@@ -99,8 +99,8 @@ describe Mirage::Template do
99
99
  request_url = "base_url/requests/#{id}"
100
100
 
101
101
  template = Template.new("", "")
102
- template.url = template_url
103
- template.requests_url = request_url
102
+ template.url template_url
103
+ template.requests_url request_url
104
104
 
105
105
 
106
106
  template.stub(:id).and_return(id)
@@ -112,138 +112,4 @@ describe Mirage::Template do
112
112
  end
113
113
 
114
114
  end
115
-
116
- describe "json representation" do
117
-
118
- describe 'response body' do
119
- it 'should base64 encode response values' do
120
- response = Template.new "endpoint", "value"
121
- JSON.parse(response.to_json)["response"]["body"].should == Base64.encode64("value")
122
- end
123
- end
124
-
125
- describe 'required request parameters' do
126
-
127
- it 'should contain expected request parameters' do
128
- response = Template.new "endpoint", "value"
129
- required_parameters = {:key => "value"}
130
- response.required_parameters = required_parameters
131
- JSON.parse(response.to_json)["request"]["parameters"].should == convert_keys_to_strings(required_parameters)
132
- end
133
-
134
- it 'should encode parameter requirements that are regexs' do
135
- response = Template.new "endpoint", "value"
136
- response.required_parameters = {:key => /regex/}
137
- JSON.parse(response.to_json)["request"]["parameters"].should == convert_keys_to_strings({:key => "%r{regex}"})
138
- end
139
- end
140
-
141
- describe 'required body content' do
142
- it 'should contain expected body content' do
143
- response = Template.new "endpoint", "value"
144
- required_body_content = ["body content"]
145
- response.required_body_content = required_body_content
146
- JSON.parse(response.to_json)["request"]["body_content"].should == required_body_content
147
- end
148
-
149
- it 'should encode body content requirements that are regexs' do
150
- response = Template.new "endpoint", "value"
151
- response.required_body_content = [/regex/]
152
- JSON.parse(response.to_json)["request"]["body_content"].should == %w(%r{regex})
153
- end
154
- end
155
-
156
- describe 'required headers' do
157
- it 'should contain expected headers' do
158
- response = Template.new "endpoint", "value"
159
- required_headers = {:header => "value"}
160
- response.required_headers = required_headers
161
- JSON.parse(response.to_json)["request"]["headers"].should == convert_keys_to_strings(required_headers)
162
- end
163
-
164
- it 'should encode header requirements that are regexs' do
165
- response = Template.new "endpoint", "value"
166
- response.required_headers = {:header => /regex/}
167
- JSON.parse(response.to_json)["request"]["headers"].should == convert_keys_to_strings(:header => "%r{regex}")
168
- end
169
- end
170
-
171
- describe 'delay' do
172
- it 'should default to 0' do
173
- response = Template.new "endpoint", "value"
174
- JSON.parse(response.to_json)["delay"].should == 0
175
- end
176
-
177
- it 'should set the delay' do
178
- delay = 5
179
- response = Template.new "endpoint", "value"
180
- response.delay = delay
181
- JSON.parse(response.to_json)["delay"].should == delay
182
- end
183
- end
184
-
185
- describe 'status code' do
186
- it 'should default to 200' do
187
- response = Template.new "endpoint", "value"
188
- JSON.parse(response.to_json)["response"]["status"].should == 200
189
- end
190
-
191
- it 'should set the status' do
192
- status = 404
193
- response = Template.new "endpoint", "value"
194
- response.status = status
195
- JSON.parse(response.to_json)["response"]["status"].should == status
196
- end
197
- end
198
-
199
- describe 'http method' do
200
- it 'should default to get' do
201
- response = Template.new "endpoint", "value"
202
- JSON.parse(response.to_json)["request"]["http_method"].should == "get"
203
- end
204
-
205
- it 'should set the http method' do
206
- method = :post
207
- response = Template.new "endpoint", "value"
208
- response.http_method = method
209
- JSON.parse(response.to_json)["request"]["http_method"].should == "post"
210
- end
211
- end
212
-
213
- describe 'response as default' do
214
- it 'should be false by default' do
215
- response = Template.new "endpoint", "value"
216
- JSON.parse(response.to_json)["response"]["default"].should == false
217
- end
218
-
219
- it 'should set the default value' do
220
- default = true
221
- response = Template.new "endpoint", "value"
222
- response.default = default
223
- JSON.parse(response.to_json)["response"]["default"].should == default
224
- end
225
- end
226
-
227
- describe 'content type' do
228
- it 'should be text/plain by default' do
229
- response = Template.new "endpoint", "value"
230
- JSON.parse(response.to_json)["response"]["content_type"].should == "text/plain"
231
- end
232
-
233
- it 'should set the default value' do
234
- content_type = "application/json"
235
- response = Template.new "endpoint", "value"
236
- response.content_type = content_type
237
- JSON.parse(response.to_json)["response"]["content_type"].should == content_type
238
- end
239
- end
240
-
241
- it 'should set headers' do
242
- header, value = 'header', 'value'
243
- template = Template.new 'endpoint', value
244
- template.headers[header] = value
245
- JSON.parse(template.to_json)["response"]["headers"].should == {header => value}
246
- end
247
-
248
- end
249
115
  end
@@ -54,23 +54,29 @@ describe 'templates' do
54
54
 
55
55
 
56
56
 
57
- context 'template as parameter' do
57
+
58
+ context 'model as parameter' do
59
+ let!(:endpoint){'endpoint'}
60
+ let!(:model_class) do
61
+ Class.new do
62
+ extend Template::Model
63
+ endpoint endpoint
64
+ end
65
+ end
58
66
  before :each do
59
67
  @base_url = "base_url"
60
68
  @templates = Templates.new(@base_url)
61
69
  end
62
-
63
- it 'should take a preconfigured template as a parameter' do
64
-
65
- template = Template.new 'endpoint', 'value'
70
+ it 'should take a model as a parameter' do
71
+ template = model_class.new
66
72
  template.should_receive(:create)
67
73
  @templates.put template
68
- template.endpoint.should == "#{@base_url}/templates/endpoint"
74
+ template.endpoint.should == "#{@base_url}/templates/#{endpoint}"
69
75
  end
70
76
 
71
77
  it 'should accept a block to allow the template to be customised' do
72
78
  block_called = false
73
- template = Template.new 'endpoint', 'value'
79
+ template = model_class.new
74
80
  template.should_receive(:create)
75
81
  @templates.put(template) do |the_same_template|
76
82
  block_called = true
data/test.rb CHANGED
@@ -1,27 +1,18 @@
1
1
  require './lib/mirage/client'
2
2
 
3
+ class ServiceNowResponse
4
+ extend Mirage::Template::Model
3
5
 
4
- mirage = Mirage.start
5
-
6
- #mirage.clear
7
- #mirage.put('some/path/greeting', 'hello') do |response|
8
- # response.http_method = :post
9
- #end
10
- #
11
- #template = mirage.put('some/path/greeting', 'hello Michele') do |response|
12
- # response.http_method = :post
13
- # response.required_parameters['name']='Michele'
14
- # response.required_body_content << 'stara'
15
- # response.required_headers['Custom-Header']='special'
16
- #end
17
- #
18
-
19
-
20
-
21
-
22
-
23
-
24
-
6
+ endpoint 'service_now'
25
7
 
8
+ builder_methods :this,:that
26
9
 
10
+ def value
11
+ "my value : #{this}, #{that}"
12
+ end
13
+ end
27
14
 
15
+ Mirage.stop
16
+ mirage = Mirage.start
17
+ mirage.put ServiceNowResponse.new.this('foo').that('bar')
18
+ mirage.put ServiceNowResponse.new.this('foo').that('bar').required_body_content(%w(hello world))
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.3
4
+ version: 3.0.0.alpha.4
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-04-21 00:00:00.000000000 Z
12
+ date: 2013-04-24 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: sinatra
@@ -287,14 +287,19 @@ files:
287
287
  - lib/mirage/client/cli_bridge.rb
288
288
  - lib/mirage/client/client.rb
289
289
  - lib/mirage/client/error.rb
290
+ - lib/mirage/client/helpers/method_builder.rb
290
291
  - lib/mirage/client/request.rb
291
292
  - lib/mirage/client/requests.rb
292
293
  - lib/mirage/client/runner.rb
293
294
  - lib/mirage/client/template.rb
294
- - lib/mirage/client/template_configuration.rb
295
+ - lib/mirage/client/template/configuration.rb
296
+ - lib/mirage/client/template/model.rb
297
+ - lib/mirage/client/template/model/class_methods.rb
298
+ - lib/mirage/client/template/model/instance_methods.rb
295
299
  - lib/mirage/client/templates.rb
296
300
  - mirage.gemspec
297
301
  - mirage_server.rb
302
+ - profiles.rb
298
303
  - rakefile
299
304
  - responses/default_responses.rb
300
305
  - server/app.rb
@@ -306,10 +311,14 @@ files:
306
311
  - server/server.rb
307
312
  - spec/client/cli_bridge_spec.rb
308
313
  - spec/client/client_spec.rb
314
+ - spec/client/helpers/method_builder_spec.rb
309
315
  - spec/client/request_spec.rb
310
316
  - spec/client/requests_spec.rb
311
317
  - spec/client/runner_spec.rb
312
- - spec/client/template_configuration_spec.rb
318
+ - spec/client/template/configuration_spec.rb
319
+ - spec/client/template/model/class_methods_spec.rb
320
+ - spec/client/template/model/instance_methods_spec.rb
321
+ - spec/client/template/model_spec.rb
313
322
  - spec/client/template_spec.rb
314
323
  - spec/client/templates_spec.rb
315
324
  - spec/resources/binary.file
@@ -340,7 +349,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
340
349
  version: '0'
341
350
  segments:
342
351
  - 0
343
- hash: -894267295153814806
352
+ hash: -1427932946480660425
344
353
  required_rubygems_version: !ruby/object:Gem::Requirement
345
354
  none: false
346
355
  requirements:
@@ -1,22 +0,0 @@
1
- module Mirage
2
- class TemplateConfiguration
3
- attr_accessor :http_method, :status, :delay, :content_type, :default
4
- DEFAULT_HTTP_METHOD=:get
5
- DEFAULT_STATUS=200
6
- DEFAULT_DELAY=0
7
- DEFAULT_CONTENT_TYPE="text/plain"
8
- DEFAULT_DEFAULT=false
9
-
10
- def initialize
11
- reset
12
- end
13
-
14
- def reset
15
- @http_method = DEFAULT_HTTP_METHOD
16
- @status = DEFAULT_STATUS
17
- @delay = DEFAULT_DELAY
18
- @content_type = DEFAULT_CONTENT_TYPE
19
- @default = DEFAULT_DEFAULT
20
- end
21
- end
22
- end