-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add URL escape and unespace normalizers and tests to v2. (#143)
# Describe Request Add URL escape and unespace normalizers and tests to v2. # Change Type New code.
- Loading branch information
Showing
5 changed files
with
106 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// Copyright (c) 2023-2024 Onur Cinar. | ||
// Use of this source code is governed by a MIT-style | ||
// license that can be found in the LICENSE file. | ||
// https://github.com/cinar/checker | ||
|
||
package v2 | ||
|
||
import ( | ||
"net/url" | ||
"reflect" | ||
) | ||
|
||
// nameURLUnescape is the name of the URL unescape normalizer. | ||
const nameURLUnescape = "url-unescape" | ||
|
||
// URLUnescape applies URL unescaping to special characters. | ||
func URLUnescape(value string) (string, error) { | ||
unescaped, err := url.QueryUnescape(value) | ||
return unescaped, err | ||
} | ||
|
||
// reflectURLUnescape applies URL unescaping to special characters. | ||
func reflectURLUnescape(value reflect.Value) (reflect.Value, error) { | ||
newValue, err := URLUnescape(value.Interface().(string)) | ||
return reflect.ValueOf(newValue), err | ||
} | ||
|
||
// makeURLUnescape returns the URL unescape normalizer function. | ||
func makeURLUnescape(_ string) CheckFunc[reflect.Value] { | ||
return reflectURLUnescape | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
// Copyright (c) 2023-2024 Onur Cinar. | ||
// Use of this source code is governed by a MIT-style | ||
// license that can be found in the LICENSE file. | ||
// https://github.com/cinar/checker | ||
|
||
package v2_test | ||
|
||
import ( | ||
"testing" | ||
|
||
v2 "github.com/cinar/checker/v2" | ||
) | ||
|
||
func TestURLUnescape(t *testing.T) { | ||
input := "param1%2Fparam2+%3D+1+%2B+2+%26+3+%2B+4" | ||
expected := "param1/param2 = 1 + 2 & 3 + 4" | ||
|
||
actual, err := v2.URLUnescape(input) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
if actual != expected { | ||
t.Fatalf("actual %s expected %s", actual, expected) | ||
} | ||
} | ||
|
||
func TestReflectURLUnescape(t *testing.T) { | ||
type Request struct { | ||
Query string `checkers:"url-unescape"` | ||
} | ||
|
||
request := &Request{ | ||
Query: "param1%2Fparam2+%3D+1+%2B+2+%26+3+%2B+4", | ||
} | ||
|
||
expected := "param1/param2 = 1 + 2 & 3 + 4" | ||
|
||
errs, ok := v2.CheckStruct(request) | ||
if !ok { | ||
t.Fatalf("got unexpected errors %v", errs) | ||
} | ||
|
||
if request.Query != expected { | ||
t.Fatalf("actual %s expected %s", request.Query, expected) | ||
} | ||
} |