github.com/vnpaycloud-console/gophercloud/v2@v2.0.5/testing/errors_test.go (about) 1 package testing 2 3 import ( 4 "fmt" 5 "net/http" 6 "testing" 7 8 "github.com/vnpaycloud-console/gophercloud/v2" 9 th "github.com/vnpaycloud-console/gophercloud/v2/testhelper" 10 ) 11 12 func TestErrUnexpectedResponseCode(t *testing.T) { 13 err := gophercloud.ErrUnexpectedResponseCode{ 14 URL: "http://example.com", 15 Method: "GET", 16 Expected: []int{200}, 17 Actual: 404, 18 Body: []byte("the response body"), 19 ResponseHeader: nil, 20 } 21 22 th.AssertEquals(t, err.GetStatusCode(), 404) 23 th.AssertEquals(t, gophercloud.ResponseCodeIs(err, http.StatusNotFound), true) 24 th.AssertEquals(t, gophercloud.ResponseCodeIs(err, http.StatusInternalServerError), false) 25 26 //even if application code wraps our error, ResponseCodeIs() should still work 27 errWrapped := fmt.Errorf("could not frobnicate the foobar: %w", err) 28 th.AssertEquals(t, gophercloud.ResponseCodeIs(errWrapped, http.StatusNotFound), true) 29 th.AssertEquals(t, gophercloud.ResponseCodeIs(errWrapped, http.StatusInternalServerError), false) 30 }