Skip to content

Commit

Permalink
Merge pull request #6 from khodzha/trailing_whitespace
Browse files Browse the repository at this point in the history
trim trailing whitespace from raw requests
  • Loading branch information
cosmin-harangus committed Nov 9, 2018
2 parents 5562206 + 3a2c786 commit 5f92b65
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
16 changes: 15 additions & 1 deletion lib/eximap/imap/request.ex
Expand Up @@ -5,7 +5,21 @@ defmodule Eximap.Imap.Request do
defstruct tag: "TAG", command: nil, params: []

def add_tag(req, tag), do: %Eximap.Imap.Request{req | tag: tag}
def raw(req), do: "#{req.tag} #{req.command} #{Enum.join(req.params, " ")}\r\n"

def raw(req) do
params =
case req.params do
[] -> nil
_ -> Enum.join(req.params, " ")
end
s =
[req.tag, req.command, params]
|> Enum.filter(& &1)
|> Enum.map(&to_string(&1))
|> Enum.join(" ")
s <> "\r\n"
end


@doc ~S"""
The NOOP command always succeeds. It does nothing.
Expand Down
16 changes: 16 additions & 0 deletions test/eximap/request_test.exs
@@ -0,0 +1,16 @@
defmodule RequestTest do
use ExUnit.Case
alias Eximap.Imap.Request

test "NOOP shouldnt have trailing whitespace before CRLF" do
noop = Request.noop()
assert Request.raw(noop) == "#{noop.tag} NOOP\r\n"
end

test "trailing CRLFs in APPEND params shouldnt be trimmed" do
text = "Delivered-To: test@localhost\r\n\r\nTest\r\n"
msize = byte_size(text)
req = Request.append("INBOX", ["\{#{msize}+\}\r\n", text])
assert Request.raw(req) == "#{req.tag} APPEND INBOX " <> "\{#{msize}+\}\r\n" <> text <> "\r\n"
end
end

0 comments on commit 5f92b65

Please sign in to comment.