-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathvalues.go
184 lines (153 loc) · 5.17 KB
/
values.go
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/*******************************************************************************
*
* Copyright 2018 SAP SE
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You should have received a copy of the License along with this
* program. If not, you may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*******************************************************************************/
package assert
import (
"bytes"
"encoding/json"
"fmt"
"io"
"os"
"os/exec"
"path/filepath"
"strings"
"testing"
"github.com/sergi/go-diff/diffmatchpatch"
"github.com/sapcc/go-bits/osext"
)
// ByteData implements the HTTPRequestBody and HTTPResponseBody for plain bytestrings.
type ByteData []byte
// GetRequestBody implements the HTTPRequestBody interface.
func (b ByteData) GetRequestBody() (io.Reader, error) {
return bytes.NewReader([]byte(b)), nil
}
func logDiff(t *testing.T, expected, actual string) {
t.Helper()
if osext.GetenvBool("GOBITS_PRETTY_DIFF") {
dmp := diffmatchpatch.New()
diffs := dmp.DiffMain(fmt.Sprintf("%q\n", expected), fmt.Sprintf("%q\n", actual), false)
t.Log(dmp.DiffPrettyText(diffs))
} else {
t.Logf("\texpected = %q\n", expected)
t.Logf("\t actual = %q\n", actual)
}
}
// AssertResponseBody implements the HTTPResponseBody interface.
func (b ByteData) AssertResponseBody(t *testing.T, requestInfo string, responseBody []byte) bool {
t.Helper()
if !bytes.Equal([]byte(b), responseBody) {
t.Error(requestInfo + ": got unexpected response body")
logDiff(t, string(b), string(responseBody))
return false
}
return true
}
// StringData implements HTTPRequestBody and HTTPResponseBody for plain strings.
type StringData string
// GetRequestBody implements the HTTPRequestBody interface.
func (s StringData) GetRequestBody() (io.Reader, error) {
return strings.NewReader(string(s)), nil
}
// AssertResponseBody implements the HTTPResponseBody interface.
func (s StringData) AssertResponseBody(t *testing.T, requestInfo string, responseBody []byte) bool {
t.Helper()
responseStr := string(responseBody)
if responseStr != string(s) {
t.Errorf("%s: got unexpected response body", requestInfo)
logDiff(t, string(s), responseStr)
return false
}
return true
}
// JSONObject implements HTTPRequestBody and HTTPResponseBody for JSON objects.
type JSONObject map[string]any
// GetRequestBody implements the HTTPRequestBody interface.
func (o JSONObject) GetRequestBody() (io.Reader, error) {
buf, err := json.Marshal(o)
return bytes.NewReader(buf), err
}
// AssertResponseBody implements the HTTPResponseBody interface.
func (o JSONObject) AssertResponseBody(t *testing.T, requestInfo string, responseBody []byte) bool {
t.Helper()
buf, err := json.Marshal(o)
if err != nil {
t.Error(err.Error())
return false
}
// need to decode and re-encode the responseBody to ensure identical ordering of keys
var data map[string]any
err = json.Unmarshal(responseBody, &data)
if err == nil {
responseBody, err = json.Marshal(data)
if err != nil {
t.Errorf("JSON marshalling failed: %s", err.Error())
return false
}
}
if string(responseBody) != string(buf) {
t.Errorf("%s: got unexpected response body", requestInfo)
logDiff(t, string(buf), string(responseBody))
return false
}
return true
}
// JSONFixtureFile implements HTTPResponseBody by locating the expected JSON
// response body in the given file.
type JSONFixtureFile string
// AssertResponseBody implements the HTTPResponseBody interface.
func (f JSONFixtureFile) AssertResponseBody(t *testing.T, requestInfo string, responseBody []byte) bool {
t.Helper()
var buf bytes.Buffer
err := json.Indent(&buf, responseBody, "", " ")
if err != nil {
t.Logf("Response body: %s", responseBody)
t.Fatal(err)
return false
}
buf.WriteByte('\n')
return FixtureFile(f).AssertResponseBody(t, requestInfo, buf.Bytes())
}
// FixtureFile implements HTTPResponseBody by locating the expected
// plain-text response body in the given file.
type FixtureFile string
// AssertResponseBody implements the HTTPResponseBody interface.
func (f FixtureFile) AssertResponseBody(t *testing.T, requestInfo string, responseBody []byte) bool {
t.Helper()
// write actual content to file to make it easy to copy the computed result over
// to the fixture path when a new test is added or an existing one is modified
fixturePathAbs, err := filepath.Abs(string(f))
if err != nil {
t.Fatal(err)
return false
}
actualPathAbs := fixturePathAbs + ".actual"
err = os.WriteFile(actualPathAbs, responseBody, 0o666)
if err != nil {
t.Fatal(err)
return false
}
cmd := exec.Command("diff", "-u", fixturePathAbs, actualPathAbs)
cmd.Stdin = nil
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err = cmd.Run()
if err != nil {
t.Errorf("%s: body does not match: %s", requestInfo, err.Error())
}
return err == nil
}