Skip to content

Commit

Permalink
Fail /_cat/ API tests if any JSON fields are ignored
Browse files Browse the repository at this point in the history
This commit alters the tests for the /_cat/ API endpoints to fail if any
fields in the JSON returned from the API go undecoded.
  • Loading branch information
jtdoepke committed May 22, 2019
1 parent b63655f commit 3577f46
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 10 deletions.
4 changes: 2 additions & 2 deletions cat_aliases_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
)

func TestCatAliases(t *testing.T) {
client := setupTestClientAndCreateIndexAndAddDocs(t) //, SetTraceLog(log.New(os.Stdout, "", 0)))
client := setupTestClientAndCreateIndexAndAddDocs(t, SetDecoder(&strictDecoder{})) //, SetTraceLog(log.New(os.Stdout, "", 0)))
ctx := context.Background()

// Add two aliases
Expand All @@ -30,7 +30,7 @@ func TestCatAliases(t *testing.T) {
}()

// Check all aliases
res, err := client.CatAliases().Pretty(true).Do(ctx)
res, err := client.CatAliases().Pretty(true).Columns("*").Do(ctx)
if err != nil {
t.Fatal(err)
}
Expand Down
4 changes: 2 additions & 2 deletions cat_allocation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import (
)

func TestCatAllocation(t *testing.T) {
client := setupTestClientAndCreateIndexAndAddDocs(t) // , SetTraceLog(log.New(os.Stdout, "", 0)))
client := setupTestClientAndCreateIndexAndAddDocs(t, SetDecoder(&strictDecoder{})) // , SetTraceLog(log.New(os.Stdout, "", 0)))
ctx := context.Background()
res, err := client.CatAllocation().Do(ctx)
res, err := client.CatAllocation().Columns("*").Do(ctx)
if err != nil {
t.Fatal(err)
}
Expand Down
4 changes: 2 additions & 2 deletions cat_count_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import (
)

func TestCatCount(t *testing.T) {
client := setupTestClientAndCreateIndexAndAddDocs(t) // , SetTraceLog(log.New(os.Stdout, "", 0)))
client := setupTestClientAndCreateIndexAndAddDocs(t, SetDecoder(&strictDecoder{})) // , SetTraceLog(log.New(os.Stdout, "", 0)))
ctx := context.Background()
res, err := client.CatCount().Pretty(true).Do(ctx)
res, err := client.CatCount().Pretty(true).Columns("*").Do(ctx)
if err != nil {
t.Fatal(err)
}
Expand Down
4 changes: 2 additions & 2 deletions cat_health_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import (
)

func TestCatHealth(t *testing.T) {
client := setupTestClientAndCreateIndexAndAddDocs(t) // , SetTraceLog(log.New(os.Stdout, "", 0)))
client := setupTestClientAndCreateIndexAndAddDocs(t, SetDecoder(&strictDecoder{})) // , SetTraceLog(log.New(os.Stdout, "", 0)))
ctx := context.Background()
res, err := client.CatHealth().Do(ctx)
res, err := client.CatHealth().Columns("*").Do(ctx)
if err != nil {
t.Fatal(err)
}
Expand Down
4 changes: 2 additions & 2 deletions cat_indices_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import (
)

func TestCatIndices(t *testing.T) {
client := setupTestClientAndCreateIndexAndAddDocs(t) // , SetTraceLog(log.New(os.Stdout, "", 0)))
client := setupTestClientAndCreateIndexAndAddDocs(t, SetDecoder(&strictDecoder{})) // , SetTraceLog(log.New(os.Stdout, "", 0)))
ctx := context.Background()
res, err := client.CatIndices().Do(ctx)
res, err := client.CatIndices().Columns("*").Do(ctx)
if err != nil {
t.Fatal(err)
}
Expand Down
11 changes: 11 additions & 0 deletions setup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
package elastic

import (
"bytes"
"context"
"encoding/json"
"fmt"
"log"
"math/rand"
Expand Down Expand Up @@ -292,6 +294,15 @@ type logger interface {
Logf(format string, args ...interface{})
}

// strictDecoder returns an error if any JSON fields aren't decoded.
type strictDecoder struct{}

func (d *strictDecoder) Decode(data []byte, v interface{}) error {
dec := json.NewDecoder(bytes.NewReader(data))
dec.DisallowUnknownFields()
return dec.Decode(v)
}

func setupTestClient(t logger, options ...ClientOptionFunc) (client *Client) {
var err error

Expand Down

0 comments on commit 3577f46

Please sign in to comment.