Skip to content

Commit

Permalink
add base64 encoding capabilities
Browse files Browse the repository at this point in the history
  • Loading branch information
JeanMertz committed Mar 23, 2017
1 parent 8fee586 commit 618eb88
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
7 changes: 7 additions & 0 deletions epp.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"encoding/base64"
"flag"
"fmt"
"io/ioutil"
Expand Down Expand Up @@ -31,6 +32,8 @@ func main() {
os.Exit(1)
}

pongo2.RegisterFilter("b64enc", filterBase64Encode)

fileContents, err := readInput(flag.Arg(0))
if err != nil {
fmt.Fprintf(os.Stderr, "IO error: %s\n", err)
Expand Down Expand Up @@ -85,3 +88,7 @@ func environToContext() pongo2.Context {

return ctx
}

func filterBase64Encode(in *pongo2.Value, param *pongo2.Value) (*pongo2.Value, *pongo2.Error) {
return pongo2.AsValue(base64.StdEncoding.EncodeToString([]byte(in.String()))), nil
}
16 changes: 16 additions & 0 deletions epp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"fmt"
"os"
"testing"

"github.com/flosch/pongo2"
)

func init() {
Expand Down Expand Up @@ -49,3 +51,17 @@ I should!
t.Errorf("bad expansion: expected '%s', got '%s'", expected, res)
}
}

func TestFilterBase64Encode(t *testing.T) {
actualResult, err := filterBase64Encode(pongo2.AsValue("Hello"), pongo2.AsValue(""))

if err != nil {
t.Errorf("unexpected error '%s'", err)
}

expectedResult := "SGVsbG8="

if actualResult.String() != expectedResult {
t.Errorf("Expected %s but got %s", expectedResult, actualResult)
}
}

0 comments on commit 618eb88

Please sign in to comment.