Fixed doxygen comments
[platform/core/api/http.git] / test / http_test.c
1 /*
2  * Copyright (c) 2012, 2013 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17
18 #include <stdio.h>
19 #include <string.h>
20 #include <glib.h>
21 #include <gio/gio.h>
22
23 #include "http.h"
24
25 #define ERR(x, y) printf("[ERR] %s(%d)\n", x, y)
26 #define PRG(x, y) printf("[PRG] %s(%p)\n", x, y)
27 #define DBG     printf
28 #define MAX_URI_LEN 1024
29
30 FILE* fp1 = NULL;
31 FILE* fp2 = NULL;
32
33 http_session_h session = NULL;
34 void _register_callbacks(http_transaction_h transaction);
35
36 void __transaction_header_cb(http_transaction_h transaction, char *header, size_t header_len, void *user_data)
37 {
38         PRG("transaction_header_cb", transaction);
39 }
40
41 void __transaction_body_cb(http_transaction_h transaction, char *body, size_t size, size_t nmemb, void *user_data)
42 {
43         PRG("transaction_body_cb", transaction);
44         int written = size * nmemb;
45         DBG("Received: %d\n", written);
46 }
47
48 void __transaction_write_cb(http_transaction_h transaction, int recommended_chunk_size, void *user_data)
49 {
50         PRG("transaction_write_cb", transaction);
51         DBG("recommended_chunk_size:%d\n", recommended_chunk_size);
52 }
53
54 void __transaction_completed_cb(http_transaction_h transaction, void *user_data)
55 {
56         PRG("transaction_completed_cb", transaction);
57
58         http_status_code_e status = 0;
59         int ret;
60         char *uri = NULL;
61         char id[16] = {0, };
62         char pw[16] = {0, };
63
64         ret = http_transaction_request_get_uri(transaction, &uri);
65         ret = http_transaction_response_get_status_code(transaction, &status);
66         DBG("%s - status(%d)\n", uri, status);
67         if (status == HTTP_STATUS_UNAUTHORIZED) {
68                 DBG("Authentication Required\n");
69                 http_transaction_h http_auth_transaction;
70                 http_auth_scheme_e auth_scheme = HTTP_AUTH_NONE;
71
72                 http_transaction_open_authentication(transaction, &http_auth_transaction);
73                 http_transaction_get_http_auth_scheme(http_auth_transaction, &auth_scheme);
74
75                 printf("User ID: ");
76                 ret = scanf("%15s", id);
77                 printf("Password: ");
78                 ret = scanf("%15s", pw);
79
80                 http_transaction_set_credentials(http_auth_transaction, id, pw);
81                 _register_callbacks(http_auth_transaction);
82                 http_transaction_submit(http_auth_transaction);
83         }
84
85         http_transaction_header_remove_field(transaction, "Content-Length");
86         ret = http_transaction_destroy(transaction);
87         if (ret == HTTP_ERROR_NONE) DBG("Success to close transaction\n");
88         else DBG("Fail to close transaction\n");
89
90         transaction = NULL;
91 }
92
93 void __transaction_aborted_cb(http_transaction_h transaction, int reason, void *user_data)
94 {
95         PRG("transaction_aborted_cb", transaction);
96         DBG("aborted reason: %d\n", reason);
97 }
98
99 void _register_callbacks(http_transaction_h transaction)
100 {
101         http_transaction_set_received_header_cb(transaction, __transaction_header_cb, NULL);
102         http_transaction_set_received_body_cb(transaction, __transaction_body_cb, NULL);
103         http_transaction_set_uploaded_cb(transaction, __transaction_write_cb, NULL);
104         http_transaction_set_completed_cb(transaction, __transaction_completed_cb, NULL);
105         http_transaction_set_aborted_cb(transaction, __transaction_aborted_cb, NULL);
106 }
107
108 int test_http_init(void)
109 {
110         int ret = http_init();
111         if (ret == HTTP_ERROR_NONE)
112                 return 1;
113         else return 0;
114 }
115
116 int test_http_deinit(void)
117 {
118         int ret = http_deinit();
119         if (ret == HTTP_ERROR_NONE)
120                 return 1;
121         else return 0;
122 }
123
124 int test_http_session_create(void)
125 {
126         int ret;
127         int session_mode;
128
129         printf("Select session mode(0: NORMAL 1: PIPELINING): ");
130         ret = scanf("%d", &session_mode);
131
132         ret = http_session_create(session_mode, &session);
133         if (ret != HTTP_ERROR_NONE) {
134                 ERR("Fail to create session", ret);
135                 return 0;
136         }
137         return 1;
138 }
139
140 int test_http_session_destroy(void)
141 {
142         int ret = http_session_destroy(session);
143         if (ret != HTTP_ERROR_NONE) {
144                 ERR("Fail to destroy session", ret);
145                 return 0;
146         }
147
148         session = NULL;
149
150         return 1;
151 }
152
153 int test_simple_get(void)
154 {
155         char uri[1024];
156         int ret;
157         http_transaction_h transaction = NULL;
158         http_method_e method;
159
160         printf("Input uri: ");
161         ret = scanf("%1023s", uri);
162
163         ret = http_session_open_transaction(session, HTTP_METHOD_GET, &transaction);
164         if (ret != 0) {
165                 ERR("Fail to open transaction", ret);
166                 return 0;
167         }
168
169         http_transaction_request_get_method(transaction, &method);
170         ret = http_transaction_request_set_uri(transaction, uri);
171         if (ret != 0) {
172                 ERR("Fail to set URI", ret);
173                 return 0;
174         }
175
176         _register_callbacks(transaction);
177         ret = http_transaction_submit(transaction);
178
179         if (ret != 0) {
180                 ERR("Fail to submit transaction", ret);
181                 return 0;
182         }
183
184         return 1;
185 }
186
187 int test_multiple_get(void)
188 {
189         int ret;
190         http_transaction_h transaction[10];
191         int count;
192         int i;
193
194         printf("Input count of transactions(1~10): ");
195         ret = scanf("%d", &count);
196
197         for (i = 0; i < count; i++) {
198                 char uri[1024];
199                 printf("Input uri for transaction[%d]: ", i + 1);
200                 ret = scanf("%1023s", uri);
201                 ret = http_session_open_transaction(session, HTTP_METHOD_GET, &transaction[i]);
202                 if (ret != 0) {
203                         ERR("Fail to open transaction", ret);
204                         return 0;
205                 }
206                 ret = http_transaction_request_set_uri(transaction[i], uri);
207                 if (ret != 0) {
208                         ERR("Fail to set URI", ret);
209                         return 0;
210                 }
211                 _register_callbacks(transaction[i]);
212                 http_transaction_submit(transaction[i]);
213         }
214
215         return 1;
216 }
217
218 int test_simple_post(void)
219 {
220         int ret;
221         http_transaction_h transaction;
222         const char* post_msg = "name=tizen&project=capi-network-curl";
223         char field_value[15];
224
225         ret = http_session_open_transaction(session, HTTP_METHOD_POST, &transaction);
226         if (ret != 0) {
227                 ERR("Fail to open transaction", ret);
228                 return 0;
229         }
230         ret = http_transaction_request_set_uri(transaction, "http://posttestserver.com/post.php");
231         if (ret != 0) {
232                 ERR("Fail to set URI", ret);
233                 return 0;
234         }
235         http_transaction_set_ready_to_write(transaction, TRUE);
236         http_transaction_request_write_body(transaction, post_msg);
237
238         snprintf(field_value, sizeof(field_value), "%d", (int)strlen(post_msg));
239         printf("[dbg] post size (%s)\n", field_value);
240         http_transaction_header_add_field(transaction, "Content-Length", field_value);
241
242         _register_callbacks(transaction);
243         http_transaction_submit(transaction);
244
245         return 1;
246 }
247
248 int test_simple_authentication_get(void)
249 {
250         int ret;
251         http_transaction_h transaction = NULL;
252         http_method_e method;
253         char uri[1024];
254
255         ret = http_session_open_transaction(session, HTTP_METHOD_GET, &transaction);
256         if (ret != 0) {
257                 ERR("Fail to open transaction", ret);
258                 return 0;
259         }
260
261         printf("Input uri for transaction: ");
262         ret = scanf("%1023s", uri);
263
264         http_transaction_request_get_method(transaction, &method);
265         ret = http_transaction_request_set_uri(transaction, uri);
266         if (ret != 0) {
267                 ERR("Fail to set URI", ret);
268                 return 0;
269         }
270
271         _register_callbacks(transaction);
272         ret = http_transaction_submit(transaction);
273
274         if (ret != 0) {
275                 ERR("Fail to submit transaction", ret);
276                 return 0;
277         }
278
279         return 1;
280 }
281
282 gboolean test_thread(GIOChannel *source, GIOCondition condition, gpointer data)
283 {
284         int rv;
285         char a[10];
286
287         printf("Event received from stdin\n");
288
289         rv = read(0, a, 10);
290
291         if (rv <= 0 || a[0] == '0')
292                 exit(1);
293
294         if (a[0] == '\n' || a[0] == '\r') {
295                 printf("\n\n Network Connection API Test App\n\n");
296                 printf("Options..\n");
297                 printf("1       - Initialize\n");
298                 printf("2       - Deinitialize\n");
299                 printf("3       - Create session\n");
300                 printf("4       - Destroy session\n");
301                 printf("5       - Simple GET\n");
302                 printf("6       - Multiple GET\n");
303                 printf("7       - Simple POST\n");
304                 printf("8       - Simple Authentication GET\n");
305                 printf("9       - \n");
306                 printf("0       - Exit \n");
307                 printf("ENTER  - Show options menu.......\n");
308         }
309
310         switch (a[0]) {
311         case '1':
312                 rv = test_http_init();
313                 break;
314         case '2':
315                 rv = test_http_deinit();
316                 break;
317         case '3':
318                 rv = test_http_session_create();
319                 break;
320         case '4':
321                 rv = test_http_session_destroy();
322                 break;
323         case '5':
324                 rv = test_simple_get();
325                 break;
326         case '6':
327                 rv = test_multiple_get();
328                 break;
329         case '7':
330                 rv = test_simple_post();
331                 break;
332         case '8':
333                 rv = test_simple_authentication_get();
334                 break;
335         }
336
337         if (rv == 1)
338                 printf("Operation succeeded!\n");
339         else
340                 printf("Operation failed!\n");
341
342         return true;
343 }
344
345 int main(int argc, char **argv)
346 {
347         GMainLoop *mainloop;
348
349 #if !GLIB_CHECK_VERSION(2, 36, 0)
350         g_type_init();
351 #endif
352         mainloop = g_main_loop_new(NULL, false);
353
354         GIOChannel *channel = g_io_channel_unix_new(0);
355         g_io_add_watch(channel, (G_IO_IN|G_IO_ERR|G_IO_HUP|G_IO_NVAL), test_thread, NULL);
356         printf("Test Thread created...\n");
357         g_main_loop_run(mainloop);
358
359         return 0;
360 }
361
362
363