Skip to content

Commit

Permalink
improve: Add a method to test if response matches json query
Browse files Browse the repository at this point in the history
  • Loading branch information
ikawaha committed Feb 3, 2024
1 parent 1fe5116 commit b844e48
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 0 deletions.
60 changes: 60 additions & 0 deletions tester_body.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ package httpcheck

import (
"bytes"
"encoding/json"
"fmt"
"io"
"strings"

"github.com/itchyny/gojq"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -191,3 +193,61 @@ func (tt *Tester) MustNotContainsString(substr string) *Tester {
require.NotContains(tt.t, string(body), substr)
return tt
}

func (tt *Tester) MatchesJSONQuery(q string) *Tester {
body, err := io.ReadAll(tt.response.Body)
require.NoError(tt.t, err)
tt.response.Body.Close()
defer func(body []byte) {
tt.response.Body = io.NopCloser(bytes.NewReader(body))
}(body)
var in any
require.NoError(tt.t, json.Unmarshal(body, &in), "failed to unmarshal json: %s", string(body))
jq, err := gojq.Parse(q)
require.NoError(tt.t, err, "failed to parse query %q: %s", q)
it := jq.Run(in)
var detect bool
for {
v, ok := it.Next()
if !ok {
break
}
if err, ok := v.(error); ok {
require.NoError(tt.t, err, "query %q does not match: %s", q, string(body))
}
if v != nil {
detect = true
}
}
assert.True(tt.t, detect, "query %q does not match: %s", q, string(body))
return tt
}

func (tt *Tester) NotMatchesJSONQuery(q string) *Tester {
body, err := io.ReadAll(tt.response.Body)
require.NoError(tt.t, err)
tt.response.Body.Close()
defer func(body []byte) {
tt.response.Body = io.NopCloser(bytes.NewReader(body))
}(body)
var in any
require.NoError(tt.t, json.Unmarshal(body, &in))
jq, err := gojq.Parse(q)
require.NoError(tt.t, err, "failed to parse query %q: %s", q)
it := jq.Run(in)
var detect bool
for {
v, ok := it.Next()
if !ok {
break
}
if err, ok := v.(error); ok {
require.NoError(tt.t, err, "query %q does not match: %s", q, string(body))
}
if v != nil {
detect = true
}
}
assert.False(tt.t, detect, "query %q does not match: %s", q, string(body))
return tt
}
31 changes: 31 additions & 0 deletions tester_body_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package httpcheck

import (
"net/http"
"net/http/httptest"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -152,3 +153,33 @@ func TestTester_MustNotContainsString(t *testing.T) {
t.Fatal("it is expected that this assertion will not be executed.")
})
}

func TestTester_MatchesJSONQuery(t *testing.T) {
mux := http.NewServeMux()
mux.HandleFunc("/json", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Write([]byte(`{"Name": "Some", "Age": 30}`))
})
ts := httptest.NewServer(mux)
defer ts.Close()

checker := newTestChecker()
checker.Test(t, "GET", "/json").
Check().
MatchesJSONQuery(`.Name`)
}

func TestTester_NotMatchesJSONQuery(t *testing.T) {
mux := http.NewServeMux()
mux.HandleFunc("/json", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Write([]byte(`{"Name": "Some", "Age": 30}`))
})
ts := httptest.NewServer(mux)
defer ts.Close()

checker := newTestChecker()
checker.Test(t, "GET", "/json").
Check().
NotMatchesJSONQuery(`.XXX`)
}

0 comments on commit b844e48

Please sign in to comment.