| OLD | NEW |
| (Empty) |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 package assertions | |
| 6 | |
| 7 import ( | |
| 8 "fmt" | |
| 9 | |
| 10 "github.com/GoogleCloudPlatform/go-endpoints/endpoints" | |
| 11 | |
| 12 "github.com/luci/luci-go/common/testing/assertions" | |
| 13 "github.com/smartystreets/goconvey/convey" | |
| 14 ) | |
| 15 | |
| 16 // ShouldHaveAPIError is a goconvey assertion, asserting that the supplied | |
| 17 // "actual" value is an *endpoints.APIError. | |
| 18 // | |
| 19 // One additional "expected" string may be optionally included. If included, the | |
| 20 // *endpoints.APIError's message is asserted to contain the expected string | |
| 21 // using ShouldErrorLike. | |
| 22 func ShouldHaveAPIError(actual interface{}, expected ...interface{}) string { | |
| 23 if len(expected) != 1 { | |
| 24 return "exactly one expected argument must be supplied, and it m
ust be an *endpoints.APIError." | |
| 25 } | |
| 26 eerr, ok := expected[0].(*endpoints.APIError) | |
| 27 if !ok { | |
| 28 return "expected argument must be an *endpoints.APIError." | |
| 29 } | |
| 30 | |
| 31 if err := convey.ShouldHaveSameTypeAs(actual, (*endpoints.APIError)(nil)
); err != "" { | |
| 32 return err | |
| 33 } | |
| 34 | |
| 35 aerr := actual.(*endpoints.APIError) | |
| 36 | |
| 37 // Create copies and strip the messages so we can compare values. We do
this | |
| 38 // rather than assert individual properties because the joint output is
nicer | |
| 39 // to look at. | |
| 40 aerrCopy := *aerr | |
| 41 aerrCopy.Msg = "" | |
| 42 | |
| 43 eerrCopy := *eerr | |
| 44 eerrCopy.Msg = "" | |
| 45 | |
| 46 if err := convey.ShouldResemble(aerrCopy, eerrCopy); err != "" { | |
| 47 return err | |
| 48 } | |
| 49 | |
| 50 // If a ShouldErrLike string was supplied, compare that, too. | |
| 51 if eerr.Msg != "" { | |
| 52 return assertions.ShouldErrLike(aerr, eerr.Msg) | |
| 53 } | |
| 54 return "" | |
| 55 } | |
| 56 | |
| 57 // ShouldBeInternalServerError asserts that "actual" is an *endpoints.APIError | |
| 58 // generated by endpoints.NewInternalServerError. | |
| 59 // | |
| 60 // One additional "expected" string may be optionally included. If included, the | |
| 61 // *endpoints.APIError's message is asserted to contain the expected string | |
| 62 // using ShouldErrorLike. | |
| 63 func ShouldBeInternalServerError(actual interface{}, expected ...interface{}) st
ring { | |
| 64 msg, err := getErrLikeMsg(expected) | |
| 65 if err != "" { | |
| 66 return err | |
| 67 } | |
| 68 return ShouldHaveAPIError(actual, endpoints.NewInternalServerError(msg)) | |
| 69 } | |
| 70 | |
| 71 // ShouldBeBadRequestError asserts that "actual" is an *endpoints.APIError | |
| 72 // generated by endpoints.NewBadRequestError. | |
| 73 // | |
| 74 // One additional "expected" string may be optionally included. If included, the | |
| 75 // *endpoints.APIError's message is asserted to contain the expected string | |
| 76 // using ShouldErrorLike. | |
| 77 func ShouldBeBadRequestError(actual interface{}, expected ...interface{}) string
{ | |
| 78 msg, err := getErrLikeMsg(expected) | |
| 79 if err != "" { | |
| 80 return err | |
| 81 } | |
| 82 return ShouldHaveAPIError(actual, endpoints.NewBadRequestError(msg)) | |
| 83 } | |
| 84 | |
| 85 // ShouldBeUnauthorizedError asserts that "actual" is an *endpoints.APIError | |
| 86 // generated by endpoints.NewUnauthorizedError. | |
| 87 // | |
| 88 // One additional "expected" string may be optionally included. If included, the | |
| 89 // *endpoints.APIError's message is asserted to contain the expected string | |
| 90 // using ShouldErrorLike. | |
| 91 func ShouldBeUnauthorizedError(actual interface{}, expected ...interface{}) stri
ng { | |
| 92 msg, err := getErrLikeMsg(expected) | |
| 93 if err != "" { | |
| 94 return err | |
| 95 } | |
| 96 return ShouldHaveAPIError(actual, endpoints.NewUnauthorizedError(msg)) | |
| 97 } | |
| 98 | |
| 99 // ShouldBeNotFoundError asserts that "actual" is an *endpoints.APIError | |
| 100 // generated by endpoints.NewNotFoundError. | |
| 101 // | |
| 102 // One additional "expected" string may be optionally included. If included, the | |
| 103 // *endpoints.APIError's message is asserted to contain the expected string | |
| 104 // using ShouldErrorLike. | |
| 105 func ShouldBeNotFoundError(actual interface{}, expected ...interface{}) string { | |
| 106 msg, err := getErrLikeMsg(expected) | |
| 107 if err != "" { | |
| 108 return err | |
| 109 } | |
| 110 return ShouldHaveAPIError(actual, endpoints.NewNotFoundError(msg)) | |
| 111 } | |
| 112 | |
| 113 // ShouldBeForbiddenError asserts that "actual" is an *endpoints.APIError | |
| 114 // generated by endpoints.NewForbiddenError. | |
| 115 // | |
| 116 // One additional "expected" string may be optionally included. If included, the | |
| 117 // *endpoints.APIError's message is asserted to contain the expected string | |
| 118 // using ShouldErrorLike. | |
| 119 func ShouldBeForbiddenError(actual interface{}, expected ...interface{}) string
{ | |
| 120 msg, err := getErrLikeMsg(expected) | |
| 121 if err != "" { | |
| 122 return err | |
| 123 } | |
| 124 return ShouldHaveAPIError(actual, endpoints.NewForbiddenError(msg)) | |
| 125 } | |
| 126 | |
| 127 // ShouldBeConflictError asserts that "actual" is an *endpoints.APIError | |
| 128 // generated by endpoints.NewConflictError. | |
| 129 // | |
| 130 // One additional "expected" string may be optionally included. If included, the | |
| 131 // *endpoints.APIError's message is asserted to contain the expected string | |
| 132 // using ShouldErrorLike. | |
| 133 func ShouldBeConflictError(actual interface{}, expected ...interface{}) string { | |
| 134 msg, err := getErrLikeMsg(expected) | |
| 135 if err != "" { | |
| 136 return err | |
| 137 } | |
| 138 return ShouldHaveAPIError(actual, endpoints.NewConflictError(msg)) | |
| 139 } | |
| 140 | |
| 141 func getErrLikeMsg(expected []interface{}) (string, string) { | |
| 142 switch len(expected) { | |
| 143 case 0: | |
| 144 return "", "" | |
| 145 case 1: | |
| 146 s, ok := expected[0].(string) | |
| 147 if !ok { | |
| 148 return "", fmt.Sprintf("expected message must be string,
not %T", expected[0]) | |
| 149 } | |
| 150 return s, "" | |
| 151 | |
| 152 default: | |
| 153 return "", "expected must have at most one argument" | |
| 154 } | |
| 155 } | |
| OLD | NEW |