forked from messagebird/go-rest-api
/
helper.go
46 lines (37 loc) · 1.26 KB
/
helper.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
package mbtest
import (
"bytes"
"io/ioutil"
"path/filepath"
"testing"
)
const testdataDir = "testdata"
// Testdata returns a file's bytes based on the path relative to the testdata
// directory. It fails the test if the testdata file can not be read.
func Testdata(t *testing.T, relativePath string) []byte {
path := filepath.Join(testdataDir, relativePath)
b, err := ioutil.ReadFile(path)
if err != nil {
t.Fatalf("%s", err)
}
return b
}
// AssertTestdata gets testdata and asserts it equals actual. We start by
// slicing off all leading and trailing white space, as defined by Unicode.
func AssertTestdata(t *testing.T, relativePath string, actual []byte) {
expected := bytes.TrimSpace(Testdata(t, relativePath))
actual = bytes.TrimSpace(actual)
if !bytes.Equal(expected, actual) {
t.Fatalf("expected %s, got %s", expected, actual)
}
}
// AssertEndpointCalled fails the test if the last request was not made to the
// provided endpoint (e.g. combination of HTTP method and path).
func AssertEndpointCalled(t *testing.T, method, path string) {
if Request.Method != method {
t.Fatalf("expected %s, got %s", method, Request.Method)
}
if escapedPath := Request.URL.EscapedPath(); escapedPath != path {
t.Fatalf("expected %s, got %s", path, escapedPath)
}
}