MCPcopy
hub / github.com/gin-gonic/gin / testProtoBodyBindingFail

Function testProtoBodyBindingFail

binding/binding_test.go:1406–1429  ·  view source on GitHub ↗
(t *testing.T, b Binding, name, path, badPath, body, badBody string)

Source from the content-addressed store, hash-verified

1404}
1405
1406func testProtoBodyBindingFail(t *testing.T, b Binding, name, path, badPath, body, badBody string) {
1407 assert.Equal(t, name, b.Name())
1408
1409 obj := protoexample.Test{}
1410 req := requestWithBody(http.MethodPost, path, body)
1411
1412 req.Body = io.NopCloser(&hook{})
1413 req.Header.Add("Content-Type", MIMEPROTOBUF)
1414 err := b.Bind(req, &obj)
1415 require.Error(t, err)
1416
1417 invalidobj := FooStruct{}
1418 req.Body = io.NopCloser(strings.NewReader(`{"msg":"hello"}`))
1419 req.Header.Add("Content-Type", MIMEPROTOBUF)
1420 err = b.Bind(req, &invalidobj)
1421 require.Error(t, err)
1422 assert.Equal(t, "obj is not ProtoMessage", err.Error())
1423
1424 obj = protoexample.Test{}
1425 req = requestWithBody(http.MethodPost, badPath, badBody)
1426 req.Header.Add("Content-Type", MIMEPROTOBUF)
1427 err = ProtoBuf.Bind(req, &obj)
1428 require.Error(t, err)
1429}
1430
1431func requestWithBody(method, path, body string) (req *http.Request) {
1432 req, _ = http.NewRequest(method, path, bytes.NewBufferString(body))

Callers 1

TestBindingProtoBufFailFunction · 0.85

Calls 4

requestWithBodyFunction · 0.85
NameMethod · 0.65
BindMethod · 0.65
ErrorMethod · 0.45

Tested by

no test coverage detected