|
10 | 10 | #include <vector> |
11 | 11 |
|
12 | 12 | #include "base/containers/contains.h" |
| 13 | +#include "base/cxx17_backports.h" |
13 | 14 | #include "base/memory/weak_ptr.h" |
| 15 | +#include "base/strings/string_piece.h" |
14 | 16 | #include "base/test/icu_test_util.h" |
15 | 17 | #include "base/test/values_test_util.h" |
16 | 18 | #include "base/time/time.h" |
@@ -40,6 +42,7 @@ using ::testing::ElementsAre; |
40 | 42 | using ::testing::IsEmpty; |
41 | 43 | using ::testing::Return; |
42 | 44 | using ::testing::SaveArg; |
| 45 | +using ::testing::StrEq; |
43 | 46 |
|
44 | 47 | // Keep it in-sync with the `kFinalFallbackName` returned by |
45 | 48 | // net::GetSuggestedFilename(). |
@@ -144,6 +147,8 @@ class FakePdfViewPluginBase : public PdfViewPluginBase { |
144 | 147 | using PdfViewPluginBase::UpdateGeometryOnPluginRectChanged; |
145 | 148 | using PdfViewPluginBase::UpdateScroll; |
146 | 149 |
|
| 150 | + MOCK_METHOD(std::string, GetURL, (), (override)); |
| 151 | + |
147 | 152 | MOCK_METHOD(bool, Confirm, (const std::string&), (override)); |
148 | 153 |
|
149 | 154 | MOCK_METHOD(std::string, |
@@ -1226,4 +1231,74 @@ TEST_F(PdfViewPluginBaseWithEngineTest, GetAccessibilityDocInfo) { |
1226 | 1231 | EXPECT_TRUE(doc_info.text_copyable); |
1227 | 1232 | } |
1228 | 1233 |
|
| 1234 | +class PdfViewPluginBaseSubmitFormTest : public PdfViewPluginBaseTest { |
| 1235 | + public: |
| 1236 | + void SubmitForm(const std::string& url, |
| 1237 | + base::StringPiece form_data = "data") { |
| 1238 | + EXPECT_CALL(fake_plugin_, CreateUrlLoaderInternal).WillOnce([this]() { |
| 1239 | + auto mock_loader = std::make_unique<testing::NiceMock<MockUrlLoader>>(); |
| 1240 | + EXPECT_CALL(*mock_loader, Open).WillOnce(testing::SaveArg<0>(&request_)); |
| 1241 | + return mock_loader; |
| 1242 | + }); |
| 1243 | + |
| 1244 | + fake_plugin_.SubmitForm(url, form_data.data(), form_data.size()); |
| 1245 | + } |
| 1246 | + |
| 1247 | + void SubmitFailingForm(const std::string& url) { |
| 1248 | + EXPECT_CALL(fake_plugin_, CreateUrlLoaderInternal).Times(0); |
| 1249 | + constexpr char kFormData[] = "form data"; |
| 1250 | + fake_plugin_.SubmitForm(url, kFormData, base::size(kFormData)); |
| 1251 | + } |
| 1252 | + |
| 1253 | + protected: |
| 1254 | + UrlRequest request_; |
| 1255 | +}; |
| 1256 | + |
| 1257 | +TEST_F(PdfViewPluginBaseSubmitFormTest, RequestMethodAndBody) { |
| 1258 | + EXPECT_CALL(fake_plugin_, GetURL) |
| 1259 | + .WillOnce(Return("https://www.example.com/path/to/the.pdf")); |
| 1260 | + constexpr char kFormData[] = "form data"; |
| 1261 | + SubmitForm(/*url=*/"", kFormData); |
| 1262 | + EXPECT_EQ(request_.method, "POST"); |
| 1263 | + EXPECT_THAT(request_.body, StrEq(kFormData)); |
| 1264 | +} |
| 1265 | + |
| 1266 | +TEST_F(PdfViewPluginBaseSubmitFormTest, RelativeUrl) { |
| 1267 | + EXPECT_CALL(fake_plugin_, GetURL) |
| 1268 | + .WillOnce(Return("https://www.example.com/path/to/the.pdf")); |
| 1269 | + SubmitForm("relative_endpoint"); |
| 1270 | + EXPECT_EQ(request_.url, "https://www.example.com/path/to/relative_endpoint"); |
| 1271 | +} |
| 1272 | + |
| 1273 | +TEST_F(PdfViewPluginBaseSubmitFormTest, NoRelativeUrl) { |
| 1274 | + EXPECT_CALL(fake_plugin_, GetURL) |
| 1275 | + .WillOnce(Return("https://www.example.com/path/to/the.pdf")); |
| 1276 | + SubmitForm(""); |
| 1277 | + EXPECT_EQ(request_.url, "https://www.example.com/path/to/the.pdf"); |
| 1278 | +} |
| 1279 | + |
| 1280 | +TEST_F(PdfViewPluginBaseSubmitFormTest, AbsoluteUrl) { |
| 1281 | + EXPECT_CALL(fake_plugin_, GetURL) |
| 1282 | + .WillOnce(Return("https://a.example.com/path/to/the.pdf")); |
| 1283 | + SubmitForm("https://b.example.com/relative_endpoint"); |
| 1284 | + EXPECT_EQ(request_.url, "https://b.example.com/relative_endpoint"); |
| 1285 | +} |
| 1286 | + |
| 1287 | +TEST_F(PdfViewPluginBaseSubmitFormTest, EmptyDocumentUrl) { |
| 1288 | + EXPECT_CALL(fake_plugin_, GetURL).WillOnce(Return(std::string())); |
| 1289 | + SubmitFailingForm("relative_endpoint"); |
| 1290 | +} |
| 1291 | + |
| 1292 | +TEST_F(PdfViewPluginBaseSubmitFormTest, RelativeUrlInvalidDocumentUrl) { |
| 1293 | + EXPECT_CALL(fake_plugin_, GetURL) |
| 1294 | + .WillOnce(Return(R"(https://www.%B%Ad.com/path/to/the.pdf)")); |
| 1295 | + SubmitFailingForm("relative_endpoint"); |
| 1296 | +} |
| 1297 | + |
| 1298 | +TEST_F(PdfViewPluginBaseSubmitFormTest, AbsoluteUrlInvalidDocumentUrl) { |
| 1299 | + EXPECT_CALL(fake_plugin_, GetURL) |
| 1300 | + .WillOnce(Return(R"(https://www.%B%Ad.com/path/to/the.pdf)")); |
| 1301 | + SubmitFailingForm("https://wwww.example.com"); |
| 1302 | +} |
| 1303 | + |
1229 | 1304 | } // namespace chrome_pdf |
0 commit comments