Skip to content

Commit 5be64f8

Browse files
committed
Add test helpers for testdata-based and HTTP assertions
1 parent 0e815f0 commit 5be64f8

File tree

1 file changed

+24
-2
lines changed

1 file changed

+24
-2
lines changed

internal/messagebirdtest/helper.go

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
package messagebirdtest
22

33
import (
4+
"bytes"
45
"io/ioutil"
56
"path/filepath"
67
"testing"
78
)
89

910
const testdataDir = "testdata"
1011

11-
// testdata returns a file's bytes based on the path relative to the testdata
12+
// Testdata returns a file's bytes based on the path relative to the testdata
1213
// directory. It fails the test if the testdata file can not be read.
13-
func testdata(t *testing.T, relativePath string) []byte {
14+
func Testdata(t *testing.T, relativePath string) []byte {
1415
path := filepath.Join(testdataDir, relativePath)
1516

1617
b, err := ioutil.ReadFile(path)
@@ -20,3 +21,24 @@ func testdata(t *testing.T, relativePath string) []byte {
2021

2122
return b
2223
}
24+
25+
// AssertTestdata gets testdata and asserts it equals actual.
26+
func AssertTestdata(t *testing.T, relativePath string, actual []byte) {
27+
expected := Testdata(t, relativePath)
28+
29+
if !bytes.Equal(expected, actual) {
30+
t.Fatalf("expected %s, got %s", expected, actual)
31+
}
32+
}
33+
34+
// AssertEndpointCalled fails the test if the last request was not made to the
35+
// provided endpoint (e.g. combination of HTTP method and path).
36+
func AssertEndpointCalled(t *testing.T, method, path string) {
37+
if Request.Method != method {
38+
t.Fatalf("expected %s, got %s", method, Request.Method)
39+
}
40+
41+
if escapedPath := Request.URL.EscapedPath(); escapedPath != path {
42+
t.Fatalf("expected %s, got %s", path, escapedPath)
43+
}
44+
}

0 commit comments

Comments
 (0)