Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(140)

Side by Side Diff: content/browser/experiments/api_key_unittest.cc

Issue 1521063003: Add API Key parsing for experimental APIs (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebase Created 5 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2015 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
5 #include "content/browser/experiments/api_key.h"
6
7 #include "base/memory/scoped_ptr.h"
8 #include "base/test/simple_test_clock.h"
9 #include "base/time/time.h"
10 #include "testing/gtest/include/gtest/gtest.h"
11
12 namespace content {
13
14 namespace {
15
16 const char* kFrobulateAPIName = "Frobulate";
17 const char* kFrobulateEnabledOrigin = "https://jpchase.github.io";
18
19 const char* kGoodAPIKey =
20 "Signature|https://jpchase.github.io|Frobulate|1458766277";
chasej 2015/12/15 19:43:41 I think it would help test readability/understandi
iclelland 2015/12/15 21:22:24 How about if I add another constant, above these o
chasej 2015/12/16 15:41:17 Adding another constant for "right now" is a good
iclelland 2015/12/16 17:04:37 Done -- I've reworked the tests, and now have kVal
21 const char* kGoodAPIKeySignature = "Signature";
22 const char* kGoodAPIKeyData =
23 "https://jpchase.github.io|Frobulate|1458766277";
24
25 const char* kBadDateAPIKey =
26 "Signature|https://jpchase.github.io|Frobulate|1458766275";
27 } // namespace
28
29 class ApiKeyTest : public testing::Test {
30 protected:
31 ApiKeyTest() {}
32
33 ~ApiKeyTest() override {}
34
35 void SetUp() override {
36 clock_.SetNow(base::Time::FromDoubleT(1458766276.0));
37 }
38
39 base::SimpleTestClock& clock() { return clock_; }
40
41 private:
42 base::SimpleTestClock clock_;
43 };
44
45 TEST_F(ApiKeyTest, ParseNullString) {
46 scoped_ptr<ApiKey> empty_key = ApiKey::Parse(std::string());
47 EXPECT_FALSE(empty_key);
48 }
49
50 TEST_F(ApiKeyTest, ParseEmptyString) {
51 scoped_ptr<ApiKey> empty_key = ApiKey::Parse("");
52 EXPECT_FALSE(empty_key);
53 }
54
55 TEST_F(ApiKeyTest, ParseInvalidString) {
56 scoped_ptr<ApiKey> empty_key = ApiKey::Parse("abcdef");
57 EXPECT_FALSE(empty_key);
58 }
59
60 TEST_F(ApiKeyTest, ParseValidKeyString) {
61 scoped_ptr<ApiKey> key = ApiKey::Parse(kGoodAPIKey);
62 ASSERT_TRUE(key);
63 EXPECT_EQ(kFrobulateAPIName, key->api_name());
64 EXPECT_EQ(kGoodAPIKeySignature, key->signature());
65 EXPECT_EQ(kGoodAPIKeyData, key->data());
66 EXPECT_EQ(GURL(kFrobulateEnabledOrigin), key->origin());
67 }
68
69 TEST_F(ApiKeyTest, ValidateValidKey) {
70 scoped_ptr<ApiKey> key = ApiKey::Parse(kGoodAPIKey);
71 ASSERT_TRUE(key);
72 EXPECT_TRUE(key->IsValidNow(clock().Now()));
73 }
74
75 TEST_F(ApiKeyTest, ValidateBadDate) {
76 scoped_ptr<ApiKey> bad_key = ApiKey::Parse(kBadDateAPIKey);
77 ASSERT_TRUE(bad_key);
78 EXPECT_FALSE(bad_key->IsValidNow(clock().Now()));
79 }
80
81 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698