Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HttpClient: add support for PATCH method #1722

Merged
merged 2 commits into from
Feb 29, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 13 additions & 7 deletions core/network/HttpClient-wasm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,20 @@
Copyright (c) 2012 cocos2d-x.org
Copyright (c) 2013-2016 Chukong Technologies Inc.
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.

Copyright (c) 2019-present Axmol Engine contributors (see AUTHORS.md).

http://www.cocos2d-x.org
smilediver marked this conversation as resolved.
Show resolved Hide resolved

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
Expand All @@ -31,7 +32,7 @@
#include "platform/FileUtils.h"
#include "yasio/string_view.hpp"

#if EMSCRIPTEN
#if EMSCRIPTEN
#include <emscripten/fetch.h>
#include <emscripten.h>
#endif
Expand Down Expand Up @@ -166,6 +167,11 @@ namespace network
strcpy(attr.requestMethod, "GET");
break;

case HttpRequest::Type::PATCH:
strcpy(attr.requestMethod, "PATCH");
usePostData = true;
break;

case HttpRequest::Type::POST:
strcpy(attr.requestMethod, "POST");
usePostData = true;
Expand All @@ -181,7 +187,7 @@ namespace network
break;

default:
AXASSERT(false, "CCHttpClient: unknown request type, only GET, POST, PUT or DELETE is supported");
AXASSERT(false, "HttpClient: unknown request type, only GET, PATCH, POST, PUT or DELETE is supported");
break;
}

Expand Down Expand Up @@ -256,7 +262,7 @@ namespace network

const auto isAlone = userData->isAlone;
delete userData;

