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 "chrome/app_installer/win/app_installer_util.h"
10 #include "base/files/file_util.h"
11 #include "base/path_service.h"
12 #include "base/strings/sys_string_conversions.h"
13 #include "net/test/spawned_test_server/spawned_test_server.h"
14 #include "testing/gtest/include/gtest/gtest.h"
16 namespace app_installer
{
18 TEST(AppInstallerUtilTest
, ParseTag
) {
19 std::map
<std::string
, std::string
> parsed_pairs
;
22 EXPECT_TRUE(ParseTag("key1=value1&key2=value2", &parsed_pairs
));
23 EXPECT_EQ(2, parsed_pairs
.size());
24 EXPECT_EQ("value1", parsed_pairs
["key1"]);
25 EXPECT_EQ("value2", parsed_pairs
["key2"]);
28 EXPECT_FALSE(ParseTag("a&b", &parsed_pairs
));
31 EXPECT_FALSE(ParseTag("#=a", &parsed_pairs
));
34 EXPECT_FALSE(ParseTag("a=\01", &parsed_pairs
));
37 void TestFetchUrlWithScheme(net::SpawnedTestServer::Type type
, bool success
) {
38 net::SpawnedTestServer
http_server(
39 type
, net::SpawnedTestServer::kLocalhost
,
40 base::FilePath(FILE_PATH_LITERAL("chrome/test/data")));
41 ASSERT_TRUE(http_server
.Start());
43 std::vector
<uint8_t> response_data
;
44 net::HostPortPair host_port
= http_server
.host_port_pair();
46 FetchUrl(L
"user agent", base::SysUTF8ToWide(host_port
.host()),
47 host_port
.port(), L
"files/extensions/app/manifest.json",
50 EXPECT_TRUE(response_data
.size());
51 base::FilePath source_root
;
52 ASSERT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT
, &source_root
));
53 base::FilePath file_path
= source_root
.Append(
54 FILE_PATH_LITERAL("chrome/test/data/extensions/app/manifest.json"));
55 std::string file_contents
;
56 EXPECT_TRUE(base::ReadFileToString(file_path
, &file_contents
));
57 EXPECT_EQ(file_contents
,
58 std::string(response_data
.begin(), response_data
.end()));
60 EXPECT_FALSE(response_data
.size());
63 ASSERT_TRUE(http_server
.Stop());
66 TEST(AppInstallerUtilTest
, FetchUrlHttps
) {
67 TestFetchUrlWithScheme(net::SpawnedTestServer::TYPE_HTTPS
, true);
70 TEST(AppInstallerUtilTest
, FetchUrlNoHttps
) {
71 TestFetchUrlWithScheme(net::SpawnedTestServer::TYPE_HTTP
, false);
74 } // namespace app_installer