本文整理汇总了Golang中github.com/stretchr/testify/require.EqualError函数的典型用法代码示例。如果您正苦于以下问题:Golang EqualError函数的具体用法?Golang EqualError怎么用?Golang EqualError使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了EqualError函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: TestGetSingleValueFromHeader
func TestGetSingleValueFromHeader(t *testing.T) {
t.Log("Simple OK")
{
val, err := GetSingleValueFromHeader("Key", http.Header{"Key": {"value1"}})
require.NoError(t, err)
require.Equal(t, "value1", val)
}
t.Log("Not found - empty header")
{
_, err := GetSingleValueFromHeader("No-Key", http.Header{})
require.EqualError(t, err, "No value found in HEADER for the key: No-Key")
}
t.Log("Not found - different key")
{
_, err := GetSingleValueFromHeader("No-Key", http.Header{"Key": {"value1"}})
require.EqualError(t, err, "No value found in HEADER for the key: No-Key")
}
t.Log("Multiple values")
{
_, err := GetSingleValueFromHeader("Key", http.Header{"Key": {"value1", "value2"}})
require.EqualError(t, err, "Multiple values found in HEADER for the key: Key")
}
}
示例2: Test_transformTagPushEvent
func Test_transformTagPushEvent(t *testing.T) {
t.Log("Do Transform")
{
tagPush := TagPushEventModel{
ObjectKind: "tag_push",
Ref: "refs/tags/v0.0.2",
CheckoutSHA: "7f29cdf31fdff43d7f31a279eec06c9f19ae0d6b",
}
hookTransformResult := transformTagPushEvent(tagPush)
require.NoError(t, hookTransformResult.Error)
require.False(t, hookTransformResult.ShouldSkip)
require.Equal(t, []bitriseapi.TriggerAPIParamsModel{
{
BuildParams: bitriseapi.BuildParamsModel{
Tag: "v0.0.2",
CommitHash: "7f29cdf31fdff43d7f31a279eec06c9f19ae0d6b",
},
},
}, hookTransformResult.TriggerAPIParams)
}
t.Log("No CheckoutSHA (tag delete)")
{
tagPush := TagPushEventModel{
ObjectKind: "tag_push",
Ref: "refs/tags/v0.0.2",
CheckoutSHA: "",
}
hookTransformResult := transformTagPushEvent(tagPush)
require.EqualError(t, hookTransformResult.Error, "This is a Tag Deleted event, no build is required")
require.True(t, hookTransformResult.ShouldSkip)
require.Nil(t, hookTransformResult.TriggerAPIParams)
}
t.Log("Not a tags ref")
{
tagPush := TagPushEventModel{
ObjectKind: "tag_push",
Ref: "refs/not/a/tag",
CheckoutSHA: "7f29cdf31fdff43d7f31a279eec06c9f19ae0d6b",
}
hookTransformResult := transformTagPushEvent(tagPush)
require.EqualError(t, hookTransformResult.Error, "Ref (refs/not/a/tag) is not a tags ref")
require.False(t, hookTransformResult.ShouldSkip)
require.Nil(t, hookTransformResult.TriggerAPIParams)
}
t.Log("Not a tag_push object")
{
tagPush := TagPushEventModel{
ObjectKind: "not-a-tag_push",
Ref: "refs/tags/v0.0.2",
CheckoutSHA: "7f29cdf31fdff43d7f31a279eec06c9f19ae0d6b",
}
hookTransformResult := transformTagPushEvent(tagPush)
require.EqualError(t, hookTransformResult.Error, "Not a Tag Push object: not-a-tag_push")
require.False(t, hookTransformResult.ShouldSkip)
require.Nil(t, hookTransformResult.TriggerAPIParams)
}
}
示例3: Test_detectContentTypeAndEventID
func Test_detectContentTypeAndEventID(t *testing.T) {
t.Log("Code Push event")
{
header := http.Header{
"X-Gogs-Event": {"push"},
"Content-Type": {"application/json"},
}
contentType, eventID, err := detectContentTypeAndEventID(header)
require.NoError(t, err)
require.Equal(t, "application/json", contentType)
require.Equal(t, "push", eventID)
}
t.Log("Missing X-Gogs-Event header")
{
header := http.Header{
"Content-Type": {"application/json"},
}
contentType, eventID, err := detectContentTypeAndEventID(header)
require.EqualError(t, err, "Issue with X-Gogs-Event Header: No value found in HEADER for the key: X-Gogs-Event")
require.Equal(t, "", contentType)
require.Equal(t, "", eventID)
}
t.Log("Missing Content-Type")
{
header := http.Header{
"X-Gogs-Event": {"push"},
}
contentType, eventID, err := detectContentTypeAndEventID(header)
require.EqualError(t, err, "Issue with Content-Type Header: No value found in HEADER for the key: Content-Type")
require.Equal(t, "", contentType)
require.Equal(t, "", eventID)
}
}
示例4: TestSignedFullSerializeAndToken
func TestSignedFullSerializeAndToken(t *testing.T) {
b := Signed(rsaSigner).Claims(&testClaims{"foo"})
jwt, err := b.FullSerialize()
require.NoError(t, err, "Error creating JWT.")
parsed, err := ParseSigned(jwt)
require.NoError(t, err, "Error parsing JWT.")
out := &testClaims{}
if assert.NoError(t, parsed.Claims(&testPrivRSAKey1.PublicKey, &out), "Error unmarshaling claims.") {
assert.Equal(t, &testClaims{
Subject: "foo",
}, out)
}
jwt2, err := b.Token()
require.NoError(t, err, "Error creating JWT.")
out2 := &testClaims{}
if assert.NoError(t, jwt2.Claims(&testPrivRSAKey1.PublicKey, &out2), "Error unmarshaling claims.") {
assert.Equal(t, &testClaims{
Subject: "foo",
}, out2)
}
b2 := Signed(rsaSigner).Claims(&invalidMarshalClaims{})
_, err = b2.FullSerialize()
require.EqualError(t, err, "json: error calling MarshalJSON for type *jwt.invalidMarshalClaims: Failed marshaling invalid claims.")
_, err = b2.Token()
require.EqualError(t, err, "json: error calling MarshalJSON for type *jwt.invalidMarshalClaims: Failed marshaling invalid claims.")
}
示例5: Test_HookProvider_TransformRequest
func Test_HookProvider_TransformRequest(t *testing.T) {
provider := HookProvider{}
t.Log("Should be OK")
{
request := http.Request{
Header: http.Header{
"Content-Type": {"application/x-www-form-urlencoded"},
},
}
form := url.Values{}
form.Add("trigger_word", "bitrise:")
form.Add("text", "bitrise: branch:master")
request.PostForm = form
hookTransformResult := provider.TransformRequest(&request)
require.NoError(t, hookTransformResult.Error)
require.False(t, hookTransformResult.ShouldSkip)
require.Equal(t, []bitriseapi.TriggerAPIParamsModel{
{
BuildParams: bitriseapi.BuildParamsModel{
Branch: "master",
Environments: []bitriseapi.EnvironmentItem{},
},
},
}, hookTransformResult.TriggerAPIParams)
}
t.Log("Unsupported Event Type")
{
request := http.Request{
Header: http.Header{
"Content-Type": {"application/json"},
},
}
hookTransformResult := provider.TransformRequest(&request)
require.False(t, hookTransformResult.ShouldSkip)
require.EqualError(t, hookTransformResult.Error, "Content-Type is not supported: application/json")
}
t.Log("Missing 'text' from request data")
{
request := http.Request{
Header: http.Header{
"Content-Type": {"application/x-www-form-urlencoded"},
},
}
form := url.Values{}
form.Add("trigger_word", "the trigger word")
request.PostForm = form
hookTransformResult := provider.TransformRequest(&request)
require.False(t, hookTransformResult.ShouldSkip)
require.EqualError(t, hookTransformResult.Error, "Failed to parse the request/message: 'trigger_word' parameter found, but 'text' parameter is missing or empty")
}
}
示例6: TestBrokenCall
func TestBrokenCall(t *testing.T) {
cli, listener, conn := prepTest(t)
defer endTest(t, conn, listener)
err := cli.BrokenMethod()
require.EqualError(t, err, "method 'broken' not found in protocol 'test.1.testp'")
err = cli.BrokenProtocol()
require.EqualError(t, err, "protocol not found: test.2.testp")
}
示例7: TestSetNotifiesStage
func TestSetNotifiesStage(t *testing.T) {
tnflog := &testNflog{}
s := &SetNotifiesStage{
recv: &nflogpb.Receiver{GroupName: "test"},
nflog: tnflog,
}
alerts := []*types.Alert{{}, {}, {}}
ctx := context.Background()
resctx, res, err := s.Exec(ctx, alerts...)
require.EqualError(t, err, "notification hash missing")
require.Nil(t, res)
require.NotNil(t, resctx)
ctx = WithNotificationHash(ctx, []byte{1, 2, 3})
_, res, err = s.Exec(ctx, alerts...)
require.EqualError(t, err, "group key missing")
require.Nil(t, res)
require.NotNil(t, resctx)
ctx = WithGroupKey(ctx, 1)
s.resolved = func([]*types.Alert) bool { return false }
tnflog.logActiveFunc = func(r *nflogpb.Receiver, gkey, hash []byte) error {
require.Equal(t, s.recv, r)
require.Equal(t, []byte{0, 0, 0, 0, 0, 0, 0, 1}, gkey)
require.Equal(t, []byte{1, 2, 3}, hash)
return nil
}
tnflog.logResolvedFunc = func(r *nflogpb.Receiver, gkey, hash []byte) error {
t.Fatalf("LogResolved called unexpectedly")
return nil
}
resctx, res, err = s.Exec(ctx, alerts...)
require.Nil(t, err)
require.Equal(t, alerts, res)
require.NotNil(t, resctx)
s.resolved = func([]*types.Alert) bool { return true }
tnflog.logActiveFunc = func(r *nflogpb.Receiver, gkey, hash []byte) error {
t.Fatalf("LogActive called unexpectedly")
return nil
}
tnflog.logResolvedFunc = func(r *nflogpb.Receiver, gkey, hash []byte) error {
require.Equal(t, s.recv, r)
require.Equal(t, []byte{0, 0, 0, 0, 0, 0, 0, 1}, gkey)
require.Equal(t, []byte{1, 2, 3}, hash)
return nil
}
resctx, res, err = s.Exec(ctx, alerts...)
require.Nil(t, err)
require.Equal(t, alerts, res)
require.NotNil(t, resctx)
}
示例8: Test_getInputTextFromFormRequest
func Test_getInputTextFromFormRequest(t *testing.T) {
t.Log("Proper Form content")
{
request := http.Request{}
form := url.Values{}
form.Add("trigger_word", "the trigger word")
form.Add("text", "the trigger word the text")
request.PostForm = form
text, err := getInputTextFromFormRequest(&request)
require.NoError(t, err)
require.Equal(t, "the text", text)
}
t.Log("Missing trigger_word")
{
request := http.Request{}
form := url.Values{}
form.Add("text", "the text")
request.PostForm = form
text, err := getInputTextFromFormRequest(&request)
require.EqualError(t, err, "Missing required parameter: either 'command' or 'trigger_word' should be specified")
require.Equal(t, "", text)
}
t.Log("Missing text - trigger_word")
{
request := http.Request{}
form := url.Values{}
form.Add("trigger_word", "the trigger word")
request.PostForm = form
text, err := getInputTextFromFormRequest(&request)
require.EqualError(t, err, "'trigger_word' parameter found, but 'text' parameter is missing or empty")
require.Equal(t, "", text)
}
t.Log("Missing text - command")
{
request := http.Request{}
form := url.Values{}
form.Add("command", "the-command")
request.PostForm = form
text, err := getInputTextFromFormRequest(&request)
require.EqualError(t, err, "'command' parameter found, but 'text' parameter is missing or empty")
require.Equal(t, "", text)
}
}
示例9: TestLongCallCancel
func TestLongCallCancel(t *testing.T) {
cli, listener, conn := prepTest(t)
defer endTest(t, conn, listener)
ctx, cancel := context.WithCancel(context.Background())
ctx = AddRpcTagsToContext(ctx, CtxRpcTags{"hello": []string{"world"}})
resultCh := make(chan longCallResult)
runInBg(func() error {
var longResult interface{}
var err error
longResult, err = cli.LongCall(ctx)
resultCh <- longCallResult{longResult, err}
longResult, err = cli.LongCallResult(context.Background())
resultCh <- longCallResult{longResult, err}
longResult, err = cli.LongCallDebugTags(context.Background())
resultCh <- longCallResult{longResult, err}
return nil
})
// TODO figure out a way to avoid this sleep
time.Sleep(time.Millisecond)
cancel()
res := <-resultCh
require.EqualError(t, res.err, context.Canceled.Error())
require.Equal(t, 0, res.res, "call should be canceled")
res = <-resultCh
require.Nil(t, res.err, "call should have succeeded")
require.Equal(t, -1, res.res, "canceled call should have set the longCallResult to canceled")
res = <-resultCh
require.Nil(t, res.err, "call should have succeeded")
require.Equal(t, CtxRpcTags{"hello": []interface{}{"world"}}, res.res, "canceled call should have set the debug tags")
}
示例10: TestCloseReceiver
func TestCloseReceiver(t *testing.T) {
// Call error status
waitCh := make(chan error, 1)
p := &Protocol{
Name: "waiter",
Methods: map[string]ServeHandlerDescription{
"wait": {
MakeArg: func() interface{} {
return nil
},
Handler: func(c context.Context, i interface{}) (interface{}, error) {
<-c.Done()
waitCh <- c.Err()
return nil, c.Err()
},
MethodType: MethodCall,
},
},
}
receiver, _ := testReceive(
t,
p,
makeCall(
0,
"waiter.wait",
nil,
),
)
<-receiver.Close()
err := <-waitCh
require.EqualError(t, err, context.Canceled.Error())
}
示例11: Test_MachineConfigModel_normalizeAndValidate
func Test_MachineConfigModel_normalizeAndValidate(t *testing.T) {
t.Log("Invalid CleanupMode")
{
configModel := MachineConfigModel{CleanupMode: ""}
require.EqualError(t, configModel.normalizeAndValidate(), "Invalid CleanupMode: ")
}
t.Log("Minimal valid")
{
configModel := MachineConfigModel{
CleanupMode: CleanupModeRollback,
IsCleanupBeforeSetup: true,
IsDoTimesyncAtSetup: false,
}
t.Logf("configModel: %#v", configModel)
require.NoError(t, configModel.normalizeAndValidate())
require.Equal(t, true, configModel.IsCleanupBeforeSetup)
require.Equal(t, false, configModel.IsDoTimesyncAtSetup)
// defaults
require.Equal(t, EnvItemsModel{}, configModel.Envs)
require.Equal(t, map[string]EnvItemsModel{}, configModel.ConfigTypeEnvs)
}
}
示例12: TestBadStatusCode
func TestBadStatusCode(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusServiceUnavailable)
json.NewEncoder(w).Encode(`{
"errors": {
"system": [
"The API is currently down for maintenance. It'll be back shortly."
]
}
}`)
}))
defer ts.Close()
l := NewLibrato(ts.URL)
l.ApiUser = "[email protected]"
l.ApiToken = "123456"
err := l.Connect()
require.NoError(t, err)
err = l.Write(testutil.MockBatchPoints().Points())
if err == nil {
t.Errorf("error expected but none returned")
} else {
require.EqualError(t, fmt.Errorf("received bad status code, 503\n"), err.Error())
}
}
示例13: Test_InvalidCommand
func Test_InvalidCommand(t *testing.T) {
t.Log("Invalid command")
{
_, err := cmdex.RunCommandAndReturnCombinedStdoutAndStderr(binPath(), "invalidcmd")
require.EqualError(t, err, "exit status 1")
}
}
示例14: Test_detectContentTypeAndEventID
func Test_detectContentTypeAndEventID(t *testing.T) {
t.Log("Code Push event")
{
header := http.Header{
"X-Gitlab-Event": {"Push Hook"},
"Content-Type": {"application/json"},
}
contentType, eventID, err := detectContentTypeAndEventID(header)
require.NoError(t, err)
require.Equal(t, "application/json", contentType)
require.Equal(t, "Push Hook", eventID)
}
t.Log("Unsupported event - will be handled in Transform")
{
header := http.Header{
"X-Gitlab-Event": {"Tag Push Hook"},
"Content-Type": {"application/json"},
}
contentType, eventID, err := detectContentTypeAndEventID(header)
require.NoError(t, err)
require.Equal(t, "application/json", contentType)
require.Equal(t, "Tag Push Hook", eventID)
}
t.Log("Missing X-Gitlab-Event header")
{
header := http.Header{
"Content-Type": {"application/json"},
}
contentType, eventID, err := detectContentTypeAndEventID(header)
require.EqualError(t, err, "Issue with X-Gitlab-Event Header: No value found in HEADER for the key: X-Gitlab-Event")
require.Equal(t, "", contentType)
require.Equal(t, "", eventID)
}
t.Log("Missing Content-Type")
{
header := http.Header{
"X-Gitlab-Event": {"Push Hook"},
}
contentType, eventID, err := detectContentTypeAndEventID(header)
require.EqualError(t, err, "Issue with Content-Type Header: No value found in HEADER for the key: Content-Type")
require.Equal(t, "", contentType)
require.Equal(t, "", eventID)
}
}
示例15: Test_parseGoVersionFromGoVersionOutput
func Test_parseGoVersionFromGoVersionOutput(t *testing.T) {
t.Log("Example OK")
{
verStr, err := parseGoVersionFromGoVersionOutput("go version go1.7 darwin/amd64")
require.NoError(t, err)
require.Equal(t, "1.7", verStr)
}
t.Log("Example OK 2")
{
verStr, err := parseGoVersionFromGoVersionOutput(`go version go1.7 darwin/amd64
`)
require.NoError(t, err)
require.Equal(t, "1.7", verStr)
}
t.Log("Example OK 3")
{
verStr, err := parseGoVersionFromGoVersionOutput("go version go1.7.1 darwin/amd64")
require.NoError(t, err)
require.Equal(t, "1.7.1", verStr)
}
t.Log("Empty")
{
verStr, err := parseGoVersionFromGoVersionOutput("")
require.EqualError(t, err, "Failed to parse Go version, error: version call output was empty")
require.Equal(t, "", verStr)
}
t.Log("Empty 2")
{
verStr, err := parseGoVersionFromGoVersionOutput(`
`)
require.EqualError(t, err, "Failed to parse Go version, error: version call output was empty")
require.Equal(t, "", verStr)
}
t.Log("Invalid")
{
verStr, err := parseGoVersionFromGoVersionOutput("go version REMOVED darwin/amd64")
require.EqualError(t, err, "Failed to parse Go version, error: failed to find version in input: go version REMOVED darwin/amd64")
require.Equal(t, "", verStr)
}
}