Add an abstract json layout to make it's easier to change json library (#16528)

* Add an abstract json layout to make it's easier to change json library

* Fix import

* Fix import sequence

* Fix blank lines

* Fix blank lines
This commit is contained in:
Lunny Xiao
2021-07-25 00:03:58 +08:00
committed by GitHub
parent e0f9635c06
commit 9f31f3aa8a
93 changed files with 272 additions and 264 deletions

View File

@ -10,9 +10,9 @@ import (
"testing"
"code.gitea.io/gitea/models"
"code.gitea.io/gitea/modules/json"
api "code.gitea.io/gitea/modules/structs"
jsoniter "github.com/json-iterator/go"
"github.com/stretchr/testify/assert"
)
@ -190,7 +190,6 @@ func TestAPIEditUser(t *testing.T) {
resp := session.MakeRequest(t, req, http.StatusUnprocessableEntity)
errMap := make(map[string]interface{})
json := jsoniter.ConfigCompatibleWithStandardLibrary
json.Unmarshal(resp.Body.Bytes(), &errMap)
assert.EqualValues(t, "email is not allowed to be empty string", errMap["message"].(string))

View File

@ -14,11 +14,11 @@ import (
"time"
"code.gitea.io/gitea/models"
"code.gitea.io/gitea/modules/json"
"code.gitea.io/gitea/modules/queue"
api "code.gitea.io/gitea/modules/structs"
"code.gitea.io/gitea/services/forms"
jsoniter "github.com/json-iterator/go"
"github.com/stretchr/testify/assert"
)
@ -231,7 +231,6 @@ func doAPICreatePullRequest(ctx APITestContext, owner, repo, baseBranch, headBra
}
resp := ctx.Session.MakeRequest(t, req, expected)
json := jsoniter.ConfigCompatibleWithStandardLibrary
decoder := json.NewDecoder(resp.Body)
pr := api.PullRequest{}
err := decoder.Decode(&pr)
@ -251,7 +250,6 @@ func doAPIGetPullRequest(ctx APITestContext, owner, repo string, index int64) fu
}
resp := ctx.Session.MakeRequest(t, req, expected)
json := jsoniter.ConfigCompatibleWithStandardLibrary
decoder := json.NewDecoder(resp.Body)
pr := api.PullRequest{}
err := decoder.Decode(&pr)

View File

@ -10,8 +10,9 @@ import (
"testing"
"code.gitea.io/gitea/models"
"code.gitea.io/gitea/modules/json"
api "code.gitea.io/gitea/modules/structs"
jsoniter "github.com/json-iterator/go"
"github.com/stretchr/testify/assert"
)
@ -189,7 +190,6 @@ func TestAPIPullReview(t *testing.T) {
})
resp = session.MakeRequest(t, req, http.StatusUnprocessableEntity)
errMap := make(map[string]interface{})
json := jsoniter.ConfigCompatibleWithStandardLibrary
json.Unmarshal(resp.Body.Bytes(), &errMap)
assert.EqualValues(t, "review event COMMENT requires a body or a comment", errMap["message"].(string))

View File

