Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(103)

Unified Diff: ppapi/tests/test_url_loader.cc

Issue 11417145: Provide a safer URLLoader ReadResponseBody API (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Created 8 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « ppapi/tests/test_url_loader.h ('k') | ppapi/thunk/interfaces_ppb_public_stable.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: ppapi/tests/test_url_loader.cc
diff --git a/ppapi/tests/test_url_loader.cc b/ppapi/tests/test_url_loader.cc
index 90244912681fc7c77d24b1746db811fa62ff8205..e818ea4274d072913b2474b98d967275ff37955c 100644
--- a/ppapi/tests/test_url_loader.cc
+++ b/ppapi/tests/test_url_loader.cc
@@ -97,6 +97,7 @@ bool TestURLLoader::Init() {
void TestURLLoader::RunTests(const std::string& filter) {
RUN_CALLBACK_TEST(TestURLLoader, BasicGET, filter);
+ RUN_CALLBACK_TEST(TestURLLoader, BasicGET_ToArray, filter);
RUN_CALLBACK_TEST(TestURLLoader, BasicPOST, filter);
RUN_CALLBACK_TEST(TestURLLoader, BasicFilePOST, filter);
RUN_CALLBACK_TEST(TestURLLoader, BasicFileRangePOST, filter);
@@ -158,6 +159,26 @@ std::string TestURLLoader::ReadEntireResponseBody(pp::URLLoader* loader,
PASS();
}
+std::string TestURLLoader::ReadEntireResponseBodyToArray(pp::URLLoader* loader,
+ std::string* body) {
+ TestCompletionCallbackWithOutput< std::vector<char> > callback(
+ instance_->pp_instance(), callback_type());
+
+ for (;;) {
+ // Read just a little bit so that multiple reads are needed.
+ callback.WaitForResult(loader->ReadResponseBody(2, callback));
+ int32_t rv = callback.result();
+ if (rv < 0)
+ return ReportError("URLLoader::ReadResponseBody", rv);
+ if (rv == 0)
+ break;
+ assert(rv == static_cast<int32_t>(callback.output().size()));
+ body->append(callback.output().begin(), callback.output().end());
+ }
+
+ PASS();
+}
+
std::string TestURLLoader::LoadAndCompareBody(
const pp::URLRequestInfo& request,
const std::string& expected_body) {
@@ -188,6 +209,38 @@ std::string TestURLLoader::LoadAndCompareBody(
PASS();
}
+// Exact the same test of LoadAndCompareBody, but for ReadResponseBodyToArray.
+std::string TestURLLoader::LoadAndCompareBody_ToArray(
+ const pp::URLRequestInfo& request,
+ const std::string& expected_body) {
+ TestCompletionCallback callback(instance_->pp_instance(), callback_type());
+
+ pp::URLLoader loader(instance_);
+ callback.WaitForResult(loader.Open(request, callback));
+ CHECK_CALLBACK_BEHAVIOR(callback);
+ ASSERT_EQ(PP_OK, callback.result());
+
+ pp::URLResponseInfo response_info(loader.GetResponseInfo());
+ if (response_info.is_null())
+ return "URLLoader::GetResponseInfo returned null";
+ int32_t status_code = response_info.GetStatusCode();
+ if (status_code != 200)
+ return "Unexpected HTTP status code";
+
+ std::string body;
+ std::string error = ReadEntireResponseBodyToArray(&loader, &body);
+ if (!error.empty())
+ return error;
+
+ if (body.size() != expected_body.size())
+ return "URLLoader::ReadResponseBodyToArray returned unexpected content "
+ "length";
+ if (body != expected_body)
+ return "URLLoader::ReadResponseBodyToArray returned unexpected content";
+
+ PASS();
+}
+
int32_t TestURLLoader::OpenFileSystem(pp::FileSystem* file_system,
std::string* message) {
TestCompletionCallback callback(instance_->pp_instance(), callback_type());
@@ -307,6 +360,13 @@ std::string TestURLLoader::TestBasicGET() {
return LoadAndCompareBody(request, "hello\n");
}
+// A variance of BasicGET but use ReadResponseBodyToArray internally.
+std::string TestURLLoader::TestBasicGET_ToArray() {
+ pp::URLRequestInfo request(instance_);
+ request.SetURL("test_url_loader_data/hello.txt");
+ return LoadAndCompareBody_ToArray(request, "hello\n");
+}
+
std::string TestURLLoader::TestBasicPOST() {
pp::URLRequestInfo request(instance_);
request.SetURL("/echo");
« no previous file with comments | « ppapi/tests/test_url_loader.h ('k') | ppapi/thunk/interfaces_ppb_public_stable.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698