105 lines
3.8 KiB
C++
105 lines
3.8 KiB
C++
// Copyright 2020 Google LLC. All Rights Reserved. This file and proprietary
|
|
// source code may only be used and distributed under the Widevine License
|
|
// Agreement.
|
|
|
|
#include "url_request.h"
|
|
|
|
#include <gmock/gmock.h>
|
|
#include <gtest/gtest.h>
|
|
|
|
#include "test_base.h"
|
|
|
|
namespace wvcdm {
|
|
|
|
namespace {
|
|
|
|
// A sample HTTP response which contains an HTTP header with several
|
|
// standard and non-standard header fields. The fields of interests
|
|
// those beginning with "X-Google-" or "x-google-".
|
|
const std::string kSampleResponse =
|
|
"HTTP/2 301\r\n"
|
|
"Location: https://www.google.com/\r\b"
|
|
"Content-Type: text/html; charset=UTF-8\r\n"
|
|
"X-Google-GFE-Backend-Request-Info: eid=7js14zjC48OpziR0VCF02a0\r\n"
|
|
"x-google-signals: FRAMEWORK=TEST\r\n"
|
|
"X-Google-Binary-Version: 322287520\r\n"
|
|
"X-Google-Request-Cost: 2.50\r\n"
|
|
"Date: Thu, 23 Jul 2020 22:52:55 GMT\r\n"
|
|
"Expires: Sat, 22 Aug 2020 22:52:55 GMT\r\n"
|
|
"Cache-Control: public, max-age=2592000\r\n"
|
|
"x-google-data-version: 322866295\r\n"
|
|
"Server: gws\r\n"
|
|
"X-Google-Debug-Label: /srv/alt/job/service.number/1380\r\n"
|
|
"Content-Length: 220\r\n"
|
|
"X-XSS-Protection: 0\r\n"
|
|
"X-Frame-Options: SAMEORIGIN\r\n"
|
|
"X-Google-Service: web\r\n"
|
|
"X-Google-DOS-Service-Trace: main:home\r\n"
|
|
"Alt-Svc: h3-29=\":443\"; ma=2592000,h3-27=\":443\"; "
|
|
"ma=2592000,h3-25=\":443\"; ma=2592000,h3-T050=\":443\"; "
|
|
"ma=2592000,h3-Q050=\":443\"; ma=2592000,h3-Q046=\":443\"; "
|
|
"ma=2592000,h3-Q043=\":443\"; ma=2592000,quic=\":443\"; ma=2592000; "
|
|
"v=\"46,43\"\r\n"
|
|
"\r\n"
|
|
"<HTML><HEAD><meta http-equiv=\"content-type\" "
|
|
"content=\"text/html;charset=utf-8\">\r\n"
|
|
"<TITLE>301 Moved</TITLE></HEAD><BODY>\r\n"
|
|
"<H1>301 Moved</H1>\r\n"
|
|
"The document has moved\r\n"
|
|
"<A HREF=\"https://www.google.com/\">here</A>.\r\n"
|
|
"</BODY></HTML>\r\n\r\n";
|
|
|
|
// A map containing all the Google debug header fields found in
|
|
// |kSampleResponse|.
|
|
const std::map<std::string, std::string> kSampleFields = {
|
|
{"X-Google-GFE-Backend-Request-Info", "eid=7js14zjC48OpziR0VCF02a0"},
|
|
{"x-google-signals", "FRAMEWORK=TEST"},
|
|
{"X-Google-Binary-Version", "322287520"},
|
|
{"X-Google-Request-Cost", "2.50"},
|
|
{"x-google-data-version", "322866295"},
|
|
{"X-Google-Debug-Label", "/srv/alt/job/service.number/1380"},
|
|
{"X-Google-Service", "web"},
|
|
{"X-Google-DOS-Service-Trace", "main:home"}};
|
|
|
|
// A sample HTTP response which does not contain any special header fields.
|
|
const std::string kFieldlessResponse =
|
|
"HTTP/2 301\r\n"
|
|
"Location: https://www.google.com/\r\b"
|
|
"Content-Type: text/html; charset=UTF-8\r\n"
|
|
"Date: Thu, 23 Jul 2020 22:52:55 GMT\r\n"
|
|
"Content-Length: 220\r\n"
|
|
"\r\n"
|
|
"<HTML><HEAD><meta http-equiv=\"content-type\" "
|
|
"content=\"text/html;charset=utf-8\">\r\n"
|
|
"<TITLE>301 Moved</TITLE></HEAD><BODY>\r\n"
|
|
"<H1>301 Moved</H1>\r\n"
|
|
"The document has moved\r\n"
|
|
"<A HREF=\"https://www.google.com/\">here</A>.\r\n"
|
|
"</BODY></HTML>\r\n\r\n";
|
|
|
|
} // namespace
|
|
|
|
class UrlRequestTest : public WvCdmTestBase {
|
|
protected:
|
|
void SetUp() override { WvCdmTestBase::SetUp(); }
|
|
void TearDown() override {}
|
|
};
|
|
|
|
TEST_F(UrlRequestTest, ParseDebugHeader) {
|
|
// Output map cannot be null.
|
|
EXPECT_FALSE(UrlRequest::GetDebugHeaderFields(kSampleResponse, nullptr));
|
|
std::map<std::string, std::string> fields;
|
|
// Expect false if no debug information can be determined.
|
|
EXPECT_FALSE(UrlRequest::GetDebugHeaderFields(kFieldlessResponse, &fields));
|
|
// Expect success.
|
|
EXPECT_TRUE(UrlRequest::GetDebugHeaderFields(kSampleResponse, &fields));
|
|
EXPECT_EQ(kSampleFields.size(), fields.size());
|
|
for (auto it = kSampleFields.cbegin(); it != kSampleFields.cend(); ++it) {
|
|
auto field = fields.find(it->first);
|
|
EXPECT_NE(fields.end(), field);
|
|
EXPECT_EQ(it->second, field->second) << "Key: \"" << it->first << "\"";
|
|
}
|
|
}
|
|
|
|
} // namespace wvcdm
|