func (suite *clientServerSuite) TestE2E() { suite.server.AddEndpoints( server.Endpoint{ Name: "test", Request: new(testproto.DummyRequest), Response: new(testproto.DummyResponse), Handler: func(req mercury.Request) (mercury.Response, error) { return req.Response(&testproto.DummyResponse{ Pong: "teste2e", }), nil }}) cl := client.NewClient(). SetMiddleware(DefaultClientMiddleware()). Add( client.Call{ Uid: "call", Service: testServiceName, Endpoint: "test", Body: &testproto.DummyRequest{}, Response: &testproto.DummyResponse{}, }). SetTransport(suite.trans). SetTimeout(time.Second). Execute() suite.Assert().False(cl.Errors().Any()) rsp := cl.Response("call") suite.Assert().NotNil(rsp) response := rsp.Body().(*testproto.DummyResponse) suite.Assert().Equal("teste2e", response.Pong) }
// TestErrors verifies that an error sent from a handler is correctly returned by a client func (suite *clientServerSuite) TestErrors() { suite.server.AddEndpoints(server.Endpoint{ Name: "error", Request: new(testproto.DummyRequest), Response: new(testproto.DummyResponse), Handler: func(req mercury.Request) (mercury.Response, error) { return nil, terrors.BadRequest("", "naughty naughty", nil) }}) cl := client.NewClient(). SetMiddleware(DefaultClientMiddleware()). Add( client.Call{ Uid: "call", Service: testServiceName, Endpoint: "error", Body: &testproto.DummyRequest{}, Response: &testproto.DummyResponse{}, }). SetTransport(suite.trans). SetTimeout(time.Second). Execute() suite.Assert().True(cl.Errors().Any()) err := cl.Errors().ForUid("call") suite.Assert().NotNil(err) suite.Assert().Equal(terrors.ErrBadRequest, err.Code) }
// TestE2E verifies parent request IDs are properly set on child requests func (suite *parentRequestIdMiddlewareSuite) TestE2E() { cli := client. NewClient(). SetTransport(suite.trans). SetMiddleware([]client.ClientMiddleware{Middleware()}) dummyOrigin := mercury.NewRequest() dummyOrigin.SetId("foobarbaz") ctx := context.WithValue(dummyOrigin.Context(), "Current-Service", testOriginServiceName) ctx = context.WithValue(ctx, "Current-Endpoint", "e2etest") dummyOrigin.SetContext(ctx) cli.Add(client.Call{ Uid: "call", Service: testServiceName, Endpoint: "foo", Context: dummyOrigin, Response: &testproto.DummyResponse{}, Body: &testproto.DummyRequest{}}) cli.Execute() suite.Assert().NoError(cli.Errors().Combined()) rsp := cli.Response("call") response := rsp.Body().(*testproto.DummyResponse) suite.Assert().NotEmpty(response.Pong) suite.Assert().Equal(response.Pong, rsp.Headers()[parentIdHeader]) }
func (suite *parentRequestIdMiddlewareSuite) SetupTest() { suite.trans = mock.NewTransport() suite.srv = server.NewServer(testServiceName) suite.srv.AddMiddleware(Middleware()) suite.srv.AddEndpoints( server.Endpoint{ Name: "foo", Request: &testproto.DummyRequest{}, Response: &testproto.DummyResponse{}, Handler: func(req mercury.Request) (mercury.Response, error) { // Assert first call has correct origin suite.Assert().Equal(testOriginServiceName, OriginServiceFor(req)) suite.Assert().Equal("e2etest", OriginEndpointFor(req)) // Assert first call has updated to the current service suite.Assert().Equal(testServiceName, CurrentServiceFor(req)) suite.Assert().Equal("foo", CurrentEndpointFor(req)) cl := client.NewClient(). SetTransport(suite.trans). SetMiddleware([]client.ClientMiddleware{Middleware()}). Add( client.Call{ Uid: "call", Service: testServiceName, Endpoint: "foo-2", Body: &testproto.DummyRequest{}, Response: &testproto.DummyResponse{}, Context: req, }). Execute() return cl.Response("call"), cl.Errors().Combined() }}, server.Endpoint{ Name: "foo-2", Request: &testproto.DummyRequest{}, Response: &testproto.DummyResponse{}, Handler: func(req mercury.Request) (mercury.Response, error) { // Assert origin headers were set correctly as previous service suite.Assert().Equal(testServiceName, OriginServiceFor(req)) suite.Assert().Equal("foo", OriginEndpointFor(req)) // And that our current service's headers were set suite.Assert().Equal(testServiceName, CurrentServiceFor(req)) suite.Assert().Equal("foo-2", CurrentEndpointFor(req)) return req.Response(&testproto.DummyResponse{ Pong: ParentRequestIdFor(req)}), nil }}) suite.srv.Start(suite.trans) }
// TestErrors verifies that an error sent from a handler is correctly returned by a client func (suite *clientServerSuite) TestErrors() { suite.server.AddEndpoints(server.Endpoint{ Name: "error", Request: new(testproto.DummyRequest), Response: new(testproto.DummyResponse), Handler: func(req mercury.Request) (mercury.Response, error) { return nil, terrors.BadRequest("", "naughty naughty", nil) }}) cl := client.NewClient(). SetMiddleware(DefaultClientMiddleware()). Add( client.Call{ Uid: "call", Service: testServiceName, Endpoint: "error", Body: &testproto.DummyRequest{}, Response: &testproto.DummyResponse{}, }). SetTransport(suite.trans). SetTimeout(time.Second). Execute() suite.Assert().True(cl.Errors().Any()) err := cl.Errors().ForUid("call") suite.Require().NotNil(err) suite.Assert().Equal(terrors.ErrBadRequest, err.Code) rsp := mercury.FromTyphonResponse(cl.Response("call").Copy()) rsp.SetBody("FOO") // Deliberately set this to verify it is not mutated while accessing the error suite.Require().NotNil(rsp) suite.Assert().True(rsp.IsError()) suite.Assert().NotNil(rsp.Error()) suite.Assert().IsType(&terrors.Error{}, rsp.Error()) err = rsp.Error().(*terrors.Error) suite.Assert().Equal(terrors.ErrBadRequest, err.Code) suite.Assert().Equal("FOO", rsp.Body()) }
// TestJSON verifies a JSON request and response can be received from a protobuf handler func (suite *clientServerSuite) TestJSON() { suite.server.AddEndpoints( server.Endpoint{ Name: "test", Request: new(testproto.DummyRequest), Response: new(testproto.DummyResponse), Handler: func(req mercury.Request) (mercury.Response, error) { request := req.Body().(*testproto.DummyRequest) return req.Response(&testproto.DummyResponse{ Pong: request.Ping, }), nil }}) req := mercury.NewRequest() req.SetService(testServiceName) req.SetEndpoint("test") req.SetPayload([]byte(`{ "ping": "blah blah blah" }`)) req.SetHeader(marshaling.ContentTypeHeader, "application/json") req.SetHeader(marshaling.AcceptHeader, "application/json") cl := client.NewClient(). SetMiddleware(DefaultClientMiddleware()). AddRequest("call", req). SetTransport(suite.trans). SetTimeout(time.Second). Execute() suite.Assert().False(cl.Errors().Any()) rsp := cl.Response("call") suite.Assert().NotNil(rsp) var body map[string]string suite.Assert().NoError(json.Unmarshal(rsp.Payload(), &body)) suite.Assert().NotNil(body) suite.Assert().Equal(1, len(body)) suite.Assert().Equal("blah blah blah", body["pong"]) }