func Test_recovery_production(t *testing.T) { // Setup server & test server ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { request := createRequestContext(r, w) defer recovery(request, false) panic(util.Status404()) })) defer ts.Close() response, _ := http.Post(ts.URL, "application/x-www-form-urlencoded", strings.NewReader("key=value")) if response.StatusCode != 404 { t.Errorf(test.ExpectedNumberButFoundNumber, 404, response.StatusCode) } else { data, _ := ioutil.ReadAll(response.Body) response.Body.Close() fmt.Println(string(data)) } }
//////////////////////////////////////////////////////////////////////////////////////////////////// // ServeHTTP handle HTTP request and HTTP response. func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) { request := createRequestContext(r, w) oauth := createOAuthContext(request) // Handle error defer recovery(request, s.sandbox) /* Condition validation: validate request method */ if !methodsValidation.MatchString(request.Method) { panic(util.Status405()) } // Should redirect request to static folder or not? if request.Method == Get && len(Cfg.StaticFolders) > 0 { for prefix, folder := range Cfg.StaticFolders { if path := request.Path; strings.HasPrefix(path, prefix) { path = strings.Replace(path, prefix, folder, 1) if file, err := os.Open(path); err == nil { defer file.Close() if info, _ := file.Stat(); !info.IsDir() { http.ServeContent(w, r, path, info.ModTime(), file) return } } panic(util.Status404()) } } } // Find route to handle request if route, pathParams := s.router.matchRoute(request, oauth); route != nil { if pathParams != nil { request.PathParams = pathParams } route.invokeHandler(request, oauth) } else { panic(util.Status503()) } }