/
fake_host.rb
216 lines (178 loc) · 4.54 KB
/
fake_host.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
require 'sinatra/base'
require 'mimic/api'
module Mimic
class FakeHost
attr_reader :hostname, :url_map
attr_accessor :log
def initialize(options = {})
@hostname = options[:hostname]
@remote_configuration_path = options[:remote_configuration_path]
@log = options[:log]
@imports = []
clear
build_url_map!
end
def received_requests
@stubs.select { |s| s.received }
end
def get(path, &block)
request("GET", path, &block)
end
def post(path, &block)
request("POST", path, &block)
end
def put(path, &block)
request("PUT", path, &block)
end
def delete(path, &block)
request("DELETE", path, &block)
end
def head(path, &block)
request("HEAD", path, &block)
end
def import(path)
if File.exists?(path)
@imports << path unless @imports.include?(path)
instance_eval(File.read(path))
else
raise "Could not locate file for stub import: #{path}"
end
end
def call(env)
@stubs.each(&:build)
@app.call(env)
end
def clear
@stubs = []
@app = Sinatra.new
@app.use Rack::CommonLogger, self.log if self.log
@app.not_found do
[404, {}, ""]
end
@app.helpers do
include Helpers
end
@imports.each { |file| import(file) }
end
def inspect
@stubs.inspect
end
private
def method_missing(method, *args, &block)
@app.send(method, *args, &block)
end
def request(method, path, &block)
if block_given?
@app.send(method.downcase, path, &block)
else
@stubs << StubbedRequest.new(@app, method, path)
@stubs.last
end
end
def build_url_map!
routes = {'/' => self}
if @remote_configuration_path
API.host = self
routes[@remote_configuration_path] = API
end
@url_map = Rack::URLMap.new(routes)
end
module Helpers
def echo_request!(format)
RequestEcho.new(request).response_as(format)
end
end
class RequestEcho
def initialize(request)
@request = request
end
def response_as(format)
content_type = case format
when :json, :plist
"application/#{format.to_s.downcase}"
else
"text/plain"
end
[200, {"Content-Type" => content_type}, to_s(format)]
end
def to_s(format)
case format
when :json
to_hash.to_json
when :plist
to_hash.to_plist
when :text
to_hash.inspect
end
end
def to_hash
{"echo" => {
"params" => @request.params,
"env" => env_without_rack_and_async_env,
"body" => @request.body.read
}}
end
private
def env_without_rack_and_async_env
Hash[*@request.env.select { |key, value| key !~ /^(rack|async)/i }.flatten]
end
end
class StubbedRequest
attr_accessor :received
def initialize(app, method, path)
@method, @path = method, path
@code = 200
@headers = {}
@params = {}
@body = ""
@app = app
@received = false
end
def to_hash
token = "#{@method} #{@path}"
Digest::MD5.hexdigest(token)
end
def returning(body, code = 200, headers = {})
tap do
@body = body
@code = code
@headers = headers
end
end
def with_query_parameters(params)
tap do
@params = params
end
end
def echo_request!(format = :json)
@echo_request_format = format
end
def matches?(request)
if @params.any?
request.params == @params
else
true
end
end
def matched_response
[@code, @headers, @body]
end
def unmatched_response
[404, "", {}]
end
def response_for_request(request)
if @echo_request_format
@body = RequestEcho.new(request).to_s(@echo_request_format)
end
matches?(request) ? matched_response : unmatched_response
end
def build
stub = self
@app.send(@method.downcase, @path) do
stub.received = true
stub.response_for_request(request)
end
end
end
end
end