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