OLD | NEW |
| (Empty) |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "mojo/fetcher/data_fetcher.h" | |
6 | |
7 #include <stdint.h> | |
8 | |
9 #include <utility> | |
10 | |
11 #include "base/auto_reset.h" | |
12 #include "base/bind.h" | |
13 #include "base/logging.h" | |
14 #include "base/macros.h" | |
15 #include "base/memory/scoped_ptr.h" | |
16 #include "base/message_loop/message_loop.h" | |
17 #include "base/run_loop.h" | |
18 #include "mojo/public/cpp/system/data_pipe.h" | |
19 #include "testing/gtest/include/gtest/gtest.h" | |
20 | |
21 namespace mojo { | |
22 namespace fetcher { | |
23 namespace { | |
24 | |
25 class FetchCallbackHelper { | |
26 public: | |
27 FetchCallbackHelper() : run_loop_(nullptr) {} | |
28 ~FetchCallbackHelper() {} | |
29 | |
30 shell::Fetcher::FetchCallback GetCallback() { | |
31 return base::Bind(&FetchCallbackHelper::CallbackHandler, | |
32 base::Unretained(this)); | |
33 } | |
34 | |
35 void WaitForCallback() { | |
36 base::RunLoop run_loop; | |
37 base::AutoReset<base::RunLoop*> auto_reset(&run_loop_, &run_loop); | |
38 run_loop.Run(); | |
39 } | |
40 | |
41 shell::Fetcher* fetcher() const { return fetcher_.get(); } | |
42 | |
43 private: | |
44 void CallbackHandler(scoped_ptr<shell::Fetcher> fetcher) { | |
45 fetcher_ = std::move(fetcher); | |
46 if (run_loop_) | |
47 run_loop_->Quit(); | |
48 } | |
49 | |
50 // If it is not null, it points to a stack-allocated base::RunLoop instance in | |
51 // WaitForCallback(). | |
52 base::RunLoop* run_loop_; | |
53 scoped_ptr<shell::Fetcher> fetcher_; | |
54 DISALLOW_COPY_AND_ASSIGN(FetchCallbackHelper); | |
55 }; | |
56 | |
57 class DataFetcherTest : public testing::Test { | |
58 public: | |
59 DataFetcherTest() {} | |
60 ~DataFetcherTest() override {} | |
61 | |
62 protected: | |
63 void TestFetchURL(const std::string& url, | |
64 uint32_t expected_status_code, | |
65 const std::string& expected_mime_type, | |
66 const std::string& expected_body) { | |
67 FetchCallbackHelper helper; | |
68 DataFetcher::Start(GURL(url), helper.GetCallback()); | |
69 helper.WaitForCallback(); | |
70 | |
71 ASSERT_TRUE(helper.fetcher()); | |
72 URLResponsePtr response = helper.fetcher()->AsURLResponse(nullptr, 0); | |
73 ASSERT_TRUE(response); | |
74 EXPECT_EQ(url, response->url); | |
75 EXPECT_EQ(expected_status_code, response->status_code); | |
76 | |
77 if (expected_status_code != 200) | |
78 return; | |
79 | |
80 ASSERT_TRUE(response->body.is_valid()); | |
81 EXPECT_EQ(expected_mime_type, response->mime_type); | |
82 | |
83 uint32_t num_bytes = 0; | |
84 Handle body_handle = response->body.release(); | |
85 | |
86 MojoHandleSignalsState hss; | |
87 ASSERT_EQ(MOJO_RESULT_OK, | |
88 MojoWait(body_handle.value(), MOJO_HANDLE_SIGNAL_READABLE, | |
89 MOJO_DEADLINE_INDEFINITE, &hss)); | |
90 | |
91 MojoResult result = MojoReadData(body_handle.value(), nullptr, &num_bytes, | |
92 MOJO_READ_DATA_FLAG_QUERY); | |
93 ASSERT_EQ(MOJO_RESULT_OK, result); | |
94 | |
95 scoped_ptr<char[]> body(new char[num_bytes]); | |
96 result = MojoReadData(body_handle.value(), body.get(), &num_bytes, | |
97 MOJO_READ_DATA_FLAG_ALL_OR_NONE); | |
98 ASSERT_EQ(MOJO_RESULT_OK, result); | |
99 EXPECT_EQ(expected_body, std::string(body.get(), num_bytes)); | |
100 } | |
101 | |
102 private: | |
103 base::MessageLoop loop_; | |
104 | |
105 DISALLOW_COPY_AND_ASSIGN(DataFetcherTest); | |
106 }; | |
107 | |
108 TEST_F(DataFetcherTest, BasicSuccess) { | |
109 TestFetchURL("data:text/html,Hello world", 200, "text/html", "Hello world"); | |
110 } | |
111 | |
112 TEST_F(DataFetcherTest, BasicFailure) { | |
113 TestFetchURL("about:blank", 400, std::string(), std::string()); | |
114 TestFetchURL("data:;base64,aGVs_-_-", 400, std::string(), std::string()); | |
115 } | |
116 | |
117 } // namespace | |
118 } // namespace fetcher | |
119 } // namespace mojo | |
OLD | NEW |