Skip to content

Commit

Permalink
Improved support for numbers
Browse files Browse the repository at this point in the history
This patch improves support for big numbers. We now use json.Number to request and check responses.
  • Loading branch information
martinrode committed Nov 8, 2024
1 parent b3d12df commit 97f156d
Show file tree
Hide file tree
Showing 20 changed files with 283 additions and 45 deletions.
14 changes: 12 additions & 2 deletions api_testcase.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ func (testCase Case) breakResponseIsPresent(response api.Response) (bool, error)
return false, nil
}

// checkCollectResponse loops over all given collect responses and than
// checkCollectResponse loops over all given collect responses and then
// If this continue response is present it returns a true.
// If no continue response is set, it also returns true to keep the testsuite running
func (testCase *Case) checkCollectResponse(response api.Response) (int, error) {
Expand Down Expand Up @@ -218,8 +218,13 @@ func (testCase *Case) checkCollectResponse(response api.Response) (int, error) {
if err != nil {
return -1, fmt.Errorf("error matching check responses: %s", err)
}
// !eq && !reverse -> add
// !eq && reverse -> don't add
// eq && !reverse -> don't add
// eq && reverse -> add

if !responsesMatch.Equal {
if !responsesMatch.Equal && !testCase.ReverseTestResult ||
responsesMatch.Equal && testCase.ReverseTestResult {
leftResponses = append(leftResponses, v)
}
}
Expand Down Expand Up @@ -428,6 +433,11 @@ func (testCase Case) run() (successs bool, apiResponse api.Response, err error)
logrus.Errorf("[%s] %s", v.Key, v.Message)
r.SaveToReportLog(fmt.Sprintf("[%s] %s", v.Key, v.Message))
}
} else {
for _, v := range responsesMatch.Failures {
logrus.Infof("Reverse Test Result of: [%s] %s", v.Key, v.Message)
r.SaveToReportLog(fmt.Sprintf("reverse test result: [%s] %s", v.Key, v.Message))
}
}

collectArray, ok := testCase.CollectResponse.(util.JsonArray)
Expand Down
22 changes: 11 additions & 11 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,31 +11,31 @@ require (
github.com/moul/http2curl v1.0.0
github.com/pkg/errors v0.9.1
github.com/programmfabrik/go-test-utils v0.0.0-20191114143449-b8e16b04adb1
github.com/programmfabrik/golib v0.0.0-20240701125551-843bc5e3be55
github.com/programmfabrik/golib v0.0.0-20241108131848-768316b22b7e
github.com/sergi/go-diff v1.3.1
github.com/sirupsen/logrus v1.9.3
github.com/spf13/afero v1.9.5
github.com/spf13/cobra v1.7.0
github.com/spf13/viper v1.16.0
github.com/stretchr/testify v1.8.4
github.com/stretchr/testify v1.9.0
github.com/tidwall/gjson v1.14.4
github.com/tidwall/jsonc v0.3.2
github.com/yudai/pp v2.0.1+incompatible
golang.org/x/mod v0.12.0
golang.org/x/net v0.21.0
golang.org/x/mod v0.17.0
golang.org/x/net v0.30.0
golang.org/x/oauth2 v0.9.0
)

