8c370c7066871647306e14fa41e536b265cbe706
[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
35 void __transaction_header_cb(http_transaction_h transaction, char *header, size_t header_len, void *user_data)
36 {
37         PRG("transaction_header_cb", transaction);
38 }
39
40 void __transaction_body_cb(http_transaction_h transaction, char *body, size_t size, size_t nmemb, void *user_data)
41 {
42         PRG("transaction_body_cb", transaction);
43         int written = size * nmemb;
44         DBG("Received: %d\n", written);
45 }
46
47 void __transaction_write_cb(http_transaction_h transaction, int recommended_chunk_size, void *user_data)
48 {
49         PRG("transaction_write_cb", transaction);
50         DBG("recommended_chunk_size:%d\n", recommended_chunk_size);
51 }
52
53 void __transaction_completed_cb(http_transaction_h transaction, void *user_data)
54 {
55         PRG("transaction_completed_cb", transaction);
56
57         http_status_code_e status = 0;
58         int ret;
59         char *uri = NULL;
60
61         ret = http_transaction_request_get_uri(transaction, &uri);
62         ret = http_transaction_response_get_status_code(transaction, &status);
63
64         http_transaction_header_remove_field(transaction, "Content-Length");
65         DBG("%s - status(%d)\n", uri, status);
66         ret = http_transaction_destroy(transaction);
67         if (ret == HTTP_ERROR_NONE) DBG("Success to close transaction\n");
68         else DBG("Fail to close transaction\n");
69 }
70
71 void __transaction_aborted_cb(http_transaction_h transaction, int reason, void *user_data)
72 {
73         PRG("transaction_aborted_cb", transaction);
74         DBG("aborted reason: %d\n", reason);
75 }
76
77 void _register_callbacks(http_transaction_h transaction)
78 {
79         http_transaction_set_received_header_cb(transaction, __transaction_header_cb, NULL);
80         http_transaction_set_received_body_cb(transaction, __transaction_body_cb, NULL);
81         http_transaction_set_uploaded_cb(transaction, __transaction_write_cb, NULL);
82         http_transaction_set_completed_cb(transaction, __transaction_completed_cb, NULL);
83         http_transaction_set_aborted_cb(transaction, __transaction_aborted_cb, NULL);
84 }
85
86 int test_http_init(void)
87 {
88         int ret = http_init();
89         if (ret == HTTP_ERROR_NONE)
90                 return 1;
91         else return 0;
92 }
93
94 int test_http_deinit(void)
95 {
96         int ret = http_deinit();
97         if (ret == HTTP_ERROR_NONE)
98                 return 1;
99         else return 0;
100 }
101
102 int test_http_session_create(void)
103 {
104         int ret;
105         int session_mode;
106
107         printf("Select session mode(0: NORMAL 1: PIPELINING): ");
108         ret = scanf("%d", &session_mode);
109
110         ret = http_session_create(session_mode, &session);
111         if (ret != HTTP_ERROR_NONE) {
112                 ERR("Fail to create session", ret);
113                 return 0;
114         }
115         return 1;
116 }
117
118 int test_http_session_destroy(void)
119 {
120         int ret = http_session_destroy(session);
121         if (ret != HTTP_ERROR_NONE) {
122                 ERR("Fail to destroy session", ret);
123                 return 0;
124         }
125
126         return 1;
127 }
128
129 int test_simple_get(void)
130 {
131         char uri[1024];
132         int ret;
133         http_transaction_h transaction = NULL;
134         http_method_e method;
135
136         printf("Input uri: ");
137         ret = scanf("%1023s", uri);
138
139         ret = http_session_open_transaction(session, HTTP_METHOD_GET, &transaction);
140         if (ret != 0) {
141                 ERR("Fail to open transaction", ret);
142                 return 0;
143         }
144
145         http_transaction_request_get_method(transaction, &method);
146         ret = http_transaction_request_set_uri(transaction, uri);
147         if (ret != 0) {
148                 ERR("Fail to set URI", ret);
149                 return 0;
150         }
151
152         _register_callbacks(transaction);
153         ret = http_transaction_submit(transaction);
154
155         if (ret != 0) {
156                 ERR("Fail to submit transaction", ret);
157                 return 0;
158         }
159
160         return 1;
161 }
162
163 int test_multiple_get(void)
164 {
165         int ret;
166         http_transaction_h transaction[10];
167         int count;
168         int i;
169
170         printf("Input count of transactions(1~10): ");
171         ret = scanf("%d", &count);
172
173         for (i = 0; i < count; i++) {
174                 char uri[1024];
175                 printf("Input uri for transaction[%d]: ", i + 1);
176                 ret = scanf("%1023s", uri);
177                 ret = http_session_open_transaction(session, HTTP_METHOD_GET, &transaction[i]);
178                 if (ret != 0) {
179                         ERR("Fail to open transaction", ret);
180                         return 0;
181                 }
182                 ret = http_transaction_request_set_uri(transaction[i], uri);
183                 if (ret != 0) {
184                         ERR("Fail to set URI", ret);
185                         return 0;
186                 }
187                 _register_callbacks(transaction[i]);
188                 http_transaction_submit(transaction[i]);
189         }
190
191         return 1;
192 }
193
194 int test_simple_post(void)
195 {
196         int ret;
197         http_transaction_h transaction;
198         const char* post_msg = "name=tizen&project=capi-network-curl";
199         char field_value[15];
200
201         ret = http_session_open_transaction(session, HTTP_METHOD_POST, &transaction);
202         if (ret != 0) {
203                 ERR("Fail to open transaction", ret);
204                 return 0;
205         }
206         ret = http_transaction_request_set_uri(transaction, "http://posttestserver.com/post.php");
207         if (ret != 0) {
208                 ERR("Fail to set URI", ret);
209                 return 0;
210         }
211         http_transaction_set_ready_to_write(transaction, TRUE);
212         http_transaction_request_write_body(transaction, post_msg);
213
214         sprintf(field_value, "%d", strlen(post_msg));
215         printf("[dbg] post size (%s)\n", field_value);
216         http_transaction_header_add_field(transaction, "Content-Length", field_value);
217
218         _register_callbacks(transaction);
219         http_transaction_submit(transaction);
220
221         return 1;
222 }
223
224 gboolean test_thread(GIOChannel *source, GIOCondition condition, gpointer data)
225 {
226         int rv;
227         char a[10];
228
229         printf("Event received from stdin\n");
230
231         rv = read(0, a, 10);
232
233         if (rv <= 0 || a[0] == '0')
234                 exit(1);
235
236         if (a[0] == '\n' || a[0] == '\r') {
237                 printf("\n\n Network Connection API Test App\n\n");
238                 printf("Options..\n");
239                 printf("1       - Initialize\n");
240                 printf("2       - Deinitialize\n");
241                 printf("3       - Create session\n");
242                 printf("4       - Destroy session\n");
243                 printf("5       - Simple GET\n");
244                 printf("6       - Multiple GET\n");
245                 printf("7       - Simple POST\n");
246                 printf("8       - \n");
247                 printf("0       - Exit \n");
248                 printf("ENTER  - Show options menu.......\n");
249         }
250
251         switch (a[0]) {
252         case '1':
253                 rv = test_http_init();
254                 break;
255         case '2':
256                 rv = test_http_deinit();
257                 break;
258         case '3':
259                 rv = test_http_session_create();
260                 break;
261         case '4':
262                 rv = test_http_session_destroy();
263                 break;
264         case '5':
265                 rv = test_simple_get();
266                 break;
267         case '6':
268                 rv = test_multiple_get();
269                 break;
270         case '7':
271                 rv = test_simple_post();
272                 break;
273         }
274
275         if (rv == 1)
276                 printf("Operation succeeded!\n");
277         else
278                 printf("Operation failed!\n");
279
280         return true;
281 }
282
283 int main(int argc, char **argv)
284 {
285         GMainLoop *mainloop;
286
287 #if !GLIB_CHECK_VERSION(2, 36, 0)
288         g_type_init();
289 #endif
290         mainloop = g_main_loop_new(NULL, false);
291
292         GIOChannel *channel = g_io_channel_unix_new(0);
293         g_io_add_watch(channel, (G_IO_IN|G_IO_ERR|G_IO_HUP|G_IO_NVAL), test_thread, NULL);
294         printf("Test Thread created...\n");
295         g_main_loop_run(mainloop);
296
297         return 0;
298 }
299
300
301