go.uber.org/yarpc@v1.72.1/x/debug/debug_test.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 debug 22 23 import ( 24 "encoding/json" 25 "errors" 26 "io/ioutil" 27 "net/http" 28 "net/http/httptest" 29 "testing" 30 "text/template" 31 32 "go.uber.org/yarpc" 33 34 "github.com/stretchr/testify/require" 35 yarpchttp "go.uber.org/yarpc/transport/http" 36 ) 37 38 var ( 39 _jsonTestTmpl = template.Must(template.New("tmpl").Funcs(template.FuncMap{ 40 "jsonMarshal": func(v interface{}) (string, error) { 41 data, err := json.Marshal(v) 42 if err != nil { 43 return "", err 44 } 45 return string(data), nil 46 }, 47 }).Parse(`{{jsonMarshal .}}`)) 48 49 _errorTestTmpl = template.Must(template.New("tmpl").Funcs(template.FuncMap{ 50 "returnError": func(_ interface{}) (string, error) { 51 return "", errors.New("error") 52 }, 53 }).Parse(`{{returnError .}}`)) 54 ) 55 56 func TestHandler(t *testing.T) { 57 dispatcher := newTestDispatcher() 58 59 expectedData, err := json.Marshal(newTmplData(dispatcher.Introspect())) 60 require.NoError(t, err) 61 62 responseRecorder := httptest.NewRecorder() 63 NewHandler(dispatcher, tmpl(_jsonTestTmpl))(responseRecorder, nil) 64 65 require.Equal(t, http.StatusOK, responseRecorder.Code) 66 data, err := ioutil.ReadAll(responseRecorder.Body) 67 require.NoError(t, err) 68 require.Equal(t, string(expectedData), string(data)) 69 } 70 71 func TestHandlerError(t *testing.T) { 72 dispatcher := newTestDispatcher() 73 74 responseRecorder := httptest.NewRecorder() 75 NewHandler(dispatcher, tmpl(_errorTestTmpl))(responseRecorder, nil) 76 require.Equal(t, http.StatusInternalServerError, responseRecorder.Code) 77 } 78 79 func newTestDispatcher() *yarpc.Dispatcher { 80 httpTransport := yarpchttp.NewTransport() 81 return yarpc.NewDispatcher(yarpc.Config{ 82 Name: "test", 83 Inbounds: yarpc.Inbounds{ 84 httpTransport.NewInbound("127.0.0.1:0"), 85 }, 86 Outbounds: yarpc.Outbounds{ 87 "test-client": { 88 Unary: httpTransport.NewSingleOutbound("http://127.0.0.1:1234"), 89 Oneway: httpTransport.NewSingleOutbound("http://127.0.0.1:1234"), 90 }, 91 }, 92 }) 93 }