-
Notifications
You must be signed in to change notification settings - Fork 62
/
Copy pathpubsub_test.go
922 lines (808 loc) · 31.2 KB
/
pubsub_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
package stanza_test
import (
"encoding/xml"
"errors"
"gosrc.io/xmpp/stanza"
"strings"
"testing"
)
var submitFormExample = stanza.NewForm([]*stanza.Field{
{Var: "FORM_TYPE", Type: stanza.FieldTypeHidden, ValuesList: []string{"http://jabber.org/protocol/pubsub#node_config"}},
{Var: "pubsub#title", ValuesList: []string{"Princely Musings (Atom)"}},
{Var: "pubsub#deliver_notifications", ValuesList: []string{"1"}},
{Var: "pubsub#access_model", ValuesList: []string{"roster"}},
{Var: "pubsub#roster_groups_allowed", ValuesList: []string{"friends", "servants", "courtiers"}},
{Var: "pubsub#type", ValuesList: []string{"http://www.w3.org/2005/Atom"}},
{
Var: "pubsub#notification_type",
Type: "list-single",
Label: "Specify the delivery style for event notifications",
ValuesList: []string{"headline"},
Options: []stanza.Option{
{ValuesList: []string{"normal"}},
{ValuesList: []string{"headline"}},
},
},
}, stanza.FormTypeSubmit)
// ***********************************
// * 6.1 Subscribe to a Node
// ***********************************
func TestNewSubRequest(t *testing.T) {
expectedReq := "<iq type=\"set\"id=\"sub1\"to=\"pubsub.shakespeare.lit\"> " +
"<pubsub xmlns=\"http://jabber.org/protocol/pubsub\"> <subscribe node=\"princely_musings\"jid=\"francisco@denmark.lit\"></subscribe>" +
" </pubsub> </iq>"
subInfo := stanza.SubInfo{
Node: "princely_musings", Jid: "francisco@denmark.lit",
}
subR, err := stanza.NewSubRq("pubsub.shakespeare.lit", subInfo)
if err != nil {
t.Fatalf("failed to create a sub request: %v", err)
}
subR.Id = "sub1"
if _, e := checkMarshalling(t, subR); e != nil {
t.Fatalf("Failed to check marshalling for generated sub request : %s", e)
}
data, err := xml.Marshal(subR)
if err := compareMarshal(expectedReq, string(data)); err != nil {
t.Fatalf(err.Error())
}
}
func TestNewSubResp(t *testing.T) {
response := `
<iq type="result" from="pubsub.shakespeare.lit" to="francisco@denmark.lit/barracks" id="sub1">
<pubsub xmlns="http://jabber.org/protocol/pubsub">
<subscription node="princely_musings" jid="francisco@denmark.lit"
subid="ba49252aaa4f5d320c24d3766f0bdcade78c78d3" subscription="subscribed"/>
</pubsub>
</iq>
`
pubsub, err := getPubSubGenericPayload(response)
if err != nil {
t.Fatalf(err.Error())
}
if pubsub.Subscription == nil {
t.Fatalf("subscription node is nil")
}
if pubsub.Subscription.Node == "" ||
pubsub.Subscription.Jid == "" ||
pubsub.Subscription.SubId == nil ||
pubsub.Subscription.SubStatus == "" {
t.Fatalf("one or more of the subscription attributes was not successfully decoded")
}
}
// ***********************************
// * 6.2 Unsubscribe from a Node
// ***********************************
func TestNewUnSubRequest(t *testing.T) {
expectedReq := "<iq type=\"set\"id=\"unsub1\"to=\"pubsub.shakespeare.lit\"> " +
"<pubsub xmlns=\"http://jabber.org/protocol/pubsub\"> " +
"<unsubscribe node=\"princely_musings\"jid=\"francisco@denmark.lit\"></unsubscribe> </pubsub> </iq>"
subInfo := stanza.SubInfo{
Node: "princely_musings", Jid: "francisco@denmark.lit",
}
subR, err := stanza.NewUnsubRq("pubsub.shakespeare.lit", subInfo)
if err != nil {
t.Fatalf("failed to create an unsub request: %v", err)
}
subR.Id = "unsub1"
if _, e := checkMarshalling(t, subR); e != nil {
t.Fatalf("Failed to check marshalling for generated sub request : %s", e)
}
pubsub, ok := subR.Payload.(*stanza.PubSubGeneric)
if !ok {
t.Fatalf("payload is not a pubsub !")
}
if pubsub.Unsubscribe == nil {
t.Fatalf("Unsubscribe tag should be present in sub config options request")
}
data, err := xml.Marshal(subR)
if err := compareMarshal(expectedReq, string(data)); err != nil {
t.Fatalf(err.Error())
}
}
func TestNewUnsubResp(t *testing.T) {
response := `
<iq type="result" from="pubsub.shakespeare.lit" to="francisco@denmark.lit/barracks" id="unsub1">
<pubsub xmlns="http://jabber.org/protocol/pubsub">
<subscription node="princely_musings" jid="francisco@denmark.lit" subscription="none"
subid="ba49252aaa4f5d320c24d3766f0bdcade78c78d3"/>
</pubsub>
</iq>
`
pubsub, err := getPubSubGenericPayload(response)
if err != nil {
t.Fatalf(err.Error())
}
if pubsub.Subscription == nil {
t.Fatalf("subscription node is nil")
}
if pubsub.Subscription.Node == "" ||
pubsub.Subscription.Jid == "" ||
pubsub.Subscription.SubId == nil ||
pubsub.Subscription.SubStatus == "" {
t.Fatalf("one or more of the subscription attributes was not successfully decoded")
}
}
// ***************************************
// * 6.3 Configure Subscription Options
// ***************************************
func TestNewSubOptsRq(t *testing.T) {
expectedReq := "<iq type=\"get\"id=\"options1\"to=\"pubsub.shakespeare.lit\"> " +
"<pubsub xmlns=\"http://jabber.org/protocol/pubsub\"> " +
"<options node=\"princely_musings\" jid=\"francisco@denmark.lit\"></options> </pubsub> </iq>"
subInfo := stanza.SubInfo{
Node: "princely_musings", Jid: "francisco@denmark.lit",
}
subR, err := stanza.NewSubOptsRq("pubsub.shakespeare.lit", subInfo)
if err != nil {
t.Fatalf("failed to create a sub options request: %v", err)
}
subR.Id = "options1"
if _, e := checkMarshalling(t, subR); e != nil {
t.Fatalf("Failed to check marshalling for generated sub request : %s", e)
}
pubsub, ok := subR.Payload.(*stanza.PubSubGeneric)
if !ok {
t.Fatalf("payload is not a pubsub !")
}
if pubsub.SubOptions == nil {
t.Fatalf("Options tag should be present in sub config options request")
}
data, err := xml.Marshal(subR)
if err := compareMarshal(expectedReq, string(data)); err != nil {
t.Fatalf(err.Error())
}
}
func TestNewNewConfOptsRsp(t *testing.T) {
response := `
<iq type="result" from="pubsub.shakespeare.lit" to="francisco@denmark.lit/barracks" id="options1">
<pubsub xmlns="http://jabber.org/protocol/pubsub">
<options node="princely_musings" jid="francisco@denmark.lit">
<x xmlns="jabber:x:data" type="form">
<field var="FORM_TYPE" type="hidden">
<value>http://jabber.org/protocol/pubsub#subscribe_options</value>
</field>
<field var="pubsub#deliver" type="boolean" label="Enable delivery?">
<value>1</value>
</field>
<field var="pubsub#digest" type="boolean"
label="Receive digest notifications (approx. one per day)?">
<value>0</value>
</field>
<field var="pubsub#include_body" type="boolean"
label="Receive message body in addition to payload?">
<value>false</value>
</field>
<field var="pubsub#show-values" type="list-multi"
label="Select the presence types which are
allowed to receive event notifications">
<option label="Want to Chat">
<value>chat</value>
</option>
<option label="Available">
<value>online</value>
</option>
<option label="Away">
<value>away</value>
</option>
<option label="Extended Away">
<value>xa</value>
</option>
<option label="Do Not Disturb">
<value>dnd</value>
</option>
<value>chat</value>
<value>online</value>
</field>
</x>
</options>
</pubsub>
</iq>
`
pubsub, err := getPubSubGenericPayload(response)
if err != nil {
t.Fatalf(err.Error())
}
if pubsub.SubOptions == nil {
t.Fatalf("sub options node is nil")
}
if pubsub.SubOptions.Form == nil {
t.Fatalf("the response form is nil")
}
if len(pubsub.SubOptions.Form.Fields) != 5 {
t.Fatalf("one or more fields in the response form could not be parsed correctly")
}
}
// ***************************************
// * 6.3.5 Form Submission
// ***************************************
func TestNewFormSubmission(t *testing.T) {
expectedReq := "<iq type=\"set\" id=\"options2\" to=\"pubsub.shakespeare.lit\"> " +
"<pubsub xmlns=\"http://jabber.org/protocol/pubsub\"> <options node=\"princely_musings\" jid=\"francisco@denmark.lit\"> " +
"<x xmlns=\"jabber:x:data\" type=\"submit\"> <field var=\"FORM_TYPE\" type=\"hidden\">" +
" <value>http://jabber.org/protocol/pubsub#node_config</value> </field> <field var=\"pubsub#title\"> " +
"<value>Princely Musings (Atom)</value> </field> <field var=\"pubsub#deliver_notifications\"> " +
"<value>1</value> </field> <field var=\"pubsub#access_model\"> <value>roster</value> </field> " +
"<field var=\"pubsub#roster_groups_allowed\"> <value>friends</value> <value>servants</value>" +
" <value>courtiers</value> </field> <field var=\"pubsub#type\"> <value>http://www.w3.org/2005/Atom</value> " +
"</field> <field var=\"pubsub#notification_type\" type=\"list-single\"label=\"Specify the delivery style for event notifications\"> " +
"<value>headline</value> <option> <value>normal</value> </option> <option> <value>headline</value> </option> " +
"</field> </x> </options> </pubsub> </iq>"
subInfo := stanza.SubInfo{
Node: "princely_musings", Jid: "francisco@denmark.lit",
}
subR, err := stanza.NewFormSubmission("pubsub.shakespeare.lit", subInfo, submitFormExample)
if err != nil {
t.Fatalf("failed to create a form submission request: %v", err)
}
subR.Id = "options2"
if _, e := checkMarshalling(t, subR); e != nil {
t.Fatalf("Failed to check marshalling for generated sub request : %s", e)
}
pubsub, ok := subR.Payload.(*stanza.PubSubGeneric)
if !ok {
t.Fatalf("payload is not a pubsub !")
}
if pubsub.SubOptions == nil {
t.Fatalf("Options tag should be present in sub config options request")
}
if pubsub.SubOptions.Form == nil {
t.Fatalf("No form in form submit request !")
}
data, err := xml.Marshal(subR)
if err := compareMarshal(expectedReq, string(data)); err != nil {
t.Fatalf(err.Error())
}
}
// ***************************************
// * 6.3.7 Subscribe and Configure
// ***************************************
func TestNewSubAndConfig(t *testing.T) {
expectedReq := "<iq type=\"set\"id=\"sub1\"to=\"pubsub.shakespeare.lit\">" +
" <pubsub xmlns=\"http://jabber.org/protocol/pubsub\"> <subscribe node=\"princely_musings\" jid=\"francisco@denmark.lit\"> " +
"</subscribe>" +
"<options> <x xmlns=\"jabber:x:data\" type=\"submit\"> <field var=\"FORM_TYPE\" type=\"hidden\">" +
" <value>http://jabber.org/protocol/pubsub#node_config</value> </field> <field var=\"pubsub#title\"> " +
"<value>Princely Musings (Atom)</value> </field> <field var=\"pubsub#deliver_notifications\"> " +
"<value>1</value> </field> <field var=\"pubsub#access_model\"> <value>roster</value> </field> " +
"<field var=\"pubsub#roster_groups_allowed\"> <value>friends</value> <value>servants</value>" +
" <value>courtiers</value> </field> <field var=\"pubsub#type\"> <value>http://www.w3.org/2005/Atom</value> " +
"</field> <field var=\"pubsub#notification_type\" type=\"list-single\"label=\"Specify the delivery style for event notifications\"> " +
"<value>headline</value> <option> <value>normal</value> </option> <option> <value>headline</value> </option> " +
"</field> </x> </options> </pubsub> </iq>"
subInfo := stanza.SubInfo{
Node: "princely_musings", Jid: "francisco@denmark.lit",
}
subR, err := stanza.NewSubAndConfig("pubsub.shakespeare.lit", subInfo, submitFormExample)
if err != nil {
t.Fatalf("failed to create a sub and config request: %v", err)
}
subR.Id = "sub1"
if _, e := checkMarshalling(t, subR); e != nil {
t.Fatalf("Failed to check marshalling for generated sub request : %s", e)
}
pubsub, ok := subR.Payload.(*stanza.PubSubGeneric)
if !ok {
t.Fatalf("payload is not a pubsub !")
}
if pubsub.SubOptions == nil {
t.Fatalf("Options tag should be present in sub config options request")
}
if pubsub.SubOptions.Form == nil {
t.Fatalf("No form in form submit request !")
}
// The <options/> element MUST NOT possess a 'node' attribute or 'jid' attribute
// See XEP-0060
if pubsub.SubOptions.SubInfo.Node != "" || pubsub.SubOptions.SubInfo.Jid != "" {
t.Fatalf("SubInfo node and jid should be empty for the options tag !")
}
if pubsub.Subscribe.Node == "" || pubsub.Subscribe.Jid == "" {
t.Fatalf("SubInfo node and jid should NOT be empty for the subscribe tag !")
}
data, err := xml.Marshal(subR)
if err := compareMarshal(expectedReq, string(data)); err != nil {
t.Fatalf(err.Error())
}
}
func TestNewSubAndConfigResp(t *testing.T) {
response := `
<iq type="result" from="pubsub.shakespeare.lit" to="francisco@denmark.lit/barracks" id="sub1">
<pubsub xmlns="http://jabber.org/protocol/pubsub">
<subscription node="princely_musings" jid="francisco@denmark.lit"
subid="ba49252aaa4f5d320c24d3766f0bdcade78c78d3" subscription="subscribed"/>
<options>
<x xmlns="jabber:x:data" type="result">
<field var="FORM_TYPE" type="hidden">
<value>http://jabber.org/protocol/pubsub#subscribe_options</value>
</field>
<field var="pubsub#deliver">
<value>1</value>
</field>
<field var="pubsub#digest">
<value>0</value>
</field>
<field var="pubsub#include_body">
<value>false</value>
</field>
<field var="pubsub#show-values">
<value>chat</value>
<value>online</value>
<value>away</value>
</field>
</x>
</options>
</pubsub>
</iq>
`
pubsub, err := getPubSubGenericPayload(response)
if err != nil {
t.Fatalf(err.Error())
}
if pubsub.Subscription == nil {
t.Fatalf("sub node is nil")
}
if pubsub.SubOptions == nil {
t.Fatalf("sub options node is nil")
}
if pubsub.SubOptions.Form == nil {
t.Fatalf("the response form is nil")
}
if len(pubsub.SubOptions.Form.Fields) != 5 {
t.Fatalf("one or more fields in the response form could not be parsed correctly")
}
}
// ***************************************
// * 6.5.2 Requesting All List
// ***************************************
func TestNewItemsRequest(t *testing.T) {
subR, err := stanza.NewItemsRequest("pubsub.shakespeare.lit", "princely_musings", 0)
if err != nil {
t.Fatalf("Could not create an items request : %s", err)
}
if _, e := checkMarshalling(t, subR); e != nil {
t.Fatalf("Failed to check marshalling for generated sub request : %s", e)
}
pubsub, ok := subR.Payload.(*stanza.PubSubGeneric)
if !ok {
t.Fatalf("payload is not a pubsub !")
}
if pubsub.Items == nil {
t.Fatalf("List tag should be present to request items from a service")
}
if len(pubsub.Items.List) != 0 {
t.Fatalf("There should be no items in the <items> tag to request all items from a service")
}
}
func TestNewItemsResp(t *testing.T) {
response := `
<iq type="result" from="pubsub.shakespeare.lit" to="francisco@denmark.lit/barracks" id="items2">
<pubsub xmlns="http://jabber.org/protocol/pubsub">
<items node="princely_musings">
<item id="4e30f35051b7b8b42abe083742187228">
<entry xmlns="http://www.w3.org/2005/Atom">
<title>Alone</title>
<summary> Now I am alone. O, what a rogue and peasant slave am I! </summary>
<link rel="alternate" type="text/html"
href="http://denmark.lit/2003/12/13/atom03"/>
<id>tag:denmark.lit,2003:entry-32396</id>
<published>2003-12-13T11:09:53Z</published>
<updated>2003-12-13T11:09:53Z</updated>
</entry>
</item>
<item id="ae890ac52d0df67ed7cfdf51b644e901">
<entry xmlns="http://www.w3.org/2005/Atom">
<title>Soliloquy</title>
<summary> To be, or not to be: that is the question: Whether 'tis nobler in the
mind to suffer The slings and arrows of outrageous fortune, Or to take arms
against a sea of troubles, And by opposing end them? </summary>
<link rel="alternate" type="text/html"
href="http://denmark.lit/2003/12/13/atom03"/>
<id>tag:denmark.lit,2003:entry-32397</id>
<published>2003-12-13T18:30:02Z</published>
<updated>2003-12-13T18:30:02Z</updated>
</entry>
</item>
</items>
</pubsub>
</iq>
`
pubsub, err := getPubSubGenericPayload(response)
if err != nil {
t.Fatalf(err.Error())
}
if pubsub.Items == nil {
t.Fatalf("sub options node is nil")
}
if pubsub.Items.List == nil {
t.Fatalf("the response form is nil")
}
if len(pubsub.Items.List) != 2 {
t.Fatalf("one or more items in the response could not be parsed correctly")
}
}
// ***************************************
// * 6.5.8 Requesting a Particular Item
// ***************************************
func TestNewSpecificItemRequest(t *testing.T) {
expectedReq := "<iq type=\"get\" id=\"items3\"to=\"pubsub.shakespeare.lit\"> " +
"<pubsub xmlns=\"http://jabber.org/protocol/pubsub\"> <items node=\"princely_musings\"> " +
"<item id=\"ae890ac52d0df67ed7cfdf51b644e901\"></item> </items> </pubsub> </iq>"
subR, err := stanza.NewSpecificItemRequest("pubsub.shakespeare.lit", "princely_musings", "ae890ac52d0df67ed7cfdf51b644e901")
if err != nil {
t.Fatalf("failed to create a specific item request: %v", err)
}
subR.Id = "items3"
if _, e := checkMarshalling(t, subR); e != nil {
t.Fatalf("Failed to check marshalling for generated sub request : %s", e)
}
pubsub, ok := subR.Payload.(*stanza.PubSubGeneric)
if !ok {
t.Fatalf("payload is not a pubsub !")
}
if pubsub.Items == nil {
t.Fatalf("List tag should be present to request items from a service")
}
data, err := xml.Marshal(subR)
if err := compareMarshal(expectedReq, string(data)); err != nil {
t.Fatalf(err.Error())
}
}
// ***************************************
// * 7.1 Publish an Item to a Node
// ***************************************
func TestNewPublishItemRq(t *testing.T) {
item := stanza.Item{
XMLName: xml.Name{},
Id: "",
Publisher: "",
Any: &stanza.Node{
XMLName: xml.Name{
Space: "http://www.w3.org/2005/Atom",
Local: "entry",
},
Attrs: nil,
Content: "",
Nodes: []stanza.Node{
{
XMLName: xml.Name{Space: "", Local: "title"},
Attrs: nil,
Content: "My pub item title",
Nodes: nil,
},
{
XMLName: xml.Name{Space: "", Local: "summary"},
Attrs: nil,
Content: "My pub item content summary",
Nodes: nil,
},
{
XMLName: xml.Name{Space: "", Local: "link"},
Attrs: []xml.Attr{
{
Name: xml.Name{Space: "", Local: "rel"},
Value: "alternate",
},
{
Name: xml.Name{Space: "", Local: "type"},
Value: "text/html",
},
{
Name: xml.Name{Space: "", Local: "href"},
Value: "http://denmark.lit/2003/12/13/atom03",
},
},
},
{
XMLName: xml.Name{Space: "", Local: "id"},
Attrs: nil,
Content: "My pub item content ID",
Nodes: nil,
},
{
XMLName: xml.Name{Space: "", Local: "published"},
Attrs: nil,
Content: "2003-12-13T18:30:02Z",
Nodes: nil,
},
{
XMLName: xml.Name{Space: "", Local: "updated"},
Attrs: nil,
Content: "2003-12-13T18:30:02Z",
Nodes: nil,
},
},
},
}
subR, err := stanza.NewPublishItemRq("pubsub.shakespeare.lit", "princely_musings", "bnd81g37d61f49fgn581", item)
if err != nil {
t.Fatalf("Could not create an item pub request : %s", err)
}
if _, e := checkMarshalling(t, subR); e != nil {
t.Fatalf("Failed to check marshalling for generated sub request : %s", e)
}
pubsub, ok := subR.Payload.(*stanza.PubSubGeneric)
if !ok {
t.Fatalf("payload is not a pubsub !")
}
if strings.TrimSpace(pubsub.Publish.Node) == "" {
t.Fatalf("the <publish/> element MUST possess a 'node' attribute, specifying the NodeID of the node.")
}
if pubsub.Publish.Items[0].Id == "" {
t.Fatalf("an id was provided for the item and it should be used")
}
}
// ***************************************
// * 7.1.5 Publishing Options
// ***************************************
func TestNewPublishItemOptsRq(t *testing.T) {
expectedReq := "<iq type=\"set\"id=\"pub1\"to=\"pubsub.shakespeare.lit\"> <pubsub xmlns=\"http://jabber.org/protocol/pubsub\"> " +
"<publish node=\"princely_musings\"> <item id=\"ae890ac52d0df67ed7cfdf51b644e901\"> " +
"<entry xmlns=\"http://www.w3.org/2005/Atom\"> <title>Soliloquy</title> " +
"<summary> To be, or not to be: that is the question: Whether \"tis nobler in the mind to suffer The " +
"slings and arrows of outrageous fortune, Or to take arms against a sea of troubles, And by opposing end them? " +
"</summary> <link rel=\"alternate\" type=\"text/html\"href=\"http://denmark.lit/2003/12/13/atom03\"></link> " +
"<id>tag:denmark.lit,2003:entry-32397</id> <published>2003-12-13T18:30:02Z</published> " +
"<updated>2003-12-13T18:30:02Z</updated> </entry> </item> </publish> <publish-options> " +
"<x xmlns=\"jabber:x:data\" type=\"submit\"> <field var=\"FORM_TYPE\" type=\"hidden\"> " +
"<value>http://jabber.org/protocol/pubsub#publish-options</value> </field> <field var=\"pubsub#access_model\"> " +
"<value>presence</value> </field> </x> </publish-options> </pubsub> </iq>"
var iq stanza.IQ
err := xml.Unmarshal([]byte(expectedReq), &iq)
if err != nil {
t.Fatalf("could not unmarshal example request : %s", err)
}
pubsub, ok := iq.Payload.(*stanza.PubSubGeneric)
if !ok {
t.Fatalf("payload is not a pubsub !")
}
if pubsub.Publish == nil {
t.Fatalf("Publish tag is empty")
}
if len(pubsub.Publish.Items) != 1 {
t.Fatalf("could not parse item properly")
}
}
// ***************************************
// * 7.2 Delete an Item from a Node
// ***************************************
func TestNewDelItemFromNode(t *testing.T) {
expectedReq := "<iq type=\"set\"id=\"retract1\"to=\"pubsub.shakespeare.lit\"> " +
"<pubsub xmlns=\"http://jabber.org/protocol/pubsub\"> <retract node=\"princely_musings\"> " +
"<item id=\"ae890ac52d0df67ed7cfdf51b644e901\"></item> </retract> </pubsub> </iq>"
subR, err := stanza.NewDelItemFromNode("pubsub.shakespeare.lit", "princely_musings", "ae890ac52d0df67ed7cfdf51b644e901", nil)
if err != nil {
t.Fatalf("failed to create a delete item from node request: %v", err)
}
subR.Id = "retract1"
if _, e := checkMarshalling(t, subR); e != nil {
t.Fatalf("Failed to check marshalling for generated del item request : %s", e)
}
pubsub, ok := subR.Payload.(*stanza.PubSubGeneric)
if !ok {
t.Fatalf("payload is not a pubsub !")
}
if pubsub.Retract == nil {
t.Fatalf("Retract tag should be present to del an item from a service")
}
if strings.TrimSpace(pubsub.Retract.Items[0].Id) == "" {
t.Fatalf("Item id, for the item to delete, should be non empty")
}
if pubsub.Retract.Items[0].Any != nil {
t.Fatalf("Item node must be empty")
}
data, err := xml.Marshal(subR)
if err := compareMarshal(expectedReq, string(data)); err != nil {
t.Fatalf(err.Error())
}
}
// ***************************************
// * 8.1 Create a Node
// ***************************************
func TestNewCreateNode(t *testing.T) {
expectedReq := "<iq type=\"set\"id=\"create1\"to=\"pubsub.shakespeare.lit\"> " +
"<pubsub xmlns=\"http://jabber.org/protocol/pubsub\"> <create node=\"princely_musings\"></create> </pubsub> </iq>"
subR, err := stanza.NewCreateNode("pubsub.shakespeare.lit", "princely_musings")
if err != nil {
t.Fatalf("failed to create a create node request: %v", err)
}
subR.Id = "create1"
if _, e := checkMarshalling(t, subR); e != nil {
t.Fatalf("Failed to check marshalling for generated del item request : %s", e)
}
pubsub, ok := subR.Payload.(*stanza.PubSubGeneric)
if !ok {
t.Fatalf("payload is not a pubsub !")
}
if pubsub.Create == nil {
t.Fatalf("Create tag should be present to create a node on a service")
}
if strings.TrimSpace(pubsub.Create.Node) == "" {
t.Fatalf("Expected node name to be present")
}
data, err := xml.Marshal(subR)
if err := compareMarshal(expectedReq, string(data)); err != nil {
t.Fatalf(err.Error())
}
}
func TestNewCreateNodeResp(t *testing.T) {
response := `
<iq type="result" from="pubsub.shakespeare.lit" to="hamlet@denmark.lit/elsinore" id="create2">
<pubsub xmlns="http://jabber.org/protocol/pubsub">
<create node="25e3d37dabbab9541f7523321421edc5bfeb2dae"/>
</pubsub>
</iq>
`
pubsub, err := getPubSubGenericPayload(response)
if err != nil {
t.Fatalf(err.Error())
}
if pubsub.Create == nil {
t.Fatalf("create segment is nil")
}
if pubsub.Create.Node == "" {
t.Fatalf("could not parse generated nodeId")
}
}
// ***************************************
// * 8.1.3 Create and Configure a Node
// ***************************************
func TestNewCreateAndConfigNode(t *testing.T) {
expectedReq := "<iq type=\"set\" id=\"create1\" to=\"pubsub.shakespeare.lit\" > " +
"<pubsub xmlns=\"http://jabber.org/protocol/pubsub\"> <create node=\"princely_musings\"></create> " +
"<configure> <x xmlns=\"jabber:x:data\" type=\"submit\"> <field var=\"FORM_TYPE\" type=\"hidden\" > " +
"<value>http://jabber.org/protocol/pubsub#node_config</value> </field> <field var=\"pubsub#notify_retract\"> " +
"<value>0</value> </field> <field var=\"pubsub#notify_sub\"> <value>0</value> </field> " +
"<field var=\"pubsub#max_payload_size\"> <value>1028</value> </field> </x> </configure> </pubsub> </iq>"
subR, err := stanza.NewCreateAndConfigNode("pubsub.shakespeare.lit",
"princely_musings",
&stanza.Form{
Type: stanza.FormTypeSubmit,
Fields: []*stanza.Field{
{Var: "FORM_TYPE", Type: stanza.FieldTypeHidden, ValuesList: []string{"http://jabber.org/protocol/pubsub#node_config"}},
{Var: "pubsub#notify_retract", ValuesList: []string{"0"}},
{Var: "pubsub#notify_sub", ValuesList: []string{"0"}},
{Var: "pubsub#max_payload_size", ValuesList: []string{"1028"}},
},
})
if err != nil {
t.Fatalf("failed to create a create and config node request: %v", err)
}
subR.Id = "create1"
if _, e := checkMarshalling(t, subR); e != nil {
t.Fatalf("Failed to check marshalling for generated del item request : %s", e)
}
pubsub, ok := subR.Payload.(*stanza.PubSubGeneric)
if !ok {
t.Fatalf("payload is not a pubsub !")
}
if pubsub.Create == nil {
t.Fatalf("Create tag should be present to create a node on a service")
}
if strings.TrimSpace(pubsub.Create.Node) == "" {
t.Fatalf("Expected node name to be present")
}
if pubsub.Configure == nil {
t.Fatalf("Configure tag should be present to configure a node during its creation on a service")
}
if pubsub.Configure.Form == nil {
t.Fatalf("Expected a form to be present, to configure the node")
}
if len(pubsub.Configure.Form.Fields) != 4 {
t.Fatalf("Expected 4 elements to be present in the config form but got : %v", len(pubsub.Configure.Form.Fields))
}
data, err := xml.Marshal(subR)
if err := compareMarshal(expectedReq, string(data)); err != nil {
t.Fatalf(err.Error())
}
}
// ********************************
// * 5.7 Retrieve Subscriptions
// ********************************
func TestNewRetrieveAllSubsRequest(t *testing.T) {
expected := "<iq type=\"get\" id=\"subscriptions1\" to=\"pubsub.shakespeare.lit\"> " +
"<pubsub xmlns=\"http://jabber.org/protocol/pubsub\"> <subscriptions></subscriptions> </pubsub> </iq>"
subR, err := stanza.NewRetrieveAllSubsRequest("pubsub.shakespeare.lit")
if err != nil {
t.Fatalf("failed to create a get all subs request: %v", err)
}
subR.Id = "subscriptions1"
if _, e := checkMarshalling(t, subR); e != nil {
t.Fatalf("Failed to check marshalling for generated del item request : %s", e)
}
data, err := xml.Marshal(subR)
if err := compareMarshal(expected, string(data)); err != nil {
t.Fatalf(err.Error())
}
}
func TestRetrieveAllSubsResp(t *testing.T) {
response := `
<iq type="result" from="pubsub.shakespeare.lit" to="francisco@denmark.lit" id="subscriptions1">
<pubsub xmlns="http://jabber.org/protocol/pubsub">
<subscriptions>
<subscription node="node1" jid="francisco@denmark.lit" subscription="subscribed"/>
<subscription node="node2" jid="francisco@denmark.lit" subscription="subscribed"/>
<subscription node="node5" jid="francisco@denmark.lit" subscription="unconfigured"/>
<subscription node="node6" jid="francisco@denmark.lit" subscription="subscribed"
subid="123-abc"/>
<subscription node="node6" jid="francisco@denmark.lit" subscription="subscribed"
subid="004-yyy"/>
</subscriptions>
</pubsub>
</iq>
`
var respIQ stanza.IQ
err := xml.Unmarshal([]byte(response), &respIQ)
if err != nil {
t.Fatalf("could not unmarshal response: %s", err)
}
pubsub, ok := respIQ.Payload.(*stanza.PubSubGeneric)
if !ok {
t.Fatalf("umarshalled payload is not a pubsub")
}
if pubsub.Subscriptions == nil {
t.Fatalf("subscriptions node is nil")
}
if len(pubsub.Subscriptions.List) != 5 {
t.Fatalf("incorrect number of decoded subscriptions")
}
}
// ********************************
// * 5.7 Retrieve Affiliations
// ********************************
func TestNewRetrieveAllAffilsRequest(t *testing.T) {
expected := "<iq type=\"get\"id=\"affil1\"to=\"pubsub.shakespeare.lit\"> " +
"<pubsub xmlns=\"http://jabber.org/protocol/pubsub\"> <affiliations></affiliations> </pubsub> </iq>"
subR, err := stanza.NewRetrieveAllAffilsRequest("pubsub.shakespeare.lit")
if err != nil {
t.Fatalf("failed to create a get all affiliations request: %v", err)
}
subR.Id = "affil1"
if _, e := checkMarshalling(t, subR); e != nil {
t.Fatalf("Failed to check marshalling for generated retreive all affiliations request : %s", e)
}
data, err := xml.Marshal(subR)
if err := compareMarshal(expected, string(data)); err != nil {
t.Fatalf(err.Error())
}
}
func TestRetrieveAllAffilsResp(t *testing.T) {
response := `
<iq type="result" from="pubsub.shakespeare.lit" to="francisco@denmark.lit" id="affil1">
<pubsub xmlns="http://jabber.org/protocol/pubsub">
<affiliations>
<affiliation node="node1" affiliation="owner"/>
<affiliation node="node2" affiliation="publisher"/>
<affiliation node="node5" affiliation="outcast"/>
<affiliation node="node6" affiliation="owner"/>
</affiliations>
</pubsub>
</iq>
`
var respIQ stanza.IQ
err := xml.Unmarshal([]byte(response), &respIQ)
if err != nil {
t.Fatalf("could not unmarshal response: %s", err)
}
pubsub, ok := respIQ.Payload.(*stanza.PubSubGeneric)
if !ok {
t.Fatalf("umarshalled payload is not a pubsub")
}
if pubsub.Affiliations == nil {
t.Fatalf("subscriptions node is nil")
}
if len(pubsub.Affiliations.List) != 4 {
t.Fatalf("incorrect number of decoded subscriptions")
}
}
func getPubSubGenericPayload(response string) (*stanza.PubSubGeneric, error) {
var respIQ stanza.IQ
err := xml.Unmarshal([]byte(response), &respIQ)
if err != nil {
return &stanza.PubSubGeneric{}, err
}
pubsub, ok := respIQ.Payload.(*stanza.PubSubGeneric)
if !ok {
return nil, errors.New("this iq payload is not a pubsub")
}
return pubsub, nil
}