1 // Copyright 2013 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 "chrome/browser/metrics/variations/variations_http_header_provider.h"
9 #include "base/base64.h"
10 #include "base/message_loop/message_loop.h"
11 #include "chrome/common/metrics/proto/chrome_experiments.pb.h"
12 #include "net/http/http_request_headers.h"
13 #include "testing/gtest/include/gtest/gtest.h"
16 namespace chrome_variations
{
20 // Decodes the variations header and extracts the variation ids.
21 bool ExtractVariationIds(const std::string
& variations
,
22 std::set
<VariationID
>* variation_ids
) {
23 std::string serialized_proto
;
24 if (!base::Base64Decode(variations
, &serialized_proto
))
26 metrics::ChromeVariations proto
;
27 if (!proto
.ParseFromString(serialized_proto
))
29 for (int i
= 0; i
< proto
.variation_id_size(); ++i
)
30 variation_ids
->insert(proto
.variation_id(i
));
36 TEST(VariationsHttpHeaderProviderTest
, ShouldAppendHeaders
) {
39 bool should_append_headers
;
41 { "http://google.com", true },
42 { "http://www.google.com", true },
43 { "http://m.google.com", true },
44 { "http://google.ca", true },
45 { "https://google.ca", true },
46 { "http://google.co.uk", true },
47 { "http://google.co.uk:8080/", true },
48 { "http://www.google.co.uk:8080/", true },
49 { "http://youtube.com", true },
50 { "http://www.youtube.com", true },
51 { "http://www.youtube.ca", true },
52 { "http://www.youtube.co.uk:8080/", true },
53 { "https://www.youtube.com", true },
54 { "http://www.yahoo.com", false },
55 { "http://youtube", false },
56 { "http://google", false },
59 for (size_t i
= 0; i
< ARRAYSIZE_UNSAFE(cases
); ++i
) {
60 const GURL
url(cases
[i
].url
);
61 EXPECT_EQ(cases
[i
].should_append_headers
,
62 VariationsHttpHeaderProvider::ShouldAppendHeaders(url
)) << url
;
66 TEST(VariationsHttpHeaderProviderTest
, SetDefaultVariationIds_Valid
) {
67 base::MessageLoop loop
;
68 VariationsHttpHeaderProvider provider
;
69 GURL
url("http://www.google.com");
70 net::HttpRequestHeaders headers
;
71 std::string variations
;
73 // Valid experiment ids.
74 EXPECT_TRUE(provider
.SetDefaultVariationIds("12,456"));
75 provider
.InitVariationIDsCacheIfNeeded();
76 provider
.AppendHeaders(url
, false, false, &headers
);
77 EXPECT_TRUE(headers
.HasHeader("X-Client-Data"));
78 headers
.GetHeader("X-Client-Data", &variations
);
79 std::set
<VariationID
> variation_ids
;
80 ASSERT_TRUE(ExtractVariationIds(variations
, &variation_ids
));
81 EXPECT_TRUE(variation_ids
.find(12) != variation_ids
.end());
82 EXPECT_TRUE(variation_ids
.find(456) != variation_ids
.end());
85 TEST(VariationsHttpHeaderProviderTest
, SetDefaultVariationIds_Invalid
) {
86 base::MessageLoop loop
;
87 VariationsHttpHeaderProvider provider
;
88 GURL
url("http://www.google.com");
89 net::HttpRequestHeaders headers
;
91 // Invalid experiment ids.
92 EXPECT_FALSE(provider
.SetDefaultVariationIds("abcd12,456"));
93 provider
.InitVariationIDsCacheIfNeeded();
94 provider
.AppendHeaders(url
, false, false, &headers
);
95 EXPECT_FALSE(headers
.HasHeader("X-Client-Data"));
98 } // namespace chrome_variations