2022-08-26 15:44:59 +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-10-10 23:51:11 +00:00
|
|
|
#include <packager/media/base/http_key_fetcher.h>
|
2013-12-16 17:13:29 +00:00
|
|
|
|
2022-11-04 22:46:41 +00:00
|
|
|
#include <algorithm>
|
|
|
|
|
2023-10-13 19:42:47 +00:00
|
|
|
#include <absl/log/log.h>
|
2023-10-11 08:49:50 +00:00
|
|
|
|
2023-10-10 23:51:11 +00:00
|
|
|
#include <packager/media/test/test_web_server.h>
|
|
|
|
#include <packager/status/status_test_util.h>
|
2013-12-16 17:13:29 +00:00
|
|
|
|
2023-07-13 22:55:36 +00:00
|
|
|
namespace shaka {
|
|
|
|
namespace media {
|
|
|
|
|
2023-07-14 01:55:48 +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:
|
2023-07-18 23:19:52 +00:00
|
|
|
void SetUp() override { ASSERT_TRUE(server_.Start()); }
|
2023-07-14 01:55:48 +00:00
|
|
|
|
|
|
|
TestWebServer server_;
|
|
|
|
};
|
|
|
|
|
|
|
|
TEST_F(HttpKeyFetcherTest, HttpGet) {
|
2023-07-13 22:55:36 +00:00
|
|
|
HttpKeyFetcher fetcher;
|
|
|
|
std::string response;
|
2023-07-18 23:19:52 +00:00
|
|
|
ASSERT_OK(fetcher.Get(server_.ReflectUrl(), &response));
|
2023-07-14 01:55:48 +00:00
|
|
|
EXPECT_NE(std::string::npos, response.find("\"method\":\"GET\""));
|
2023-05-01 23:56:46 +00:00
|
|
|
}
|
|
|
|
|
2023-07-14 01:55:48 +00:00
|
|
|
TEST_F(HttpKeyFetcherTest, HttpPost) {
|
2023-07-13 22:55:36 +00:00
|
|
|
HttpKeyFetcher fetcher;
|
|
|
|
std::string response;
|
2023-07-18 23:19:52 +00:00
|
|
|
ASSERT_OK(fetcher.Post(server_.ReflectUrl(), "", &response));
|
2023-07-14 01:55:48 +00:00
|
|
|
EXPECT_NE(std::string::npos, response.find("\"method\":\"POST\""));
|
2013-12-16 17:13:29 +00:00
|
|
|
}
|
|
|
|
|
2023-07-14 01:55:48 +00:00
|
|
|
TEST_F(HttpKeyFetcherTest, HttpFetchKeys) {
|
2023-07-13 22:55:36 +00:00
|
|
|
HttpKeyFetcher fetcher;
|
|
|
|
std::string response;
|
2023-07-18 23:19:52 +00:00
|
|
|
ASSERT_OK(
|
|
|
|
fetcher.FetchKeys(server_.ReflectUrl(), "foo=62&type=mp4", &response));
|
2023-07-13 22:55:36 +00:00
|
|
|
EXPECT_NE(std::string::npos, response.find("\"foo=62&type=mp4\""));
|
2014-10-07 21:33:08 +00:00
|
|
|
}
|
2014-06-18 01:33:07 +00:00
|
|
|
|
2023-07-14 01:55:48 +00:00
|
|
|
TEST_F(HttpKeyFetcherTest, InvalidUrl) {
|
2023-07-13 22:55:36 +00:00
|
|
|
HttpKeyFetcher fetcher;
|
|
|
|
std::string response;
|
2023-07-18 23:19:52 +00:00
|
|
|
Status status = fetcher.FetchKeys(server_.StatusCodeUrl(404), "", &response);
|
2023-07-13 22:55:36 +00:00
|
|
|
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-07-14 01:55:48 +00:00
|
|
|
TEST_F(HttpKeyFetcherTest, SmallTimeout) {
|
2021-08-04 18:56:44 +00:00
|
|
|
const int32_t kTimeoutInSeconds = 1;
|
2023-07-13 22:55:36 +00:00
|
|
|
HttpKeyFetcher fetcher(kTimeoutInSeconds);
|
|
|
|
std::string response;
|
2023-07-18 23:19:52 +00:00
|
|
|
Status status = fetcher.FetchKeys(server_.DelayUrl(2), "", &response);
|
2023-07-13 22:55:36 +00:00
|
|
|
EXPECT_EQ(error::TIME_OUT, status.error_code());
|
2014-06-18 01:33:07 +00:00
|
|
|
}
|
|
|
|
|
2023-07-14 01:55:48 +00:00
|
|
|
TEST_F(HttpKeyFetcherTest, BigTimeout) {
|
2023-07-13 22:55:36 +00:00
|
|
|
const int32_t kTimeoutInSeconds = 5;
|
|
|
|
HttpKeyFetcher fetcher(kTimeoutInSeconds);
|
|
|
|
std::string response;
|
2023-07-18 23:19:52 +00:00
|
|
|
Status status = fetcher.FetchKeys(server_.DelayUrl(2), "", &response);
|
2023-07-13 22:55:36 +00:00
|
|
|
EXPECT_OK(status);
|
2014-06-18 01:33:07 +00:00
|
|
|
}
|
|
|
|
|
2013-12-16 17:13:29 +00:00
|
|
|
} // namespace media
|
2016-05-20 21:19:33 +00:00
|
|
|
} // namespace shaka
|