...
1 package urn
2
3 import (
4 "encoding/json"
5 "fmt"
6 "testing"
7
8 scimschema "github.com/leodido/go-urn/scim/schema"
9 "github.com/stretchr/testify/assert"
10 "github.com/stretchr/testify/require"
11 )
12
13 func TestSCIMJSONMarshaling(t *testing.T) {
14 t.Run("roundtrip", func(t *testing.T) {
15
16 exp := SCIM{Type: scimschema.Schemas, Name: "core", Other: "extension:enterprise:2.0:User"}
17 mar, err := json.Marshal(exp)
18 require.NoError(t, err)
19 require.Equal(t, `"urn:ietf:params:scim:schemas:core:extension:enterprise:2.0:User"`, string(mar))
20
21
22 var act SCIM
23 err = json.Unmarshal(mar, &act)
24 require.NoError(t, err)
25 exp.pos = 34
26 require.Equal(t, exp, act)
27 })
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 })
71 }
72
View as plain text