Skip to content

Instantly share code, notes, and snippets.

@bosb
Created March 12, 2018 15:12
Show Gist options
  • Save bosb/aecbd09e302473711a8b6d9c5756a7d6 to your computer and use it in GitHub Desktop.
Save bosb/aecbd09e302473711a8b6d9c5756a7d6 to your computer and use it in GitHub Desktop.
API stubbing for frontend testing: webserver method
require 'ap'
require 'json'
require 'webrick'
# API stubbing for frontend testing: webserver method
# Thorsten Bosbach 03/2018
if ARGV.count != 3
ap "Usage: ruby webserver.rb port request-path file"
# ruby webserver.rb 7080 /notification-center/api/nc profiles_vomp_mixed_aggregated.json
return
end
puts "port: #{ARGV[0]}\n"
puts "path: #{ARGV[1]}\n"
puts "file: #{ARGV[2]}\n"
server = WEBrick::HTTPServer.new(
:Port => ARGV[0],
)
# will respond to this and all paths below. e.g. set to /foo, will also answer for /foo/bar
server.mount_proc "#{ARGV[1]}" do |req, res|
ap req.path
file = File.expand_path("../#{ARGV[2]}", __FILE__)
http_body = File.read(file)
JSON.parse(http_body) # 'validation'
res.body = http_body
end
server.start
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment