Skip to content

Commit

Permalink
Swap to using url.Parse
Browse files Browse the repository at this point in the history
  • Loading branch information
lkysow committed Nov 7, 2023
1 parent d9c0f6d commit cdfb827
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 26 deletions.
26 changes: 12 additions & 14 deletions dependency/health_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
}

Expand Down
14 changes: 2 additions & 12 deletions dependency/health_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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,
},
Expand Down

0 comments on commit cdfb827

Please sign in to comment.