mirror of
https://gitee.com/Lyon1998/pikapython.git
synced 2025-01-15 17:02:53 +08:00
finish post test
This commit is contained in:
parent
a206227993
commit
0aaf68a189
2
port/linux/.vscode/launch.json
vendored
2
port/linux/.vscode/launch.json
vendored
@ -11,7 +11,7 @@
|
||||
"program": "${workspaceFolder}/build/test/pikascript_test",
|
||||
// "program": "${workspaceFolder}/build/boot/demo06-pikamain/pikascript_demo06-pikamain",
|
||||
"args": [
|
||||
"--gtest_filter=requests.std_request"
|
||||
"--gtest_filter=requests.post_data"
|
||||
],
|
||||
"stopAtEntry": false,
|
||||
"cwd": "${workspaceFolder}",
|
||||
|
@ -22,8 +22,6 @@ class Response:
|
||||
connect: str) -> int: ...
|
||||
|
||||
def header_write(self, header: str, value: str) -> int: ...
|
||||
# 表单数据对应input file
|
||||
def form_data(self, data: str) -> int: ...
|
||||
|
||||
def __init__(self): ...
|
||||
def __del__(self): ...
|
||||
|
@ -67,14 +67,20 @@ int _requests_Response_request(PikaObj* self,
|
||||
return -1;
|
||||
}
|
||||
ret = webclient_response(session, &resp_data, &resp_len);
|
||||
if (ret <= 0) {
|
||||
if (ret < 0) {
|
||||
return -1;
|
||||
}
|
||||
/* 正常得到了数据 */
|
||||
obj_setInt(self, "state_code", session->resp_status);
|
||||
obj_setInt(self, "status_code", session->resp_status);
|
||||
obj_setInt(self, "content_length", resp_len);
|
||||
obj_setStr(self, "text", (char*)resp_data);
|
||||
obj_setStr(self, "headers", session->header->buffer);
|
||||
/* 释放申请的缓冲区内存 */
|
||||
if (resp_data != NULL) {
|
||||
obj_setStr(self, "text", (char*)resp_data);
|
||||
web_free(resp_data);
|
||||
} else {
|
||||
obj_setStr(self, "text", "");
|
||||
}
|
||||
} else if (strEqu(method, "POST")) {
|
||||
if (data == NULL) {
|
||||
data_len = 0;
|
||||
@ -97,18 +103,25 @@ int _requests_Response_request(PikaObj* self,
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
RQ_debug("header buffer:%.4096s", session->header->buffer);
|
||||
if (webclient_post(session, url, data, data_len) != 200) {
|
||||
return -1;
|
||||
}
|
||||
ret = webclient_response(session, &resp_data, &resp_len);
|
||||
if (ret <= 0) {
|
||||
if (ret < 0) {
|
||||
return -1;
|
||||
}
|
||||
/* 正常得到了数据 */
|
||||
obj_setInt(self, "state_code", session->resp_status);
|
||||
obj_setInt(self, "status_code", session->resp_status);
|
||||
obj_setInt(self, "content_length", resp_len);
|
||||
obj_setStr(self, "text", (char*)resp_data);
|
||||
obj_setStr(self, "headers", session->header->buffer);
|
||||
/* 释放申请的缓冲区内存 */
|
||||
if (resp_data != NULL) {
|
||||
obj_setStr(self, "text", (char*)resp_data);
|
||||
web_free(resp_data);
|
||||
} else {
|
||||
obj_setStr(self, "text", "");
|
||||
}
|
||||
} else {
|
||||
return -1;
|
||||
}
|
||||
@ -116,58 +129,10 @@ int _requests_Response_request(PikaObj* self,
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 提供对 multipart/form-data的支持
|
||||
*/
|
||||
int _requests_Response_form_data(PikaObj *self, char* data)
|
||||
{
|
||||
struct webclient_session* session;
|
||||
int ret, data_len;
|
||||
|
||||
session = (struct webclient_session*)obj_getInt(self, "session_address");
|
||||
if (unlikely(session == NULL)) {
|
||||
RQ_cli("Sorry, can not operate NULL session object.\n");
|
||||
return -1;
|
||||
}
|
||||
data_len = strlen(data);
|
||||
if (unlikely(data_len == 0)) {
|
||||
RQ_cli("Sorry, data length is zero.\n");
|
||||
return -1;
|
||||
}
|
||||
RQ_debug("Add multipart/form-data fileds:");
|
||||
/* 首先添加一些固定的头 */
|
||||
if (strstr(session->header->buffer, "Connection:") == RT_NULL) {
|
||||
ret = webclient_header_fields_add(session, "Connection: keep-alive\r\n");
|
||||
if (ret < 0) {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
/**
|
||||
* FIXME: 需要寻找一种算法,但这里先展示一下功能,生成一个随机字符串
|
||||
* 32位重复可能性很小,因此失败可能性小......
|
||||
* 忽略分割字符串可能与数据重复的问题。
|
||||
*/
|
||||
/* random_string(buffer, 32); */
|
||||
|
||||
|
||||
if (strstr(session->header->buffer, "Content-Type") == RT_NULL)
|
||||
{
|
||||
/* 二进制数据流 */
|
||||
ret = webclient_header_fields_add(
|
||||
session, "Content-Type: application/octet-stream\r\n");
|
||||
if (ret < 0)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int _requests_Response_header_write(PikaObj* self, char* header, char* value) {
|
||||
struct webclient_session* session;
|
||||
|
||||
session = (struct webclient_session*)obj_getInt(self, "session_address");
|
||||
session = (struct webclient_session*)obj_getPtr(self, "session_address");
|
||||
if (unlikely(session == NULL)) {
|
||||
RQ_cli("Sorry, can not operate NULL session object.\n");
|
||||
return -1;
|
||||
@ -343,11 +308,6 @@ PikaObj* _requests_Response_request_del(PikaObj* self) {
|
||||
webclient_close(session);
|
||||
RQ_debug("Response free session memory.");
|
||||
}
|
||||
/* 初始化 */
|
||||
obj_setNone(self, "url");
|
||||
obj_setNone(self, "text");
|
||||
obj_setNone(self, "headers");
|
||||
obj_setPtr(self, "session_address", NULL);
|
||||
RQ_debug("Response set variables None.");
|
||||
return NULL;
|
||||
}
|
||||
|
@ -86,7 +86,7 @@ def request(
|
||||
return None
|
||||
ret = rqst.request(method, rqst.url, timeout, data)
|
||||
if ret != 1:
|
||||
return rqst
|
||||
return None
|
||||
return rqst
|
||||
|
||||
def get(url: str, params=None) -> Response:
|
||||
|
@ -1,5 +1,7 @@
|
||||
import requests
|
||||
|
||||
b = "kkk"
|
||||
|
||||
a = requests.request("GET", "http://pikascript.com/package", params = {"name":"get-test"})
|
||||
|
||||
print(a.headers)
|
||||
|
10
test/python/requests/post_data.py
Normal file
10
test/python/requests/post_data.py
Normal file
@ -0,0 +1,10 @@
|
||||
import requests
|
||||
form_data = '------WebKitFormBoundaryrEPACvZYkAbE4bYB\r\nContent-Disposition: form-data; name="file"; filename="test_file.txt"\r\nContent-Type: text/plain\r\n\r\nhello, pikascript!\r\n------WebKitFormBoundaryrEPACvZYkAbE4bYB\r\nContent-Disposition: form-data; name="id"\r\n\r\n1670666272201\r\n------WebKitFormBoundaryrEPACvZYkAbE4bYB\r\nContent-Disposition: form-data; name="uploadFileNum"\r\n\r\n1\r\n------WebKitFormBoundaryrEPACvZYkAbE4bYB--\r\n'
|
||||
|
||||
header = {"Content-Type": "multipart/form-data; boundary=----WebKitFormBoundaryrEPACvZYkAbE4bYB"}
|
||||
|
||||
a = requests.request("POST", "http://pikascript.com/upload", headers=header, data=form_data)
|
||||
|
||||
print(a.headers)
|
||||
print(a.content_length)
|
||||
print(a.text)
|
@ -267,4 +267,16 @@ TEST(requests, get_basic) {
|
||||
EXPECT_EQ(pikaMemNow(), 0);
|
||||
}
|
||||
|
||||
TEST(requests, post_data) {
|
||||
PikaObj* pikaMain = newRootObj("pikaMain", New_PikaMain);
|
||||
extern unsigned char pikaModules_py_a[];
|
||||
obj_linkLibrary(pikaMain, pikaModules_py_a);
|
||||
pikaVM_runSingleFile(pikaMain, "test/python/requests/post_data.py");
|
||||
/* assert */
|
||||
EXPECT_STREQ(log_buff[2], "HTTP/1.1 200 OK\r\n");
|
||||
/* deinit */
|
||||
obj_deinit(pikaMain);
|
||||
EXPECT_EQ(pikaMemNow(), 0);
|
||||
}
|
||||
|
||||
#endif
|
||||
|
Loading…
x
Reference in New Issue
Block a user