Skip to content

Instantly share code, notes, and snippets.

@robdbirch
Last active August 29, 2015 14:15
Show Gist options
  • Save robdbirch/f775fcccdba31f803b19 to your computer and use it in GitHub Desktop.
Save robdbirch/f775fcccdba31f803b19 to your computer and use it in GitHub Desktop.
Simple http to serve up mock JSON for simple integration testing
#!/usr/bin/env ruby
require 'optparse'
require 'webrick'
require 'pp'
options = { :port => 8080, :root => '.' }
OptionParser.new do |opts|
opts.banner = 'Usage: dataServicesServer.rb -p port -r web_root'
opts.on('-p', '--port', 'Port') do |p|
options[:port] = p
end
opts.on('-r', '--root-dir', '') do |r|
options[:root] = r
end
end.parse!
server = WEBrick::HTTPServer.new :Port => options[:port], :DocumentRoot => options[:root]
trap 'INT' do server.shutdown end
server.mount_proc '/dataServices' do |req, res|
file = '.' + req.path
if (! File.directory? file) && (File.exists? file)
json = File.read file
res['Content-Type'] = 'application/json'
res.body = json
res.status = 200
else
res.status = 404
res['Content-Type'] = 'text/html'
res.body = '<html><body><h1>Not Found</h1></body></html>'
end
end
server.start
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment