Permalink
Browse files

Use tables to deal with huge responses.

Instead of accumulating the response in a string, use a table. This reduces the chances of "out of memory" errors when testing endpoints that produces huge amounts of data.

It also changes the call to `common.send_content` to match the way it is used in wsapi itself.
  • Loading branch information...
ignacio committed Sep 25, 2013
1 parent de611a0 commit ba09fe6389b8d65995fd5b0db8df119d40b824c5
Showing with 9 additions and 6 deletions.
  1. +9 −6 src/wsapi/mock.lua
View
@@ -55,20 +55,23 @@ end
-- Override common's output handler to avoid writing headers
-- in the reponse body.
function common.send_output(out, status, headers, res_iter, write_method,res_line)
common.send_content(out, res_iter, out:write())
common.send_content(out, res_iter, "write")
end
-- Mock IO objects
local function make_io_object(content)
local receiver = { buffer = content or "", bytes_read = 0 }
local receiver = { buffer = { content }, bytes_read = 0 }
function receiver:write(content)
if content then
self.buffer = self.buffer .. content
end
self.buffer[#self.buffer + 1] = content
return true
end
function receiver:read(len)
-- first read will turn the buffer into a string
if type(self.buffer) == "table" then
self.buffer = table.concat(self.buffer)
end
len = len or (#self.buffer - self.bytes_read)
if self.bytes_read >= #self.buffer then return nil end
local s = self.buffer:sub(self.bytes_read + 1, self.bytes_read + len)
@@ -78,7 +81,7 @@ local function make_io_object(content)
end
function receiver:clear()
self.buffer = ""
self.buffer = {}
self.bytes_read = 0
end

0 comments on commit ba09fe6

Please sign in to comment.