@ -13,10 +13,10 @@ import (
"testing"
"code.gitea.io/gitea/models"
"code.gitea.io/gitea/modules/json"
"code.gitea.io/gitea/modules/lfs"
"code.gitea.io/gitea/modules/setting"
jsoniter "github.com/json-iterator/go"
"github.com/stretchr/testify/assert"
)
@ -86,7 +86,6 @@ func TestAPILFSBatch(t *testing.T) {
decodeResponse := func(t *testing.T, b *bytes.Buffer) *lfs.BatchResponse {
var br lfs.BatchResponse
json := jsoniter.ConfigCompatibleWithStandardLibrary
assert.NoError(t, json.Unmarshal(b.Bytes(), &br))
return &br
}

View File

@ -12,12 +12,12 @@ import (
"path/filepath"
"testing"
"code.gitea.io/gitea/modules/json"
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/util"
"code.gitea.io/gitea/routers"
"gitea.com/go-chi/session"
jsoniter "github.com/json-iterator/go"
"github.com/stretchr/testify/assert"
)
@ -63,7 +63,6 @@ func TestSessionFileCreation(t *testing.T) {
var config session.Options
json := jsoniter.ConfigCompatibleWithStandardLibrary
err := json.Unmarshal([]byte(oldSessionConfig), &config)
assert.NoError(t, err)

View File

@ -28,6 +28,7 @@ import (
"code.gitea.io/gitea/modules/base"
"code.gitea.io/gitea/modules/git"
"code.gitea.io/gitea/modules/graceful"
"code.gitea.io/gitea/modules/json"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/queue"
"code.gitea.io/gitea/modules/setting"
@ -37,7 +38,6 @@ import (
"code.gitea.io/gitea/routers"
"github.com/PuerkitoBio/goquery"
jsoniter "github.com/json-iterator/go"
"github.com/stretchr/testify/assert"
)
@ -416,7 +416,6 @@ func NewRequestWithValues(t testing.TB, method, urlStr string, values map[string
func NewRequestWithJSON(t testing.TB, method, urlStr string, v interface{}) *http.Request {
t.Helper()
json := jsoniter.ConfigCompatibleWithStandardLibrary
jsonBytes, err := json.Marshal(v)
assert.NoError(t, err)
req := NewRequestWithBody(t, method, urlStr, bytes.NewBuffer(jsonBytes))
@ -508,7 +507,6 @@ func logUnexpectedResponse(t testing.TB, recorder *httptest.ResponseRecorder) {
func DecodeJSON(t testing.TB, resp *httptest.ResponseRecorder, v interface{}) {
t.Helper()
json := jsoniter.ConfigCompatibleWithStandardLibrary
decoder := json.NewDecoder(resp.Body)
assert.NoError(t, decoder.Decode(v))
}

View File

@ -14,11 +14,11 @@ import (
"code.gitea.io/gitea/models"
"code.gitea.io/gitea/modules/git"
"code.gitea.io/gitea/modules/json"
"code.gitea.io/gitea/modules/lfs"
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/routers/web"
jsoniter "github.com/json-iterator/go"
gzipp "github.com/klauspost/compress/gzip"
"github.com/stretchr/testify/assert"
)
@ -206,7 +206,7 @@ func TestGetLFSRange(t *testing.T) {
assert.Equal(t, tt.out, resp.Body.String())
} else {
var er lfs.ErrorResponse
err := jsoniter.Unmarshal(resp.Body.Bytes(), &er)
err := json.Unmarshal(resp.Body.Bytes(), &er)
assert.NoError(t, err)
assert.Equal(t, tt.out, er.Message)
}

View File

@ -9,9 +9,9 @@ import (
"io/ioutil"
"testing"
"code.gitea.io/gitea/modules/json"
"code.gitea.io/gitea/modules/setting"
jsoniter "github.com/json-iterator/go"
"github.com/stretchr/testify/assert"
)
@ -71,7 +71,6 @@ func TestAccessTokenExchange(t *testing.T) {
}
parsed := new(response)
json := jsoniter.ConfigCompatibleWithStandardLibrary
assert.NoError(t, json.Unmarshal(resp.Body.Bytes(), parsed))
assert.True(t, len(parsed.AccessToken) > 10)
assert.True(t, len(parsed.RefreshToken) > 10)
@ -96,7 +95,6 @@ func TestAccessTokenExchangeWithoutPKCE(t *testing.T) {
}
parsed := new(response)
json := jsoniter.ConfigCompatibleWithStandardLibrary
assert.NoError(t, json.Unmarshal(resp.Body.Bytes(), parsed))
assert.True(t, len(parsed.AccessToken) > 10)
assert.True(t, len(parsed.RefreshToken) > 10)
@ -186,7 +184,6 @@ func TestAccessTokenExchangeWithBasicAuth(t *testing.T) {
}
parsed := new(response)
json := jsoniter.ConfigCompatibleWithStandardLibrary
assert.NoError(t, json.Unmarshal(resp.Body.Bytes(), parsed))
assert.True(t, len(parsed.AccessToken) > 10)
assert.True(t, len(parsed.RefreshToken) > 10)
@ -230,7 +227,6 @@ func TestRefreshTokenInvalidation(t *testing.T) {
}
parsed := new(response)
json := jsoniter.ConfigCompatibleWithStandardLibrary
assert.NoError(t, json.Unmarshal(resp.Body.Bytes(), parsed))
// test without invalidation

View File

@ -10,10 +10,10 @@ import (
"path"
"testing"
"code.gitea.io/gitea/modules/json"
"code.gitea.io/gitea/modules/setting"
api "code.gitea.io/gitea/modules/structs"
jsoniter "github.com/json-iterator/go"
"github.com/stretchr/testify/assert"
)
@ -86,7 +86,6 @@ func doTestRepoCommitWithStatus(t *testing.T, state string, classes ...string) {
}
func testRepoCommitsWithStatus(t *testing.T, resp, respOne *httptest.ResponseRecorder, state string) {
json := jsoniter.ConfigCompatibleWithStandardLibrary
var statuses []*api.CommitStatus
assert.NoError(t, json.Unmarshal(resp.Body.Bytes(), &statuses))
var status api.CombinedStatus

View File

@ -14,9 +14,9 @@ import (
"testing"
"time"
"code.gitea.io/gitea/modules/json"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/queue"
jsoniter "github.com/json-iterator/go"
)
var (
@ -158,7 +158,6 @@ func NewTestLogger() log.LoggerProvider {
// Init inits connection writer with json config.
// json config only need key "level".
func (log *TestLogger) Init(config string) error {
json := jsoniter.ConfigCompatibleWithStandardLibrary
err := json.Unmarshal([]byte(config), log)
if err != nil {
return err