From cdfb827f81d2991ea334edaed9deba818c672167 Mon Sep 17 00:00:00 2001 From: Luke Kysow <1034429+lkysow@users.noreply.github.com> Date: Mon, 6 Nov 2023 16:18:55 -0800 Subject: [PATCH] Swap to using url.Parse --- dependency/health_service.go | 26 ++++++++++++-------------- dependency/health_service_test.go | 14 ++------------ 2 files changed, 14 insertions(+), 26 deletions(-) diff --git a/dependency/health_service.go b/dependency/health_service.go index d879e3766..d9462a09f 100644 --- a/dependency/health_service.go +++ b/dependency/health_service.go @@ -118,22 +118,20 @@ func healthServiceQuery(s string, connect bool) (*HealthServiceQuery, error) { } // Parse optional query into key pairs. - queryParams := make(map[string]string) + queryParams := url.Values{} if queryRaw := m["query"]; queryRaw != "" { - keyPairs := strings.Split(queryRaw, "&") - for _, keyPair := range keyPairs { - split := strings.Split(keyPair, "=") - if len(split) != 2 { - return nil, fmt.Errorf( - "health.service: invalid query: %q expected key pair %q to have two parts when split on \"=\"", queryRaw, keyPair) - } - - key, val := split[0], split[1] + var err error + queryParams, err = url.ParseQuery(queryRaw) + if err != nil { + return nil, fmt.Errorf( + "health.service: invalid query: %q: %s", queryRaw, err) + } + // Validate keys. + for key := range queryParams { switch key { case QueryNamespace, QueryPeer, QueryPartition: - queryParams[key] = val default: return nil, fmt.Errorf("health.service: invalid query parameter key %q in query %q: supported keys: %s,%s,%s", key, queryRaw, QueryNamespace, QueryPeer, QueryPartition) @@ -149,9 +147,9 @@ func healthServiceQuery(s string, connect bool) (*HealthServiceQuery, error) { near: m["near"], tag: m["tag"], connect: connect, - namespace: queryParams[QueryNamespace], - peer: queryParams[QueryPeer], - partition: queryParams[QueryPartition], + namespace: queryParams.Get(QueryNamespace), + peer: queryParams.Get(QueryPeer), + partition: queryParams.Get(QueryPartition), }, nil } diff --git a/dependency/health_service_test.go b/dependency/health_service_test.go index 7d9d3ba45..58e9f8aba 100644 --- a/dependency/health_service_test.go +++ b/dependency/health_service_test.go @@ -48,16 +48,6 @@ func TestNewHealthServiceQuery(t *testing.T) { nil, true, }, - { - name: "invalid query param (extra =)", - i: "name?ns==", - err: true, - }, - { - name: "invalid query param (no =)", - i: "name?ns", - err: true, - }, { name: "invalid query param (unsupported key)", i: "name?unsupported=test", @@ -190,12 +180,12 @@ func TestNewHealthServiceQuery(t *testing.T) { false, }, { - "name_ns_ns", + "namespace set twice should use first", "name?ns=foo&ns=bar", &HealthServiceQuery{ filters: []string{"passing"}, name: "name", - namespace: "bar", + namespace: "foo", }, false, },