...
1
2
3
4
5
6
7
8
9
10
11
12
13 package chttp
14
15 import (
16 "io"
17 "regexp"
18 "testing"
19
20 "gitlab.com/flimzy/testy"
21 )
22
23
24
25 func statusErrorRE(t *testing.T, expected string, status int, actual error) {
26 t.Helper()
27 var err string
28 var actualStatus int
29 if actual != nil {
30 err = actual.Error()
31 actualStatus = testy.StatusCode(actual)
32 }
33 match, e := regexp.MatchString(expected, err)
34 if e != nil {
35 t.Fatal(e)
36 }
37 if !match {
38 t.Errorf("Unexpected error: %s (expected %s)", err, expected)
39 }
40 if status != actualStatus {
41 t.Errorf("Unexpected status code: %d (expected %d) [%s]", actualStatus, status, err)
42 }
43 if actual != nil {
44 t.SkipNow()
45 }
46 }
47
48 type errReader struct {
49 io.Reader
50 err error
51 }
52
53 func (r *errReader) Read(p []byte) (int, error) {
54 c, err := r.Reader.Read(p)
55 if err == io.EOF {
56 err = r.err
57 }
58 return c, err
59 }
60
61 type errCloser struct {
62 io.Reader
63 err error
64 }
65
66 func (r *errCloser) Close() error {
67 return r.err
68 }
69
View as plain text