go.uber.org/yarpc@v1.72.1/x/yarpctest/handler_unary.go (about)

     1  // Copyright (c) 2022 Uber Technologies, Inc.
     2  //
     3  // Permission is hereby granted, free of charge, to any person obtaining a copy
     4  // of this software and associated documentation files (the "Software"), to deal
     5  // in the Software without restriction, including without limitation the rights
     6  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     7  // copies of the Software, and to permit persons to whom the Software is
     8  // furnished to do so, subject to the following conditions:
     9  //
    10  // The above copyright notice and this permission notice shall be included in
    11  // all copies or substantial portions of the Software.
    12  //
    13  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    14  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    15  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    16  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    17  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    18  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    19  // THE SOFTWARE.
    20  
    21  package yarpctest
    22  
    23  import (
    24  	"context"
    25  	"io"
    26  	"io/ioutil"
    27  
    28  	"go.uber.org/yarpc/api/transport"
    29  	"go.uber.org/yarpc/x/yarpctest/api"
    30  	"go.uber.org/yarpc/x/yarpctest/types"
    31  )
    32  
    33  // EchoHandler is a Unary Handler that will echo the body of the request
    34  // into the response.
    35  func EchoHandler(mw ...api.UnaryInboundMiddleware) *types.UnaryHandler {
    36  	return &types.UnaryHandler{Handler: newEchoHandler(), Middleware: mw}
    37  }
    38  
    39  func newEchoHandler() api.UnaryHandler {
    40  	return api.UnaryHandlerFunc(
    41  		func(_ context.Context, req *transport.Request, resw transport.ResponseWriter) error {
    42  			_, err := io.Copy(resw, req.Body)
    43  			return err
    44  		},
    45  	)
    46  }
    47  
    48  // StaticHandler will always return the same response.
    49  func StaticHandler(msg string, mw ...api.UnaryInboundMiddleware) *types.UnaryHandler {
    50  	return &types.UnaryHandler{Handler: newStaticHandler(msg), Middleware: mw}
    51  }
    52  
    53  func newStaticHandler(msg string) api.UnaryHandler {
    54  	return api.UnaryHandlerFunc(
    55  		func(_ context.Context, _ *transport.Request, resw transport.ResponseWriter) error {
    56  			_, err := io.WriteString(resw, msg)
    57  			return err
    58  		},
    59  	)
    60  }
    61  
    62  // ErrorHandler will always return an Error.
    63  func ErrorHandler(err error, mw ...api.UnaryInboundMiddleware) *types.UnaryHandler {
    64  	return &types.UnaryHandler{Handler: newErrorHandler(err), Middleware: mw}
    65  }
    66  
    67  func newErrorHandler(err error) api.UnaryHandler {
    68  	return api.UnaryHandlerFunc(
    69  		func(context.Context, *transport.Request, transport.ResponseWriter) error {
    70  			return err
    71  		},
    72  	)
    73  }
    74  
    75  // EchoHandlerWithPrefix will echo the request it receives into the
    76  // response, but, it will insert a prefix in front of the message.
    77  func EchoHandlerWithPrefix(prefix string, mw ...api.UnaryInboundMiddleware) *types.UnaryHandler {
    78  	return &types.UnaryHandler{Handler: newEchoHandlerWithPrefix(prefix), Middleware: mw}
    79  }
    80  
    81  func newEchoHandlerWithPrefix(prefix string) api.UnaryHandler {
    82  	return api.UnaryHandlerFunc(
    83  		func(_ context.Context, req *transport.Request, resw transport.ResponseWriter) error {
    84  			body, err := ioutil.ReadAll(req.Body)
    85  			if err != nil {
    86  				return err
    87  			}
    88  			newMsg := prefix + string(body)
    89  			_, err = io.WriteString(resw, newMsg)
    90  			return err
    91  		},
    92  	)
    93  }
    94  
    95  // OrderedRequestHandler will execute a series of Handlers in the order they
    96  // are passed in.  If the number of requests does not match, it will return an
    97  // unknown error.
    98  func OrderedRequestHandler(options ...api.HandlerOption) *types.OrderedHandler {
    99  	opts := api.HandlerOpts{}
   100  	for _, option := range options {
   101  		option.ApplyHandler(&opts)
   102  	}
   103  	return &types.OrderedHandler{
   104  		Handlers: opts.Handlers,
   105  	}
   106  }