-
Notifications
You must be signed in to change notification settings - Fork 112
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #192 from zalando-incubator/extend-jsonpath-support
Support bracket notation in jsonPath
- Loading branch information
Showing
8 changed files
with
105 additions
and
147 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
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 |
---|---|---|
@@ -1,89 +1,65 @@ | ||
package httpmetrics | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"net/http" | ||
"net/http/httptest" | ||
"net/url" | ||
"testing" | ||
|
||
"github.com/oliveagle/jsonpath" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestCastSlice(t *testing.T) { | ||
res1, err1 := castSlice([]interface{}{1, 2, 3}) | ||
require.NoError(t, err1) | ||
require.Equal(t, []float64{1.0, 2.0, 3.0}, res1) | ||
|
||
res2, err2 := castSlice([]interface{}{float32(1.0), float32(2.0), float32(3.0)}) | ||
require.NoError(t, err2) | ||
require.Equal(t, []float64{1.0, 2.0, 3.0}, res2) | ||
|
||
res3, err3 := castSlice([]interface{}{float64(1.0), float64(2.0), float64(3.0)}) | ||
require.NoError(t, err3) | ||
require.Equal(t, []float64{1.0, 2.0, 3.0}, res3) | ||
|
||
res4, err4 := castSlice([]interface{}{1, 2, "some string"}) | ||
require.Errorf(t, err4, "slice was returned by JSONPath, but value inside is unsupported: %T", "string") | ||
require.Equal(t, []float64(nil), res4) | ||
} | ||
|
||
type testValueResponse struct { | ||
Value int64 `json:"value"` | ||
} | ||
|
||
type testValueArrayResponse struct { | ||
Value []int64 `json:"value"` | ||
} | ||
|
||
func makeTestHTTPServer(t *testing.T, values ...int64) *httptest.Server { | ||
func makeTestHTTPServer(t *testing.T, response []byte) *httptest.Server { | ||
h := func(w http.ResponseWriter, r *http.Request) { | ||
require.Equal(t, r.URL.Path, "/metrics") | ||
w.Header().Set("Content-Type", "application/json") | ||
var ( | ||
response []byte | ||
err error | ||
) | ||
if len(values) == 1 { | ||
response, err = json.Marshal(testValueResponse{Value: values[0]}) | ||
require.NoError(t, err) | ||
} else { | ||
response, err = json.Marshal(testValueArrayResponse{Value: values}) | ||
require.NoError(t, err) | ||
} | ||
_, err = w.Write(response) | ||
_, err := w.Write(response) | ||
require.NoError(t, err) | ||
} | ||
return httptest.NewServer(http.HandlerFunc(h)) | ||
} | ||
|
||
func TestJSONPathMetricsGetter(t *testing.T) { | ||
for _, tc := range []struct { | ||
name string | ||
input []int64 | ||
output float64 | ||
aggregator AggregatorFunc | ||
name string | ||
jsonResponse []byte | ||
jsonPath string | ||
result float64 | ||
aggregator AggregatorFunc | ||
}{ | ||
{ | ||
name: "basic average", | ||
input: []int64{3, 4, 5}, | ||
output: 4, | ||
aggregator: Average, | ||
name: "basic single value", | ||
jsonResponse: []byte(`{"value":3}`), | ||
jsonPath: "$.value", | ||
result: 3, | ||
aggregator: Average, | ||
}, | ||
{ | ||
name: "basic average", | ||
jsonResponse: []byte(`{"value":[3,4,5]}`), | ||
jsonPath: "$.value", | ||
result: 4, | ||
aggregator: Average, | ||
}, | ||
{ | ||
name: "dotted key", | ||
jsonResponse: []byte(`{"metric.value":5}`), | ||
jsonPath: "$['metric.value']", | ||
result: 5, | ||
aggregator: Average, | ||
}, | ||
} { | ||
t.Run(tc.name, func(t *testing.T) { | ||
server := makeTestHTTPServer(t, tc.input...) | ||
server := makeTestHTTPServer(t, tc.jsonResponse) | ||
defer server.Close() | ||
path, err := jsonpath.Compile("$.value") | ||
getter, err := NewJSONPathMetricsGetter(DefaultMetricsHTTPClient(), tc.aggregator, tc.jsonPath) | ||
require.NoError(t, err) | ||
getter := NewJSONPathMetricsGetter(DefaultMetricsHTTPClient(), tc.aggregator, path) | ||
url, err := url.Parse(fmt.Sprintf("%s/metrics", server.URL)) | ||
require.NoError(t, err) | ||
metric, err := getter.GetMetric(*url) | ||
require.NoError(t, err) | ||
require.Equal(t, tc.output, metric) | ||
require.Equal(t, tc.result, metric) | ||
}) | ||
} | ||
} |
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
Oops, something went wrong.