github.com/go-kivik/kivik/v4@v4.3.2/couchdb/chttp/common_test.go (about) 1 // Licensed under the Apache License, Version 2.0 (the "License"); you may not 2 // use this file except in compliance with the License. You may obtain a copy of 3 // the License at 4 // 5 // http://www.apache.org/licenses/LICENSE-2.0 6 // 7 // Unless required by applicable law or agreed to in writing, software 8 // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 9 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 10 // License for the specific language governing permissions and limitations under 11 // the License. 12 13 package chttp 14 15 import ( 16 "io" 17 "regexp" 18 "testing" 19 20 "gitlab.com/flimzy/testy" 21 ) 22 23 // statusErrorRE is a modified version of testy.StatusError, which handles 24 // exit statuses as well. 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 }