Merge: doc: fixed some typos and other misc. corrections
[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
18 intrude import curl
19
20 class CallbackManager
21 super CurlCallbacks
22
23 redef fun body_callback(line) do end
24 end
25
26 private fun error_manager(err: CURLCode) do if not err.is_ok then print err
27
28 var url = "http://example.org/"
29
30 var curl = new NativeCurl.easy_init
31 if not curl.is_init then print "failed init"
32
33 var error:CURLCode
34 error = curl.easy_setopt(new CURLOption.url, url)
35 error_manager(error)
36
37 # Activate for advanced debugging
38 #error = curl.easy_setopt(new CURLOption.verbose, 1)
39 #error_manager(error)
40
41 var cbManager = new CallbackManager
42 error = curl.register_callback_body(cbManager)
43 error_manager(error)
44
45 error = curl.easy_perform
46 error_manager(error)
47
48 # Long set
49 var info = curl.easy_getinfo_long(new CURLInfoLong.header_size)
50 assert infoResp:info != null
51
52 info = curl.easy_getinfo_long(new CURLInfoLong.response_code)
53 assert infoResp:info != null
54
55 info = curl.easy_getinfo_long(new CURLInfoLong.http_connectcode)
56 assert infoResp:info != null
57
58 info = curl.easy_getinfo_long(new CURLInfoLong.filetime)
59 assert infoResp:info != null
60
61 info = curl.easy_getinfo_long(new CURLInfoLong.redirect_count)
62 assert infoResp:info != null
63
64 info = curl.easy_getinfo_long(new CURLInfoLong.request_size)
65 assert infoResp:info != null
66
67 info = curl.easy_getinfo_long(new CURLInfoLong.ssl_verifyresult)
68 assert infoResp:info != null
69
70 info = curl.easy_getinfo_long(new CURLInfoLong.httpauth_avail)
71 assert infoResp:info != null
72
73 info = curl.easy_getinfo_long(new CURLInfoLong.proxyauth_avail)
74 assert infoResp:info != null
75
76 info = curl.easy_getinfo_long(new CURLInfoLong.os_errno)
77 assert infoResp:info != null
78
79 info = curl.easy_getinfo_long(new CURLInfoLong.primary_port)
80 assert infoResp:info != null
81
82 info = curl.easy_getinfo_long(new CURLInfoLong.num_connects)
83 assert infoResp:info != null
84
85 info = curl.easy_getinfo_long(new CURLInfoLong.local_port)
86 assert infoResp:info != null
87
88 info = curl.easy_getinfo_long(new CURLInfoLong.lastsocket)
89 assert infoResp:info != null
90
91 info = curl.easy_getinfo_long(new CURLInfoLong.condition_unmet)
92 assert infoResp:info != null
93
94 info = curl.easy_getinfo_long(new CURLInfoLong.rtsp_client_cseq)
95 assert infoResp:info != null
96
97 info = curl.easy_getinfo_long(new CURLInfoLong.rtsp_server_cseq)
98 assert infoResp:info != null
99
100 info = curl.easy_getinfo_long(new CURLInfoLong.rtsp_cseq_self)
101 assert infoResp:info != null
102
103 # Double
104 var infoDouble = curl.easy_getinfo_double(new CURLInfoDouble.total_time)
105 assert infoResp:infoDouble != null
106
107 infoDouble = curl.easy_getinfo_double(new CURLInfoDouble.namelookup_time)
108 assert infoResp:infoDouble != null
109
110 infoDouble = curl.easy_getinfo_double(new CURLInfoDouble.connect_time)
111 assert infoResp:infoDouble != null
112
113 infoDouble = curl.easy_getinfo_double(new CURLInfoDouble.appconnect_time)
114 assert infoResp:infoDouble != null
115
116 infoDouble = curl.easy_getinfo_double(new CURLInfoDouble.pretransfer_time)
117 assert infoResp:infoDouble != null
118
119 infoDouble = curl.easy_getinfo_double(new CURLInfoDouble.starttransfer_time)
120 assert infoResp:infoDouble != null
121
122 infoDouble = curl.easy_getinfo_double(new CURLInfoDouble.redirect_time)
123 assert infoResp:infoDouble != null
124
125 infoDouble = curl.easy_getinfo_double(new CURLInfoDouble.size_upload)
126 assert infoResp:infoDouble != null
127
128 infoDouble = curl.easy_getinfo_double(new CURLInfoDouble.size_download)
129 assert infoResp:infoDouble != null
130
131 infoDouble = curl.easy_getinfo_double(new CURLInfoDouble.speed_download)
132 assert infoResp:infoDouble != null
133
134 infoDouble = curl.easy_getinfo_double(new CURLInfoDouble.speed_upload)
135 assert infoResp:infoDouble != null
136
137 infoDouble = curl.easy_getinfo_double(new CURLInfoDouble.content_length_download)
138 assert infoResp:infoDouble != null
139
140 infoDouble = curl.easy_getinfo_double(new CURLInfoDouble.content_length_upload)
141 assert infoResp:infoDouble != null
142
143 # String set
144 var infoStr = curl.easy_getinfo_chars(new CURLInfoChars.content_type)
145 assert infoResp:infoStr != null
146
147 infoStr = curl.easy_getinfo_chars(new CURLInfoChars.effective_url)
148 assert infoResp:infoStr != null
149
150 # follow_location not set, so returns null
151 infoStr = curl.easy_getinfo_chars(new CURLInfoChars.redirect_url)
152 assert infoStr == null
153
154 infoStr = curl.easy_getinfo_chars(new CURLInfoChars.primary_ip)
155 assert infoResp:infoStr != null
156
157 infoStr = curl.easy_getinfo_chars(new CURLInfoChars.local_ip)
158 assert infoResp:infoStr != null
159
160 # Not connecting to FTP so `null`
161 infoStr = curl.easy_getinfo_chars(new CURLInfoChars.ftp_entry_path)
162 assert infoStr == null
163
164 # opt private not set nor implemented, so returns null
165 infoStr = curl.easy_getinfo_chars(new CURLInfoChars.private_data)
166 assert infoStr == null
167
168 # Not an RTSP connection so `null`
169 infoStr = curl.easy_getinfo_chars(new CURLInfoChars.rtsp_session_id)
170 assert infoStr == null
171
172 # CURLSList set
173 var infoList = curl.easy_getinfo_slist(new CURLInfoSList.ssl_engines)
174 assert infoResp:infoList != null
175
176 infoList = curl.easy_getinfo_slist(new CURLInfoSList.cookielist)
177 assert infoResp:infoList != null
178
179 # CURLSList to Array
180 var mailList = new CURLSList.with_str("titi")
181 mailList.append("toto")
182 mailList.append("toto2")
183 mailList.append("toto3")
184 mailList.append("toto4")
185 mailList.append("toto9")
186 if mailList.is_init then
187 var content = mailList.to_a
188 print "CURLSList to array - content: {content.to_s}"
189 print "CURLSList to array - length: {content.length.to_s}"
190 mailList.destroy
191 else
192 print "CURLSList to array: CURLSList wrong init"
193 end
194
195 # CURLSList from Array
196 var mailRecipientsArray = new Array[String]
197 mailRecipientsArray.add("tata")
198 mailRecipientsArray.add("tata2")
199 var mailRecipientsList: CURLSList = mailRecipientsArray.to_curlslist
200 if mailRecipientsList.is_init then
201 print "Array to CURLSList - content: {mailRecipientsList.to_a.to_s}"
202 print "Array to CURLSList - length: {mailRecipientsList.to_a.length.to_s}"
203 mailRecipientsList.destroy
204 else
205 print "CURLSList to array: CURLSList wrong init"
206 end
207
208 # HashMap Refines
209 var hashMapRefined = new HeaderMap
210 hashMapRefined["hello"] = "toto"
211 hashMapRefined["hello"] = "tata"
212 hashMapRefined["allo"] = "foo"
213 print hashMapRefined.to_url_encoded(new Curl)