diff --git a/core/network/HttpClient-wasm.cpp b/core/network/HttpClient-wasm.cpp index 1874851ebadb..871a8d356a36 100644 --- a/core/network/HttpClient-wasm.cpp +++ b/core/network/HttpClient-wasm.cpp @@ -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. - - http://www.cocos2d-x.org - + Copyright (c) 2019-present Axmol Engine contributors (see AUTHORS.md). + + https://axmolengine.github.io/ + 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 @@ -31,7 +32,7 @@ #include "platform/FileUtils.h" #include "yasio/string_view.hpp" -#if EMSCRIPTEN +#if EMSCRIPTEN #include #include #endif @@ -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; @@ -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; } @@ -256,7 +262,7 @@ namespace network const auto isAlone = userData->isAlone; delete userData; - + if (_httpClient) { // call back diff --git a/core/network/HttpClient.cpp b/core/network/HttpClient.cpp index d6cca7cc753a..3d2a70f69c01 100644 --- a/core/network/HttpClient.cpp +++ b/core/network/HttpClient.cpp @@ -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; diff --git a/core/network/HttpRequest.h b/core/network/HttpRequest.h index a1890b8cb7ca..079746d364a5 100644 --- a/core/network/HttpRequest.h +++ b/core/network/HttpRequest.h @@ -73,6 +73,7 @@ class AX_DLL HttpRequest : public Ref enum class Type { GET, + PATCH, POST, PUT, DELETE, diff --git a/extensions/scripting/lua-bindings/manual/network/lua_xml_http_request.cpp b/extensions/scripting/lua-bindings/manual/network/lua_xml_http_request.cpp index 77d5005e74b2..998c0f6f7a7e 100644 --- a/extensions/scripting/lua-bindings/manual/network/lua_xml_http_request.cpp +++ b/extensions/scripting/lua-bindings/manual/network/lua_xml_http_request.cpp @@ -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); diff --git a/tests/cpp-tests/Source/NetworkTest/HttpClientTest/HttpClientTest.cpp b/tests/cpp-tests/Source/NetworkTest/HttpClientTest/HttpClientTest.cpp index 36897d28cc7d..503f94a56809 100644 --- a/tests/cpp-tests/Source/NetworkTest/HttpClientTest/HttpClientTest.cpp +++ b/tests/cpp-tests/Source/NetworkTest/HttpClientTest/HttpClientTest.cpp @@ -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(); @@ -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 @@ -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 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 diff --git a/tests/cpp-tests/Source/NetworkTest/HttpClientTest/HttpClientTest.h b/tests/cpp-tests/Source/NetworkTest/HttpClientTest/HttpClientTest.h index 7bd8fcf29fbc..6ddd3b144bf6 100644 --- a/tests/cpp-tests/Source/NetworkTest/HttpClientTest/HttpClientTest.h +++ b/tests/cpp-tests/Source/NetworkTest/HttpClientTest/HttpClientTest.h @@ -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/ @@ -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);