Less regexp please

This commit is contained in:
Martin Polden 2014-12-22 00:55:56 +01:00
parent e16e9a7aa9
commit 78f4ed34f8
2 changed files with 14 additions and 2 deletions

View File

@ -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 {

View File

@ -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)
}
}