Documentation ¶
Overview ¶
Package assertjson provides methods for testing JSON values. Selecting JSON values provided by JSON Pointer Syntax (https://tools.ietf.org/html/rfc6901).
Example usage
import ( "net/http" "net/http/httptest" "testing" "github.com/muonsoft/api-testing/assertjson" ) func TestYourAPI(t *testing.T) { recorder := httptest.NewRecorder() handler := createHTTPHandler() request, _ := http.NewRequest("GET", "/content", nil) handler.ServeHTTP(recorder, request) assertjson.Has(t, recorder.Body.Bytes(), func(json *assertjson.AssertJSON) { // common assertions json.Node("/nullNode").Exists() json.Node("/notExistingNode").DoesNotExist() json.Node("/nullNode").IsNull() json.Node("/stringNode").IsNotNull() json.Node("/trueBooleanNode").IsTrue() json.Node("/falseBooleanNode").IsFalse() // string assertions json.Node("/stringNode").IsString() json.Node("/stringNode").EqualToTheString("stringValue") json.Node("/stringNode").Matches("^string.*$") json.Node("/stringNode").DoesNotMatch("^notMatch$") json.Node("/stringNode").Contains("string") json.Node("/stringNode").DoesNotContain("notContain") json.Node("/stringNode").IsStringWithLength(11) json.Node("/stringNode").IsStringWithLengthInRange(11, 11) // numeric assertions json.Node("/integerNode").IsInteger() json.Node("/integerNode").EqualToTheInteger(123) json.Node("/integerNode").IsNumberInRange(122, 124) json.Node("/integerNode").IsNumberGreaterThan(122) json.Node("/integerNode").IsNumberGreaterThanOrEqual(123) json.Node("/integerNode").IsNumberLessThan(124) json.Node("/integerNode").IsNumberLessThanOrEqual(123) json.Node("/floatNode").IsFloat() json.Node("/floatNode").EqualToTheFloat(123.123) json.Node("/floatNode").IsNumberInRange(122, 124) json.Node("/floatNode").IsNumberGreaterThan(122) json.Node("/floatNode").IsNumberGreaterThanOrEqual(123.123) json.Node("/floatNode").IsNumberLessThan(124) json.Node("/floatNode").IsNumberLessThanOrEqual(123.123) // array assertions json.Node("/arrayNode").IsArrayWithElementsCount(1) // object assertions json.Node("/objectNode").IsObjectWithPropertiesCount(1) // json pointer expression json.Node("/complexNode/items/1/key").EqualToTheString("value") }) }
Index ¶
- func FileHas(t *testing.T, filename string, jsonAssert JSONAssertFunc)
- func Has(t *testing.T, data []byte, jsonAssert JSONAssertFunc)
- type AssertJSON
- type AssertNode
- func (node *AssertNode) Contains(contain string, msgAndArgs ...interface{})
- func (node *AssertNode) DoesNotContain(contain string, msgAndArgs ...interface{})
- func (node *AssertNode) DoesNotExist(msgAndArgs ...interface{})
- func (node *AssertNode) DoesNotMatch(regexp string, msgAndArgs ...interface{})
- func (node *AssertNode) EqualToTheFloat(expectedValue float64, msgAndArgs ...interface{})
- func (node *AssertNode) EqualToTheInteger(expectedValue int, msgAndArgs ...interface{})
- func (node *AssertNode) EqualToTheString(expectedValue string, msgAndArgs ...interface{})
- func (node *AssertNode) Exists(msgAndArgs ...interface{})
- func (node *AssertNode) IsArrayWithElementsCount(count int, msgAndArgs ...interface{})
- func (node *AssertNode) IsFalse(msgAndArgs ...interface{})
- func (node *AssertNode) IsFloat(msgAndArgs ...interface{})
- func (node *AssertNode) IsInteger(msgAndArgs ...interface{})
- func (node *AssertNode) IsNotNull(msgAndArgs ...interface{})
- func (node *AssertNode) IsNull(msgAndArgs ...interface{})
- func (node *AssertNode) IsNumberGreaterThan(value float64, msgAndArgs ...interface{})
- func (node *AssertNode) IsNumberGreaterThanOrEqual(value float64, msgAndArgs ...interface{})
- func (node *AssertNode) IsNumberInRange(min float64, max float64, msgAndArgs ...interface{})
- func (node *AssertNode) IsNumberLessThan(value float64, msgAndArgs ...interface{})
- func (node *AssertNode) IsNumberLessThanOrEqual(value float64, msgAndArgs ...interface{})
- func (node *AssertNode) IsObjectWithPropertiesCount(count int, msgAndArgs ...interface{})
- func (node *AssertNode) IsString(msgAndArgs ...interface{})
- func (node *AssertNode) IsStringWithLength(length int, msgAndArgs ...interface{})
- func (node *AssertNode) IsStringWithLengthInRange(min int, max int, msgAndArgs ...interface{})
- func (node *AssertNode) IsTrue(msgAndArgs ...interface{})
- func (node *AssertNode) Matches(regexp string, msgAndArgs ...interface{})
- type JSONAssertFunc
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type AssertJSON ¶
type AssertJSON struct {
// contains filtered or unexported fields
}
AssertJSON - main structure that holds parsed JSON.
func (*AssertJSON) Node ¶
func (j *AssertJSON) Node(path string) *AssertNode
Node searches for JSON node by JSON Path Syntax. Returns struct for asserting the node values.
type AssertNode ¶
type AssertNode struct {
// contains filtered or unexported fields
}
AssertNode - structure for asserting JSON node.
func (*AssertNode) Contains ¶
func (node *AssertNode) Contains(contain string, msgAndArgs ...interface{})
Contains asserts that the JSON node has a string value that contains a string.
func (*AssertNode) DoesNotContain ¶
func (node *AssertNode) DoesNotContain(contain string, msgAndArgs ...interface{})
DoesNotContain asserts that the JSON node has a string value that does not contain a string.
func (*AssertNode) DoesNotExist ¶
func (node *AssertNode) DoesNotExist(msgAndArgs ...interface{})
DoesNotExist asserts that the JSON node does not exist.
func (*AssertNode) DoesNotMatch ¶
func (node *AssertNode) DoesNotMatch(regexp string, msgAndArgs ...interface{})
DoesNotMatch asserts that the JSON node has a string value that does not match the regular expression.
func (*AssertNode) EqualToTheFloat ¶
func (node *AssertNode) EqualToTheFloat(expectedValue float64, msgAndArgs ...interface{})
EqualToTheFloat asserts that the JSON node has a float value equals to the given value.
func (*AssertNode) EqualToTheInteger ¶
func (node *AssertNode) EqualToTheInteger(expectedValue int, msgAndArgs ...interface{})
EqualToTheInteger asserts that the JSON node has an integer value equals to the given value.
func (*AssertNode) EqualToTheString ¶
func (node *AssertNode) EqualToTheString(expectedValue string, msgAndArgs ...interface{})
EqualToTheString asserts that the JSON node has a string value equals to the given value.
func (*AssertNode) Exists ¶
func (node *AssertNode) Exists(msgAndArgs ...interface{})
Exists asserts that the JSON node exists.
func (*AssertNode) IsArrayWithElementsCount ¶
func (node *AssertNode) IsArrayWithElementsCount(count int, msgAndArgs ...interface{})
IsArrayWithElementsCount asserts that the JSON node is an array with given elements count.
func (*AssertNode) IsFalse ¶
func (node *AssertNode) IsFalse(msgAndArgs ...interface{})
IsFalse asserts that the JSON node equals to the boolean with `false` value.
func (*AssertNode) IsFloat ¶
func (node *AssertNode) IsFloat(msgAndArgs ...interface{})
IsFloat asserts that the JSON node has a float value.
func (*AssertNode) IsInteger ¶
func (node *AssertNode) IsInteger(msgAndArgs ...interface{})
IsInteger asserts that the JSON node has an integer value.
func (*AssertNode) IsNotNull ¶
func (node *AssertNode) IsNotNull(msgAndArgs ...interface{})
IsNotNull asserts that the JSON node not equals to `null` value.
func (*AssertNode) IsNull ¶
func (node *AssertNode) IsNull(msgAndArgs ...interface{})
IsNull asserts that the JSON node equals to `null` value.
func (*AssertNode) IsNumberGreaterThan ¶
func (node *AssertNode) IsNumberGreaterThan(value float64, msgAndArgs ...interface{})
IsNumberGreaterThan asserts that the JSON node has a number greater than the given value.
func (*AssertNode) IsNumberGreaterThanOrEqual ¶
func (node *AssertNode) IsNumberGreaterThanOrEqual(value float64, msgAndArgs ...interface{})
IsNumberGreaterThanOrEqual asserts that the JSON node has a number greater than or equal to the given value.
func (*AssertNode) IsNumberInRange ¶
func (node *AssertNode) IsNumberInRange(min float64, max float64, msgAndArgs ...interface{})
IsNumberInRange asserts that the JSON node has a number with value in the given range.
func (*AssertNode) IsNumberLessThan ¶
func (node *AssertNode) IsNumberLessThan(value float64, msgAndArgs ...interface{})
IsNumberLessThan asserts that the JSON node has a number less than the given value.
func (*AssertNode) IsNumberLessThanOrEqual ¶
func (node *AssertNode) IsNumberLessThanOrEqual(value float64, msgAndArgs ...interface{})
IsNumberLessThanOrEqual asserts that the JSON node has a number less than or equal to the given value.
func (*AssertNode) IsObjectWithPropertiesCount ¶
func (node *AssertNode) IsObjectWithPropertiesCount(count int, msgAndArgs ...interface{})
IsObjectWithPropertiesCount asserts that the JSON node is an object with given properties count.
func (*AssertNode) IsString ¶
func (node *AssertNode) IsString(msgAndArgs ...interface{})
IsString asserts that the JSON node has a string value.
func (*AssertNode) IsStringWithLength ¶
func (node *AssertNode) IsStringWithLength(length int, msgAndArgs ...interface{})
IsStringWithLength asserts that the JSON node has a string value with length equal to the given value.
func (*AssertNode) IsStringWithLengthInRange ¶
func (node *AssertNode) IsStringWithLengthInRange(min int, max int, msgAndArgs ...interface{})
IsStringWithLengthInRange asserts that the JSON node has a string value with length in the given range.
func (*AssertNode) IsTrue ¶
func (node *AssertNode) IsTrue(msgAndArgs ...interface{})
IsTrue asserts that the JSON node equals to the boolean with `true` value.
func (*AssertNode) Matches ¶
func (node *AssertNode) Matches(regexp string, msgAndArgs ...interface{})
Matches asserts that the JSON node has a string value that matches the regular expression.
type JSONAssertFunc ¶
type JSONAssertFunc func(json *AssertJSON)
JSONAssertFunc - callback function used for asserting JSON nodes.