I’m trying to build a simple JSON service that routes like Roda to HTTP::Handler
-like instances:
class App
include HTTP::Handler
include Routing
def call(context)
route context do |r, response, session|
r.root { HomePage.new.call context }
r.on "api" { API.new.call context }
# other routes...
end
end
end
Since it’s just passing off the context, I’m looking for a way to unit-test these individual routes, building up the HTTP::Server::Context
myself and stubbing out other services. Unfortunately, when I parse the response in this spec:
describe "GET /people" do
it "returns a list of people" do
io = IO::Memory.new
response = HTTP::Server::Response.new io
context = HTTP::Server::Context.new(
request: HTTP::Request.new("GET", "/people"),
response: response,
)
response.flush
ExampleWebService::App.new.call context
io.rewind.to_s STDOUT
response_body = JSON.parse(io.rewind)
response_body.should eq({
people: [
{ id: 123, name: "Jamie" },
],
})
end
end
The response IO
is empty, so I get a JSON parsing error in the spec. This is the code:
module ExampleWebService
class App
include HTTP::Handler
def call(context)
{
people: [
Person.new(id: 123, name: "Jamie"),
],
}.to_json context.response
end
end
It’s all hardcoded right now while I flesh out the pattern of unit-testing it, so there’s nothing really to go wrong. When I run it through an HTTP::Server
, it works just fine, it just seems like the data isn’t making it to the IO
instance inside the spec. I’m using response.flush
, is there something else I need to be doing there?