-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from wneessen/subcap-test
Added README.md and test coverage to subject_capitalize
- Loading branch information
Showing
2 changed files
with
66 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<!-- | ||
SPDX-FileCopyrightText: 2022 Winni Neessen <[email protected]> | ||
SPDX-License-Identifier: CC0-1.0 | ||
--> | ||
|
||
## Capitalize your subject based on a given language | ||
|
||
This is a simple middlware that makes use of the powerful [golang.org/x/text/cases](https://golang.org/x/text/cases) | ||
library. It will read the currently set subject of the `mail.Msg` and use the `cases` library to capitalize the | ||
subject based on the given language. | ||
|
||
### Example | ||
```go | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"github.com/wneessen/go-mail" | ||
"github.com/wneessen/go-mail-middleware/subject_capitalize" | ||
"golang.org/x/text/language" | ||
"os" | ||
) | ||
|
||
func main() { | ||
m := mail.NewMsg(mail.WithMiddleware(subcap.New(language.English))) | ||
m.Subject("this is a test message") | ||
if err := m.WriteToFile("testmail.eml"); err != nil { | ||
fmt.Printf("failed to write mail message to file: %s\n", err) | ||
os.Exit(1) | ||
} | ||
} | ||
``` |
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 |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// SPDX-FileCopyrightText: 2022 Winni Neessen <[email protected]> | ||
// | ||
// SPDX-License-Identifier: MIT | ||
|
||
package subcap | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"github.com/wneessen/go-mail" | ||
"golang.org/x/text/language" | ||
"strings" | ||
"testing" | ||
) | ||
|
||
func TestNew(t *testing.T) { | ||
mw := New(language.English) | ||
if fmt.Sprintf("%s", mw.l) != "en" { | ||
t.Errorf("New() failed. Expected language: %q, got: %q", "en", fmt.Sprintf("%s", mw.l)) | ||
} | ||
} | ||
|
||
func TestMiddleware_Handle(t *testing.T) { | ||
m := mail.NewMsg(mail.WithMiddleware(New(language.English))) | ||
m.Subject("this is a test") | ||
buf := bytes.Buffer{} | ||
if _, err := m.WriteTo(&buf); err != nil { | ||
t.Errorf("failed to write mail message to buffer: %s", err) | ||
} | ||
if !strings.Contains(buf.String(), "This Is A Test") { | ||
t.Errorf("middleware failed. Expected: %q in subject, got: %q", "This Is A Test", buf.String()) | ||
} | ||
} |