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