This source file includes following definitions.
- TEST
- TEST
- TEST
- TEST
- TEST
- TEST
- TEST
- TEST
#include "net/filter/filter.h"
#include "net/filter/mock_filter_context.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace net {
class FilterTest : public testing::Test {
};
TEST(FilterTest, ContentTypeId) {
EXPECT_EQ(Filter::FILTER_TYPE_DEFLATE,
Filter::ConvertEncodingToType("deflate"));
EXPECT_EQ(Filter::FILTER_TYPE_DEFLATE,
Filter::ConvertEncodingToType("deflAte"));
EXPECT_EQ(Filter::FILTER_TYPE_GZIP,
Filter::ConvertEncodingToType("gzip"));
EXPECT_EQ(Filter::FILTER_TYPE_GZIP,
Filter::ConvertEncodingToType("GzIp"));
EXPECT_EQ(Filter::FILTER_TYPE_GZIP,
Filter::ConvertEncodingToType("x-gzip"));
EXPECT_EQ(Filter::FILTER_TYPE_GZIP,
Filter::ConvertEncodingToType("X-GzIp"));
EXPECT_EQ(Filter::FILTER_TYPE_SDCH,
Filter::ConvertEncodingToType("sdch"));
EXPECT_EQ(Filter::FILTER_TYPE_SDCH,
Filter::ConvertEncodingToType("sDcH"));
EXPECT_EQ(Filter::FILTER_TYPE_UNSUPPORTED,
Filter::ConvertEncodingToType("weird"));
EXPECT_EQ(Filter::FILTER_TYPE_UNSUPPORTED,
Filter::ConvertEncodingToType("strange"));
}
TEST(FilterTest, ApacheGzip) {
MockFilterContext filter_context;
filter_context.SetSdchResponse(false);
const std::string kGzipMime1("application/x-gzip");
const std::string kGzipMime2("application/gzip");
const std::string kGzipMime3("application/x-gunzip");
std::vector<Filter::FilterType> encoding_types;
encoding_types.clear();
encoding_types.push_back(Filter::FILTER_TYPE_GZIP);
filter_context.SetMimeType(kGzipMime1);
Filter::FixupEncodingTypes(filter_context, &encoding_types);
EXPECT_TRUE(encoding_types.empty());
encoding_types.clear();
encoding_types.push_back(Filter::FILTER_TYPE_GZIP);
filter_context.SetMimeType(kGzipMime2);
Filter::FixupEncodingTypes(filter_context, &encoding_types);
EXPECT_TRUE(encoding_types.empty());
encoding_types.clear();
encoding_types.push_back(Filter::FILTER_TYPE_GZIP);
filter_context.SetMimeType(kGzipMime3);
Filter::FixupEncodingTypes(filter_context, &encoding_types);
EXPECT_TRUE(encoding_types.empty());
encoding_types.clear();
encoding_types.push_back(Filter::FILTER_TYPE_SDCH);
filter_context.SetMimeType(kGzipMime1);
Filter::FixupEncodingTypes(filter_context, &encoding_types);
ASSERT_EQ(1U, encoding_types.size());
EXPECT_EQ(Filter::FILTER_TYPE_SDCH, encoding_types.front());
encoding_types.clear();
encoding_types.push_back(Filter::FILTER_TYPE_GZIP);
filter_context.SetMimeType("other/mime");
Filter::FixupEncodingTypes(filter_context, &encoding_types);
ASSERT_EQ(1U, encoding_types.size());
EXPECT_EQ(Filter::FILTER_TYPE_GZIP, encoding_types.front());
}
TEST(FilterTest, GzipContentDispositionFilename) {
MockFilterContext filter_context;
filter_context.SetSdchResponse(false);
const std::string kGzipMime("application/x-tar");
const std::string kContentDisposition("attachment; filename=\"foo.tgz\"");
const std::string kURL("http://foo.com/getfoo.php");
std::vector<Filter::FilterType> encoding_types;
encoding_types.push_back(Filter::FILTER_TYPE_GZIP);
filter_context.SetMimeType(kGzipMime);
filter_context.SetURL(GURL(kURL));
filter_context.SetContentDisposition(kContentDisposition);
Filter::FixupEncodingTypes(filter_context, &encoding_types);
ASSERT_EQ(0U, encoding_types.size());
}
TEST(FilterTest, SdchEncoding) {
const std::string kTextHtmlMime("text/html");
MockFilterContext filter_context;
filter_context.SetSdchResponse(true);
std::vector<Filter::FilterType> encoding_types;
encoding_types.clear();
encoding_types.push_back(Filter::FILTER_TYPE_SDCH);
encoding_types.push_back(Filter::FILTER_TYPE_GZIP);
filter_context.SetMimeType(kTextHtmlMime);
Filter::FixupEncodingTypes(filter_context, &encoding_types);
ASSERT_EQ(2U, encoding_types.size());
EXPECT_EQ(Filter::FILTER_TYPE_SDCH, encoding_types[0]);
EXPECT_EQ(Filter::FILTER_TYPE_GZIP, encoding_types[1]);
encoding_types.clear();
encoding_types.push_back(Filter::FILTER_TYPE_SDCH);
encoding_types.push_back(Filter::FILTER_TYPE_GZIP);
filter_context.SetMimeType("other/type");
Filter::FixupEncodingTypes(filter_context, &encoding_types);
ASSERT_EQ(2U, encoding_types.size());
EXPECT_EQ(Filter::FILTER_TYPE_SDCH, encoding_types[0]);
EXPECT_EQ(Filter::FILTER_TYPE_GZIP, encoding_types[1]);
encoding_types.clear();
encoding_types.push_back(Filter::FILTER_TYPE_SDCH);
Filter::FixupEncodingTypes(filter_context, &encoding_types);
ASSERT_EQ(2U, encoding_types.size());
EXPECT_EQ(Filter::FILTER_TYPE_SDCH, encoding_types[0]);
EXPECT_EQ(Filter::FILTER_TYPE_GZIP_HELPING_SDCH, encoding_types[1]);
}
TEST(FilterTest, MissingSdchEncoding) {
const std::string kTextHtmlMime("text/html");
MockFilterContext filter_context;
filter_context.SetSdchResponse(true);
std::vector<Filter::FilterType> encoding_types;
encoding_types.clear();
filter_context.SetMimeType(kTextHtmlMime);
Filter::FixupEncodingTypes(filter_context, &encoding_types);
ASSERT_EQ(2U, encoding_types.size());
EXPECT_EQ(Filter::FILTER_TYPE_SDCH_POSSIBLE, encoding_types[0]);
EXPECT_EQ(Filter::FILTER_TYPE_GZIP_HELPING_SDCH, encoding_types[1]);
encoding_types.clear();
filter_context.SetMimeType("text/html; charset=UTF-8");
Filter::FixupEncodingTypes(filter_context, &encoding_types);
ASSERT_EQ(2U, encoding_types.size());
EXPECT_EQ(Filter::FILTER_TYPE_SDCH_POSSIBLE, encoding_types[0]);
EXPECT_EQ(Filter::FILTER_TYPE_GZIP_HELPING_SDCH, encoding_types[1]);
encoding_types.clear();
filter_context.SetMimeType("other/mime");
Filter::FixupEncodingTypes(filter_context, &encoding_types);
ASSERT_EQ(2U, encoding_types.size());
EXPECT_EQ(Filter::FILTER_TYPE_SDCH_POSSIBLE, encoding_types[0]);
EXPECT_EQ(Filter::FILTER_TYPE_GZIP_HELPING_SDCH, encoding_types[1]);
}
TEST(FilterTest, Svgz) {
MockFilterContext filter_context;
const std::string kSvgzMime("image/svg+xml");
const std::string kSvgzUrl("http://ignore.com/foo.svgz");
const std::string kSvgUrl("http://ignore.com/foo.svg");
std::vector<Filter::FilterType> encoding_types;
encoding_types.clear();
encoding_types.push_back(Filter::FILTER_TYPE_GZIP);
filter_context.SetDownload(false);
filter_context.SetMimeType(kSvgzMime);
filter_context.SetURL(GURL(kSvgzUrl));
Filter::FixupEncodingTypes(filter_context, &encoding_types);
ASSERT_EQ(1U, encoding_types.size());
EXPECT_EQ(Filter::FILTER_TYPE_GZIP, encoding_types.front());
encoding_types.clear();
encoding_types.push_back(Filter::FILTER_TYPE_GZIP);
filter_context.SetDownload(true);
filter_context.SetMimeType(kSvgzMime);
filter_context.SetURL(GURL(kSvgzUrl));
Filter::FixupEncodingTypes(filter_context, &encoding_types);
EXPECT_TRUE(encoding_types.empty());
encoding_types.clear();
encoding_types.push_back(Filter::FILTER_TYPE_GZIP);
filter_context.SetDownload(false);
filter_context.SetMimeType(kSvgzMime);
filter_context.SetURL(GURL(kSvgUrl));
Filter::FixupEncodingTypes(filter_context, &encoding_types);
ASSERT_EQ(1U, encoding_types.size());
EXPECT_EQ(Filter::FILTER_TYPE_GZIP, encoding_types.front());
encoding_types.clear();
encoding_types.push_back(Filter::FILTER_TYPE_GZIP);
filter_context.SetDownload(true);
filter_context.SetMimeType(kSvgzMime);
filter_context.SetURL(GURL(kSvgUrl));
Filter::FixupEncodingTypes(filter_context, &encoding_types);
ASSERT_EQ(1U, encoding_types.size());
EXPECT_EQ(Filter::FILTER_TYPE_GZIP, encoding_types.front());
}
TEST(FilterTest, UnsupportedMimeGzip) {
MockFilterContext filter_context;
std::vector<Filter::FilterType> encoding_types;
const std::string kTarMime("application/x-tar");
const std::string kCpioMime("application/x-cpio");
const std::string kTarUrl("http://ignore.com/foo.tar");
const std::string kTargzUrl("http://ignore.com/foo.tar.gz");
const std::string kTgzUrl("http://ignore.com/foo.tgz");
const std::string kBadTgzUrl("http://ignore.com/foo.targz");
const std::string kUrl("http://ignore.com/foo");
encoding_types.clear();
encoding_types.push_back(Filter::FILTER_TYPE_GZIP);
filter_context.SetDownload(false);
filter_context.SetMimeType(kTarMime);
filter_context.SetURL(GURL(kTargzUrl));
Filter::FixupEncodingTypes(filter_context, &encoding_types);
EXPECT_TRUE(encoding_types.empty());
encoding_types.clear();
encoding_types.push_back(Filter::FILTER_TYPE_GZIP);
filter_context.SetDownload(false);
filter_context.SetMimeType(kTarMime);
filter_context.SetURL(GURL(kTgzUrl));
Filter::FixupEncodingTypes(filter_context, &encoding_types);
EXPECT_TRUE(encoding_types.empty());
encoding_types.clear();
encoding_types.push_back(Filter::FILTER_TYPE_GZIP);
filter_context.SetDownload(false);
filter_context.SetMimeType(kCpioMime);
filter_context.SetURL(GURL(kTgzUrl));
Filter::FixupEncodingTypes(filter_context, &encoding_types);
EXPECT_TRUE(encoding_types.empty());
encoding_types.clear();
encoding_types.push_back(Filter::FILTER_TYPE_GZIP);
filter_context.SetDownload(true);
filter_context.SetMimeType(kCpioMime);
filter_context.SetURL(GURL(kTgzUrl));
Filter::FixupEncodingTypes(filter_context, &encoding_types);
EXPECT_TRUE(encoding_types.empty());
encoding_types.clear();
encoding_types.push_back(Filter::FILTER_TYPE_GZIP);
filter_context.SetDownload(false);
filter_context.SetMimeType(kTarMime);
filter_context.SetURL(GURL(kUrl));
Filter::FixupEncodingTypes(filter_context, &encoding_types);
ASSERT_EQ(1U, encoding_types.size());
EXPECT_EQ(Filter::FILTER_TYPE_GZIP, encoding_types.front());
encoding_types.clear();
encoding_types.push_back(Filter::FILTER_TYPE_GZIP);
filter_context.SetDownload(false);
filter_context.SetMimeType(kTarMime);
filter_context.SetURL(GURL(kTarUrl));
Filter::FixupEncodingTypes(filter_context, &encoding_types);
ASSERT_EQ(1U, encoding_types.size());
EXPECT_EQ(Filter::FILTER_TYPE_GZIP, encoding_types.front());
encoding_types.clear();
encoding_types.push_back(Filter::FILTER_TYPE_GZIP);
filter_context.SetDownload(false);
filter_context.SetMimeType(kTarMime);
filter_context.SetURL(GURL(kBadTgzUrl));
Filter::FixupEncodingTypes(filter_context, &encoding_types);
ASSERT_EQ(1U, encoding_types.size());
EXPECT_EQ(Filter::FILTER_TYPE_GZIP, encoding_types.front());
encoding_types.clear();
encoding_types.push_back(Filter::FILTER_TYPE_GZIP);
filter_context.SetDownload(true);
filter_context.SetMimeType(kTarMime);
filter_context.SetURL(GURL(kBadTgzUrl));
Filter::FixupEncodingTypes(filter_context, &encoding_types);
ASSERT_EQ(1U, encoding_types.size());
EXPECT_EQ(Filter::FILTER_TYPE_GZIP, encoding_types.front());
}
TEST(FilterTest, SupportedMimeGzip) {
MockFilterContext filter_context;
std::vector<Filter::FilterType> encoding_types;
const std::string kGzUrl("http://ignore.com/foo.gz");
const std::string kUrl("http://ignore.com/foo");
const std::string kHtmlMime("text/html");
const std::string kJavascriptMime("text/javascript");
encoding_types.clear();
encoding_types.push_back(Filter::FILTER_TYPE_GZIP);
filter_context.SetDownload(false);
filter_context.SetMimeType(kHtmlMime);
filter_context.SetURL(GURL(kUrl));
Filter::FixupEncodingTypes(filter_context, &encoding_types);
ASSERT_EQ(1U, encoding_types.size());
EXPECT_EQ(Filter::FILTER_TYPE_GZIP, encoding_types.front());
encoding_types.clear();
encoding_types.push_back(Filter::FILTER_TYPE_GZIP);
filter_context.SetDownload(true);
filter_context.SetMimeType(kHtmlMime);
filter_context.SetURL(GURL(kUrl));
Filter::FixupEncodingTypes(filter_context, &encoding_types);
ASSERT_EQ(1U, encoding_types.size());
EXPECT_EQ(Filter::FILTER_TYPE_GZIP, encoding_types.front());
encoding_types.clear();
encoding_types.push_back(Filter::FILTER_TYPE_GZIP);
filter_context.SetDownload(false);
filter_context.SetMimeType(kHtmlMime);
filter_context.SetURL(GURL(kGzUrl));
Filter::FixupEncodingTypes(filter_context, &encoding_types);
ASSERT_EQ(1U, encoding_types.size());
EXPECT_EQ(Filter::FILTER_TYPE_GZIP, encoding_types.front());
encoding_types.clear();
encoding_types.push_back(Filter::FILTER_TYPE_GZIP);
filter_context.SetDownload(false);
filter_context.SetMimeType(kJavascriptMime);
filter_context.SetURL(GURL(kGzUrl));
Filter::FixupEncodingTypes(filter_context, &encoding_types);
ASSERT_EQ(1U, encoding_types.size());
EXPECT_EQ(Filter::FILTER_TYPE_GZIP, encoding_types.front());
encoding_types.clear();
encoding_types.push_back(Filter::FILTER_TYPE_GZIP);
filter_context.SetDownload(true);
filter_context.SetMimeType(kHtmlMime);
filter_context.SetURL(GURL(kGzUrl));
Filter::FixupEncodingTypes(filter_context, &encoding_types);
EXPECT_TRUE(encoding_types.empty());
}
}