mirror of
https://github.com/jhillyerd/inbucket.git
synced 2025-12-17 09:37:02 +00:00
More MIME dev, new unit test libs
MIME parsing code is now in an acceptable state to integrate into web UI. It should be able to display text and/or HTML (sans attachments). Added "testify" unit testing library, providing assertions and mocks.
This commit is contained in:
@@ -64,6 +64,21 @@ func (n *MIMENode) String() string {
|
||||
return fmt.Sprintf("[%v %v] %v", n.Type, children, siblings)
|
||||
}
|
||||
|
||||
func IsMIMEMessage(mailMsg *mail.Message) bool {
|
||||
// Parse top-level multipart
|
||||
ctype := mailMsg.Header.Get("Content-Type")
|
||||
mediatype, _, err := mime.ParseMediaType(ctype)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
switch mediatype {
|
||||
case "multipart/alternative":
|
||||
return true
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
func ParseMIMEMessage(mailMsg *mail.Message) (*MIMEMessage, error) {
|
||||
mimeMsg := new(MIMEMessage)
|
||||
|
||||
@@ -86,9 +101,7 @@ func ParseMIMEMessage(mailMsg *mail.Message) (*MIMEMessage, error) {
|
||||
|
||||
// Root Node of our tree
|
||||
root := NewMIMENode(nil, mediatype)
|
||||
|
||||
err = parseNodes(root, mailMsg.Body, boundary)
|
||||
fmt.Println(root.String())
|
||||
|
||||
// Locate text body
|
||||
match := root.BreadthFirstSearch(func(node *MIMENode) bool {
|
||||
@@ -106,8 +119,6 @@ func ParseMIMEMessage(mailMsg *mail.Message) (*MIMEMessage, error) {
|
||||
mimeMsg.Html = string(match.Content)
|
||||
}
|
||||
|
||||
fmt.Println(mimeMsg.String())
|
||||
|
||||
return mimeMsg, err
|
||||
}
|
||||
|
||||
|
||||
@@ -2,27 +2,68 @@ package inbucket
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"github.com/stretchrcom/testify/assert"
|
||||
"net/mail"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestSomething(t *testing.T) {
|
||||
// Open test email for parsing
|
||||
raw, err := os.Open("../../test-data/html-mime-attach.raw")
|
||||
func TestIdentifyNonMime(t *testing.T) {
|
||||
msg := readMessage("non-mime.raw")
|
||||
assert.False(t, IsMIMEMessage(msg), "Failed to identify non-MIME message")
|
||||
}
|
||||
|
||||
func TestIdentifyMime(t *testing.T) {
|
||||
msg := readMessage("html-mime-inline.raw")
|
||||
assert.True(t, IsMIMEMessage(msg), "Failed to identify MIME message")
|
||||
}
|
||||
|
||||
func TestParseNonMime(t *testing.T) {
|
||||
msg := readMessage("non-mime.raw")
|
||||
|
||||
_, err := ParseMIMEMessage(msg)
|
||||
assert.NotNil(t, err, "Expected error parsing a non-MIME message")
|
||||
}
|
||||
|
||||
func TestParseInlineText(t *testing.T) {
|
||||
msg := readMessage("html-mime-inline.raw")
|
||||
|
||||
mime, err := ParseMIMEMessage(msg)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to open test data: %v", err)
|
||||
t.Fatalf("Failed to parse mime: %v", err)
|
||||
}
|
||||
|
||||
assert.Equal(t, mime.Text, "Test of HTML section")
|
||||
}
|
||||
|
||||
func TestParseInlineHtml(t *testing.T) {
|
||||
msg := readMessage("html-mime-inline.raw")
|
||||
|
||||
mime, err := ParseMIMEMessage(msg)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to parse mime: %v", err)
|
||||
}
|
||||
|
||||
assert.Contains(t, mime.Html, "<html>")
|
||||
assert.Contains(t, mime.Html, "Test of HTML section")
|
||||
}
|
||||
|
||||
// readMessage is a test utility function to fetch a mail.Message object.
|
||||
func readMessage(filename string) *mail.Message {
|
||||
// Open test email for parsing
|
||||
raw, err := os.Open(filepath.Join("..", "..", "test-data", filename))
|
||||
if err != nil {
|
||||
panic(fmt.Sprintf("Failed to open test data: %v", err))
|
||||
}
|
||||
|
||||
// Parse email into a mail.Message object like we do
|
||||
reader := bufio.NewReader(raw)
|
||||
msg, err := mail.ReadMessage(reader)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to read message: %v", err)
|
||||
panic(fmt.Sprintf("Failed to read message: %v", err))
|
||||
}
|
||||
|
||||
_, err = ParseMIMEMessage(msg)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to parse mime: %v", err)
|
||||
}
|
||||
return msg
|
||||
}
|
||||
|
||||
8
test-data/non-mime.raw
Normal file
8
test-data/non-mime.raw
Normal file
@@ -0,0 +1,8 @@
|
||||
Date: Sun, 14 Oct 2012 16:09:01 -0700
|
||||
To: greg@inbucket.com
|
||||
From: James Hillyerd <james@hillyerd.com>
|
||||
Subject: test Sun, 14 Oct 2012 16:09:01 -0700
|
||||
X-Mailer: swaks v20120320.0 jetmore.org/john/code/swaks/
|
||||
|
||||
This is a test mailing
|
||||
|
||||
Reference in New Issue
Block a user