github.com/MontFerret/ferret@v0.18.0/pkg/stdlib/strings/decode_test.go (about)

     1  package strings_test
     2  
     3  import (
     4  	"context"
     5  	"testing"
     6  
     7  	"github.com/MontFerret/ferret/pkg/runtime/values"
     8  
     9  	"github.com/MontFerret/ferret/pkg/stdlib/strings"
    10  
    11  	. "github.com/smartystreets/goconvey/convey"
    12  )
    13  
    14  func TestFromBase64(t *testing.T) {
    15  	Convey("When args are not passed", t, func() {
    16  		Convey("It should return an error", func() {
    17  			var err error
    18  			_, err = strings.FromBase64(context.Background())
    19  
    20  			So(err, ShouldBeError)
    21  		})
    22  	})
    23  
    24  	Convey("When hash is not valid base64", t, func() {
    25  		Convey("It should return an error", func() {
    26  			var err error
    27  			_, err = strings.FromBase64(
    28  				context.Background(),
    29  				values.NewString("foobar"),
    30  			)
    31  
    32  			So(err, ShouldBeError)
    33  		})
    34  	})
    35  
    36  	Convey("Should decode a given hash", t, func() {
    37  		out, err := strings.FromBase64(
    38  			context.Background(),
    39  			values.NewString("Zm9vYmFy"),
    40  		)
    41  
    42  		So(err, ShouldBeNil)
    43  		So(out, ShouldNotEqual, "Zm9vYmFy")
    44  		So(out, ShouldEqual, "foobar")
    45  	})
    46  }
    47  
    48  func TestDecodeURIComponent(t *testing.T) {
    49  
    50  	Convey("Decode", t, func() {
    51  		testCases := []struct {
    52  			Name   string
    53  			InURI  string
    54  			OutURI string
    55  		}{
    56  			{
    57  				Name:   "Unicode",
    58  				InURI:  "https://thedomain/alphabet=M\u0026borough=Bronx\u0026a=b",
    59  				OutURI: "https://thedomain/alphabet=M&borough=Bronx&a=b",
    60  			},
    61  			{
    62  				Name:   "Percent-encoding",
    63  				InURI:  "https://ru.wikipedia.org/wiki/%D0%AF%D0%B7%D1%8B%D0%BA_%D0%BF%D1%80%D0%BE%D0%B3%D1%80%D0%B0%D0%BC%D0%BC%D0%B8%D1%80%D0%BE%D0%B2%D0%B0%D0%BD%D0%B8%D1%8F",
    64  				OutURI: "https://ru.wikipedia.org/wiki/Язык_программирования",
    65  			},
    66  		}
    67  
    68  		for _, tC := range testCases {
    69  			Convey(tC.Name, func() {
    70  				out, err := strings.DecodeURIComponent(
    71  					context.Background(),
    72  					values.NewString(tC.InURI),
    73  				)
    74  				So(err, ShouldBeNil)
    75  
    76  				So(out.String(), ShouldEqual, tC.OutURI)
    77  			})
    78  		}
    79  	})
    80  }