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:
parent
0433a329f3
commit
cbb4311b65
@ -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
|
||||
}
|
||||
|
@ -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)
|
||||
})
|
||||
}
|
||||
})
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user