Skip to content

Commit ebf11e5

Browse files
committed
test: check custom (un)marshalling for SCIM URNs
Signed-off-by: Leonardo Di Donato <leodidonato@gmail.com>
1 parent bba54fc commit ebf11e5

File tree

2 files changed

+49
-1
lines changed

2 files changed

+49
-1
lines changed

scim_test.go

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@ package urn
22

33
import (
44
"encoding/json"
5+
"fmt"
56
"testing"
67

78
scimschema "github.com/leodido/go-urn/scim/schema"
9+
"github.com/stretchr/testify/assert"
810
"github.com/stretchr/testify/require"
911
)
1012

@@ -14,12 +16,56 @@ func TestSCIMJSONMarshaling(t *testing.T) {
1416
exp := SCIM{Type: scimschema.Schemas, Name: "core", Other: "extension:enterprise:2.0:User"}
1517
mar, err := json.Marshal(exp)
1618
require.NoError(t, err)
19+
require.Equal(t, `"urn:ietf:params:scim:schemas:core:extension:enterprise:2.0:User"`, string(mar))
1720

1821
// Unmarshal
1922
var act SCIM
2023
err = json.Unmarshal(mar, &act)
2124
require.NoError(t, err)
22-
25+
exp.pos = 34
2326
require.Equal(t, exp, act)
2427
})
28+
29+
t.Run("unmarshal", func(t *testing.T) {
30+
exp := `urn:ietf:params:scim:schemas:extension:enterprise:2.0:User`
31+
var got SCIM
32+
err := json.Unmarshal([]byte(fmt.Sprintf(`"%s"`, exp)), &got)
33+
if !assert.NoError(t, err) {
34+
return
35+
}
36+
assert.Equal(t, exp, got.String())
37+
assert.Equal(t, SCIM{
38+
Type: scimschema.Schemas,
39+
Name: "extension",
40+
Other: "enterprise:2.0:User",
41+
pos: 39,
42+
}, got)
43+
})
44+
45+
t.Run("unmarshal without the <other> part", func(t *testing.T) {
46+
exp := `urn:ietf:params:scim:schemas:core`
47+
var got SCIM
48+
err := json.Unmarshal([]byte(fmt.Sprintf(`"%s"`, exp)), &got)
49+
if !assert.NoError(t, err) {
50+
return
51+
}
52+
assert.Equal(t, exp, got.String())
53+
assert.Equal(t, SCIM{
54+
Type: scimschema.Schemas,
55+
Name: "core",
56+
pos: 29,
57+
}, got)
58+
})
59+
60+
t.Run("invalid URN", func(t *testing.T) {
61+
var actual SCIM
62+
err := json.Unmarshal([]byte(`"not a URN"`), &actual)
63+
assert.EqualError(t, err, "invalid SCIM URN: not a URN")
64+
})
65+
66+
t.Run("empty", func(t *testing.T) {
67+
var actual SCIM
68+
err := actual.UnmarshalJSON(nil)
69+
assert.EqualError(t, err, "unexpected end of JSON input")
70+
})
2571
}

urn_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,13 @@ func TestJSONMarshaling(t *testing.T) {
3838
}
3939
assert.Equal(t, expected.String(), actual.String())
4040
})
41+
4142
t.Run("invalid URN", func(t *testing.T) {
4243
var actual URN
4344
err := json.Unmarshal([]byte(`"not a URN"`), &actual)
4445
assert.EqualError(t, err, "invalid URN: not a URN")
4546
})
47+
4648
t.Run("empty", func(t *testing.T) {
4749
var actual URN
4850
err := actual.UnmarshalJSON(nil)

0 commit comments

Comments
 (0)