require (
github.com/Masterminds/goutils v1.1.1 // indirect
github.com/Masterminds/semver/v3 v3.2.1 // indirect
github.com/andybalholm/cascadia v1.3.2 // indirect
github.com/antchfx/xmlquery v1.3.18 // indirect
github.com/antchfx/xpath v1.2.5 // indirect
github.com/antchfx/xmlquery v1.4.2 // indirect
github.com/antchfx/xpath v1.3.2 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/emersion/go-sasl v0.0.0-20200509203442-7bfe0ed36a21 // indirect
github.com/fsnotify/fsnotify v1.6.0 // indirect
github.com/gabriel-vasile/mimetype v1.4.3 // indirect
github.com/gabriel-vasile/mimetype v1.4.6 // indirect
github.com/gofrs/uuid v4.4.0+incompatible // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/protobuf v1.5.3 // indirect
Expand Down Expand Up @@ -63,10 +63,10 @@ require (
github.com/subosito/gotenv v1.4.2 // indirect
github.com/tidwall/match v1.1.1 // indirect
github.com/tidwall/pretty v1.2.0 // indirect
github.com/yuin/goldmark v1.7.0 // indirect
golang.org/x/crypto v0.19.0 // indirect
golang.org/x/sys v0.17.0 // indirect
golang.org/x/text v0.14.0 // indirect
github.com/yuin/goldmark v1.7.8 // indirect
golang.org/x/crypto v0.29.0 // indirect
golang.org/x/sys v0.27.0 // indirect
golang.org/x/text v0.20.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/protobuf v1.30.0 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
Expand Down
20 changes: 20 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,13 @@ github.com/andybalholm/cascadia v1.3.2 h1:3Xi6Dw5lHF15JtdcmAHD3i1+T8plmv7BQ/nsVi
github.com/andybalholm/cascadia v1.3.2/go.mod h1:7gtRlve5FxPPgIgX36uWBX58OdBsSS6lUvCFb+h7KvU=
github.com/antchfx/xmlquery v1.3.18 h1:FSQ3wMuphnPPGJOFhvc+cRQ2CT/rUj4cyQXkJcjOwz0=
github.com/antchfx/xmlquery v1.3.18/go.mod h1:Afkq4JIeXut75taLSuI31ISJ/zeq+3jG7TunF7noreA=
github.com/antchfx/xmlquery v1.4.2 h1:MZKd9+wblwxfQ1zd1AdrTsqVaMjMCwow3IqkCSe00KA=
github.com/antchfx/xmlquery v1.4.2/go.mod h1:QXhvf5ldTuGqhd1SHNvvtlhhdQLks4dD0awIVhXIDTA=
github.com/antchfx/xpath v1.2.4/go.mod h1:i54GszH55fYfBmoZXapTHN8T8tkcHfRgLyVwwqzXNcs=
github.com/antchfx/xpath v1.2.5 h1:hqZ+wtQ+KIOV/S3bGZcIhpgYC26um2bZYP2KVGcR7VY=
github.com/antchfx/xpath v1.2.5/go.mod h1:i54GszH55fYfBmoZXapTHN8T8tkcHfRgLyVwwqzXNcs=
github.com/antchfx/xpath v1.3.2 h1:LNjzlsSjinu3bQpw9hWMY9ocB80oLOWuQqFvO6xt51U=
github.com/antchfx/xpath v1.3.2/go.mod h1:i54GszH55fYfBmoZXapTHN8T8tkcHfRgLyVwwqzXNcs=
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWRnGsAI=
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5PlCu98SY8svDHJxuZscDgtXS6KTTbou5AhLI=
Expand Down Expand Up @@ -85,6 +89,8 @@ github.com/fsnotify/fsnotify v1.6.0 h1:n+5WquG0fcWoWp6xPWfHdbskMCQaFnG6PfBrh1Ky4
github.com/fsnotify/fsnotify v1.6.0/go.mod h1:sl3t1tCWJFWoRz9R8WJCbQihKKwmorjAbSClcnxKAGw=
github.com/gabriel-vasile/mimetype v1.4.3 h1:in2uUcidCuFcDKtdcBxlR0rJ1+fsokWf+uqxgUFjbI0=
github.com/gabriel-vasile/mimetype v1.4.3/go.mod h1:d8uq/6HKRL6CGdk+aubisF/M5GcPfT7nKyLpA0lbSSk=
github.com/gabriel-vasile/mimetype v1.4.6 h1:3+PzJTKLkvgjeTbts6msPJt4DixhT4YtFNf1gtGe3zc=
github.com/gabriel-vasile/mimetype v1.4.6/go.mod h1:JX1qVKqZd40hUPpAfiNTe0Sne7hdfKSbOqqmkq8GCXc=
github.com/go-gl/glfw v0.0.0-20190409004039-e6da0acd62b1/go.mod h1:vR7hzQXu2zJy9AVAgeJqvqgH9Q5CA+iKCZ2gyEVpxRU=
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20191125211704-12ad95a8df72/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8=
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20200222043503-6f7a984d4dc4/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8=
Expand Down Expand Up @@ -219,6 +225,8 @@ github.com/programmfabrik/go-test-utils v0.0.0-20191114143449-b8e16b04adb1 h1:Nb
github.com/programmfabrik/go-test-utils v0.0.0-20191114143449-b8e16b04adb1/go.mod h1:6Tg7G+t9KYiFa0sU8PpISt9RUgIpgrEI+tXvWz3tSIU=
github.com/programmfabrik/golib v0.0.0-20240701125551-843bc5e3be55 h1:VBYGpSvjwHSa5ARrs6uPlUOJF1+n6rFWn49+++h20IU=
github.com/programmfabrik/golib v0.0.0-20240701125551-843bc5e3be55/go.mod h1:qb4pSUhPsZ/UfvM/MBNwKHb6W7xL85uSi4od9emNHHw=
github.com/programmfabrik/golib v0.0.0-20241108131848-768316b22b7e h1:gxIqj0QRj5JIezZg25auRHt3A7r5sMznBo+2Z90MZs0=
github.com/programmfabrik/golib v0.0.0-20241108131848-768316b22b7e/go.mod h1:wlGT5wyqIE9E49zkol/DF8R4g4Kn+0H3Iqn/7TO+dCs=
github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8=
Expand Down Expand Up @@ -260,6 +268,7 @@ github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO
github.com/stretchr/testify v1.8.3/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/subosito/gotenv v1.4.2 h1:X1TuBLAMDFbaTAChgCBLu3DU3UPyELpnF2jjJ2cz/S8=
github.com/subosito/gotenv v1.4.2/go.mod h1:ayKnFf/c6rvx/2iiLrJUk1e6plDbT3edrFNGqEflhK0=
github.com/tidwall/gjson v1.14.4 h1:uo0p8EbA09J7RQaflQ1aBRffTR7xedD2bcIVSYxLnkM=
Expand All @@ -279,6 +288,8 @@ github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9dec
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
github.com/yuin/goldmark v1.7.0 h1:EfOIvIMZIzHdB/R/zVrikYLPPwJlfMcNczJFMs1m6sA=
github.com/yuin/goldmark v1.7.0/go.mod h1:uzxRWxtg69N339t3louHJ7+O03ezfj6PlliRlaOzY1E=
github.com/yuin/goldmark v1.7.8 h1:iERMLn0/QJeHFhxSt3p6PeN9mGnvIKSpG9YYorDMnic=
github.com/yuin/goldmark v1.7.8/go.mod h1:uzxRWxtg69N339t3louHJ7+O03ezfj6PlliRlaOzY1E=
go.opencensus.io v0.21.0/go.mod h1:mSImk1erAIZhrmZN+AvHh14ztQfjbGwt4TtuofqLduU=
go.opencensus.io v0.22.0/go.mod h1:+kGneAE2xo2IficOXnaByMWTGM9T73dGwxeWcUqIpI8=
go.opencensus.io v0.22.2/go.mod h1:yxeiOL68Rb0Xd1ddK5vPZ/oVn4vY4Ynel7k9FzqtOIw=
Expand All @@ -296,6 +307,8 @@ golang.org/x/crypto v0.0.0-20220722155217-630584e8d5aa/go.mod h1:IxCIyHEi3zRg3s0
golang.org/x/crypto v0.3.0/go.mod h1:hebNnKkNXi2UzZN1eVRvBB7co0a+JxK6XbPiWVs/3J4=
golang.org/x/crypto v0.19.0 h1:ENy+Az/9Y1vSrlrvBSyna3PITt4tiZLf7sgCjZBX7Wo=
golang.org/x/crypto v0.19.0/go.mod h1:Iy9bg/ha4yyC70EfRS8jz+B6ybOBKMaSxLj6P6oBDfU=
golang.org/x/crypto v0.29.0 h1:L5SG1JTTXupVV3n6sUqMTeWbjAyfPwoda2DLX8J8FrQ=
golang.org/x/crypto v0.29.0/go.mod h1:+F4F4N5hv6v38hfeYwTdx20oUvLLc+QfrE9Ax9HtgRg=
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
golang.org/x/exp v0.0.0-20190306152737-a1d7652674e8/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
golang.org/x/exp v0.0.0-20190510132918-efd6b22b2522/go.mod h1:ZjyILWgesfNpC6sMxTJOJm9Kp84zZh5NQWvqDGG3Qr8=
Expand Down Expand Up @@ -333,6 +346,7 @@ golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91
golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/mod v0.12.0 h1:rmsUpXtvNzj340zd98LZ4KntptpfRHwpFOHG188oHXc=
golang.org/x/mod v0.12.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/mod v0.17.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20190108225652-1e06a53dbb7e/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
Expand Down Expand Up @@ -373,6 +387,8 @@ golang.org/x/net v0.7.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
golang.org/x/net v0.9.0/go.mod h1:d48xBJpPfHeWQsugry2m+kC02ZBRGRgulfHnEXEuWns=
golang.org/x/net v0.21.0 h1:AQyQV4dYCvJ7vGmJyKki9+PBdyvhkSd8EIx/qb0AYv4=
golang.org/x/net v0.21.0/go.mod h1:bIjVDfnllIU7BJ2DNgfnXvpSvtn8VRwhlsaeUTyUS44=
golang.org/x/net v0.30.0 h1:AcW1SDZMkb8IpzCdQUaIq2sP4sZ4zw+55h6ynffypl4=
golang.org/x/net v0.30.0/go.mod h1:2wGyMJ5iFasEhkwi13ChkO/t1ECNC4X4eBKkVFyYFlU=
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
Expand Down Expand Up @@ -442,6 +458,8 @@ golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.7.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.17.0 h1:25cE3gD+tdBA7lp7QfhuV+rJiE9YXTcS3VG1SqssI/Y=
golang.org/x/sys v0.17.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.27.0 h1:wBqf8DvsY9Y/2P8gAfPDEYNuS30J4lPHJxXSb/nJZ+s=
golang.org/x/sys v0.27.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.2.0/go.mod h1:TVmDHMZPmdnySmBfhjOoOdhjzdE1h4u1VwSiw2l1Nuc=
Expand All @@ -460,6 +478,8 @@ golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
golang.org/x/text v0.20.0 h1:gK/Kv2otX8gz+wn7Rmb3vT96ZwuoxnQlY+HlJVj7Qug=
golang.org/x/text v0.20.0/go.mod h1:D4IsuqiFMhST5bX19pQ9ikHC2GsaKyk/oF+pn3ducp4=
golang.org/x/time v0.0.0-20181108054448-85acf8d2951c/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/time v0.0.0-20190308202827-9d24e82272b4/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/time v0.0.0-20191024005414-555d28b269f0/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
Expand Down
3 changes: 1 addition & 2 deletions http_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package main
import (
"bytes"
"context"
"encoding/json"
"io"
"net/http"
"net/url"
Expand Down Expand Up @@ -176,7 +175,7 @@ func bounceJSON(w http.ResponseWriter, r *http.Request) {
QueryParams: r.URL.Query(),
}
if len(bodyBytes) > 0 {
err = json.Unmarshal(bodyBytes, &bodyJSON)
err = golib.JsonUnmarshalWithNumber(bodyBytes, &bodyJSON)
if err != nil {
errorResponse(w, 500, err, errorBody)
return
Expand Down
3 changes: 2 additions & 1 deletion pkg/lib/api/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/moul/http2curl"
"github.com/programmfabrik/apitest/pkg/lib/datastore"
"github.com/programmfabrik/apitest/pkg/lib/util"
"github.com/programmfabrik/golib"
)

var httpClient *http.Client
Expand Down Expand Up @@ -239,7 +240,7 @@ func (request Request) buildHttpRequest() (req *http.Request, err error) {
if err != nil {
return nil, fmt.Errorf("could not marshal cookie '%s' from Datastore", storeKey)
}
err = json.Unmarshal(ckBytes, &ck)
err = golib.JsonUnmarshalWithNumber(ckBytes, &ck)
if err != nil {
return nil, fmt.Errorf("could not unmarshal cookie '%s' from Datastore: %s", storeKey, string(ckBytes))
}
Expand Down
13 changes: 9 additions & 4 deletions pkg/lib/api/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,10 @@ func (response Response) ServerResponseToGenericJSON(responseFormat ResponseForm
if err != nil {
return res, err
}
json.Unmarshal(responseBytes, &res)
err = golib.JsonUnmarshalWithNumber(responseBytes, &res)
if err != nil {
return res, err
}

return res, nil
}
Expand All @@ -308,7 +311,7 @@ func (response Response) ToGenericJSON() (any, error) {
// We have a json, and thereby try to unmarshal it into our body
resBody := response.Body
if len(resBody) > 0 {
err = json.Unmarshal(resBody, &bodyJSON)
err = golib.JsonUnmarshalWithNumber(resBody, &bodyJSON)
if err != nil {
return res, err
}
Expand Down Expand Up @@ -350,8 +353,10 @@ func (response Response) ToGenericJSON() (any, error) {
if err != nil {
return res, err
}
json.Unmarshal(responseBytes, &res)

err = golib.JsonUnmarshalWithNumber(responseBytes, &res)
if err != nil {
return res, err
}
return res, nil
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/lib/api/response_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func TestResponse_ToGenericJson(t *testing.T) {
if !ok {
t.Fatalf("responseJsonObj should have status code field")
}
if statusCode != float64(200) {
if statusCode != json.Number("200") {
t.Errorf("responseJson had wrong statuscode, expected 200, got: %d", statusCode)
}
jsonHeaders, ok := jsonObjResp["header"]
Expand Down
80 changes: 80 additions & 0 deletions pkg/lib/compare/comparer.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package compare

import (
"encoding/json"
"fmt"
"strings"

"github.com/programmfabrik/apitest/pkg/lib/util"
)
Expand All @@ -24,6 +26,52 @@ func (f CompareFailure) Error() string {
return f.String()
}

// jsonNumberEq is comparing ints, floats or strings of the number. It fails to
// compare different formats, 1e10 != 10000000000, although it is the same mathematical value.
func jsonNumberEq(numberExp, numberGot json.Number) (eq bool) {

expInt, expIntErr := numberExp.Int64()
gotInt, gotIntErr := numberGot.Int64()
expFloat, expFloatErr := numberExp.Float64()
gotFloat, gotFloatErr := numberGot.Float64()

var cmp string
_ = cmp

if expIntErr == nil && gotIntErr == nil {
cmp = "int"
} else if expFloatErr == nil && gotFloatErr == nil {
cmp = "float"
} else {
cmp = "string"
}

// if any of the interpretations is out of range, we compare by string
for _, e := range []error{
expIntErr, gotIntErr, expFloatErr, gotFloatErr,
} {
if e == nil {
continue
}
if strings.Contains(e.Error(), "range") {
cmp = "string"
break
}
}

switch cmp {
case "int":
eq = expInt == gotInt
case "float":
eq = expFloat == gotFloat
case "string":
eq = numberExp == numberGot
}

// golib.Pln("exp %q == got %q : %t %s", numberExp, numberGot, eq, cmp)
return eq
}

func JsonEqual(left, right any, control ComparisonContext) (res CompareResult, err error) {
//left may be nil, because we dont specify the content of the field
if left == nil && right == nil {
Expand All @@ -46,6 +94,38 @@ func JsonEqual(left, right any, control ComparisonContext) (res CompareResult, e
}

switch typedLeft := left.(type) {
case json.Number:
typedRight, ok := right.(json.Number)
if !ok {
res := CompareResult{
false,
[]CompareFailure{
{
"$",
fmt.Sprintf("expected json.Number, but got %T", right),
},
},
}
return res, nil
}

if jsonNumberEq(typedLeft, typedRight) {
res = CompareResult{
Equal: true,
}
} else {
res = CompareResult{
Equal: false,
Failures: []CompareFailure{
{
"",
fmt.Sprintf("Got '%s', expected '%s'", typedRight, typedLeft),
},
},
}
}
return res, nil

case util.JsonObject:
rightAsObject, ok := right.(util.JsonObject)
if !ok {
Expand Down
26 changes: 26 additions & 0 deletions pkg/lib/compare/comparer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"testing"

"github.com/programmfabrik/apitest/pkg/lib/util"
"github.com/stretchr/testify/assert"
)

var trivialComparerTestData = []struct {
Expand Down Expand Up @@ -391,3 +392,28 @@ func TestTrivialJsonComparer(t *testing.T) {
})
}
}

func TestJsonNumberEq(t *testing.T) {
if !assert.Equal(t, true, jsonNumberEq("200", "200")) {
return
}
if !assert.Equal(t, false, jsonNumberEq("-9223372036854775808", "-9223372036854775809")) {
return
}
if !assert.Equal(t, false, jsonNumberEq("-9223372036854775809", "-9223372036854775808")) {
return
}
if !assert.Equal(t, true, jsonNumberEq("1e10", "10000000000")) {
return
}
// Although this is the same value, we cannot say its equal
if !assert.Equal(t, true, jsonNumberEq("-9.223372036854775808e+18", "-9223372036854775808")) {
return
}
if !assert.Equal(t, true, jsonNumberEq("-9.223372036854775808e+18", "-9.223372036854775808e+18")) {
return
}
if !assert.Equal(t, false, jsonNumberEq("-9.223372036854775809e+18", "-9223372036854775809")) {
return
}
}
Loading

0 comments on commit 97f156d

Please sign in to comment.