url_loader_inttest.mm 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. // Copyright 2017 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 <memory>
  5. #include "base/run_loop.h"
  6. #include "base/test/bind.h"
  7. #include "ios/web/public/test/fakes/fake_browser_state.h"
  8. #import "ios/web/public/test/web_test.h"
  9. #import "ios/web/public/web_client.h"
  10. #include "net/test/embedded_test_server/embedded_test_server.h"
  11. #include "net/traffic_annotation/network_traffic_annotation_test_helper.h"
  12. #include "services/network/public/cpp/resource_request.h"
  13. #include "services/network/public/cpp/simple_url_loader.h"
  14. #include "services/network/public/mojom/url_response_head.mojom.h"
  15. #if !defined(__has_feature) || !__has_feature(objc_arc)
  16. #error "This file requires ARC support."
  17. #endif
  18. namespace web {
  19. class URLLoaderTest : public WebTest {
  20. protected:
  21. URLLoaderTest() : WebTest(WebTaskEnvironment::Options::IO_MAINLOOP) {}
  22. protected:
  23. net::EmbeddedTestServer server_;
  24. };
  25. // Tests that basic URLLoader wrapper works.
  26. TEST_F(URLLoaderTest, Basic) {
  27. server_.AddDefaultHandlers(FILE_PATH_LITERAL(base::FilePath()));
  28. ASSERT_TRUE(server_.Start());
  29. std::unique_ptr<network::ResourceRequest> request =
  30. std::make_unique<network::ResourceRequest>();
  31. request->url = server_.GetURL("/echo");
  32. // Adds kCorsExemptHeaderName into the cors_exempt_headers, that use should be
  33. // allowed by FakeBrowserState. If BrowserState implementation does not
  34. // permit to use this header in |cors_exempt_headers| explicitly, the request
  35. // fails with net::ERR_INVALID_ARGUMENT.
  36. request->cors_exempt_headers.SetHeader(
  37. FakeBrowserState::kCorsExemptTestHeaderName, "Test");
  38. auto loader = network::SimpleURLLoader::Create(std::move(request),
  39. TRAFFIC_ANNOTATION_FOR_TESTS);
  40. std::string result;
  41. base::RunLoop run_loop;
  42. loader->DownloadToStringOfUnboundedSizeUntilCrashAndDie(
  43. GetBrowserState()->GetURLLoaderFactory(),
  44. base::BindLambdaForTesting(
  45. [&](std::unique_ptr<std::string> response_body) {
  46. if (response_body)
  47. result = *response_body;
  48. run_loop.Quit();
  49. }));
  50. run_loop.Run();
  51. EXPECT_EQ(0, loader->NetError());
  52. EXPECT_EQ(result, "Echo");
  53. auto* response_info = loader->ResponseInfo();
  54. ASSERT_TRUE(!!response_info);
  55. ASSERT_TRUE(!!response_info->headers);
  56. EXPECT_EQ(200, response_info->headers->response_code());
  57. }
  58. } // namespace web