Skip to content

Commit

Permalink
Abstractions for enforcing maximum or minimum length of values (#12)
Browse files Browse the repository at this point in the history
* Add abstraction for checking minimum length

* Add abstraction for checking maximum length

* Refactor: MoreThan -> GreaterThan
  • Loading branch information
remnestal authored and steinfletcher committed Jan 16, 2020
1 parent 2b2f9d0 commit e98e53e
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 2 deletions.
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

0 comments on commit e98e53e

Please sign in to comment.