Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Abstractions for enforcing maximum or minimum length of values #12

Merged
merged 3 commits into from
Jan 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 27 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ apitest.New().
End()
```

given the response is `{"a": "hello", "b": 12345}`
given the response is `{"a": "hello", "b": 12345}`

### Contains

Expand Down Expand Up @@ -63,6 +63,32 @@ apitest.New().
End()
```

### GreaterThan

Use `GreaterThan` to enforce a minimum length on the returned value.

```go
apitest.New().
Handler(handler).
Get("/articles?category=golang").
Expect(t).
Assert(jsonpath.GreaterThan(`$.items`, 2).
End()
```

### LessThan

Use `LessThan` to enforce a maximum length on the returned value.

```go
apitest.New().
Handler(handler).
Get("/articles?category=golang").
Expect(t).
Assert(jsonpath.LessThan(`$.items`, 4).
End()
```

### Present / NotPresent

Use `Present` and `NotPresent` to check the presence of a field in the response without evaluating its value
Expand Down
30 changes: 30 additions & 0 deletions jsonpath.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,36 @@ func Len(expression string, expectedLength int) apitest.Assert {
}
}

func GreaterThan(expression string, minimumLength int) apitest.Assert {
return func(res *http.Response, req *http.Request) error {
value, err := jsonPath(res.Body, expression)
if err != nil {
return err
}

v := reflect.ValueOf(value)
if v.Len() < minimumLength {
return errors.New(fmt.Sprintf("\"%d\" is greater than \"%d\"", v.Len(), minimumLength))
}
return nil
}
}

func LessThan(expression string, maximumLength int) apitest.Assert {
return func(res *http.Response, req *http.Request) error {
value, err := jsonPath(res.Body, expression)
if err != nil {
return err
}

v := reflect.ValueOf(value)
if v.Len() > maximumLength {
return errors.New(fmt.Sprintf("\"%d\" is less than \"%d\"", v.Len(), maximumLength))
}
return nil
}
}

func Present(expression string) apitest.Assert {
return func(res *http.Response, req *http.Request) error {
value, _ := jsonPath(res.Body, expression)
Expand Down
42 changes: 41 additions & 1 deletion jsonpath_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ package jsonpath
import (
"bytes"
"fmt"
"github.com/stretchr/testify/assert"
"io/ioutil"
"net/http"
"testing"

"github.com/steinfletcher/apitest"
"github.com/stretchr/testify/assert"
)

func TestApiTest_Contains(t *testing.T) {
Expand Down Expand Up @@ -157,6 +157,46 @@ func TestApiTest_Len(t *testing.T) {
End()
}

func TestApiTest_GreaterThan(t *testing.T) {
handler := http.NewServeMux()
handler.HandleFunc("/hello", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Header().Set("Content-Type", "application/json")
_, err := w.Write([]byte(`{"a": [1, 2, 3], "b": "c"}`))
if err != nil {
panic(err)
}
})

apitest.New().
Handler(handler).
Get("/hello").
Expect(t).
Assert(GreaterThan(`$.a`, 2)).
Assert(GreaterThan(`$.b`, 0)).
End()
}

func TestApiTest_LessThan(t *testing.T) {
handler := http.NewServeMux()
handler.HandleFunc("/hello", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Header().Set("Content-Type", "application/json")
_, err := w.Write([]byte(`{"a": [1, 2, 3], "b": "c"}`))
if err != nil {
panic(err)
}
})

apitest.New().
Handler(handler).
Get("/hello").
Expect(t).
Assert(LessThan(`$.a`, 4)).
Assert(LessThan(`$.b`, 2)).
End()
}

func TestApiTest_Present(t *testing.T) {
handler := http.NewServeMux()
handler.HandleFunc("/hello", func(w http.ResponseWriter, r *http.Request) {
Expand Down