bc89868903ceab05ac82b3007c6ea52c670265f9
[nit.git] / tests / test_curl.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Copyright 2013 Matthieu Lucas <lucasmatthieu@gmail.com>
4 # Copyright 2013 Alexis Laferrière <alexis.laf@xymus.net>
5 #
6 # Licensed under the Apache License, Version 2.0 (the "License");
7 # you may not use this file except in compliance with the License.
8 # You may obtain a copy of the License at
9 #
10 # http://www.apache.org/licenses/LICENSE-2.0
11 #
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS,
14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 # See the License for the specific language governing permissions and
16 # limitations under the License.
17 module test_curl
18
19 import curl
20
21 fun error_manager(err: CURLCode) do if not err.is_ok then print err
22
23 var url = "http://example.org/"
24
25 var curl = new CCurl.easy_init
26 if not curl.is_init then print "failed init"
27
28 var error:CURLCode
29 error = curl.easy_setopt(new CURLOption.url, url)
30 error_manager(error)
31
32 error = curl.easy_setopt(new CURLOption.verbose, 1)
33 error_manager(error)
34
35 error = curl.easy_perform
36 error_manager(error)
37
38 # Long set
39 var info:nullable CURLInfoResponseLong
40 info = curl.easy_getinfo_long(new CURLInfoLong.header_size)
41 assert infoResp:info != null
42 print "GetinfoLong:: Header size: " + info.response.to_s
43
44 info = curl.easy_getinfo_long(new CURLInfoLong.response_code)
45 assert infoResp:info != null
46 print "GetinfoLong:: Response code: " + info.response.to_s
47
48 info = curl.easy_getinfo_long(new CURLInfoLong.http_connectcode)
49 assert infoResp:info != null
50 print "GetinfoLong:: http_connectcode: " + info.response.to_s
51
52 info = curl.easy_getinfo_long(new CURLInfoLong.filetime)
53 assert infoResp:info != null
54 print "GetinfoLong:: filetime: " + info.response.to_s
55
56 info = curl.easy_getinfo_long(new CURLInfoLong.redirect_count)
57 assert infoResp:info != null
58 print "GetinfoLong:: redirect_count: " + info.response.to_s
59
60 info = curl.easy_getinfo_long(new CURLInfoLong.request_size)
61 assert infoResp:info != null
62 print "GetinfoLong:: request_size: " + info.response.to_s
63
64 info = curl.easy_getinfo_long(new CURLInfoLong.ssl_verifyresult)
65 assert infoResp:info != null
66 print "GetinfoLong:: ssl_verifyresult: " + info.response.to_s
67
68 info = curl.easy_getinfo_long(new CURLInfoLong.httpauth_avail)
69 assert infoResp:info != null
70 print "GetinfoLong:: httpauth_avail: " + info.response.to_s
71
72 info = curl.easy_getinfo_long(new CURLInfoLong.proxyauth_avail)
73 assert infoResp:info != null
74 print "GetinfoLong:: proxyauth_avail: " + info.response.to_s
75
76 info = curl.easy_getinfo_long(new CURLInfoLong.os_errno)
77 assert infoResp:info != null
78 print "GetinfoLong:: os_errno: " + info.response.to_s
79
80 info = curl.easy_getinfo_long(new CURLInfoLong.primary_port)
81 assert infoResp:info != null
82 print "GetinfoLong:: primary_port: " + info.response.to_s
83
84 info = curl.easy_getinfo_long(new CURLInfoLong.num_connects)
85 assert infoResp:info != null
86 print "GetinfoLong:: num_connects: " + info.response.to_s
87
88 info = curl.easy_getinfo_long(new CURLInfoLong.local_port)
89 assert infoResp:info != null
90 print "GetinfoLong:: local_port: " + info.response.to_s
91
92 info = curl.easy_getinfo_long(new CURLInfoLong.lastsocket)
93 assert infoResp:info != null
94 print "GetinfoLong:: lastsocket: " + info.response.to_s
95
96 info = curl.easy_getinfo_long(new CURLInfoLong.condition_unmet)
97 assert infoResp:info != null
98 print "GetinfoLong:: condition_unmet: " + info.response.to_s
99
100 info = curl.easy_getinfo_long(new CURLInfoLong.rtsp_client_cseq)
101 assert infoResp:info != null
102 print "GetinfoLong:: rtsp_client_cseq: " + info.response.to_s
103
104 info = curl.easy_getinfo_long(new CURLInfoLong.rtsp_server_cseq)
105 assert infoResp:info != null
106 print "GetinfoLong:: rtsp_server_cseq: " + info.response.to_s
107
108 info = curl.easy_getinfo_long(new CURLInfoLong.rtsp_cseq_recv)
109 assert infoResp:info != null
110 print "GetinfoLong:: rtsp_cseq_recv: " + info.response.to_s
111
112 # Double
113 var infoDouble: nullable CURLInfoResponseDouble
114 infoDouble = curl.easy_getinfo_double(new CURLInfoDouble.total_time)
115 assert infoResp:infoDouble != null
116 print "GetinfoDouble:: total_time: " + infoDouble.response.to_s
117
118 infoDouble = curl.easy_getinfo_double(new CURLInfoDouble.namelookup_time)
119 assert infoResp:infoDouble != null
120 print "GetinfoDouble:: namelookup_time: " + infoDouble.response.to_s
121
122 infoDouble = curl.easy_getinfo_double(new CURLInfoDouble.connect_time)
123 assert infoResp:infoDouble != null
124 print "GetinfoDouble:: connect_time: " + infoDouble.response.to_s
125
126 infoDouble = curl.easy_getinfo_double(new CURLInfoDouble.appconnect_time)
127 assert infoResp:infoDouble != null
128 print "GetinfoDouble:: appconnect_time: " + infoDouble.response.to_s
129
130 infoDouble = curl.easy_getinfo_double(new CURLInfoDouble.pretransfer_time)
131 assert infoResp:infoDouble != null
132 print "GetinfoDouble:: pretransfer_time: " + infoDouble.response.to_s
133
134 infoDouble = curl.easy_getinfo_double(new CURLInfoDouble.starttransfer_time)
135 assert infoResp:infoDouble != null
136 print "GetinfoDouble:: starttransfer_time: " + infoDouble.response.to_s
137
138 infoDouble = curl.easy_getinfo_double(new CURLInfoDouble.redirect_time)
139 assert infoResp:infoDouble != null
140 print "GetinfoDouble:: redirect_time: " + infoDouble.response.to_s
141
142 infoDouble = curl.easy_getinfo_double(new CURLInfoDouble.size_upload)
143 assert infoResp:infoDouble != null
144 print "GetinfoDouble:: size_upload: " + infoDouble.response.to_s
145
146 infoDouble = curl.easy_getinfo_double(new CURLInfoDouble.size_download)
147 assert infoResp:infoDouble != null
148 print "GetinfoDouble:: size_download: " + infoDouble.response.to_s
149
150 infoDouble = curl.easy_getinfo_double(new CURLInfoDouble.speed_download)
151 assert infoResp:infoDouble != null
152 print "GetinfoDouble:: speed_download: " + infoDouble.response.to_s
153
154 infoDouble = curl.easy_getinfo_double(new CURLInfoDouble.speed_upload)
155 assert infoResp:infoDouble != null
156 print "GetinfoDouble:: speed_upload: " + infoDouble.response.to_s
157
158 infoDouble = curl.easy_getinfo_double(new CURLInfoDouble.content_length_download)
159 assert infoResp:infoDouble != null
160 print "GetinfoDouble:: content_length_download: " + infoDouble.response.to_s
161
162 infoDouble = curl.easy_getinfo_double(new CURLInfoDouble.content_length_upload)
163 assert infoResp:infoDouble != null
164 print "GetinfoDouble:: content_length_upload: " + infoDouble.response.to_s
165
166 # String set
167 var infoStr:nullable CURLInfoResponseString
168 infoStr = curl.easy_getinfo_chars(new CURLInfoChars.content_type)
169 assert infoResp:infoStr != null
170 print "GetinfoStr:: Content type: " + infoStr.response
171
172 infoStr = curl.easy_getinfo_chars(new CURLInfoChars.effective_url)
173 assert infoResp:infoStr != null
174 print "GetinfoStr:: Effective url: " + infoStr.response
175
176 infoStr = curl.easy_getinfo_chars(new CURLInfoChars.redirect_url)
177 assert infoResp:infoStr != null
178 print "GetinfoStr:: Redirect url: " + infoStr.response
179
180 infoStr = curl.easy_getinfo_chars(new CURLInfoChars.primary_ip)
181 assert infoResp:infoStr != null
182 print( "GetinfoStr:: primary_ip not empty: " + (not infoStr.response.is_empty).to_s)
183
184 infoStr = curl.easy_getinfo_chars(new CURLInfoChars.local_ip)
185 assert infoResp:infoStr != null
186 print( "GetinfoStr:: local_ip not empty: " + (not infoStr.response.is_empty).to_s)
187
188 infoStr = curl.easy_getinfo_chars(new CURLInfoChars.ftp_entry_path)
189 assert infoResp:infoStr != null
190 print "GetinfoStr:: ftp_entry_path: " + infoStr.response
191
192 infoStr = curl.easy_getinfo_chars(new CURLInfoChars.private_data)
193 assert infoResp:infoStr != null
194 print "GetinfoStr:: private_data: " + infoStr.response
195
196 infoStr = curl.easy_getinfo_chars(new CURLInfoChars.rtsp_session_id)
197 assert infoResp:infoStr != null
198 print "GetinfoStr:: rtsp_session_id: " + infoStr.response
199
200 # CURLSList set
201 var infoList:nullable CURLInfoResponseArray
202 infoList = curl.easy_getinfo_slist(new CURLInfoSList.ssl_engines)
203 assert infoResp:infoList != null
204 print "GetSList:: ssl_engines: " + infoList.response.to_s
205
206 infoList = curl.easy_getinfo_slist(new CURLInfoSList.cookielist)
207 assert infoResp:infoList != null
208 print "GetSList:: cookielist: " + infoList.response.to_s
209
210 # CURLSList to Array
211 var mailList = new CURLSList.with_str("titi")
212 mailList.append("toto")
213 mailList.append("toto2")
214 mailList.append("toto3")
215 mailList.append("toto4")
216 mailList.append("toto9")
217 if mailList.is_init then
218 var content = mailList.to_a
219 print "CURLSList to array - content: {content.to_s}"
220 print "CURLSList to array - length: {content.length.to_s}"
221 mailList.destroy
222 else
223 print "CURLSList to array: CURLSList wrong init"
224 end
225
226 # CURLSList from Array
227 var mailRecipientsArray = new Array[String]
228 mailRecipientsArray.add("tata")
229 mailRecipientsArray.add("tata2")
230 var mailRecipientsList: CURLSList = mailRecipientsArray.to_curlslist
231 if mailRecipientsList.is_init then
232 print "Array to CURLSList - content: {mailRecipientsList.to_a.to_s}"
233 print "Array to CURLSList - length: {mailRecipientsList.to_a.length.to_s}"
234 mailRecipientsList.destroy
235 else
236 print "CURLSList to array: CURLSList wrong init"
237 end
238
239 # HashMap Refines
240 var hashMapRefined = new HeaderMap
241 hashMapRefined["hello"] = "toto"
242 hashMapRefined["hello"] = "tata"
243 hashMapRefined["allo"] = "foo"
244 print hashMapRefined.to_url_encoded(new CCurl.easy_init)