| 1 |
/* |
| 2 |
Copyright 2013-2014 Canonical Ltd. |
| 3 |
|
| 4 |
This program is free software: you can redistribute it and/or modify it |
| 5 |
under the terms of the GNU General Public License version 3, as published |
| 6 |
by the Free Software Foundation. |
| 7 |
|
| 8 |
This program is distributed in the hope that it will be useful, but |
| 9 |
WITHOUT ANY WARRANTY; without even the implied warranties of |
| 10 |
MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR |
| 11 |
PURPOSE. See the GNU General Public License for more details. |
| 12 |
|
| 13 |
You should have received a copy of the GNU General Public License along |
| 14 |
with this program. If not, see <http://www.gnu.org/licenses/>. |
| 15 |
*/ |
| 16 |
|
| 17 |
package api |
| 18 |
|
| 19 |
import ( |
| 20 |
"net/http" |
| 21 |
"net/http/httptest" |
| 22 |
|
| 23 |
. "gopkg.in/check.v1" |
| 24 |
|
| 25 |
helpers "gitlab.com/ubports/development/core/lomiri-push-service/testing" |
| 26 |
) |
| 27 |
|
| 28 |
type middlewareSuite struct{} |
| 29 |
|
| 30 |
var _ = Suite(&middlewareSuite{}) |
| 31 |
|
| 32 |
func (s *middlewareSuite) TestPanicTo500Handler(c *C) { |
| 33 |
logger := helpers.NewTestLogger(c, "debug") |
| 34 |
panicking := http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { |
| 35 |
panic("panic in handler") |
| 36 |
}) |
| 37 |
|
| 38 |
h := PanicTo500Handler(panicking, logger) |
| 39 |
w := httptest.NewRecorder() |
| 40 |
h.ServeHTTP(w, nil) |
| 41 |
c.Check(w.Code, Equals, 500) |
| 42 |
c.Check(logger.Captured(), Matches, "(?s)ERROR\\(PANIC\\) serving http: panic in handler:.*") |
| 43 |
c.Check(w.Header().Get("Content-Type"), Equals, "application/json") |
| 44 |
c.Check(w.Body.String(), Equals, `{"error":"internal","message":"INTERNAL SERVER ERROR"}`) |
| 45 |
} |