dhcp_pac_file_fetcher_mojo_unittest.cc 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. // Copyright 2019 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 "services/network/dhcp_pac_file_fetcher_mojo.h"
  5. #include "base/memory/ptr_util.h"
  6. #include "net/base/test_completion_callback.h"
  7. #include "net/proxy_resolution/mock_pac_file_fetcher.h"
  8. #include "net/test/gtest_util.h"
  9. #include "net/test/test_with_task_environment.h"
  10. #include "net/traffic_annotation/network_traffic_annotation_test_helper.h"
  11. #include "net/url_request/url_request_context.h"
  12. #include "net/url_request/url_request_context_builder.h"
  13. #include "net/url_request/url_request_test_util.h"
  14. #include "services/network/dhcp_pac_file_fetcher_mojo.h"
  15. #include "services/network/mock_mojo_dhcp_wpad_url_client.h"
  16. #include "testing/gtest/include/gtest/gtest.h"
  17. namespace {
  18. using net::test::IsError;
  19. using net::test::IsOk;
  20. } // namespace
  21. namespace network {
  22. class DhcpPacFileFetcherMojoTest : public testing::Test {
  23. public:
  24. DhcpPacFileFetcherMojoTest() = default;
  25. ~DhcpPacFileFetcherMojoTest() override {}
  26. protected:
  27. void CreateFetcher(const std::string& pac_url) {
  28. auto context_builder = net::CreateTestURLRequestContextBuilder();
  29. auto context = context_builder->Build();
  30. dhcp_pac_file_fetcher_mojo_ = std::make_unique<DhcpPacFileFetcherMojo>(
  31. context.get(),
  32. network::MockMojoDhcpWpadUrlClient::CreateWithSelfOwnedReceiver(
  33. pac_url));
  34. mock_pac_file_fetcher_ = new net::MockPacFileFetcher();
  35. dhcp_pac_file_fetcher_mojo_->SetPacFileFetcherForTesting(
  36. base::WrapUnique(mock_pac_file_fetcher_));
  37. }
  38. std::unique_ptr<DhcpPacFileFetcherMojo> dhcp_pac_file_fetcher_mojo_;
  39. net::MockPacFileFetcher* mock_pac_file_fetcher_;
  40. private:
  41. base::test::TaskEnvironment task_environment_;
  42. };
  43. // Test that the PAC URL set by the client is used.
  44. TEST_F(DhcpPacFileFetcherMojoTest, UsePacSctipt) {
  45. GURL pac_url("http://wpad.test.com/wpad.dat");
  46. CreateFetcher(pac_url.spec());
  47. net::TestCompletionCallback callback;
  48. std::u16string pac_text;
  49. dhcp_pac_file_fetcher_mojo_->Fetch(&pac_text, callback.callback(),
  50. net::NetLogWithSource(),
  51. TRAFFIC_ANNOTATION_FOR_TESTS);
  52. mock_pac_file_fetcher_->WaitUntilFetch();
  53. EXPECT_EQ(pac_url, mock_pac_file_fetcher_->pending_request_url());
  54. mock_pac_file_fetcher_->NotifyFetchCompletion(net::OK, "script");
  55. EXPECT_THAT(callback.WaitForResult(), IsOk());
  56. }
  57. // Test that error is returned when PAC URL is missing.
  58. TEST_F(DhcpPacFileFetcherMojoTest, PacScriptMissing) {
  59. CreateFetcher(std::string());
  60. net::TestCompletionCallback callback;
  61. std::u16string pac_text;
  62. dhcp_pac_file_fetcher_mojo_->Fetch(&pac_text, callback.callback(),
  63. net::NetLogWithSource(),
  64. TRAFFIC_ANNOTATION_FOR_TESTS);
  65. EXPECT_THAT(callback.WaitForResult(), net::ERR_PAC_NOT_IN_DHCP);
  66. }
  67. } // namespace network