-
Notifications
You must be signed in to change notification settings - Fork 4
/
grapnel_test.el
194 lines (183 loc) · 8.52 KB
/
grapnel_test.el
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
(require 'ert)
(require 'grapnel)
(ert-deftest grapnel-test-well-documented ()
(let ((needs-docs nil))
(mapatoms (lambda (x)
(when (and (fboundp x)
(string-match "^grapnel" (symbol-name x))
(or (not (documentation x t))
(string= "" (s-trim (documentation x t)))))
(setq needs-docs (cons (symbol-name x) needs-docs)))))
(should (eql nil (sort needs-docs 'string<)))))
(ert-deftest grapnel-test-url-escape ()
(should (equal (downcase "fire%20%3d%3f%2bfly")
(downcase (grapnel-url-escape "fire =?+fly")))))
(ert-deftest grapnel-test-format-params ()
(should (equal "fire=fly&brown=coat"
(grapnel-format-params '(("fire" . "fly")
("brown" . "coat"))))))
(ert-deftest grapnel-test-command ()
(let ((grapnel-program "curl"))
(should (equal (concat "curl --include --silent --request GET"
" http\\://www.google.com")
(grapnel-command "http://www.google.com")))
(should (equal (concat "curl --header Content-Length\\:\\ 0 --include"
" --silent --request POST http\\://www.google.com")
(grapnel-command "http://www.google.com" "POST")))
(should (equal (concat "curl --header Content-Length\\:\\ 0 --include"
" --silent --request POST"
" http\\://www.google.com\\?q\\=serenity")
(grapnel-command "http://www.google.com" "POST"
'(("q" . "serenity")))))
(let* ((data-file (expand-file-name (grapnel-prepare-data-file
"1"))))
(unwind-protect
(progn
(should (equal
(concat "curl --header Content-Length\\:\\ 1 --include"
" --silent --request POST --data @" data-file
" http\\://www.google.com\\?q\\=serenity")
(grapnel-command "http://www.google.com" "POST"
'(("q" . "serenity"))
data-file)))
(should (equal
(concat "curl --header Content-Length\\:\\ 1"
" --header header\\:\\ value --include --silent"
" --request POST --data @" data-file
" http\\://www.google.com\\?q\\=serenity")
(grapnel-command "http://www.google.com" "POST"
'(("q" . "serenity"))
data-file
'(("header" . "value"))))))
(condition-case err
(delete-file data-file)
(error nil))))))
(ert-deftest grapnel-test-parse-headers ()
(let ((headers (concat
"HTTP/1.1 200 OK\n"
"Date: Sat, 23 Mar 2013 21:49:44 GMT\n"
"Expires: -1\n"
"Cache-Control: private, max-age=0\n"
"Content-Type: text/html; charset=ISO-8859-1\n")))
(should (equal (grapnel-parse-headers headers)
'(("response-code" 200)
("Date" "Sat, 23 Mar 2013 21:49:44 GMT")
("Expires" "-1")
("Cache-Control" "private, max-age=0")
("Content-Type" "text/html; charset=ISO-8859-1"))))))
(ert-deftest grapnel-test-parse-headers-continue ()
(with-temp-buffer
(insert (concat
"HTTP/1.1 100 Continue\n"
"\n"
"HTTP/1.1 200 OK\n"
"Date: Sat, 23 Mar 2013 21:49:44 GMT\n"
"Expires: -1\n"
"Cache-Control: private, max-age=0\n"
"Content-Type: text/html; charset=ISO-8859-1\n"
"\n"))
(goto-char (point-min))
(should (equal (grapnel-parse-headers (grapnel-response-headers))
'(("response-code" 200)
("Date" "Sat, 23 Mar 2013 21:49:44 GMT")
("Expires" "-1")
("Cache-Control" "private, max-age=0")
("Content-Type" "text/html; charset=ISO-8859-1"))))))
(ert-deftest grapnel-test-response-headers ()
(let ((headers (concat
"HTTP/1.1 200 OK\n"
"Date: Sat, 23 Mar 2013 21:49:44 GMT\n"
"Expires: -1\n"
"Cache-Control: private, max-age=0\n"
"Content-Type: text/html; charset=ISO-8859-1\n")))
(with-temp-buffer
(insert headers
"\n"
"body")
(should (equal headers (grapnel-response-headers))))))
(ert-deftest grapnel-test-callback-dispatch ()
(let ((dispatch '((201 . (lambda (_ _) "201"))
(success . (lambda (_ _) "success"))
(failure . (lambda (_ _) "failure"))
(complete . (lambda (_ _) "complete"))
(error . (lambda (_ _) "error"))))
(complete '((201 . (lambda (_ _) 201))
(complete . (lambda (_ _) "complete"))
(error . (lambda (_ _) "error")))))
(should (equal "success"
(grapnel-callback-dispatch dispatch 0 1
'(("response-code" 200)))))
(should (equal "201"
(grapnel-callback-dispatch dispatch 0 1
'(("response-code" 201)))))
(should (equal "failure"
(grapnel-callback-dispatch dispatch 0 1
'(("response-code" 400)))))
(should (equal "error" (grapnel-callback-dispatch dispatch 1 1 nil)))
(should (equal "complete"
(grapnel-callback-dispatch complete 0 1
'(("response-code" 200)))))
(should (equal "complete"
(grapnel-callback-dispatch complete 0 1
'(("response-code" 400)))))
;; TODO: figure out how to test that a warning occurs
;;(grapnel-callback-dispatch '() 1 1 '())
))
(defun grapnel-test-callback (filename resp headers)
"Create the file that the test looks for"
(with-temp-file filename
(insert resp)))
(ert-deftest grapnel-test-retrieve-url-sync ()
(should
(equal "success"
(grapnel-retrieve-url-sync
"www.google.com"
'((success . (lambda (resp hdrs) "success"))
(failure . (lambda (&rest args) "failure")))))))
(ert-deftest grapnel-test-retrieve-url ()
(let* ((tmp-file (make-temp-file "grapnel_test" nil "tmp"))
(tries 10))
(unwind-protect
(progn
(grapnel-retrieve-url
"www.google.com"
`((success . ,(apply-partially 'grapnel-test-callback tmp-file))
(failure . (lambda (&rest args)
(should
(error "The request should have succeeded"))))))
;; kinda gross and will possibly fail on slow connections. :/
(while (and (not (file-exists-p tmp-file)) (< 0 tries))
(setq tries (- tries 1))
(sleep-for 0.5))
(should (file-exists-p tmp-file)))
(condition-case err
(delete-file tmp-file)
(error nil)))))
(ert-deftest grapnel-test-headers ()
(let ((opera-mini-resp
(grapnel-retrieve-url-sync
"www.google.com"
`((success . (lambda (resp hdrs) resp))
(failure . (lambda (&rest args) (error "The request failed"))))
"GET"
nil
nil
'(("Accept" . (mapconcat 'identity
'("application/vnd.wap.wmlc"
"application/vnd.wap.wmlscriptc"
"image/vnd.wap.wbmp"
"application/vnd.wap.wtls-ca-certificate"
"image/gif"
"text/plain")
", "))
;; this is here just for the embedded quotes
("If-None-Match" "\"abc123\"")
("User-Agent" . "Nokia8310/1.0 (04.53)"))))
(response
(grapnel-retrieve-url-sync
"www.google.com"
`((success . (lambda (resp hdrs) resp))
(failure . (lambda (&rest args) (error "The request failed"))))
"GET")))
(should (string-match "WAPFORUM//DTD" opera-mini-resp))
(should (> (length response) (length opera-mini-resp)))))