network_fetcher_unittest.cc 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. // Copyright 2021 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. #include "components/winhttp/network_fetcher.h"
  5. #include "base/bind.h"
  6. #include "base/files/file.h"
  7. #include "base/memory/scoped_refptr.h"
  8. #include "base/run_loop.h"
  9. #include "base/test/bind.h"
  10. #include "base/test/task_environment.h"
  11. #include "components/winhttp/scoped_hinternet.h"
  12. #include "testing/gtest/include/gtest/gtest.h"
  13. #include "url/gurl.h"
  14. namespace winhttp {
  15. TEST(WinHttpNetworkFetcher, InvalidUrlPost) {
  16. base::test::TaskEnvironment environment;
  17. base::RunLoop run_loop;
  18. winhttp::ScopedHInternet session_handle = CreateSessionHandle(
  19. L"WinHttpNetworkFetcher.InvalidUrlPost", WINHTTP_ACCESS_TYPE_NO_PROXY);
  20. auto network_fetcher = base::MakeRefCounted<NetworkFetcher>(
  21. session_handle.get(), base::MakeRefCounted<ProxyConfiguration>());
  22. network_fetcher->PostRequest(
  23. /*url*/ GURL("file://afile"),
  24. /*content_type*/ "text/plain",
  25. /*post_data*/ "a request body",
  26. /*post_additional_headers*/ {},
  27. /*fetch_started_callback*/
  28. base::BindOnce([](int response_code, int64_t content_length) {}),
  29. /*fetch_progress_callback*/ base::BindRepeating([](int64_t current) {}),
  30. /*fetch_complete_callback*/
  31. base::BindLambdaForTesting(
  32. [&run_loop](int response_code) { run_loop.Quit(); }));
  33. run_loop.Run();
  34. EXPECT_EQ(network_fetcher->GetNetError(), E_INVALIDARG);
  35. }
  36. TEST(WinHttpNetworkFetcher, InvalidUrlDownload) {
  37. base::test::TaskEnvironment environment;
  38. base::RunLoop run_loop;
  39. winhttp::ScopedHInternet session_handle = CreateSessionHandle(
  40. L"WinHttpNetworkFetcher.InvalidUrlPost", WINHTTP_ACCESS_TYPE_NO_PROXY);
  41. auto network_fetcher = base::MakeRefCounted<NetworkFetcher>(
  42. session_handle.get(), base::MakeRefCounted<ProxyConfiguration>());
  43. network_fetcher->DownloadToFile(
  44. /*url*/ GURL("file://afile"),
  45. /*file_path*/ {},
  46. /*fetch_started_callback*/
  47. base::BindOnce([](int response_code, int64_t content_length) {}),
  48. /*fetch_progress_callback*/ base::BindRepeating([](int64_t current) {}),
  49. /*fetch_complete_callback*/
  50. base::BindLambdaForTesting(
  51. [&run_loop](int response_code) { run_loop.Quit(); }));
  52. run_loop.Run();
  53. EXPECT_EQ(network_fetcher->GetNetError(), E_INVALIDARG);
  54. }
  55. } // namespace winhttp