Skip to content

Commit 7303895

Browse files
committed
Improve error message for Downloader and add test case
1 parent 05da273 commit 7303895

File tree

4 files changed

+32
-4
lines changed

4 files changed

+32
-4
lines changed

cocos/network/CCDownloader-apple.mm

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ of this software and associated documentation files (the "Software"), to deal
2525
#include "network/CCDownloader-apple.h"
2626

2727
#include "network/CCDownloader.h"
28+
#include "CCString.h"
2829
#include <queue>
2930

3031
////////////////////////////////////////////////////////////////////////////////
@@ -399,7 +400,7 @@ - (void)URLSession:(NSURLSession *)session task :(NSURLSessionTask *)task
399400
else if(![wrapper get]->storagePath.length()) {
400401
// call onTaskFinish for a data task
401402
// (for a file download task, callback is called in didFinishDownloadingToURL)
402-
std::string errorString;
403+
std::string errorString = cocos2d::StringUtils::format("Downloader: Failed to download %s with no error", [task.originalRequest.URL.absoluteString cStringUsingEncoding:NSUTF8StringEncoding]);
403404

404405
const int64_t buflen = [wrapper totalBytesReceived];
405406
char buf[buflen];
@@ -522,7 +523,7 @@ - (void)URLSession:(NSURLSession *)session downloadTask :(NSURLSessionDownloadTa
522523
NSInteger statusCode = ((NSHTTPURLResponse*)downloadTask.response).statusCode;
523524
if (statusCode >= 400) {
524525
std::vector<unsigned char> buf; // just a placeholder
525-
std::string response = [[NSString stringWithContentsOfURL:location] UTF8String] ?: "";
526+
std::string response = cocos2d::StringUtils::format("Downloader: Failed to download %s with status code (%d)", [downloadTask.originalRequest.URL.absoluteString cStringUsingEncoding:NSUTF8StringEncoding], (int)statusCode);
526527
_outer->onTaskFinish(*[wrapper get], cocos2d::network::DownloadTask::ERROR_IMPL_INTERNAL, noErr, response, buf);
527528
return;
528529
}

tests/cpp-tests/Classes/DownloaderTest/DownloaderTest.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,15 @@ static const char* sURLList[] =
3636
{
3737
"http://www.cocos2d-x.org/attachments/802/cocos2dx_landscape.png",
3838
"http://www.cocos2d-x.org/docs/manual/framework/native/wiki/logo-resources-of-cocos2d-x/res/2dx_icon_512_rounded.png",
39-
"http://www.cocos2d-x.org/attachments/1503/Cocos2CoordinateRelease.png",
39+
"http://www.cocos2d-x.org/attachments/1503/inexist.png",
4040
"http://download.sdkbox.com/installer/v1/sdkbox-iap_v1.2.3.3.tar.gz",
4141
};
4242
const static int sListSize = (sizeof(sURLList)/sizeof(sURLList[0]));
4343
static const char* sNameList[sListSize] =
4444
{
4545
"cocos2dx_landscape.png",
4646
"2dx_icon_512_rounded.png",
47-
"Cocos2CoordinateRelease.png",
47+
"inexist file",
4848
"big file",
4949
};
5050

@@ -112,6 +112,7 @@ struct DownloaderTest : public TestCase
112112
label->setPosition(Vec2(viewSize.width / 2, viewSize.height / 2));
113113
label->setContentSize(Size(viewSize.width, 0));
114114
label->setAlignment(TextHAlignment::CENTER, TextVAlignment::CENTER);
115+
label->setDimensions(viewSize.width, viewSize.height);
115116
bg->addChild(label, 20);
116117

117118
return bg;

tests/cpp-tests/proj.ios/Info.plist

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,15 @@
88
<false/>
99
<key>NSExceptionDomains</key>
1010
<dict>
11+
<key>sdkbox.com</key>
12+
<dict>
13+
<key>NSIncludesSubdomains</key>
14+
<true/>
15+
<key>NSTemporaryExceptionAllowsInsecureHTTPLoads</key>
16+
<true/>
17+
<key>NSTemporaryExceptionMinimumTLSVersion</key>
18+
<string>TLSv1.1</string>
19+
</dict>
1120
<key>cocos2d-x.org</key>
1221
<dict>
1322
<key>NSIncludesSubdomains</key>

tests/js-tests/src/TextureCacheTest/TextureCacheTest.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,8 @@ var RemoteTextureTest = TextureCacheTestBase.extend({
282282
for (var i = 0; i < imageUrlArray.length; i++) {
283283
cc.textureCache.addImageAsync(imageUrlArray[i], this.texLoaded, this);
284284
}
285+
286+
cc.loader.loadImg("http://www.cocos2d-x.org/no_such_file.jpg", this.failLoaded.bind(this));
285287
},
286288

287289
texLoaded: function(texture) {
@@ -296,6 +298,21 @@ var RemoteTextureTest = TextureCacheTestBase.extend({
296298
else {
297299
cc.log("Fail to load remote texture");
298300
}
301+
},
302+
303+
failLoaded: function (err, img) {
304+
var str = "";
305+
if (err) {
306+
str = "Correct behavior: failed to download from wrong url";
307+
}
308+
else {
309+
str = "!!! Wrong behavior: succeed to download from wrong url";
310+
}
311+
312+
var label = new cc.LabelTTF(str, "Times New Roman", 28);
313+
label.x = winSize.width / 2;
314+
label.y = winSize.height / 2;
315+
this.addChild(label, 100);
299316
}
300317
});
301318

0 commit comments

Comments
 (0)