-
Notifications
You must be signed in to change notification settings - Fork 1
/
client-test.lisp
158 lines (140 loc) · 8.23 KB
/
client-test.lisp
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
(defpackage #:client-test
(:use #:CL #:lisp-unit)
(:import-from #:github-client
#:token-p
#:token
#:token-p-or-input
#:api-client
#:http-call
#:github-api-call))
(in-package #:client-test)
(define-test token-p-test
(assert-false (token-p
(make-instance 'api-client)))
(assert-true (token-p
(make-instance 'api-client :token "123"))))
;; make fake http server and receive http-call
(define-test http-call-test
(let (handler
env
(clt (make-instance 'api-client
:token "123")))
(unwind-protect
(progn (setf handler
(clack:clackup
(lambda (request)
(setf env request) ;; update env
(list 200
'(:content-type "text/plain")
(list (gethash "authorization" (car (last env))))))
:server :woo))
;; default method is :GET
(assert-equal :GET
(progn (http-call clt "http://127.0.0.1:5000")
(alexandria:doplist (k v env)
(if (eq k :REQUEST-METHOD)
(return v)))))
;; give some method
(assert-equal :DELETE
(progn (http-call clt
"http://127.0.0.1:5000"
:method "delete")
(alexandria:doplist (k v env)
(if (eq k :REQUEST-METHOD)
(return v)))))
;; give token
(assert-equal "token 456"
(car (multiple-value-list
(http-call clt
"http://127.0.0.1:5000"
:token "456"))))
;; don't give token, use client token
(assert-equal "token 123"
(car (multiple-value-list
(http-call clt
"http://127.0.0.1:5000"))))
;; :content keyword will give the body
(assert-equal "this is content"
(progn (http-call clt
"http://127.0.0.1:5000"
:method "post"
:content "this is content")
(let* ((content-length (gethash :CONTENT-LENGTH
(alexandria:plist-hash-table env)))
(content (make-array content-length
:element-type 'flexi-streams:octet)))
(read-sequence content (gethash :RAW-BODY
(alexandria:plist-hash-table env)))
(flexi-streams:octets-to-string content)
)))
(assert-equal ""
(progn (http-call clt
"http://127.0.0.1:5000"
:method "post") ;; empty content
(let* ((content-length (gethash :CONTENT-LENGTH
(alexandria:plist-hash-table env)))
(content (make-array content-length
:element-type 'flexi-streams:octet)))
(read-sequence content (gethash :RAW-BODY
(alexandria:plist-hash-table env)))
(flexi-streams:octets-to-string content)
)))
;; check the accept headers
(assert-equal "application/vnd.github+json"
(progn (http-call clt
"http://127.0.0.1:5000"
:method "post"
:content "this is content")
(gethash "accept" (gethash :headers (alexandria:plist-hash-table env)))
))
;; check the given headers
(http-call clt
"http://127.0.0.1:5000"
:method "post"
:content "this is content"
:headers '(("a" . "b")))
(assert-equal "b" (gethash "a" (gethash :headers (alexandria:plist-hash-table env))))
(assert-equal "application/vnd.github+json"
(gethash "accept" (gethash :headers (alexandria:plist-hash-table env))))
;; :content keyword directly give to dexador
(assert-equal "key=value&key1=1"
(progn (apply #'http-call
clt
"http://127.0.0.1:5000"
:method "post"
'(:content (("key" . "value") ("key1" . 1)))) ;; use apply rather than directly call
(let* ((content-length (gethash :CONTENT-LENGTH
(alexandria:plist-hash-table env)))
(content (make-array content-length
:element-type 'flexi-streams:octet)))
(read-sequence content (gethash :RAW-BODY
(alexandria:plist-hash-table env)))
(flexi-streams:octets-to-string content)
)))
;; other methods won't give content to server
(assert-equal ""
(progn (http-call clt
"http://127.0.0.1:5000"
:method "get"
:content "this is content")
(let* ((content-length (gethash :CONTENT-LENGTH
(alexandria:plist-hash-table env)))
(content (make-array content-length
:element-type 'flexi-streams:octet)))
(read-sequence content (gethash :RAW-BODY
(alexandria:plist-hash-table env)))
(flexi-streams:octets-to-string content)
)))
;; give username and passd
(setf (github-client::token clt) "") ;; empty token first
(assert-equal (format nil "Basic ~a"
(cl-base64:string-to-base64-string "aa:bb"))
(car (multiple-value-list
(http-call clt
"http://127.0.0.1:5000"
:user-name "aa"
:passd "bb")))))
(clack:stop handler))))
(let ((*print-errors* t)
(*print-failures* t))
(run-tests :all :client-test))