-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[TT-9829] Fix checking json.Marshal errors in tests (#5426)
- Added `test.MarshalJSON` utility for encoding json in tests and asserting the error return is nil, - Updated usage in tests This is required to resolve some sonarcloud errors in existing tests code. https://tyktech.atlassian.net/browse/TT-9829 --------- Co-authored-by: Tit Petric <tit@tyk.io> (cherry picked from commit c985ee4)
- Loading branch information
Showing
7 changed files
with
64 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package test | ||
|
||
import ( | ||
"encoding/json" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
// MarshalJSON returns a closure returning the marshalled json | ||
// while asserting no error occurred during marshalling. | ||
func MarshalJSON(tb testing.TB) func(interface{}) []byte { | ||
tb.Helper() | ||
|
||
return func(in interface{}) []byte { | ||
b, err := json.Marshal(in) | ||
require.NoError(tb, err) | ||
return b | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestMarshalJSON(t *testing.T) { | ||
value := "foo" | ||
marshal := MarshalJSON(t) | ||
out := marshal(value) | ||
assert.Equal(t, []byte(`"foo"`), out) | ||
} |