-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(GODT-2427): Fix header parsing logic
Ensure the header parser treats the sequence `\r\n ` as a fold in accordance to RFC5322. The original tests for the header parser did not adhere to this convention and led to the incorrect parser logic.
- Loading branch information
1 parent
be34573
commit 72aeeb9
Showing
2 changed files
with
82 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
package rfc822 | ||
|
||
import ( | ||
"strings" | ||
"testing" | ||
|
||
"github.com/bradenaw/juniper/xslices" | ||
|
@@ -188,35 +189,35 @@ func TestParseHeaderFoldedLine(t *testing.T) { | |
} | ||
|
||
func TestParseHeaderMultilineFilename(t *testing.T) { | ||
const literal = "Content-Type: application/msword; name=\"this is a very long\nfilename.doc\"" | ||
const literal = "Content-Type: application/msword; name=\"this is a very long\n filename.doc\"" | ||
|
||
header, err := NewHeader([]byte(literal)) | ||
require.NoError(t, err) | ||
|
||
assert.Equal(t, [][]byte{ | ||
[]byte("Content-Type: application/msword; name=\"this is a very long\nfilename.doc\""), | ||
[]byte("Content-Type: application/msword; name=\"this is a very long\n filename.doc\""), | ||
}, header.getLines()) | ||
} | ||
|
||
func TestParseHeaderMultilineFilenameWithColon(t *testing.T) { | ||
const literal = "Content-Type: application/msword; name=\"this is a very long\nfilename: too long.doc\"" | ||
const literal = "Content-Type: application/msword; name=\"this is a very long\n filename: too long.doc\"" | ||
|
||
header, err := NewHeader([]byte(literal)) | ||
require.NoError(t, err) | ||
|
||
assert.Equal(t, [][]byte{ | ||
[]byte("Content-Type: application/msword; name=\"this is a very long\nfilename: too long.doc\""), | ||
[]byte("Content-Type: application/msword; name=\"this is a very long\n filename: too long.doc\""), | ||
}, header.getLines()) | ||
} | ||
|
||
func TestParseHeaderMultilineFilenameWithColonAndNewline(t *testing.T) { | ||
const literal = "Content-Type: application/msword; name=\"this is a very long\nfilename: too long.doc\"\n" | ||
const literal = "Content-Type: application/msword; name=\"this is a very long\n filename: too long.doc\"\n" | ||
|
||
header, err := NewHeader([]byte(literal)) | ||
require.NoError(t, err) | ||
|
||
assert.Equal(t, [][]byte{ | ||
[]byte("Content-Type: application/msword; name=\"this is a very long\nfilename: too long.doc\"\n"), | ||
[]byte("Content-Type: application/msword; name=\"this is a very long\n filename: too long.doc\"\n"), | ||
}, header.getLines()) | ||
} | ||
|
||
|
@@ -234,20 +235,20 @@ func TestParseHeaderMultilineIndent(t *testing.T) { | |
|
||
func TestParseHeaderMultipleMultilineFilenames(t *testing.T) { | ||
const literal = `Content-Type: application/msword; name="=E5=B8=B6=E6=9C=89=E5=A4=96=E5=9C=8B=E5=AD=97=E7=AC=A6=E7=9A=84=E9=99=84=E4= | ||
=BB=B6.DOC" | ||
=BB=B6.DOC" | ||
Content-Transfer-Encoding: base64 | ||
Content-Disposition: attachment; filename="=E5=B8=B6=E6=9C=89=E5=A4=96=E5=9C=8B=E5=AD=97=E7=AC=A6=E7=9A=84=E9=99=84=E4= | ||
=BB=B6.DOC" | ||
=BB=B6.DOC" | ||
Content-ID: <> | ||
` | ||
|
||
header, err := NewHeader([]byte(literal)) | ||
require.NoError(t, err) | ||
|
||
assert.Equal(t, [][]byte{ | ||
[]byte("Content-Type: application/msword; name=\"=E5=B8=B6=E6=9C=89=E5=A4=96=E5=9C=8B=E5=AD=97=E7=AC=A6=E7=9A=84=E9=99=84=E4=\n=BB=B6.DOC\"\n"), | ||
[]byte("Content-Type: application/msword; name=\"=E5=B8=B6=E6=9C=89=E5=A4=96=E5=9C=8B=E5=AD=97=E7=AC=A6=E7=9A=84=E9=99=84=E4=\n =BB=B6.DOC\"\n"), | ||
[]byte("Content-Transfer-Encoding: base64\n"), | ||
[]byte("Content-Disposition: attachment; filename=\"=E5=B8=B6=E6=9C=89=E5=A4=96=E5=9C=8B=E5=AD=97=E7=AC=A6=E7=9A=84=E9=99=84=E4=\n=BB=B6.DOC\"\n"), | ||
[]byte("Content-Disposition: attachment; filename=\"=E5=B8=B6=E6=9C=89=E5=A4=96=E5=9C=8B=E5=AD=97=E7=AC=A6=E7=9A=84=E9=99=84=E4=\n =BB=B6.DOC\"\n"), | ||
[]byte("Content-ID: <>\n"), | ||
}, header.getLines()) | ||
} | ||
|
@@ -352,3 +353,40 @@ func TestHeader_Erase(t *testing.T) { | |
assert.Equal(t, literal, newLiteral) | ||
} | ||
} | ||
|
||
func TestHeader_SubjectWithRandomQuote(t *testing.T) { | ||
raw := lines(`Subject: All " your " random " brackets " ' ' : belong to us () {}`, | ||
`Date: Sun, 30 Jan 2000 11:49:30 +0700`, | ||
`Content-Type: multipart/alternative; boundary="----=_BOUNDARY_"`) | ||
|
||
header, err := NewHeader(raw) | ||
require.NoError(t, err) | ||
|
||
require.Equal( | ||
t, | ||
`All " your " random " brackets " ' ' : belong to us () {}`, | ||
header.Get("Subject"), | ||
) | ||
} | ||
|
||
func lines(s ...string) []byte { | ||
return append([]byte(strings.Join(s, "\r\n")), '\r', '\n') | ||
} | ||
|
||
func TestHeader_WithTrailingSpaces(t *testing.T) { | ||
const literal = `From: Nathaniel Borenstein <[email protected]> | ||
To: Ned Freed <[email protected]> | ||
Subject: Sample message | ||
MIME-Version: 1.0 | ||
Content-type: multipart/mixed; boundary="simple boundary" | ||
` | ||
|
||
header, err := NewHeader([]byte(literal)) | ||
require.NoError(t, err) | ||
|
||
require.Equal(t, "Nathaniel Borenstein <[email protected]>", header.Get("From")) | ||
require.Equal(t, "Ned Freed <[email protected]>", header.Get("To")) | ||
require.Equal(t, "Sample message", header.Get("Subject")) | ||
require.Equal(t, "1.0", header.Get("MIME-Version")) | ||
require.Equal(t, `multipart/mixed; boundary="simple boundary"`, header.Get("Content-type")) | ||
} |