1
0
mirror of https://github.com/MontFerret/ferret.git synced 2025-03-05 15:16:07 +02:00

add strconv.Unquote into DecodeURIComponent (#499)

This commit is contained in:
Vladimir Fetisov 2020-05-08 05:34:21 +03:00 committed by GitHub
parent 0433a329f3
commit cbb4311b65
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 42 additions and 0 deletions

View File

@ -4,6 +4,7 @@ import (
"context"
"encoding/base64"
"net/url"
"strconv"
"github.com/MontFerret/ferret/pkg/runtime/values"
@ -46,5 +47,12 @@ func DecodeURIComponent(_ context.Context, args ...core.Value) (core.Value, erro
return values.None, err
}
// hack for decoding unicode symbols.
// eg. convert "\u0026" -> "&""
str, err = strconv.Unquote("\"" + str + "\"")
if err != nil {
return values.None, err
}
return values.NewString(str), nil
}

View File

@ -44,3 +44,37 @@ func TestFromBase64(t *testing.T) {
So(out, ShouldEqual, "foobar")
})
}
func TestDecodeURIComponent(t *testing.T) {
Convey("Decode", t, func() {
testCases := []struct {
Name string
InURI string
OutURI string
}{
{
Name: "Unicode",
InURI: "https://thedomain/alphabet=M\u0026borough=Bronx\u0026a=b",
OutURI: "https://thedomain/alphabet=M&borough=Bronx&a=b",
},
{
Name: "Percent-encoding",
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",
OutURI: "https://ru.wikipedia.org/wiki/Язык_программирования",
},
}
for _, tC := range testCases {
Convey(tC.Name, func() {
out, err := strings.DecodeURIComponent(
context.Background(),
values.NewString(tC.InURI),
)
So(err, ShouldBeNil)
So(out.String(), ShouldEqual, tC.OutURI)
})
}
})
}