mirror of
https://github.com/mpolden/echoip.git
synced 2024-11-10 15:37:24 +01:00
Less regexp please
This commit is contained in:
parent
e16e9a7aa9
commit
78f4ed34f8
@ -36,8 +36,8 @@ func parseRealIP(req *http.Request) net.IP {
|
||||
}
|
||||
|
||||
func pathToKey(path string) string {
|
||||
re := regexp.MustCompile("^\\/|\\.json$")
|
||||
return re.ReplaceAllLiteralString(strings.ToLower(path), "")
|
||||
trimmed := strings.TrimSuffix(strings.TrimPrefix(path, "/"), ".json")
|
||||
return strings.ToLower(trimmed)
|
||||
}
|
||||
|
||||
func isJSON(req *http.Request) bool {
|
||||
|
@ -19,3 +19,15 @@ func TestIsCLI(t *testing.T) {
|
||||
t.Errorf("Expected false for %s", browserUserAgent)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPathToKey(t *testing.T) {
|
||||
if key := pathToKey("/ip"); key != "ip" {
|
||||
t.Fatalf("Expected 'ip', got '%s'", key)
|
||||
}
|
||||
if key := pathToKey("/User-Agent"); key != "user-agent" {
|
||||
t.Fatalf("Expected 'user-agent', got '%s'", key)
|
||||
}
|
||||
if key := pathToKey("/all.json"); key != "all" {
|
||||
t.Fatalf("Expected 'all', got '%s'", key)
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user