2012-08-09 10:45:11 +08:00
|
|
|
#include "HttpClientTest.h"
|
|
|
|
#include "../ExtensionsTest.h"
|
|
|
|
#include <string>
|
|
|
|
|
|
|
|
USING_NS_CC;
|
|
|
|
USING_NS_CC_EXT;
|
|
|
|
|
|
|
|
HttpClientTest::HttpClientTest()
|
2013-06-15 14:03:30 +08:00
|
|
|
: _labelStatusCode(NULL)
|
2012-08-09 10:45:11 +08:00
|
|
|
{
|
2013-07-12 06:24:23 +08:00
|
|
|
Size winSize = Director::getInstance()->getWinSize();
|
2013-04-22 00:23:27 +08:00
|
|
|
|
|
|
|
const int MARGIN = 40;
|
|
|
|
const int SPACE = 35;
|
2012-08-09 10:45:11 +08:00
|
|
|
|
2013-06-20 14:17:10 +08:00
|
|
|
LabelTTF *label = LabelTTF::create("Http Request Test", "Arial", 28);
|
2013-07-12 14:11:55 +08:00
|
|
|
label->setPosition(Point(winSize.width / 2, winSize.height - MARGIN));
|
2012-08-09 10:45:11 +08:00
|
|
|
addChild(label, 0);
|
|
|
|
|
2013-06-20 14:17:10 +08:00
|
|
|
Menu *menuRequest = Menu::create();
|
2013-07-12 14:47:36 +08:00
|
|
|
menuRequest->setPosition(Point::ZERO);
|
2012-08-09 10:45:11 +08:00
|
|
|
addChild(menuRequest);
|
|
|
|
|
|
|
|
// Get
|
2013-06-20 14:17:10 +08:00
|
|
|
LabelTTF *labelGet = LabelTTF::create("Test Get", "Arial", 22);
|
|
|
|
MenuItemLabel *itemGet = MenuItemLabel::create(labelGet, CC_CALLBACK_1(HttpClientTest::onMenuGetTestClicked, this));
|
2013-07-12 14:11:55 +08:00
|
|
|
itemGet->setPosition(Point(winSize.width / 2, winSize.height - MARGIN - SPACE));
|
2012-08-09 10:45:11 +08:00
|
|
|
menuRequest->addChild(itemGet);
|
|
|
|
|
|
|
|
// Post
|
2013-06-20 14:17:10 +08:00
|
|
|
LabelTTF *labelPost = LabelTTF::create("Test Post", "Arial", 22);
|
|
|
|
MenuItemLabel *itemPost = MenuItemLabel::create(labelPost, CC_CALLBACK_1(HttpClientTest::onMenuPostTestClicked, this));
|
2013-07-12 14:11:55 +08:00
|
|
|
itemPost->setPosition(Point(winSize.width / 2, winSize.height - MARGIN - 2 * SPACE));
|
2012-08-09 10:45:11 +08:00
|
|
|
menuRequest->addChild(itemPost);
|
|
|
|
|
|
|
|
// Post Binary
|
2013-06-20 14:17:10 +08:00
|
|
|
LabelTTF *labelPostBinary = LabelTTF::create("Test Post Binary", "Arial", 22);
|
|
|
|
MenuItemLabel *itemPostBinary = MenuItemLabel::create(labelPostBinary, CC_CALLBACK_1(HttpClientTest::onMenuPostBinaryTestClicked, this));
|
2013-07-12 14:11:55 +08:00
|
|
|
itemPostBinary->setPosition(Point(winSize.width / 2, winSize.height - MARGIN - 3 * SPACE));
|
2012-08-09 10:45:11 +08:00
|
|
|
menuRequest->addChild(itemPostBinary);
|
2013-04-22 00:23:27 +08:00
|
|
|
|
|
|
|
// Put
|
2013-06-20 14:17:10 +08:00
|
|
|
LabelTTF *labelPut = LabelTTF::create("Test Put", "Arial", 22);
|
|
|
|
MenuItemLabel *itemPut = MenuItemLabel::create(labelPut, CC_CALLBACK_1(HttpClientTest::onMenuPutTestClicked, this));
|
2013-07-12 14:11:55 +08:00
|
|
|
itemPut->setPosition(Point(winSize.width / 2, winSize.height - MARGIN - 4 * SPACE));
|
2013-04-22 00:23:27 +08:00
|
|
|
menuRequest->addChild(itemPut);
|
|
|
|
|
|
|
|
// Delete
|
2013-06-20 14:17:10 +08:00
|
|
|
LabelTTF *labelDelete = LabelTTF::create("Test Delete", "Arial", 22);
|
|
|
|
MenuItemLabel *itemDelete = MenuItemLabel::create(labelDelete, CC_CALLBACK_1(HttpClientTest::onMenuDeleteTestClicked, this));
|
2013-07-12 14:11:55 +08:00
|
|
|
itemDelete->setPosition(Point(winSize.width / 2, winSize.height - MARGIN - 5 * SPACE));
|
2013-04-22 00:23:27 +08:00
|
|
|
menuRequest->addChild(itemDelete);
|
2012-08-09 10:45:11 +08:00
|
|
|
|
|
|
|
// Response Code Label
|
2013-06-20 14:17:10 +08:00
|
|
|
_labelStatusCode = LabelTTF::create("HTTP Status Code", "Marker Felt", 20);
|
2013-07-12 14:11:55 +08:00
|
|
|
_labelStatusCode->setPosition(Point(winSize.width / 2, winSize.height - MARGIN - 6 * SPACE));
|
2013-06-15 14:03:30 +08:00
|
|
|
addChild(_labelStatusCode);
|
2012-08-09 10:45:11 +08:00
|
|
|
|
|
|
|
// Back Menu
|
2013-06-20 14:17:10 +08:00
|
|
|
MenuItemFont *itemBack = MenuItemFont::create("Back", CC_CALLBACK_1(HttpClientTest::toExtensionsMainLayer, this));
|
2013-07-12 14:11:55 +08:00
|
|
|
itemBack->setPosition(Point(VisibleRect::rightBottom().x - 50, VisibleRect::rightBottom().y + 25));
|
2013-06-20 14:17:10 +08:00
|
|
|
Menu *menuBack = Menu::create(itemBack, NULL);
|
2013-07-12 14:47:36 +08:00
|
|
|
menuBack->setPosition(Point::ZERO);
|
2012-08-09 10:45:11 +08:00
|
|
|
addChild(menuBack);
|
|
|
|
}
|
|
|
|
|
|
|
|
HttpClientTest::~HttpClientTest()
|
|
|
|
{
|
2013-06-20 14:17:10 +08:00
|
|
|
HttpClient::getInstance()->destroyInstance();
|
2012-08-09 10:45:11 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:17:10 +08:00
|
|
|
void HttpClientTest::onMenuGetTestClicked(cocos2d::Object *sender)
|
2012-08-09 10:45:11 +08:00
|
|
|
{
|
|
|
|
// test 1
|
|
|
|
{
|
2013-06-20 14:17:10 +08:00
|
|
|
HttpRequest* request = new HttpRequest();
|
2012-08-09 10:45:11 +08:00
|
|
|
request->setUrl("http://just-make-this-request-failed.com");
|
2013-06-20 14:17:10 +08:00
|
|
|
request->setRequestType(HttpRequest::kHttpGet);
|
2013-04-28 22:42:31 +08:00
|
|
|
request->setResponseCallback(this, httpresponse_selector(HttpClientTest::onHttpRequestCompleted));
|
2012-08-09 10:45:11 +08:00
|
|
|
request->setTag("GET test1");
|
2013-06-20 14:17:10 +08:00
|
|
|
HttpClient::getInstance()->send(request);
|
2012-08-09 10:45:11 +08:00
|
|
|
request->release();
|
|
|
|
}
|
|
|
|
|
|
|
|
// test 2
|
|
|
|
{
|
2013-06-20 14:17:10 +08:00
|
|
|
HttpRequest* request = new HttpRequest();
|
2012-08-09 10:45:11 +08:00
|
|
|
// required fields
|
2013-05-07 15:13:55 +08:00
|
|
|
request->setUrl("http://httpbin.org/ip");
|
2013-06-20 14:17:10 +08:00
|
|
|
request->setRequestType(HttpRequest::kHttpGet);
|
2013-04-28 22:42:31 +08:00
|
|
|
request->setResponseCallback(this, httpresponse_selector(HttpClientTest::onHttpRequestCompleted));
|
2012-08-09 10:45:11 +08:00
|
|
|
// optional fields
|
|
|
|
request->setTag("GET test2");
|
|
|
|
|
2013-06-20 14:17:10 +08:00
|
|
|
HttpClient::getInstance()->send(request);
|
2012-08-09 10:45:11 +08:00
|
|
|
|
|
|
|
// don't forget to release it, pair to new
|
|
|
|
request->release();
|
|
|
|
}
|
|
|
|
|
|
|
|
// test 3
|
|
|
|
{
|
2013-06-20 14:17:10 +08:00
|
|
|
HttpRequest* request = new HttpRequest();
|
2013-05-07 15:13:55 +08:00
|
|
|
request->setUrl("http://httpbin.org/get");
|
2013-06-20 14:17:10 +08:00
|
|
|
request->setRequestType(HttpRequest::kHttpGet);
|
2013-04-28 22:42:31 +08:00
|
|
|
request->setResponseCallback(this, httpresponse_selector(HttpClientTest::onHttpRequestCompleted));
|
2012-08-09 10:45:11 +08:00
|
|
|
request->setTag("GET test3");
|
2013-06-20 14:17:10 +08:00
|
|
|
HttpClient::getInstance()->send(request);
|
2012-08-09 10:45:11 +08:00
|
|
|
request->release();
|
|
|
|
}
|
|
|
|
|
|
|
|
// waiting
|
2013-06-15 14:03:30 +08:00
|
|
|
_labelStatusCode->setString("waiting...");
|
2012-08-09 10:45:11 +08:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:17:10 +08:00
|
|
|
void HttpClientTest::onMenuPostTestClicked(cocos2d::Object *sender)
|
2012-08-09 10:45:11 +08:00
|
|
|
{
|
2012-09-24 17:15:53 +08:00
|
|
|
// test 1
|
|
|
|
{
|
2013-06-20 14:17:10 +08:00
|
|
|
HttpRequest* request = new HttpRequest();
|
2013-05-07 15:13:55 +08:00
|
|
|
request->setUrl("http://httpbin.org/post");
|
2013-06-20 14:17:10 +08:00
|
|
|
request->setRequestType(HttpRequest::kHttpPost);
|
2013-04-28 22:42:31 +08:00
|
|
|
request->setResponseCallback(this, httpresponse_selector(HttpClientTest::onHttpRequestCompleted));
|
2012-09-24 17:15:53 +08:00
|
|
|
|
|
|
|
// write the post data
|
|
|
|
const char* postData = "visitor=cocos2d&TestSuite=Extensions Test/NetworkTest";
|
|
|
|
request->setRequestData(postData, strlen(postData));
|
|
|
|
|
|
|
|
request->setTag("POST test1");
|
2013-06-20 14:17:10 +08:00
|
|
|
HttpClient::getInstance()->send(request);
|
2012-09-24 17:15:53 +08:00
|
|
|
request->release();
|
|
|
|
}
|
2012-08-09 10:45:11 +08:00
|
|
|
|
2012-09-24 17:15:53 +08:00
|
|
|
// test 2: set Content-Type
|
|
|
|
{
|
2013-06-20 14:17:10 +08:00
|
|
|
HttpRequest* request = new HttpRequest();
|
2013-05-07 15:13:55 +08:00
|
|
|
request->setUrl("http://httpbin.org/post");
|
2013-06-20 14:17:10 +08:00
|
|
|
request->setRequestType(HttpRequest::kHttpPost);
|
2012-09-24 17:15:53 +08:00
|
|
|
std::vector<std::string> headers;
|
|
|
|
headers.push_back("Content-Type: application/json; charset=utf-8");
|
|
|
|
request->setHeaders(headers);
|
2013-04-28 22:42:31 +08:00
|
|
|
request->setResponseCallback(this, httpresponse_selector(HttpClientTest::onHttpRequestCompleted));
|
2012-09-24 17:15:53 +08:00
|
|
|
|
|
|
|
// write the post data
|
|
|
|
const char* postData = "visitor=cocos2d&TestSuite=Extensions Test/NetworkTest";
|
|
|
|
request->setRequestData(postData, strlen(postData));
|
|
|
|
|
|
|
|
request->setTag("POST test2");
|
2013-06-20 14:17:10 +08:00
|
|
|
HttpClient::getInstance()->send(request);
|
2012-09-24 17:15:53 +08:00
|
|
|
request->release();
|
|
|
|
}
|
2012-08-09 10:45:11 +08:00
|
|
|
|
|
|
|
// waiting
|
2013-06-15 14:03:30 +08:00
|
|
|
_labelStatusCode->setString("waiting...");
|
2012-08-09 10:45:11 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:17:10 +08:00
|
|
|
void HttpClientTest::onMenuPostBinaryTestClicked(cocos2d::Object *sender)
|
2012-08-09 10:45:11 +08:00
|
|
|
{
|
2013-06-20 14:17:10 +08:00
|
|
|
HttpRequest* request = new HttpRequest();
|
2013-05-07 15:13:55 +08:00
|
|
|
request->setUrl("http://httpbin.org/post");
|
2013-06-20 14:17:10 +08:00
|
|
|
request->setRequestType(HttpRequest::kHttpPost);
|
2013-04-28 22:42:31 +08:00
|
|
|
request->setResponseCallback(this, httpresponse_selector(HttpClientTest::onHttpRequestCompleted));
|
2012-08-09 10:45:11 +08:00
|
|
|
|
|
|
|
// write the post data
|
|
|
|
char postData[22] = "binary=hello\0\0cocos2d"; // including \0, the strings after \0 should not be cut in response
|
|
|
|
request->setRequestData(postData, 22);
|
|
|
|
|
|
|
|
request->setTag("POST Binary test");
|
2013-06-20 14:17:10 +08:00
|
|
|
HttpClient::getInstance()->send(request);
|
2012-08-09 10:45:11 +08:00
|
|
|
request->release();
|
|
|
|
|
|
|
|
// waiting
|
2013-06-15 14:03:30 +08:00
|
|
|
_labelStatusCode->setString("waiting...");
|
2012-08-09 10:45:11 +08:00
|
|
|
}
|
|
|
|
|
2013-05-07 15:37:25 +08:00
|
|
|
|
|
|
|
|
2013-06-20 14:17:10 +08:00
|
|
|
void HttpClientTest::onMenuPutTestClicked(Object *sender)
|
2013-04-22 00:23:27 +08:00
|
|
|
{
|
|
|
|
// test 1
|
|
|
|
{
|
2013-06-20 14:17:10 +08:00
|
|
|
HttpRequest* request = new HttpRequest();
|
2013-05-07 15:37:25 +08:00
|
|
|
request->setUrl("http://httpbin.org/put");
|
2013-06-20 14:17:10 +08:00
|
|
|
request->setRequestType(HttpRequest::kHttpPut);
|
2013-05-07 15:37:25 +08:00
|
|
|
request->setResponseCallback(this, httpresponse_selector(HttpClientTest::onHttpRequestCompleted));
|
2013-04-22 00:23:27 +08:00
|
|
|
|
|
|
|
// write the post data
|
|
|
|
const char* postData = "visitor=cocos2d&TestSuite=Extensions Test/NetworkTest";
|
|
|
|
request->setRequestData(postData, strlen(postData));
|
|
|
|
|
|
|
|
request->setTag("PUT test1");
|
2013-06-20 14:17:10 +08:00
|
|
|
HttpClient::getInstance()->send(request);
|
2013-04-22 00:23:27 +08:00
|
|
|
request->release();
|
|
|
|
}
|
|
|
|
|
|
|
|
// test 2: set Content-Type
|
|
|
|
{
|
2013-06-20 14:17:10 +08:00
|
|
|
HttpRequest* request = new HttpRequest();
|
2013-05-07 15:37:25 +08:00
|
|
|
request->setUrl("http://httpbin.org/put");
|
2013-06-20 14:17:10 +08:00
|
|
|
request->setRequestType(HttpRequest::kHttpPut);
|
2013-04-22 00:23:27 +08:00
|
|
|
std::vector<std::string> headers;
|
|
|
|
headers.push_back("Content-Type: application/json; charset=utf-8");
|
|
|
|
request->setHeaders(headers);
|
2013-05-07 15:37:25 +08:00
|
|
|
request->setResponseCallback(this, httpresponse_selector(HttpClientTest::onHttpRequestCompleted));
|
2013-04-22 00:23:27 +08:00
|
|
|
|
|
|
|
// write the post data
|
|
|
|
const char* postData = "visitor=cocos2d&TestSuite=Extensions Test/NetworkTest";
|
|
|
|
request->setRequestData(postData, strlen(postData));
|
|
|
|
|
|
|
|
request->setTag("PUT test2");
|
2013-06-20 14:17:10 +08:00
|
|
|
HttpClient::getInstance()->send(request);
|
2013-04-22 00:23:27 +08:00
|
|
|
request->release();
|
|
|
|
}
|
|
|
|
|
|
|
|
// waiting
|
2013-06-15 14:03:30 +08:00
|
|
|
_labelStatusCode->setString("waiting...");
|
2013-04-22 00:23:27 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:17:10 +08:00
|
|
|
void HttpClientTest::onMenuDeleteTestClicked(Object *sender)
|
2013-04-22 00:23:27 +08:00
|
|
|
{
|
|
|
|
// test 1
|
|
|
|
{
|
2013-06-20 14:17:10 +08:00
|
|
|
HttpRequest* request = new HttpRequest();
|
2013-04-22 00:23:27 +08:00
|
|
|
request->setUrl("http://just-make-this-request-failed.com");
|
2013-06-20 14:17:10 +08:00
|
|
|
request->setRequestType(HttpRequest::kHttpDelete);
|
2013-05-07 15:37:25 +08:00
|
|
|
request->setResponseCallback(this, httpresponse_selector(HttpClientTest::onHttpRequestCompleted));
|
2013-04-22 00:23:27 +08:00
|
|
|
request->setTag("DELETE test1");
|
2013-06-20 14:17:10 +08:00
|
|
|
HttpClient::getInstance()->send(request);
|
2013-04-22 00:23:27 +08:00
|
|
|
request->release();
|
|
|
|
}
|
|
|
|
|
|
|
|
// test 2
|
|
|
|
{
|
2013-06-20 14:17:10 +08:00
|
|
|
HttpRequest* request = new HttpRequest();
|
2013-05-07 15:37:25 +08:00
|
|
|
request->setUrl("http://httpbin.org/delete");
|
2013-06-20 14:17:10 +08:00
|
|
|
request->setRequestType(HttpRequest::kHttpDelete);
|
2013-05-07 15:37:25 +08:00
|
|
|
request->setResponseCallback(this, httpresponse_selector(HttpClientTest::onHttpRequestCompleted));
|
2013-04-22 00:23:27 +08:00
|
|
|
request->setTag("DELETE test2");
|
2013-06-20 14:17:10 +08:00
|
|
|
HttpClient::getInstance()->send(request);
|
2013-04-22 00:23:27 +08:00
|
|
|
request->release();
|
|
|
|
}
|
|
|
|
|
|
|
|
// waiting
|
2013-06-15 14:03:30 +08:00
|
|
|
_labelStatusCode->setString("waiting...");
|
2013-04-22 00:23:27 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:17:10 +08:00
|
|
|
void HttpClientTest::onHttpRequestCompleted(HttpClient *sender, HttpResponse *response)
|
2012-08-09 10:45:11 +08:00
|
|
|
{
|
|
|
|
if (!response)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// You can get original request type from: response->request->reqType
|
|
|
|
if (0 != strlen(response->getHttpRequest()->getTag()))
|
|
|
|
{
|
2013-07-24 06:20:22 +08:00
|
|
|
log("%s completed", response->getHttpRequest()->getTag());
|
2012-08-09 10:45:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int statusCode = response->getResponseCode();
|
|
|
|
char statusString[64] = {};
|
|
|
|
sprintf(statusString, "HTTP Status Code: %d, tag = %s", statusCode, response->getHttpRequest()->getTag());
|
2013-06-15 14:03:30 +08:00
|
|
|
_labelStatusCode->setString(statusString);
|
2013-07-24 06:20:22 +08:00
|
|
|
log("response code: %d", statusCode);
|
2012-08-09 10:45:11 +08:00
|
|
|
|
|
|
|
if (!response->isSucceed())
|
|
|
|
{
|
2013-07-24 06:20:22 +08:00
|
|
|
log("response failed");
|
|
|
|
log("error buffer: %s", response->getErrorBuffer());
|
2012-08-09 10:45:11 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// dump data
|
|
|
|
std::vector<char> *buffer = response->getResponseData();
|
|
|
|
printf("Http Test, dump data: ");
|
2012-08-24 14:35:25 +08:00
|
|
|
for (unsigned int i = 0; i < buffer->size(); i++)
|
2012-08-09 10:45:11 +08:00
|
|
|
{
|
|
|
|
printf("%c", (*buffer)[i]);
|
|
|
|
}
|
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:17:10 +08:00
|
|
|
void HttpClientTest::toExtensionsMainLayer(cocos2d::Object *sender)
|
2012-08-09 10:45:11 +08:00
|
|
|
{
|
2013-07-23 08:25:44 +08:00
|
|
|
ExtensionsTestScene *scene = new ExtensionsTestScene();
|
|
|
|
scene->runThisTest();
|
|
|
|
scene->release();
|
2012-08-09 10:45:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void runHttpClientTest()
|
|
|
|
{
|
2013-07-23 08:25:44 +08:00
|
|
|
Scene *scene = Scene::create();
|
|
|
|
HttpClientTest *layer = new HttpClientTest();
|
|
|
|
scene->addChild(layer);
|
2012-08-09 10:45:11 +08:00
|
|
|
|
2013-07-23 08:25:44 +08:00
|
|
|
Director::getInstance()->replaceScene(scene);
|
|
|
|
layer->release();
|
2012-08-09 10:45:11 +08:00
|
|
|
}
|