if (_httpClient)
{
// call back
Expand Down
4 changes: 4 additions & 0 deletions core/network/HttpClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,10 @@ void HttpClient::handleNetworkEvent(yasio::io_event* event)
case HttpRequest::Type::GET:
obs.write_bytes("GET");
break;
case HttpRequest::Type::PATCH:
obs.write_bytes("PATCH");
usePostData = true;
break;
case HttpRequest::Type::POST:
obs.write_bytes("POST");
usePostData = true;
Expand Down
1 change: 1 addition & 0 deletions core/network/HttpRequest.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ class AX_DLL HttpRequest : public Ref
enum class Type
{
GET,
PATCH,
POST,
PUT,
DELETE,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -791,6 +791,10 @@ static int axlua_XMLHttpRequest_open(lua_State* L)
{
self->getHttpRequest()->setRequestType(network::HttpRequest::Type::GET);
}
else if (method.compare("patch") == 0 || method.compare("PATCH") == 0)
{
self->getHttpRequest()->setRequestType(network::HttpRequest::Type::PATCH);
}
else if (method.compare("put") == 0 || method.compare("PUT") == 0)
{
self->getHttpRequest()->setRequestType(network::HttpRequest::Type::PUT);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ HttpClientTest::HttpClientTest() : _labelStatusCode(nullptr)
const int MARGIN = 40;
const int SPACE = 35;

const int LEFT = winSize.width / 2;
const int LEFT = winSize.width / 4 * 1;
const int CENTER = winSize.width / 2;
const int RIGHT = winSize.width / 4 * 3;

auto menuRequest = Menu::create();
Expand All @@ -63,32 +64,38 @@ HttpClientTest::HttpClientTest() : _labelStatusCode(nullptr)
// Get
auto labelGet = Label::createWithTTF("Test Get", "fonts/arial.ttf", 22);
auto itemGet = MenuItemLabel::create(labelGet, AX_CALLBACK_1(HttpClientTest::onMenuGetTestClicked, this));
itemGet->setPosition(LEFT, winSize.height - MARGIN - SPACE);
itemGet->setPosition(CENTER, winSize.height - MARGIN - SPACE);
menuRequest->addChild(itemGet);

// Patch
auto labelPatch = Label::createWithTTF("Test Patch", "fonts/arial.ttf", 22);
auto itemPatch = MenuItemLabel::create(labelPatch, AX_CALLBACK_1(HttpClientTest::onMenuPatchTestClicked, this));
itemPatch->setPosition(CENTER, winSize.height - MARGIN - 2 * SPACE);
menuRequest->addChild(itemPatch);

// Post
auto labelPost = Label::createWithTTF("Test Post", "fonts/arial.ttf", 22);
auto itemPost = MenuItemLabel::create(labelPost, AX_CALLBACK_1(HttpClientTest::onMenuPostTestClicked, this));
itemPost->setPosition(LEFT, winSize.height - MARGIN - 2 * SPACE);
itemPost->setPosition(LEFT, winSize.height - MARGIN - 3 * SPACE);
menuRequest->addChild(itemPost);

// Post Binary
auto labelPostBinary = Label::createWithTTF("Test Post Binary", "fonts/arial.ttf", 22);
auto itemPostBinary =
MenuItemLabel::create(labelPostBinary, AX_CALLBACK_1(HttpClientTest::onMenuPostBinaryTestClicked, this));
itemPostBinary->setPosition(LEFT, winSize.height - MARGIN - 3 * SPACE);
itemPostBinary->setPosition(RIGHT, winSize.height - MARGIN - 3 * SPACE);
menuRequest->addChild(itemPostBinary);

// Put
auto labelPut = Label::createWithTTF("Test Put", "fonts/arial.ttf", 22);
auto itemPut = MenuItemLabel::create(labelPut, AX_CALLBACK_1(HttpClientTest::onMenuPutTestClicked, this));
itemPut->setPosition(LEFT, winSize.height - MARGIN - 4 * SPACE);
itemPut->setPosition(CENTER, winSize.height - MARGIN - 4 * SPACE);
menuRequest->addChild(itemPut);

// Delete
auto labelDelete = Label::createWithTTF("Test Delete", "fonts/arial.ttf", 22);
auto itemDelete = MenuItemLabel::create(labelDelete, AX_CALLBACK_1(HttpClientTest::onMenuDeleteTestClicked, this));
itemDelete->setPosition(LEFT, winSize.height - MARGIN - 5 * SPACE);
itemDelete->setPosition(CENTER, winSize.height - MARGIN - 5 * SPACE);
menuRequest->addChild(itemDelete);

// Response Code Label
Expand Down Expand Up @@ -158,6 +165,45 @@ void HttpClientTest::onMenuGetTestClicked(ax::Ref* sender)
_labelStatusCode->setString("waiting...");
}

void HttpClientTest::onMenuPatchTestClicked(Ref* sender)
{
// test 1
{
HttpRequest* request = new HttpRequest();
request->setUrl("https://httpbin.org/patch");
request->setRequestType(HttpRequest::Type::PATCH);
request->setResponseCallback(AX_CALLBACK_2(HttpClientTest::onHttpRequestCompleted, this));

// write the body data
const char* bodyData = "visitor=cocos2d&TestSuite=Extensions Test/NetworkTest";
request->setRequestData(bodyData, strlen(bodyData));
request->setTag("PATCH Binary test1");
HttpClient::getInstance()->send(request);
request->release();
}

// test 2: set Content-Type
{
HttpRequest* request = new HttpRequest();
request->setUrl("https://httpbin.org/patch");
request->setRequestType(HttpRequest::Type::PATCH);
std::vector<std::string> headers;
headers.emplace_back("Content-Type: application/json; charset=utf-8");
request->setHeaders(headers);
request->setResponseCallback(AX_CALLBACK_2(HttpClientTest::onHttpRequestCompleted, this));

// write the post data
const char* bodyData = "visitor=cocos2d&TestSuite=Extensions Test/NetworkTest";
request->setRequestData(bodyData, strlen(bodyData));
request->setTag("PATCH Binary test2");
HttpClient::getInstance()->send(request);
request->release();
}

// waiting
_labelStatusCode->setString("waiting...");
}

void HttpClientTest::onMenuPostTestClicked(ax::Ref* sender)
{
// test 1
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/****************************************************************************
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
Copyright (c) 2019-present Axmol Engine contributors (see AUTHORS.md).

https://axmolengine.github.io/

Expand Down Expand Up @@ -42,6 +43,7 @@ class HttpClientTest : public TestCase

// Menu Callbacks
void onMenuGetTestClicked(ax::Ref* sender);
void onMenuPatchTestClicked(ax::Ref* sender);
void onMenuPostTestClicked(ax::Ref* sender);
void onMenuPostBinaryTestClicked(ax::Ref* sender);
void onMenuPutTestClicked(ax::Ref* sender);
Expand Down
Loading