How to Parse (not get) an HTTP Request in Ruby -


i have integration test uses eventmachine receive http requests. eventmachine handler:

class notificationrecipient < em::connection   def receive_data(data)     em.stop   end end 

i need test various properies of received request, e.g. want extract json payload http post request string i've received this. there nicely packaged way it?

googling finds lots of ways make request , parse response, e.g. rest-client parse response automatically. however, since i'm receiving request, not making it, none of these ways work me.

i make use of webrick. webrick::httprequest has serviceable parser, , need pass io object parse method , have object can manipulate.

this example declares post request json body in string , uses stringio make accessible io object.

require 'webrick' require 'stringio'  request = <<-http post /url/path http/1.1 host: my.hostname.com content-type: application/json content-length: 62  {   "firstname": "john",   "lastname": "smith",   "age": 25 } http  req = webrick::httprequest.new(webrick::config::http) req.parse(stringio.new(request))  puts req.path req.each { |head| puts "#{head}:  #{req[head]}" } puts req.body 

output

/url/path host:  my.hostname.com content-type:  application/json content-length:  62 {   "firstname": "john",   "lastname": "smith",   "age": 25 } 

Comments