Chromium Code Reviews| Index: components/policy/core/browser/policy_header_io_helper_unittest.cc |
| diff --git a/components/policy/core/browser/policy_header_io_helper_unittest.cc b/components/policy/core/browser/policy_header_io_helper_unittest.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..64ab8122823bcacd1fd096e3367dad1dfa3154e2 |
| --- /dev/null |
| +++ b/components/policy/core/browser/policy_header_io_helper_unittest.cc |
| @@ -0,0 +1,86 @@ |
| +// Copyright (c) 2013 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| +#include "base/test/test_simple_task_runner.h" |
|
dconnelly
2013/12/09 12:47:35
newline above
Andrew T Wilson (Slow)
2013/12/10 07:15:56
Done.
|
| +#include "components/policy/core/browser/policy_header_io_helper.h" |
| +#include "content/public/test/test_browser_thread_bundle.h" |
| +#include "net/http/http_request_headers.h" |
| +#include "net/url_request/url_request_test_util.h" |
| +#include "testing/gtest/include/gtest/gtest.h" |
| + |
| +namespace policy { |
| + |
| +namespace { |
| +const char kDMServerURL[] = "http://server_url"; |
| +const char kPolicyHeaderName[] = "Chrome-Policy-Posture"; |
| +const char kInitialPolicyHeader[] = "initial_header"; |
| +} // namespace |
| + |
| +class PolicyHeaderIOHelperTest : public testing::Test { |
| + public: |
| + PolicyHeaderIOHelperTest() { |
| + task_runner_ = make_scoped_refptr(new base::TestSimpleTaskRunner()); |
| + } |
| + virtual ~PolicyHeaderIOHelperTest() {} |
| + |
| + virtual void SetUp() OVERRIDE { |
| + helper_ = make_scoped_ptr(new PolicyHeaderIOHelper(kDMServerURL, |
| + kInitialPolicyHeader, |
| + task_runner_)); |
| + } |
| + virtual void TearDown() { |
| + task_runner_->RunUntilIdle(); |
| + helper_.reset(); |
| + } |
| + |
| + void ValidateHeader(const net::HttpRequestHeaders& headers, |
| + const std::string& expected) { |
| + std::string header; |
| + EXPECT_TRUE(headers.GetHeader(kPolicyHeaderName, &header)); |
| + EXPECT_EQ(header, expected); |
| + } |
| + |
| + scoped_ptr<PolicyHeaderIOHelper> helper_; |
| + net::TestURLRequestContext context_; |
| + scoped_refptr<base::TestSimpleTaskRunner> task_runner_; |
| + content::TestBrowserThreadBundle thread_bundle_; |
| +}; |
| + |
| +TEST_F(PolicyHeaderIOHelperTest, InitialHeader) { |
| + task_runner_->RunUntilIdle(); |
|
dconnelly
2013/12/09 12:47:35
Can this move to the end of SetUp instead of repro
Andrew T Wilson (Slow)
2013/12/10 07:15:56
Done.
Andrew T Wilson (Slow)
2013/12/10 07:15:56
Done.
|
| + net::TestURLRequest request( |
| + GURL(kDMServerURL), net::DEFAULT_PRIORITY, NULL, &context_); |
| + helper_->AddPolicyHeaders(&request); |
| + ValidateHeader(request.extra_request_headers(), kInitialPolicyHeader); |
| +} |
| + |
| +TEST_F(PolicyHeaderIOHelperTest, NoHeaderOnNonMatchingURL) { |
| + task_runner_->RunUntilIdle(); |
| + net::TestURLRequest request( |
| + GURL("http://non-matching.com"), net::DEFAULT_PRIORITY, NULL, &context_); |
| + helper_->AddPolicyHeaders(&request); |
| + EXPECT_TRUE(request.extra_request_headers().IsEmpty()); |
| +} |
| + |
| +TEST_F(PolicyHeaderIOHelperTest, HeaderChange) { |
| + task_runner_->RunUntilIdle(); |
| + std::string new_header = "new_header"; |
| + helper_->UpdateHeaderFromUI(new_header); |
| + task_runner_->RunUntilIdle(); |
| + net::TestURLRequest request( |
| + GURL(kDMServerURL), net::DEFAULT_PRIORITY, NULL, &context_); |
| + helper_->AddPolicyHeaders(&request); |
| + ValidateHeader(request.extra_request_headers(), new_header); |
| +} |
| + |
| +TEST_F(PolicyHeaderIOHelperTest, ChangeToNoHeader) { |
| + task_runner_->RunUntilIdle(); |
| + helper_->UpdateHeaderFromUI(""); |
| + task_runner_->RunUntilIdle(); |
| + net::TestURLRequest request( |
| + GURL(kDMServerURL), net::DEFAULT_PRIORITY, NULL, &context_); |
| + helper_->AddPolicyHeaders(&request); |
| + EXPECT_TRUE(request.extra_request_headers().IsEmpty()); |
| +} |
| + |
| +} // namespace policy |