feat: enter and exit handlers

This commit is contained in:
James Houlahan
2020-07-29 13:59:52 +02:00
parent 8bd74c5edc
commit a7da66ccbc
6 changed files with 269 additions and 90 deletions

View File

@ -13,12 +13,12 @@ func TestWalker(t *testing.T) {
walker := p.
NewWalker().
WithDefaultHandler(func(p *Part) (err error) {
WithDefaultHandler(NewPartHandler().OnEnter(func(p *Part) (err error) {
if p.Body != nil {
allBodies = append(allBodies, p.Body)
}
return
})
}))
assert.NoError(t, walker.Walk())
assert.ElementsMatch(t, [][]byte{
@ -32,9 +32,11 @@ func TestWalkerTypeHandler(t *testing.T) {
html := [][]byte{}
walker := p.
NewWalker().
WithContentTypeHandler("text/html", func(p *Part) (err error) {
walker := p.NewWalker()
walker.
RegisterContentTypeHandler("text/html").
OnEnter(func(p *Part) (err error) {
html = append(html, p.Body)
return
})
@ -50,9 +52,11 @@ func TestWalkerDispositionHandler(t *testing.T) {
attachments := [][]byte{}
walker := p.
NewWalker().
WithContentDispositionHandler("attachment", func(p *Part, hdl PartHandler) (err error) {
walker := p.NewWalker()
walker.
RegisterContentDispositionHandler("attachment").
OnEnter(func(p *Part, hdl PartHandlerFunc) (err error) {
attachments = append(attachments, p.Body)
return
})
@ -62,3 +66,25 @@ func TestWalkerDispositionHandler(t *testing.T) {
[]byte("if you are reading this, hi!"),
}, attachments)
}
func TestWalkerDispositionAndTypeHandler(t *testing.T) {
p := newTestParser(t, "text_html_octet_attachment.eml")
walker := p.NewWalker()
var enter, exit int
walker.
RegisterContentTypeHandler("application/octet-stream").
OnEnter(func(p *Part) (err error) { enter++; return }).
OnExit(func(p *Part) (err error) { exit--; return })
walker.
RegisterContentDispositionHandler("attachment").
OnEnter(func(p *Part, hdl PartHandlerFunc) (err error) { _ = hdl(p); _ = hdl(p); return }).
OnExit(func(p *Part, hdl PartHandlerFunc) (err error) { _ = hdl(p); _ = hdl(p); return })
assert.NoError(t, walker.Walk())
assert.Equal(t, 2, enter)
assert.Equal(t, -2, exit)
}