2023-12-01 17:32:19 +00:00
|
|
|
// Copyright 2014 Google LLC. All rights reserved.
|
2014-02-14 23:21:05 +00:00
|
|
|
//
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file or at
|
|
|
|
// https://developers.google.com/open-source/licenses/bsd
|
2013-12-16 17:13:29 +00:00
|
|
|
|
2023-12-01 17:32:19 +00:00
|
|
|
#include <packager/media/base/http_key_fetcher.h>
|
2013-12-16 17:13:29 +00:00
|
|
|
|
2023-12-01 17:32:19 +00:00
|
|
|
#include <algorithm>
|
2013-12-16 17:13:29 +00:00
|
|
|
|
2023-12-01 17:32:19 +00:00
|
|
|
#include <absl/log/log.h>
|
|
|
|
|
|
|
|
#include <packager/media/test/test_web_server.h>
|
|
|
|
#include <packager/status/status_test_util.h>
|
2013-12-16 17:13:29 +00:00
|
|
|
|
2016-05-20 21:19:33 +00:00
|
|
|
namespace shaka {
|
2013-12-16 17:13:29 +00:00
|
|
|
namespace media {
|
|
|
|
|
2023-12-01 17:32:19 +00:00
|
|
|
// Quoting gtest docs:
|
|
|
|
// "For each TEST_F, GoogleTest will create a fresh test fixture object,
|
|
|
|
// immediately call SetUp(), run the test body, call TearDown(), and then
|
|
|
|
// delete the test fixture object."
|
|
|
|
// So we don't need a TearDown method. The destructor on TestWebServer is good
|
|
|
|
// enough.
|
|
|
|
class HttpKeyFetcherTest : public testing::Test {
|
|
|
|
protected:
|
|
|
|
void SetUp() override { ASSERT_TRUE(server_.Start()); }
|
|
|
|
|
|
|
|
TestWebServer server_;
|
|
|
|
};
|
2013-12-16 17:13:29 +00:00
|
|
|
|
2023-12-01 17:32:19 +00:00
|
|
|
TEST_F(HttpKeyFetcherTest, HttpGet) {
|
2014-10-07 21:33:08 +00:00
|
|
|
HttpKeyFetcher fetcher;
|
2013-12-16 17:13:29 +00:00
|
|
|
std::string response;
|
2023-12-01 17:32:19 +00:00
|
|
|
ASSERT_OK(fetcher.Get(server_.ReflectUrl(), &response));
|
|
|
|
EXPECT_NE(std::string::npos, response.find("\"method\":\"GET\""));
|
2013-12-16 17:13:29 +00:00
|
|
|
}
|
|
|
|
|
2023-12-01 17:32:19 +00:00
|
|
|
TEST_F(HttpKeyFetcherTest, HttpPost) {
|
2014-10-07 21:33:08 +00:00
|
|
|
HttpKeyFetcher fetcher;
|
|
|
|
std::string response;
|
2023-12-01 17:32:19 +00:00
|
|
|
ASSERT_OK(fetcher.Post(server_.ReflectUrl(), "", &response));
|
|
|
|
EXPECT_NE(std::string::npos, response.find("\"method\":\"POST\""));
|
2014-10-07 21:33:08 +00:00
|
|
|
}
|
2014-06-18 01:33:07 +00:00
|
|
|
|
2023-12-01 17:32:19 +00:00
|
|
|
TEST_F(HttpKeyFetcherTest, HttpFetchKeys) {
|
2014-10-07 21:33:08 +00:00
|
|
|
HttpKeyFetcher fetcher;
|
2013-12-16 17:13:29 +00:00
|
|
|
std::string response;
|
2023-12-01 17:32:19 +00:00
|
|
|
ASSERT_OK(
|
|
|
|
fetcher.FetchKeys(server_.ReflectUrl(), "foo=62&type=mp4", &response));
|
|
|
|
EXPECT_NE(std::string::npos, response.find("\"foo=62&type=mp4\""));
|
2013-12-16 17:13:29 +00:00
|
|
|
}
|
|
|
|
|
2023-12-01 17:32:19 +00:00
|
|
|
TEST_F(HttpKeyFetcherTest, InvalidUrl) {
|
2014-10-07 21:33:08 +00:00
|
|
|
HttpKeyFetcher fetcher;
|
|
|
|
std::string response;
|
2023-12-01 17:32:19 +00:00
|
|
|
Status status = fetcher.FetchKeys(server_.StatusCodeUrl(404), "", &response);
|
|
|
|
EXPECT_EQ(error::HTTP_FAILURE, status.error_code());
|
|
|
|
EXPECT_NE(std::string::npos, status.error_message().find("404"));
|
2013-12-16 17:13:29 +00:00
|
|
|
}
|
|
|
|
|
2023-12-01 17:32:19 +00:00
|
|
|
TEST_F(HttpKeyFetcherTest, SmallTimeout) {
|
2021-08-04 18:56:44 +00:00
|
|
|
const int32_t kTimeoutInSeconds = 1;
|
2014-10-07 21:33:08 +00:00
|
|
|
HttpKeyFetcher fetcher(kTimeoutInSeconds);
|
2014-06-18 01:33:07 +00:00
|
|
|
std::string response;
|
2023-12-01 17:32:19 +00:00
|
|
|
Status status = fetcher.FetchKeys(server_.DelayUrl(2), "", &response);
|
2014-06-18 01:33:07 +00:00
|
|
|
EXPECT_EQ(error::TIME_OUT, status.error_code());
|
|
|
|
}
|
|
|
|
|
2023-12-01 17:32:19 +00:00
|
|
|
TEST_F(HttpKeyFetcherTest, BigTimeout) {
|
2021-08-04 18:56:44 +00:00
|
|
|
const int32_t kTimeoutInSeconds = 5;
|
2014-10-07 21:33:08 +00:00
|
|
|
HttpKeyFetcher fetcher(kTimeoutInSeconds);
|
2014-06-18 01:33:07 +00:00
|
|
|
std::string response;
|
2023-12-01 17:32:19 +00:00
|
|
|
Status status = fetcher.FetchKeys(server_.DelayUrl(2), "", &response);
|
2014-06-18 01:33:07 +00:00
|
|
|
EXPECT_OK(status);
|
|
|
|
}
|
|
|
|
|
2013-12-16 17:13:29 +00:00
|
|
|
} // namespace media
|
2016-05-20 21:19:33 +00:00
|
|
|
} // namespace shaka
|