...
1 package testing
2
3 import (
4 "bytes"
5 "encoding/json"
6 "fmt"
7 "reflect"
8 "sort"
9 "strings"
10
11 "github.com/aws/smithy-go/testing/xml"
12 )
13
14
15
16 func JSONEqual(expectBytes, actualBytes []byte) error {
17 var expect interface{}
18 if err := json.Unmarshal(expectBytes, &expect); err != nil {
19 return fmt.Errorf("failed to unmarshal expected bytes, %v", err)
20 }
21
22 var actual interface{}
23 if err := json.Unmarshal(actualBytes, &actual); err != nil {
24 return fmt.Errorf("failed to unmarshal actual bytes, %v", err)
25 }
26
27 if !reflect.DeepEqual(expect, actual) {
28 return fmt.Errorf("JSON mismatch: %v != %v", expect, actual)
29 }
30
31 return nil
32 }
33
34
35
36
37 func AssertJSONEqual(t T, expect, actual []byte) bool {
38 t.Helper()
39
40 if err := JSONEqual(expect, actual); err != nil {
41 t.Errorf("expect JSON documents to be equal, %v", err)
42 return false
43 }
44
45 return true
46 }
47
48
49
50
51 func XMLEqual(expectBytes, actualBytes []byte) error {
52 actualString, err := xml.SortXML(bytes.NewBuffer(actualBytes), true)
53 if err != nil {
54 return err
55 }
56
57 expectedString, err := xml.SortXML(bytes.NewBuffer(expectBytes), true)
58 if err != nil {
59 return err
60 }
61
62 if expectedString != actualString {
63 return fmt.Errorf("XML mismatch: %v != %v", expectedString, actualString)
64 }
65
66 return nil
67 }
68
69
70
71
72 func AssertXMLEqual(t T, expect, actual []byte) bool {
73 t.Helper()
74
75 if err := XMLEqual(expect, actual); err != nil {
76 t.Errorf("expect XML documents to be equal, %v", err)
77 return false
78 }
79
80 return true
81 }
82
83
84
85
86 func URLFormEqual(expectBytes, actualBytes []byte) error {
87 expect := parseFormBody(expectBytes)
88 actual := parseFormBody(actualBytes)
89 if !reflect.DeepEqual(expect, actual) {
90 return fmt.Errorf("Query mismatch: %v != %v", expect, actual)
91 }
92 return nil
93 }
94
95 func parseFormBody(bytes []byte) []QueryItem {
96 str := string(bytes)
97
98
99 str = strings.Join(strings.Fields(str), "")
100 parsed := ParseRawQuery(str)
101 sort.SliceStable(parsed, func(i, j int) bool {
102 return parsed[i].Key < parsed[j].Key
103 })
104 return parsed
105 }
106
107
108
109
110 func AssertURLFormEqual(t T, expect, actual []byte) bool {
111 t.Helper()
112
113 if err := URLFormEqual(expect, actual); err != nil {
114 t.Errorf("expect URLForm documents to be equal, %v", err)
115 return false
116 }
117
118 return true
119 }
120
View as plain text