aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/github.com/aws/smithy-go/testing/bytes.go
blob: 8f966846d9db088d035a43ad80e4513d3c7786ab (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package testing

import (
	"bytes"
	"encoding/hex"
	"fmt"
	"io"
	"io/ioutil"
)

// CompareReaderEmpty checks if the reader is nil, or contains no bytes.
// Returns an error if not empty.
func CompareReaderEmpty(r io.Reader) error {
	if r == nil {
		return nil
	}
	b, err := ioutil.ReadAll(r)
	if err != nil && err != io.EOF {
		return fmt.Errorf("unable to read from reader, %v", err)
	}
	if len(b) != 0 {
		return fmt.Errorf("reader not empty, got\n%v", hex.Dump(b))
	}
	return nil
}

// CompareReaderBytes compares the reader with the expected bytes. Returns an
// error if the two bytes are not equal.
func CompareReaderBytes(r io.Reader, expect []byte) error {
	if r == nil {
		return fmt.Errorf("missing body")
	}
	actual, err := ioutil.ReadAll(r)
	if err != nil {
		return fmt.Errorf("unable to read, %v", err)
	}

	if !bytes.Equal(expect, actual) {
		return fmt.Errorf("bytes not equal\nexpect:\n%v\nactual:\n%v",
			hex.Dump(expect), hex.Dump(actual))
	}
	return nil
}

// CompareJSONReaderBytes compares the reader containing serialized JSON
// document. Deserializes the JSON documents to determine if they are equal.
// Return an error if the two JSON documents are not equal.
func CompareJSONReaderBytes(r io.Reader, expect []byte) error {
	if r == nil {
		return fmt.Errorf("missing body")
	}
	actual, err := ioutil.ReadAll(r)
	if err != nil {
		return fmt.Errorf("unable to read, %v", err)
	}

	if err := JSONEqual(expect, actual); err != nil {
		return fmt.Errorf("JSON documents not equal, %v", err)
	}
	return nil
}

// CompareXMLReaderBytes compares the reader with expected xml byte
func CompareXMLReaderBytes(r io.Reader, expect []byte) error {
	if r == nil {
		return fmt.Errorf("missing body")
	}

	actual, err := ioutil.ReadAll(r)
	if err != nil {
		return err
	}

	if err := XMLEqual(expect, actual); err != nil {
		return fmt.Errorf("XML documents not equal, %w", err)
	}
	return nil
}

// CompareURLFormReaderBytes compares the reader containing serialized URLForm
// document. Deserializes the URLForm documents to determine if they are equal.
// Return an error if the two URLForm documents are not equal.
func CompareURLFormReaderBytes(r io.Reader, expect []byte) error {
	if r == nil {
		return fmt.Errorf("missing body")
	}
	actual, err := ioutil.ReadAll(r)
	if err != nil {
		return fmt.Errorf("unable to read, %v", err)
	}

	if err := URLFormEqual(expect, actual); err != nil {
		return fmt.Errorf("URL query forms not equal, %v", err)
	}
	return nil
}