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
|
# File 'lib/http_sim.rb', line 54
def self.register_endpoint(method, path, default_response)
raise '/ is a reserved path' if path == '/'
endpoint = Endpoint.new(method, path, default_response)
@@endpoints.push endpoint
case endpoint.method
when 'GET'
Sinatra::Base.get endpoint.path do
endpoint.add_request request.body.read
endpoint.default_response
end
when 'PUT'
Sinatra::Base.put endpoint.path do
endpoint.add_request request.body.read
endpoint.default_response
end
when 'PATCH'
Sinatra::Base.patch endpoint.path do
endpoint.add_request request.body.read
endpoint.default_response
end
when 'POST'
Sinatra::Base.post endpoint.path do
endpoint.add_request request.body.read
endpoint.default_response
end
when 'DELETE'
Sinatra::Base.delete endpoint.path do
endpoint.add_request request.body.read
endpoint.default_response
end
end
Sinatra::Base.get "#{endpoint.path}/response" do
ERB.new(@@erb_files[:response]).result binding
end
Sinatra::Base.put "#{endpoint.path}/response" do
endpoint.response = request.body.read
end
Sinatra::Base.delete "#{endpoint.path}/response" do
endpoint.response = endpoint.default_response
end
Sinatra::Base.get "#{endpoint.path}/requests" do
ERB.new(@@erb_files[:request]).result binding
end
Sinatra::Base.delete "#{endpoint.path}/requests" do
endpoint.requests = []
end
end
|