From b6ee33042ed23fdeb95dc457edc76580ada07656 Mon Sep 17 00:00:00 2001 From: Roman Volosatovs Date: Tue, 19 May 2020 20:28:05 +0200 Subject: [PATCH] util: Refactor and fix errors/web tests --- pkg/errors/web/middleware_test.go | 87 +++++++++++++++++++------------ 1 file changed, 55 insertions(+), 32 deletions(-) diff --git a/pkg/errors/web/middleware_test.go b/pkg/errors/web/middleware_test.go index d96af920ae..87654867a0 100644 --- a/pkg/errors/web/middleware_test.go +++ b/pkg/errors/web/middleware_test.go @@ -32,48 +32,71 @@ func TestErrorHandling(t *testing.T) { ttnNotFound := errors.DefineNotFound("test_not_found", "test not found") - for _, tt := range []struct { - Name string - Handler echo.HandlerFunc - Assert func(a *assertions.Assertion, res *http.Response) + for _, tc := range []struct { + Name string + Error error + JSON string }{ { - Name: "Echo404", - Handler: func(c echo.Context) error { return echo.ErrNotFound }, - Assert: func(a *assertions.Assertion, res *http.Response) { - a.So(res.StatusCode, should.Equal, http.StatusNotFound) - b, _ := ioutil.ReadAll(res.Body) - a.So(string(b), should.ContainSubstring, `"namespace":"pkg/errors/web"`) - }, + Name: "Echo404", + Error: echo.ErrNotFound, + JSON: `{ + "code": 5, + "details": [ + { + "type_url": "type.googleapis.com/ttn.lorawan.v3.ErrorDetails", + "value": "Cg5wa2cvZXJyb3JzL3dlYhoJTm90IEZvdW5kIhgKFgoHbWVzc2FnZRILGglOb3QgRm91bmQ4BQ==" + } + ], + "message": "error:pkg/errors/web:unknown (Not Found)" +}`, }, { - Name: "TTN404", - Handler: func(c echo.Context) error { return ttnNotFound }, - Assert: func(a *assertions.Assertion, res *http.Response) { - a.So(res.StatusCode, should.Equal, http.StatusNotFound) - b, _ := ioutil.ReadAll(res.Body) - a.So(string(b), should.ContainSubstring, `"name":"test_not_found"`) - }, + Name: "TTN404", + Error: ttnNotFound, + JSON: `{ + "code": 5, + "details": [ + { + "type_url": "type.googleapis.com/ttn.lorawan.v3.ErrorDetails", + "value": "ChNwa2cvZXJyb3JzL3dlYl90ZXN0Eg50ZXN0X25vdF9mb3VuZBoOdGVzdCBub3QgZm91bmQ4BQ==" + } + ], + "message": "error:pkg/errors/web_test:test_not_found (test not found)" +}`, }, { - Name: "TTN404InsideEcho", - Handler: func(c echo.Context) error { - return &echo.HTTPError{Internal: ttnNotFound} - }, - Assert: func(a *assertions.Assertion, res *http.Response) { - a.So(res.StatusCode, should.Equal, http.StatusNotFound) - b, _ := ioutil.ReadAll(res.Body) - a.So(string(b), should.ContainSubstring, `"name":"test_not_found"`) - }, + Name: "TTN404InsideEcho", + Error: &echo.HTTPError{Internal: ttnNotFound}, + JSON: `{ + "code": 5, + "details": [ + { + "type_url": "type.googleapis.com/ttn.lorawan.v3.ErrorDetails", + "value": "ChNwa2cvZXJyb3JzL3dlYl90ZXN0Eg50ZXN0X25vdF9mb3VuZBoOdGVzdCBub3QgZm91bmQ4BQ==" + } + ], + "message": "error:pkg/errors/web_test:test_not_found (test not found)" +}`, }, } { - t.Run(tt.Name, func(t *testing.T) { + t.Run(tc.Name, func(t *testing.T) { a := assertions.New(t) + req, rec := httptest.NewRequest(http.MethodGet, "/", nil), httptest.NewRecorder() - c := e.NewContext(req, rec) - h := web.ErrorMiddleware(nil)(tt.Handler) - a.So(h(c), should.Equal, nil) - tt.Assert(a, rec.Result()) + if !a.So(web.ErrorMiddleware(nil)(func(echo.Context) error { + return tc.Error + })(e.NewContext(req, rec)), should.BeNil) { + t.FailNow() + } + + res := rec.Result() + b, err := ioutil.ReadAll(res.Body) + if !a.So(err, should.BeNil) { + t.FailNow() + } + a.So(res.StatusCode, should.Equal, http.StatusNotFound) + a.So(string(b), should.EqualJSON, tc.JSON) }) }