|
| 1 | +package stanza_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/xml" |
| 5 | + "gosrc.io/xmpp/stanza" |
| 6 | + "reflect" |
| 7 | + "strings" |
| 8 | + "testing" |
| 9 | +) |
| 10 | + |
| 11 | +const msg_const = ` |
| 12 | +<message |
| 13 | + from="romeo@montague.lit/laptop" |
| 14 | + to="juliet@capulet.lit/laptop"> |
| 15 | + <body>V unir avtugf pybnx gb uvqr zr sebz gurve fvtug</body> |
| 16 | + <no-copy xmlns="urn:xmpp:hints"></no-copy> |
| 17 | + <no-permanent-store xmlns="urn:xmpp:hints"></no-permanent-store> |
| 18 | + <no-store xmlns="urn:xmpp:hints"></no-store> |
| 19 | + <store xmlns="urn:xmpp:hints"></store> |
| 20 | +</message>` |
| 21 | + |
| 22 | +func TestSerializationHint(t *testing.T) { |
| 23 | + msg := stanza.NewMessage(stanza.Attrs{To: "juliet@capulet.lit/laptop", From: "romeo@montague.lit/laptop"}) |
| 24 | + msg.Body = "V unir avtugf pybnx gb uvqr zr sebz gurve fvtug" |
| 25 | + msg.Extensions = append(msg.Extensions, stanza.HintNoCopy{}, stanza.HintNoPermanentStore{}, stanza.HintNoStore{}, stanza.HintStore{}) |
| 26 | + data, _ := xml.Marshal(msg) |
| 27 | + if strings.ReplaceAll(strings.Join(strings.Fields(msg_const), ""), "\n", "") != strings.Join(strings.Fields(string(data)), "") { |
| 28 | + t.Fatalf("marshalled message does not match expected message") |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +func TestUnmarshalHints(t *testing.T) { |
| 33 | + // Init message as in the const value |
| 34 | + msgConst := stanza.NewMessage(stanza.Attrs{To: "juliet@capulet.lit/laptop", From: "romeo@montague.lit/laptop"}) |
| 35 | + msgConst.Body = "V unir avtugf pybnx gb uvqr zr sebz gurve fvtug" |
| 36 | + msgConst.Extensions = append(msgConst.Extensions, &stanza.HintNoCopy{}, &stanza.HintNoPermanentStore{}, &stanza.HintNoStore{}, &stanza.HintStore{}) |
| 37 | + |
| 38 | + // Compare message with the const value |
| 39 | + msg := stanza.Message{} |
| 40 | + err := xml.Unmarshal([]byte(msg_const), &msg) |
| 41 | + if err != nil { |
| 42 | + t.Fatal(err) |
| 43 | + } |
| 44 | + |
| 45 | + if msgConst.XMLName.Local != msg.XMLName.Local { |
| 46 | + t.Fatalf("message tags do not match. Expected: %s, Actual: %s", msgConst.XMLName.Local, msg.XMLName.Local) |
| 47 | + } |
| 48 | + if msgConst.Body != msg.Body { |
| 49 | + t.Fatalf("message bodies do not match. Expected: %s, Actual: %s", msgConst.Body, msg.Body) |
| 50 | + } |
| 51 | + |
| 52 | + if !reflect.DeepEqual(msgConst.Attrs, msg.Attrs) { |
| 53 | + t.Fatalf("attributes do not match") |
| 54 | + } |
| 55 | + |
| 56 | + if !reflect.DeepEqual(msgConst.Error, msg.Error) { |
| 57 | + t.Fatalf("attributes do not match") |
| 58 | + } |
| 59 | + var found bool |
| 60 | + for _, ext := range msgConst.Extensions { |
| 61 | + for _, strExt := range msg.Extensions { |
| 62 | + if reflect.TypeOf(ext) == reflect.TypeOf(strExt) { |
| 63 | + found = true |
| 64 | + break |
| 65 | + } |
| 66 | + } |
| 67 | + if !found { |
| 68 | + t.Fatalf("extensions do not match") |
| 69 | + } |
| 70 | + found = false |
| 71 | + } |
| 72 | +} |
0 commit comments