Roll src/third_party/WebKit 4aace46:1b4a5ed (svn 202009:202010)
[chromium-blink-merge.git] / components / webp_transcode / webp_network_client_unittest.mm
blobc5c425b684b063a652c7eddb11ba3da5f3ce0972
1 // Copyright 2014 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.
5 #include "components/webp_transcode/webp_network_client.h"
7 #include "base/mac/scoped_nsobject.h"
8 #include "base/memory/weak_ptr.h"
9 #include "base/message_loop/message_loop.h"
10 #include "base/thread_task_runner_handle.h"
11 #include "net/http/http_request_headers.h"
12 #include "net/url_request/url_request_test_util.h"
13 #include "testing/gtest/include/gtest/gtest.h"
14 #import "third_party/ocmock/OCMock/OCMock.h"
16 namespace {
17 class WebPNetworkClientTest : public testing::Test {
18  public:
19   WebPNetworkClientTest() {
20     // Set up mock original network client proxy.
21     OCMockObject* mockProxy_ = [[OCMockObject
22         niceMockForProtocol:@protocol(CRNNetworkClientProtocol)] retain];
23     mockWebProxy_.reset(mockProxy_);
25     // Link all the mock objects into the WebPNetworkClient.
26     webp_client_.reset([[WebPNetworkClient alloc]
27         initWithTaskRunner:base::ThreadTaskRunnerHandle::Get()]);
28     [webp_client_
29         setUnderlyingClient:(id<CRNNetworkClientProtocol>)mockWebProxy_];
30   }
32  protected:
33   base::MessageLoop loop_;
34   base::scoped_nsobject<WebPNetworkClient> webp_client_;
35   // Holds a mock CRNNetworkClientProtocol object.
36   base::scoped_nsobject<OCMockObject> mockWebProxy_;
38 }  // namespace
40 TEST_F(WebPNetworkClientTest, TestAcceptHeaders) {
41   const struct {
42     const std::string header_in;
43     const std::string header_out;
44   } tests[] = {
45       {"", "image/webp"},
46       {"*/*", "*/*,image/webp"},
47       {"image/webp", "image/webp"},
48       {"text/html,*/*", "text/html,*/*,image/webp"},
49       // Desktop Chrome default without image/webp.
50       {"text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
51        "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8,"
52        "image/webp"},
53       // Desktop Chrome default.
54       {"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,"
55        "*/*;q=0.8",
56        "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,"
57        "*/*;q=0.8"}};
58   GURL url("http://www.google.com");
59   scoped_ptr<net::URLRequestContext> request_context(
60       new net::TestURLRequestContext(false));
61   for (size_t i = 0; i < arraysize(tests); ++i) {
62     scoped_ptr<net::URLRequest> request =
63         request_context->CreateRequest(url, net::DEFAULT_PRIORITY, nullptr);
64     if (!tests[i].header_in.empty())
65       request->SetExtraRequestHeaderByName("Accept", tests[i].header_in, true);
66     [webp_client_ didCreateNativeRequest:request.get()];
67     const net::HttpRequestHeaders& headers = request->extra_request_headers();
68     std::string acceptHeader;
69     EXPECT_TRUE(headers.GetHeader("Accept", &acceptHeader));
70     EXPECT_EQ(tests[i].header_out, acceptHeader);
71   }