mirror of
https://gitee.com/Lyon1998/pikapython.git
synced 2025-01-29 17:22:56 +08:00
add test network
This commit is contained in:
parent
bf651c260b
commit
a96e3f5e56
196
test/network-test.cpp
Normal file
196
test/network-test.cpp
Normal file
@ -0,0 +1,196 @@
|
||||
#include "test_common.h"
|
||||
/*
|
||||
* Copyright (c) 2006-2022, RT-Thread Development Team
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Change Logs:
|
||||
* Date Author Notes
|
||||
* 2018-08-03 chenyong the first version
|
||||
*/
|
||||
|
||||
extern "C" {
|
||||
|
||||
#include "../pikascript-lib/requests/webclient.h"
|
||||
|
||||
#define GET_HEADER_BUFSZ 1024
|
||||
#define GET_RESP_BUFSZ 1024
|
||||
|
||||
#define GET_LOCAL_URI "http://www.rt-thread.com/service/rt-thread.txt"
|
||||
|
||||
/* send HTTP GET request by common request interface, it used to receive longer
|
||||
* data */
|
||||
static int webclient_get_comm(const char* uri) {
|
||||
struct webclient_session* session = RT_NULL;
|
||||
unsigned char* buffer = RT_NULL;
|
||||
int index = 0;
|
||||
int ret = 0;
|
||||
int bytes_read, resp_status;
|
||||
int content_length = -1;
|
||||
|
||||
buffer = (unsigned char*)web_malloc(GET_RESP_BUFSZ);
|
||||
if (buffer == RT_NULL) {
|
||||
rt_kprintf("no memory for receive buffer.\n");
|
||||
ret = -RT_ENOMEM;
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
/* create webclient session and set header response size */
|
||||
session = webclient_session_create(GET_HEADER_BUFSZ);
|
||||
if (session == RT_NULL) {
|
||||
ret = -RT_ENOMEM;
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
/* send GET request by default header */
|
||||
if ((resp_status = webclient_get(session, uri)) != 200) {
|
||||
rt_kprintf("webclient GET request failed, response(%d) error.\n",
|
||||
resp_status);
|
||||
ret = -RT_ERROR;
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
rt_kprintf("webclient get response data: \n");
|
||||
|
||||
content_length = webclient_content_length_get(session);
|
||||
if (content_length < 0) {
|
||||
rt_kprintf("webclient GET request type is chunked.\n");
|
||||
do {
|
||||
bytes_read = webclient_read(session, (void*)buffer, GET_RESP_BUFSZ);
|
||||
if (bytes_read <= 0) {
|
||||
break;
|
||||
}
|
||||
|
||||
for (index = 0; index < bytes_read; index++) {
|
||||
rt_kprintf("%c", buffer[index]);
|
||||
}
|
||||
} while (1);
|
||||
|
||||
rt_kprintf("\n");
|
||||
} else {
|
||||
int content_pos = 0;
|
||||
|
||||
do {
|
||||
bytes_read =
|
||||
webclient_read(session, (void*)buffer,
|
||||
content_length - content_pos > GET_RESP_BUFSZ
|
||||
? GET_RESP_BUFSZ
|
||||
: content_length - content_pos);
|
||||
if (bytes_read <= 0) {
|
||||
break;
|
||||
}
|
||||
|
||||
for (index = 0; index < bytes_read; index++) {
|
||||
rt_kprintf("%c", buffer[index]);
|
||||
}
|
||||
|
||||
content_pos += bytes_read;
|
||||
} while (content_pos < content_length);
|
||||
|
||||
rt_kprintf("\n");
|
||||
}
|
||||
|
||||
__exit:
|
||||
if (session) {
|
||||
webclient_close(session);
|
||||
}
|
||||
|
||||
if (buffer) {
|
||||
web_free(buffer);
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
/* send HTTP GET request by simplify request interface, it used to received
|
||||
* shorter data */
|
||||
static int webclient_get_smpl(const char* uri) {
|
||||
char* response = RT_NULL;
|
||||
size_t resp_len = 0;
|
||||
int index;
|
||||
|
||||
if (webclient_request(uri, RT_NULL, RT_NULL, 0, (void**)&response,
|
||||
&resp_len) < 0) {
|
||||
rt_kprintf("webclient send get request failed.");
|
||||
return -RT_ERROR;
|
||||
}
|
||||
|
||||
rt_kprintf("webclient send get request by simplify request interface.\n");
|
||||
rt_kprintf("webclient get response data: \n");
|
||||
for (index = 0; index < (int)strlen(response); index++) {
|
||||
rt_kprintf("%c", response[index]);
|
||||
}
|
||||
rt_kprintf("\n");
|
||||
|
||||
if (response) {
|
||||
web_free(response);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int webclient_get_test(int argc, char** argv) {
|
||||
char* uri = RT_NULL;
|
||||
if (argc == 1) {
|
||||
uri = web_strdup(GET_LOCAL_URI);
|
||||
if (uri == RT_NULL) {
|
||||
rt_kprintf("no memory for create get request uri buffer.\n");
|
||||
return -RT_ENOMEM;
|
||||
}
|
||||
|
||||
if (RT_EOK != webclient_get_comm(uri)) {
|
||||
return -RT_EIO;
|
||||
}
|
||||
} else if (argc == 2) {
|
||||
if (strcmp(argv[1], "-s") == 0) {
|
||||
uri = web_strdup(GET_LOCAL_URI);
|
||||
if (uri == RT_NULL) {
|
||||
rt_kprintf("no memory for create get request uri buffer.\n");
|
||||
return -RT_ENOMEM;
|
||||
}
|
||||
|
||||
if (RT_EOK != webclient_get_smpl(uri)) {
|
||||
return -RT_EIO;
|
||||
}
|
||||
} else {
|
||||
uri = web_strdup(argv[1]);
|
||||
if (uri == RT_NULL) {
|
||||
rt_kprintf("no memory for create get request uri buffer.\n");
|
||||
return -RT_ENOMEM;
|
||||
}
|
||||
if (RT_EOK != webclient_get_comm(uri)) {
|
||||
return RT_EIO;
|
||||
}
|
||||
}
|
||||
} else if (argc == 3 && strcmp(argv[1], "-s") == 0) {
|
||||
uri = web_strdup(argv[2]);
|
||||
if (uri == RT_NULL) {
|
||||
rt_kprintf("no memory for create get request uri buffer.\n");
|
||||
return -RT_ENOMEM;
|
||||
}
|
||||
|
||||
if (RT_EOK != webclient_get_smpl(uri)) {
|
||||
return -RT_EIO;
|
||||
}
|
||||
} else {
|
||||
rt_kprintf("web_get_test [URI] - webclient GET request test.\n");
|
||||
rt_kprintf(
|
||||
"web_get_test -s [URI] - webclient simplify GET request test.\n");
|
||||
return -RT_ERROR;
|
||||
}
|
||||
|
||||
if (uri) {
|
||||
web_free(uri);
|
||||
}
|
||||
|
||||
return RT_EOK;
|
||||
}
|
||||
}
|
||||
|
||||
TEST(network, get) {
|
||||
char* argv1[] = {"test", "http://www.rt-thread.com/service/rt-thread.txt"};
|
||||
EXPECT_EQ(webclient_get_test(2, argv1), 0);
|
||||
char* argv2[] = {"test", "-s",
|
||||
"http://www.rt-thread.com/service/rt-thread.txt"};
|
||||
EXPECT_EQ(webclient_get_test(3, argv2), 0);
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user