github.com/Tyktechnologies/tyk@v2.9.5+incompatible/gateway/handler_error_test.go (about) 1 package gateway 2 3 import ( 4 "bytes" 5 "io/ioutil" 6 "net/http" 7 "os" 8 "path/filepath" 9 "strings" 10 "testing" 11 12 "github.com/TykTechnologies/tyk/config" 13 "github.com/TykTechnologies/tyk/headers" 14 "github.com/TykTechnologies/tyk/test" 15 ) 16 17 func TestHandleError_text_xml(t *testing.T) { 18 file := filepath.Join(config.Global().TemplatePath, "error_500.xml") 19 xml := `<?xml version = "1.0" encoding = "UTF-8"?> 20 <error> 21 <code>500</code> 22 <message>{{.Message}}</message> 23 </error>` 24 err := ioutil.WriteFile(file, []byte(xml), 0600) 25 if err != nil { 26 t.Fatal(err) 27 } 28 defer os.Remove(file) 29 expect := `<?xml version = "1.0" encoding = "UTF-8"?> 30 <error> 31 <code>500</code> 32 <message>There was a problem proxying the request</message> 33 </error> 34 ` 35 ts := StartTest() 36 defer ts.Close() 37 38 BuildAndLoadAPI(func(spec *APISpec) { 39 spec.Proxy.ListenPath = "/" 40 spec.Proxy.TargetURL = "http://localhost:66666" 41 }) 42 ts.Run(t, test.TestCase{ 43 Path: "/", 44 Code: http.StatusInternalServerError, 45 Headers: map[string]string{ 46 headers.ContentType: headers.TextXML, 47 }, 48 BodyMatchFunc: func(b []byte) bool { 49 return strings.TrimSpace(expect) == string(bytes.TrimSpace(b)) 50 }, 51 }) 52 53 ts.Run(t, test.TestCase{ 54 Path: "/", 55 Code: http.StatusInternalServerError, 56 Headers: map[string]string{ 57 headers.ContentType: headers.TextXML + "; charset=UTF-8", 58 }, 59 BodyMatchFunc: func(b []byte) bool { 60 return strings.TrimSpace(expect) == string(bytes.TrimSpace(b)) 61 }, 62 }) 63 } 64 65 func TestHandleDefaultErrorXml(t *testing.T) { 66 67 expect := `<?xml version = "1.0" encoding = "UTF-8"?> 68 <error>There was a problem proxying the request</error>` 69 ts := StartTest() 70 defer ts.Close() 71 72 BuildAndLoadAPI(func(spec *APISpec) { 73 spec.Proxy.ListenPath = "/" 74 spec.Proxy.TargetURL = "http://localhost:66666" 75 }) 76 ts.Run(t, test.TestCase{ 77 Path: "/", 78 Code: http.StatusInternalServerError, 79 Headers: map[string]string{ 80 headers.ContentType: headers.TextXML, 81 }, 82 BodyMatchFunc: func(b []byte) bool { 83 return strings.TrimSpace(expect) == string(bytes.TrimSpace(b)) 84 }, 85 }) 86 87 ts.Run(t, test.TestCase{ 88 Path: "/", 89 Code: http.StatusInternalServerError, 90 Headers: map[string]string{ 91 headers.ContentType: headers.TextXML + "; charset=UTF-8", 92 }, 93 BodyMatchFunc: func(b []byte) bool { 94 return strings.TrimSpace(expect) == string(bytes.TrimSpace(b)) 95 }, 96 }) 97 } 98 99 func TestHandleDefaultErrorJSON(t *testing.T) { 100 101 expect := ` 102 { 103 "error": "There was a problem proxying the request" 104 } 105 ` 106 107 ts := StartTest() 108 defer ts.Close() 109 110 BuildAndLoadAPI(func(spec *APISpec) { 111 spec.Proxy.ListenPath = "/" 112 spec.Proxy.TargetURL = "http://localhost:66666" 113 }) 114 ts.Run(t, test.TestCase{ 115 Path: "/", 116 Code: http.StatusInternalServerError, 117 Headers: map[string]string{ 118 headers.ContentType: headers.ApplicationJSON, 119 }, 120 BodyMatchFunc: func(b []byte) bool { 121 return strings.TrimSpace(expect) == string(bytes.TrimSpace(b)) 122 }, 123 }) 124 125 }