Skip to content

Commit c4d6bce

Browse files
committed
fix test assertion matching on ErrDefault401 error format
This includes a backwards-incompatible change to the error message returned by ErrUnexpectedResponseCode. Error messages are not supposed to contain line breaks because several logging systems do not handle them well (e.g. syslog and fluentd), so we try at least a little bit to get rid of them.
1 parent 42c4b25 commit c4d6bce

2 files changed

Lines changed: 8 additions & 4 deletions

File tree

errors.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package gophercloud
22

33
import (
4+
"bytes"
45
"errors"
56
"fmt"
67
"net/http"
@@ -89,8 +90,8 @@ type ErrUnexpectedResponseCode struct {
8990

9091
func (e ErrUnexpectedResponseCode) Error() string {
9192
e.DefaultErrString = fmt.Sprintf(
92-
"Expected HTTP response code %v when accessing [%s %s], but got %d instead\n%s",
93-
e.Expected, e.Method, e.URL, e.Actual, e.Body,
93+
"Expected HTTP response code %v when accessing [%s %s], but got %d instead: %s",
94+
e.Expected, e.Method, e.URL, e.Actual, bytes.TrimSpace(e.Body),
9495
)
9596
return e.choseErrString()
9697
}

testing/provider_client_test.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"net"
88
"net/http"
99
"net/http/httptest"
10+
"regexp"
1011
"strconv"
1112
"strings"
1213
"sync"
@@ -364,8 +365,10 @@ func TestRequestReauthsAtMostOnce(t *testing.T) {
364365
// did not attempt reauthentication again and just passed that 401 response to
365366
// the caller as ErrDefault401.
366367
_, err := p.Request(context.TODO(), "GET", th.Endpoint()+"/route", &gophercloud.RequestOpts{})
367-
expectedErrorMessage := "Successfully re-authenticated, but got error executing request: Authentication failed"
368-
th.AssertEquals(t, expectedErrorMessage, err.Error())
368+
expectedErrorRx := regexp.MustCompile(`^Successfully re-authenticated, but got error executing request: Expected HTTP response code \[200\] when accessing \[GET http://[^/]*//route\], but got 401 instead: unauthorized$`)
369+
if !expectedErrorRx.MatchString(err.Error()) {
370+
t.Errorf("expected error that looks like %q, but got %q", expectedErrorRx.String(), err.Error())
371+
}
369372
}
370373

371374
func TestRequestWithContext(t *testing.T) {

0 commit comments

Comments
 (0)