Files
upstream-ory-kratos/selfservice/strategy/oidc/json_equal_test.go
upstream-mirror c60cc7ca75
Some checks failed
CI / Run tests and lints (push) Failing after 29s
CI / Build CLI docs (push) Has been skipped
CI / Run Playwright end-to-end tests (mysql) (push) Failing after 3s
CI / Run Playwright end-to-end tests (postgres) (push) Failing after 2s
CI / Run Playwright end-to-end tests (sqlite) (push) Failing after 3s
CodeQL / Analyze (go) (push) Failing after 4s
CodeQL / Analyze (javascript) (push) Failing after 3s
Docker Image Scanners / scanners (push) Failing after 4s
Synchronize Issue Labels / Synchronize Issue Labels (push) Has been skipped
CI / Run end-to-end tests (postgres) (push) Failing after 19s
CI / Run end-to-end tests (sqlite) (push) Failing after 2s
CI / Run Playwright end-to-end tests (cockroach) (push) Failing after 2s
CI / Draft newsletter (push) Has been skipped
CI / Generate release (push) Has been skipped
CI / Pending approval Slack notification (push) Has been skipped
CI / Send newsletter (push) Has been skipped
snapshot of https://github.com/ory/kratos.git (master, 2026-09-15; blobs > 131072B omitted)
2026-09-15 21:19:12 +05:00

105 lines
2.4 KiB
Go

// Copyright © 2023 Ory Corp
// SPDX-License-Identifier: Apache-2.0
package oidc
import (
"encoding/json"
"testing"
"github.com/stretchr/testify/assert"
)
func TestJsonEqual(t *testing.T) {
t.Parallel()
for _, tc := range []struct {
name string
a, b json.RawMessage
expected bool
}{
{
name: "both nil",
a: nil,
b: nil,
expected: true,
},
{
name: "both empty",
a: json.RawMessage{},
b: json.RawMessage{},
expected: true,
},
{
name: "one nil one empty",
a: nil,
b: json.RawMessage{},
expected: true,
},
{
name: "one nil one non-empty",
a: nil,
b: json.RawMessage(`{}`),
expected: false,
},
{
name: "identical objects",
a: json.RawMessage(`{"name":"alice","age":30}`),
b: json.RawMessage(`{"name":"alice","age":30}`),
expected: true,
},
{
name: "same content different key order",
a: json.RawMessage(`{"name":"alice","age":30}`),
b: json.RawMessage(`{"age":30,"name":"alice"}`),
expected: true,
},
{
name: "same content different whitespace",
a: json.RawMessage(`{"name": "alice"}`),
b: json.RawMessage(`{"name":"alice"}`),
expected: true,
},
{
name: "different values",
a: json.RawMessage(`{"name":"alice"}`),
b: json.RawMessage(`{"name":"bob"}`),
expected: false,
},
{
name: "nested objects equal",
a: json.RawMessage(`{"user":{"name":"alice","groups":["admin","user"]}}`),
b: json.RawMessage(`{"user":{"groups":["admin","user"],"name":"alice"}}`),
expected: true,
},
{
name: "nested objects different",
a: json.RawMessage(`{"user":{"groups":["admin"]}}`),
b: json.RawMessage(`{"user":{"groups":["admin","user"]}}`),
expected: false,
},
{
name: "extra key",
a: json.RawMessage(`{"name":"alice"}`),
b: json.RawMessage(`{"name":"alice","age":30}`),
expected: false,
},
{
name: "invalid json a",
a: json.RawMessage(`{invalid`),
b: json.RawMessage(`{"name":"alice"}`),
expected: false,
},
{
name: "invalid json b",
a: json.RawMessage(`{"name":"alice"}`),
b: json.RawMessage(`{invalid`),
expected: false,
},
} {
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
assert.Equal(t, tc.expected, jsonEqual(tc.a, tc.b))
})
}
}