summaryrefslogtreecommitdiff
path: root/spec/xmpp.scm
blob: 0b9a74b25ade684272fb7c07a202f4be9e5f9d88 (about) (plain)
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
(use-modules (gnubba xmpp))

(suite "iq/message/presence stanzas"
       (tests
        (test "iq should return a simple iq stanza" e
              (assert-equal '(*TOP* (iq (@ (to "romeo@capulet.it")
                                           (type "get")
                                           (id "1234"))
                                        "payload"))
                            (e 'iq1)))
        (test "message should return a simple message stanza" e
              (assert-equal '(*TOP* (message (@ (to "romeo@capulet.it"))
                                             (body "hello, romeo!")))
                            (e 'message1)))
        (test "presence should return a simple presence stanza" e
              (assert-equal '(*TOP* (presence (@ (id "1234"))))
                            (e 'presence1)))
        (test "presence takes optional body argument" e
              (assert-equal '(*TOP* (presence (@ (id "1234")) "body"))
                            (e 'presence2)))
        (test "iq? should return #t on an iq stanza" e
              (assert-true (iq? (e 'iq1))))
        (test "iq? should return #f on any other stanza" e
              (assert-false (iq? (e 'message1))))
        (test "message? should return #t on a message stanza" e
              (assert-true (message? (e 'message1))))
        (test "message? should return #f on any other stanza" e
              (assert-false (message? (e 'iq1))))
        (test "presence? should return #t on a presence stanza" e
              (assert-true (presence? (e 'presence1))))
        (test "presence? should return #f on any other stanza" e
              (assert-false (presence? (e 'iq1)))))
       (options)
       (setups
        (setup 'iq1       (iq "payload" #:to "romeo@capulet.it" #:id "1234"))
        (setup 'message1  (message "romeo@capulet.it" "hello, romeo!"))
        (setup 'presence1 (presence #:id "1234"))
        (setup 'presence2 (presence "body" #:id "1234"))
        (setup 'presence3 (presence "body" #:id "1234" #:from "romeo@capulet.it"))))

;; TODO
(suite "register-temp-stanza-handler-for-id" (tests))

(suite "register-stanza-handler"
       (tests
        (test "initially, *stanza-handlers* should be empty" e
              (assert-equal '() ((e 'handlers))))
        (test "after registering a handler *stanza-handlers* contains one handler" e
              (assert-equal 1
                            (begin
                              (register-stanza-handler (lambda _ 'handler1))
                              (length ((e 'handlers))))))
        (test "after registering two handlers *stanza-handlers* contains two handlers" e
              (assert-equal 2
                            (begin
                              (register-stanza-handler (lambda _ 'handler1))
                              (register-stanza-handler (lambda _ 'handler2))
                              (length ((e 'handlers))))))
        (test "handlers may have guards" e
              (assert-equal 3
                            (begin
                              (register-stanza-handler message?
                                                       (lambda _ 'handler1))
                              (register-stanza-handler iq?
                                                       (lambda _ 'handler2))
                              (register-stanza-handler #t
                                                       (lambda _ 'handler2))
                              (length ((e 'handlers)))))))
       (options)
       (setups
        (setup 'handlers  (lambda () (@@ (gnubba xmpp) *stanza-handlers*))))
       (teardowns
        ;; always reset the internal state after each test
        (teardown e (set! (@@ (gnubba xmpp) *stanza-handlers*) '()))))

(suite "try-stanza-handlers"
       (tests
        (test "a handler registered without a guard or #t is executed for any stanza" e
              (assert-equal '(handler2 handler1)
                            (begin
                              (register-stanza-handler
                               (lambda _ 'handler1))
                              (register-stanza-handler
                               #t
                               (lambda _ 'handler2))
                              ((@@ (gnubba xmpp) try-stanza-handlers) (e 'message1)))))
        (test "handlers with guards are only executed for stanzas that match (a)" e
              (assert-equal '(#f handler1)
                            (begin
                              (register-stanza-handler message?
                                                       (lambda _ 'handler1))
                              (register-stanza-handler iq?
                                                       (lambda _ 'handler2))
                              ((@@ (gnubba xmpp) try-stanza-handlers) (e 'message1)))))
        (test "handlers with guards are only executed for stanzas that match (b)" e
              (assert-equal '(handler2 #f)
                            (begin
                              (register-stanza-handler message?
                                                       (lambda _ 'handler1))
                              (register-stanza-handler iq?
                                                       (lambda _ 'handler2))
                              ((@@ (gnubba xmpp) try-stanza-handlers) (e 'iq1)))))
        (test "no handlers are executed if none of the guards match the stanza" e
              (assert-equal '(#f #f)
                            (begin
                              (register-stanza-handler message?
                                                       (lambda _ 'handler1))
                              (register-stanza-handler iq?
                                                       (lambda _ 'handler2))
                              ((@@ (gnubba xmpp) try-stanza-handlers) (e 'presence1)))))
        (test "when a handler throws 'halt no other handlers are executed" e
              (assert-equal '(handler2 handler3)
                            (let ((*executed* '()))
                              (register-stanza-handler
                               (lambda _ (set! *executed* (cons 'handler1 *executed*))))
                              (register-stanza-handler
                               (lambda _
                                 (set! *executed* (cons 'handler2 *executed*))
                                 (throw 'halt 'handler2)))
                              (register-stanza-handler
                               (lambda _ (set! *executed* (cons 'handler3 *executed*))))
                              (catch 'halt
                                (lambda _
                                  ((@@ (gnubba xmpp) try-stanza-handlers) (e 'message1)))
                                (lambda _ #t))
                              *executed*))))
       (options)
       (setups
        (setup 'iq1       (iq "payload" #:to "romeo@capulet.it" #:id "1234"))
        (setup 'message1  (message "romeo@capulet.it" "hello, romeo!"))
        (setup 'presence1 (presence #:id "1234")))
       (teardowns
        ;; always reset the internal state after each test
        (teardown e (set! (@@ (gnubba xmpp) *stanza-handlers*) '()))))

;; TODO
(suite "next-stanza-id!" (tests))
(suite "handle-stanza"
       (tests
        (test "calls id handler first, then generic handler" e
              (assert-equal
               '(generic-handler id-handler)
               (let ((results '()))
                 (register-stanza-handler
                  (lambda _ (set! results (cons 'generic-handler results))))
                 (register-temp-stanza-handler-for-id
                  (stanza-id (e 'stanza))
                  (lambda _ (set! results (cons 'id-handler results))))
                 ((@@ (gnubba xmpp) handle-stanza) (e 'stanza))
                 results)))
        (test "catches 'halt" e
              (assert-false
               (error?
                (begin
                  (register-stanza-handler
                   (lambda _ (throw 'never-executed)))
                  (register-temp-stanza-handler-for-id
                   (stanza-id (e 'stanza))
                   (lambda _ (throw 'halt)))
                  ((@@ (gnubba xmpp) handle-stanza) (e 'stanza)))))))
       (options)
       (setups
        (setup 'stanza (iq "payload" #:to "romeo@capulet.it" #:id "1234")))
       (teardowns
        ;; always reset the internal state after each test
        (teardown e (set! (@@ (gnubba xmpp) *stanza-handlers*) '()))))

(suite "run-handler-loop"
       (tests
        (test "does not die when a handler throws an exception" e
              (assert-false
               (error? (begin
                         (register-stanza-handler
                          (lambda _ (throw 'some-error "no reason")))
                         (call-with-input-string
                          "<iq from='test' id='123'/><iq from='someone' id='234'/>"
                          run-handler-loop)))))
        (test "processes all stanzas in the queue" e
              (assert-equal 2
               (let ((count 0))
                 (register-stanza-handler
                  (lambda _ (begin (set! count (1+ count))
                                   (throw 'some-error "no reason"))))
                 (call-with-input-string
                  "<iq from='test' id='123'/><iq from='someone' id='234'/>"
                  run-handler-loop)
                 count)))))