Fix core.EscapePath() reserved chars list, add test for EscapePath, add benchmarks core/url path escaping

Signed-off-by: kim (grufwub) <grufwub@gmail.com>
development
kim (grufwub) 3 years ago
parent d6df45cd41
commit d84739d018

@ -105,11 +105,11 @@ func shouldHostEscape(b byte) bool {
func shouldPathEscape(b byte) bool {
switch b {
// Reserved character in path
case '?':
case '?', ';', ',':
return true
// Allowed in path
case '$', '&', '+', ',', '/', ':', ';', '=', '@':
case '$', '&', '+', '/', ':', '=', '@':
return false
// Check all-else

@ -0,0 +1,61 @@
package core_test
import (
"fmt"
"gophi/core"
"io/ioutil"
"net/url"
"testing"
)
var toEscape = []string{
"",
"abc",
"abc+def",
"a?b",
"one two",
"10%",
" ?&=#+%!<>#\"{}|\\^[]`☺\t:@$'()*,;",
}
var escaped = []string{
"",
"abc",
"abc+def",
"a%3Fb",
"one%20two",
"10%25",
"%20%3F&=%23+%25%21%3C%3E%23%22%7B%7D%7C%5C%5E%5B%5D%60%E2%98%BA%09:@$%27%28%29%2A%2C%3B",
}
func TestCorePathEscape(t *testing.T) {
for i, path := range toEscape {
if escapedPath := core.EscapePath(path); escapedPath != escaped[i] {
t.Fatalf("Failed escaping path!\nGot: %s\nExpected: %s\n", escapedPath, escaped[i])
}
}
}
func BenchmarkCorePathEscape(b *testing.B) {
var s string
for i := 0; i < b.N; i++ {
for _, path := range toEscape {
s = core.EscapePath(path)
}
}
fmt.Fprint(ioutil.Discard, s)
}
func BenchmarkURLPathEscape(b *testing.B) {
var s string
for i := 0; i < b.N; i++ {
for _, path := range toEscape {
// This isn't *exactly* a fair comparison to core.EscapePath,
// since url.PathEscape() escapes a path _segment_ as opposed
// to any entire path, whereas core.EscapePath() escapes an entire
// filesystem path.
s = url.PathEscape(path)
}
}
fmt.Fprint(ioutil.Discard, s)
}
Loading…
Cancel
Save