diff options
author | 2022-07-12 08:32:20 +0100 | |
---|---|---|
committer | 2022-07-12 08:32:20 +0100 | |
commit | 6934ae378ab5743da80a5995fc74d167502187b1 (patch) | |
tree | 6e3f58498158f1e844504961338eadb18f5a590e /internal/api/client/instance | |
parent | [performance] Add new index to fix slow web profile queries (#706) (diff) | |
download | gotosocial-6934ae378ab5743da80a5995fc74d167502187b1.tar.xz |
[chore] improved router logging, recovery and error handling (#705)
* move panic recovery to logging middleware, improve logging + panic recovery logic
Signed-off-by: kim <grufwub@gmail.com>
* remove dead code
Signed-off-by: kim <grufwub@gmail.com>
* remove skip paths code
Signed-off-by: kim <grufwub@gmail.com>
* re-enable log quoting
Signed-off-by: kim <grufwub@gmail.com>
* use human-friendly bytesize in logging body size
Signed-off-by: kim <grufwub@gmail.com>
* only disable quoting in debug builds
Signed-off-by: kim <grufwub@gmail.com>
* use logrus level instead of debug.DEBUG() to enable/disable quoting
Signed-off-by: kim <grufwub@gmail.com>
* shutup linter
Signed-off-by: kim <grufwub@gmail.com>
* fix instance tests
Signed-off-by: kim <grufwub@gmail.com>
* fix gin test contexts created with missing engine HTML renderer
Signed-off-by: kim <grufwub@gmail.com>
* add note regarding not logging query parameters
Signed-off-by: kim <grufwub@gmail.com>
* better explain 'DisableQuoting' logic
Signed-off-by: kim <grufwub@gmail.com>
* add license text
Signed-off-by: kim <grufwub@gmail.com>
Diffstat (limited to 'internal/api/client/instance')
-rw-r--r-- | internal/api/client/instance/instance_test.go | 28 | ||||
-rw-r--r-- | internal/api/client/instance/instancepatch_test.go | 14 | ||||
-rw-r--r-- | internal/api/client/instance/instancepeersget_test.go | 33 |
3 files changed, 33 insertions, 42 deletions
diff --git a/internal/api/client/instance/instance_test.go b/internal/api/client/instance/instance_test.go index 645e70474..efdf86523 100644 --- a/internal/api/client/instance/instance_test.go +++ b/internal/api/client/instance/instance_test.go @@ -98,27 +98,29 @@ func (suite *InstanceStandardTestSuite) TearDownTest() { testrig.StandardStorageTeardown(suite.storage) } -func (suite *InstanceStandardTestSuite) newContext(recorder *httptest.ResponseRecorder, requestMethod string, requestBody []byte, requestPath string, bodyContentType string) *gin.Context { - ctx, _ := gin.CreateTestContext(recorder) - - ctx.Set(oauth.SessionAuthorizedAccount, suite.testAccounts["admin_account"]) - ctx.Set(oauth.SessionAuthorizedToken, oauth.DBTokenToToken(suite.testTokens["admin_account"])) - ctx.Set(oauth.SessionAuthorizedApplication, suite.testApplications["admin_account"]) - ctx.Set(oauth.SessionAuthorizedUser, suite.testUsers["admin_account"]) - +func (suite *InstanceStandardTestSuite) newContext(recorder *httptest.ResponseRecorder, method string, path string, body []byte, contentType string, auth bool) *gin.Context { protocol := config.GetProtocol() host := config.GetHost() baseURI := fmt.Sprintf("%s://%s", protocol, host) - requestURI := fmt.Sprintf("%s/%s", baseURI, requestPath) + requestURI := fmt.Sprintf("%s/%s", baseURI, path) - ctx.Request = httptest.NewRequest(requestMethod, requestURI, bytes.NewReader(requestBody)) // the endpoint we're hitting + req := httptest.NewRequest(method, requestURI, bytes.NewReader(body)) // the endpoint we're hitting - if bodyContentType != "" { - ctx.Request.Header.Set("Content-Type", bodyContentType) + if contentType != "" { + req.Header.Set("Content-Type", contentType) } - ctx.Request.Header.Set("accept", "application/json") + req.Header.Set("accept", "application/json") + + ctx, _ := testrig.CreateGinTestContext(recorder, req) + + if auth { + ctx.Set(oauth.SessionAuthorizedAccount, suite.testAccounts["admin_account"]) + ctx.Set(oauth.SessionAuthorizedToken, oauth.DBTokenToToken(suite.testTokens["admin_account"])) + ctx.Set(oauth.SessionAuthorizedApplication, suite.testApplications["admin_account"]) + ctx.Set(oauth.SessionAuthorizedUser, suite.testUsers["admin_account"]) + } return ctx } diff --git a/internal/api/client/instance/instancepatch_test.go b/internal/api/client/instance/instancepatch_test.go index 1aec421e1..864eb9965 100644 --- a/internal/api/client/instance/instancepatch_test.go +++ b/internal/api/client/instance/instancepatch_test.go @@ -49,7 +49,7 @@ func (suite *InstancePatchTestSuite) TestInstancePatch1() { // set up the request recorder := httptest.NewRecorder() - ctx := suite.newContext(recorder, http.MethodPatch, bodyBytes, instance.InstanceInformationPath, w.FormDataContentType()) + ctx := suite.newContext(recorder, http.MethodPatch, instance.InstanceInformationPath, bodyBytes, w.FormDataContentType(), true) // call the handler suite.instanceModule.InstanceUpdatePATCHHandler(ctx) @@ -79,7 +79,7 @@ func (suite *InstancePatchTestSuite) TestInstancePatch2() { // set up the request recorder := httptest.NewRecorder() - ctx := suite.newContext(recorder, http.MethodPatch, bodyBytes, instance.InstanceInformationPath, w.FormDataContentType()) + ctx := suite.newContext(recorder, http.MethodPatch, instance.InstanceInformationPath, bodyBytes, w.FormDataContentType(), true) // call the handler suite.instanceModule.InstanceUpdatePATCHHandler(ctx) @@ -109,7 +109,7 @@ func (suite *InstancePatchTestSuite) TestInstancePatch3() { // set up the request recorder := httptest.NewRecorder() - ctx := suite.newContext(recorder, http.MethodPatch, bodyBytes, instance.InstanceInformationPath, w.FormDataContentType()) + ctx := suite.newContext(recorder, http.MethodPatch, instance.InstanceInformationPath, bodyBytes, w.FormDataContentType(), true) // call the handler suite.instanceModule.InstanceUpdatePATCHHandler(ctx) @@ -137,7 +137,7 @@ func (suite *InstancePatchTestSuite) TestInstancePatch4() { // set up the request recorder := httptest.NewRecorder() - ctx := suite.newContext(recorder, http.MethodPatch, bodyBytes, instance.InstanceInformationPath, w.FormDataContentType()) + ctx := suite.newContext(recorder, http.MethodPatch, instance.InstanceInformationPath, bodyBytes, w.FormDataContentType(), true) // call the handler suite.instanceModule.InstanceUpdatePATCHHandler(ctx) @@ -166,7 +166,7 @@ func (suite *InstancePatchTestSuite) TestInstancePatch5() { // set up the request recorder := httptest.NewRecorder() - ctx := suite.newContext(recorder, http.MethodPatch, bodyBytes, instance.InstanceInformationPath, w.FormDataContentType()) + ctx := suite.newContext(recorder, http.MethodPatch, instance.InstanceInformationPath, bodyBytes, w.FormDataContentType(), true) ctx.Set(oauth.SessionAuthorizedAccount, suite.testAccounts["local_account_1"]) ctx.Set(oauth.SessionAuthorizedToken, oauth.DBTokenToToken(suite.testTokens["local_account_1"])) @@ -200,7 +200,7 @@ func (suite *InstancePatchTestSuite) TestInstancePatch6() { // set up the request recorder := httptest.NewRecorder() - ctx := suite.newContext(recorder, http.MethodPatch, bodyBytes, instance.InstanceInformationPath, w.FormDataContentType()) + ctx := suite.newContext(recorder, http.MethodPatch, instance.InstanceInformationPath, bodyBytes, w.FormDataContentType(), true) // call the handler suite.instanceModule.InstanceUpdatePATCHHandler(ctx) @@ -230,7 +230,7 @@ func (suite *InstancePatchTestSuite) TestInstancePatch7() { // set up the request recorder := httptest.NewRecorder() - ctx := suite.newContext(recorder, http.MethodPatch, bodyBytes, instance.InstanceInformationPath, w.FormDataContentType()) + ctx := suite.newContext(recorder, http.MethodPatch, instance.InstanceInformationPath, bodyBytes, w.FormDataContentType(), true) // call the handler suite.instanceModule.InstanceUpdatePATCHHandler(ctx) diff --git a/internal/api/client/instance/instancepeersget_test.go b/internal/api/client/instance/instancepeersget_test.go index cb35a9e50..48fb42a73 100644 --- a/internal/api/client/instance/instancepeersget_test.go +++ b/internal/api/client/instance/instancepeersget_test.go @@ -26,7 +26,7 @@ import ( "net/http/httptest" "testing" - "github.com/gin-gonic/gin" + "github.com/gin-gonic/gin/render" "github.com/stretchr/testify/suite" "github.com/superseriousbusiness/gotosocial/internal/api/client/instance" "github.com/superseriousbusiness/gotosocial/internal/config" @@ -40,7 +40,8 @@ type InstancePeersGetTestSuite struct { func (suite *InstancePeersGetTestSuite) TestInstancePeersGetNoParams() { recorder := httptest.NewRecorder() - ctx, _ := gin.CreateTestContext(recorder) + ctx, r := testrig.CreateGinTestContext(recorder, nil) + r.HTMLRender = render.HTMLDebug{} baseURI := fmt.Sprintf("%s://%s", config.GetProtocol(), config.GetHost()) requestURI := fmt.Sprintf("%s/%s", baseURI, instance.InstancePeersPath) @@ -63,11 +64,9 @@ func (suite *InstancePeersGetTestSuite) TestInstancePeersGetNoParamsUnauthorized config.SetInstanceExposePeers(false) recorder := httptest.NewRecorder() - ctx, _ := gin.CreateTestContext(recorder) - baseURI := fmt.Sprintf("%s://%s", config.GetProtocol(), config.GetHost()) requestURI := fmt.Sprintf("%s/%s", baseURI, instance.InstancePeersPath) - ctx.Request = httptest.NewRequest(http.MethodGet, requestURI, nil) + ctx := suite.newContext(recorder, http.MethodGet, requestURI, nil, "", false) suite.instanceModule.InstancePeersGETHandler(ctx) @@ -88,7 +87,7 @@ func (suite *InstancePeersGetTestSuite) TestInstancePeersGetNoParamsAuthorized() recorder := httptest.NewRecorder() baseURI := fmt.Sprintf("%s://%s", config.GetProtocol(), config.GetHost()) requestURI := fmt.Sprintf("%s/%s", baseURI, instance.InstancePeersPath) - ctx := suite.newContext(recorder, http.MethodGet, []byte{}, requestURI, "") + ctx := suite.newContext(recorder, http.MethodGet, requestURI, nil, "", true) suite.instanceModule.InstancePeersGETHandler(ctx) @@ -105,11 +104,9 @@ func (suite *InstancePeersGetTestSuite) TestInstancePeersGetNoParamsAuthorized() func (suite *InstancePeersGetTestSuite) TestInstancePeersGetOnlySuspended() { recorder := httptest.NewRecorder() - ctx, _ := gin.CreateTestContext(recorder) - baseURI := fmt.Sprintf("%s://%s", config.GetProtocol(), config.GetHost()) requestURI := fmt.Sprintf("%s/%s?filter=suspended", baseURI, instance.InstancePeersPath) - ctx.Request = httptest.NewRequest(http.MethodGet, requestURI, nil) + ctx := suite.newContext(recorder, http.MethodGet, requestURI, nil, "", false) suite.instanceModule.InstancePeersGETHandler(ctx) @@ -128,11 +125,9 @@ func (suite *InstancePeersGetTestSuite) TestInstancePeersGetOnlySuspendedUnautho config.SetInstanceExposeSuspended(false) recorder := httptest.NewRecorder() - ctx, _ := gin.CreateTestContext(recorder) - baseURI := fmt.Sprintf("%s://%s", config.GetProtocol(), config.GetHost()) requestURI := fmt.Sprintf("%s/%s?filter=suspended", baseURI, instance.InstancePeersPath) - ctx.Request = httptest.NewRequest(http.MethodGet, requestURI, nil) + ctx := suite.newContext(recorder, http.MethodGet, requestURI, nil, "", false) suite.instanceModule.InstancePeersGETHandler(ctx) @@ -153,7 +148,7 @@ func (suite *InstancePeersGetTestSuite) TestInstancePeersGetOnlySuspendedAuthori recorder := httptest.NewRecorder() baseURI := fmt.Sprintf("%s://%s", config.GetProtocol(), config.GetHost()) requestURI := fmt.Sprintf("%s/%s?filter=suspended", baseURI, instance.InstancePeersPath) - ctx := suite.newContext(recorder, http.MethodGet, []byte{}, requestURI, "") + ctx := suite.newContext(recorder, http.MethodGet, requestURI, nil, "", true) suite.instanceModule.InstancePeersGETHandler(ctx) @@ -170,11 +165,9 @@ func (suite *InstancePeersGetTestSuite) TestInstancePeersGetOnlySuspendedAuthori func (suite *InstancePeersGetTestSuite) TestInstancePeersGetAll() { recorder := httptest.NewRecorder() - ctx, _ := gin.CreateTestContext(recorder) - baseURI := fmt.Sprintf("%s://%s", config.GetProtocol(), config.GetHost()) requestURI := fmt.Sprintf("%s/%s?filter=suspended,open", baseURI, instance.InstancePeersPath) - ctx.Request = httptest.NewRequest(http.MethodGet, requestURI, nil) + ctx := suite.newContext(recorder, http.MethodGet, requestURI, nil, "", false) suite.instanceModule.InstancePeersGETHandler(ctx) @@ -202,11 +195,9 @@ func (suite *InstancePeersGetTestSuite) TestInstancePeersGetAllWithObfuscated() suite.NoError(err) recorder := httptest.NewRecorder() - ctx, _ := gin.CreateTestContext(recorder) - baseURI := fmt.Sprintf("%s://%s", config.GetProtocol(), config.GetHost()) requestURI := fmt.Sprintf("%s/%s?filter=suspended,open", baseURI, instance.InstancePeersPath) - ctx.Request = httptest.NewRequest(http.MethodGet, requestURI, nil) + ctx := suite.newContext(recorder, http.MethodGet, requestURI, nil, "", false) suite.instanceModule.InstancePeersGETHandler(ctx) @@ -223,11 +214,9 @@ func (suite *InstancePeersGetTestSuite) TestInstancePeersGetAllWithObfuscated() func (suite *InstancePeersGetTestSuite) TestInstancePeersGetFunkyParams() { recorder := httptest.NewRecorder() - ctx, _ := gin.CreateTestContext(recorder) - baseURI := fmt.Sprintf("%s://%s", config.GetProtocol(), config.GetHost()) requestURI := fmt.Sprintf("%s/%s?filter=aaaaaaaaaaaaaaaaa,open", baseURI, instance.InstancePeersPath) - ctx.Request = httptest.NewRequest(http.MethodGet, requestURI, nil) + ctx := suite.newContext(recorder, http.MethodGet, requestURI, nil, "", true) suite.instanceModule.InstancePeersGETHandler(ctx) |