Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tests for static redirect helper #58

Merged
merged 1 commit into from
Mar 23, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "brave/browser/net/brave_static_redirect_network_delegate_helper.h"

#include "brave/browser/net/url_context.h"
#include "chrome/test/base/chrome_render_view_host_test_harness.h"
#include "net/traffic_annotation/network_traffic_annotation_test_helper.h"
#include "net/url_request/url_request_test_util.h"


namespace {

class BraveStaticRedirectNetworkDelegateHelperTest: public testing::Test {
public:
BraveStaticRedirectNetworkDelegateHelperTest()
: thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP),
context_(new net::TestURLRequestContext(true)) {
}
~BraveStaticRedirectNetworkDelegateHelperTest() override {}
void SetUp() override {}
net::TestURLRequestContext* context() { return context_.get(); }

private:
content::TestBrowserThreadBundle thread_bundle_;
std::unique_ptr<net::TestURLRequestContext> context_;
};


TEST_F(BraveStaticRedirectNetworkDelegateHelperTest, NoModifyTypicalURL) {
net::TestDelegate test_delegate;
GURL url("https://bradhatesprimes.brave.com/composite_numbers_ftw");
std::unique_ptr<net::URLRequest> request =
context()->CreateRequest(url, net::IDLE, &test_delegate,
TRAFFIC_ANNOTATION_FOR_TESTS);
std::shared_ptr<brave::OnBeforeURLRequestContext>
before_url_context(new brave::OnBeforeURLRequestContext());
brave::ResponseCallback callback;
GURL new_url;
int ret =
OnBeforeURLRequest_StaticRedirectWork(request.get(), &new_url, callback,
before_url_context);
EXPECT_TRUE(new_url.is_empty());
EXPECT_EQ(ret, net::OK);
}


TEST_F(BraveStaticRedirectNetworkDelegateHelperTest, ModifyGeoURL) {
net::TestDelegate test_delegate;
GURL url("https://www.googleapis.com/geolocation/v1/geolocate?key=2_3_5_7");
std::unique_ptr<net::URLRequest> request =
context()->CreateRequest(url, net::IDLE, &test_delegate,
TRAFFIC_ANNOTATION_FOR_TESTS);
std::shared_ptr<brave::OnBeforeURLRequestContext>
before_url_context(new brave::OnBeforeURLRequestContext());
brave::ResponseCallback callback;
GURL new_url;
GURL expected_url(GURL(GOOGLEAPIS_ENDPOINT GOOGLEAPIS_API_KEY));
int ret =
OnBeforeURLRequest_StaticRedirectWork(request.get(), &new_url, callback,
before_url_context);
EXPECT_EQ(new_url, expected_url);
EXPECT_EQ(ret, net::OK);
}


} // namespace
7 changes: 6 additions & 1 deletion test/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ static_library("brave_test_support_unit") {

test("brave_unit_tests") {
sources = [
"//brave/browser/net/brave_static_redirect_network_delegate_helper_unittest.cc",
"//chrome/common/importer/mock_importer_bridge.cc",
"//chrome/common/importer/mock_importer_bridge.h",
"../utility/importer/chrome_importer_unittest.cc",
Expand All @@ -36,6 +37,10 @@ test("brave_unit_tests") {
"data/",
]

configs += [
"//brave/build/geolocation",
]

public_deps = [
"//base",
"//base/test:test_support",
Expand All @@ -46,4 +51,4 @@ test("brave_unit_tests") {
":brave_test_support_unit",
"//testing/gtest",
]
}
}