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
|
|
|
|
2014-10-07 21:33:08 +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>
|
|
|
|
|
|
|
|
#include "glog/logging.h"
|
2023-07-14 01:55:48 +00:00
|
|
|
#include "packager/media/test/test_web_server.h"
|
2022-11-04 22:46:41 +00:00
|
|
|
#include "packager/status/status_test_util.h"
|
2013-12-16 17:13:29 +00:00
|
|
|
|
|
|
|
namespace {
|
2023-07-14 01:55:48 +00:00
|
|
|
// A completely arbitrary port number, unlikely to be in use.
|
|
|
|
const int kTestServerPort = 58405;
|
|
|
|
|
|
|
|
// Reflects back the method, body, and headers of the request as JSON.
|
|
|
|
const char kTestUrl[] = "http://localhost:58405/reflect";
|
|
|
|
// Returns the requested HTTP status code.
|
|
|
|
const char kTestUrl404[] = "http://localhost:58405/status?code=404";
|
|
|
|
// Returns after the requested delay.
|
|
|
|
const char kTestUrlDelayTwoSecs[] = "http://localhost:58405/delay?seconds=2";
|
2023-05-01 23:56:46 +00:00
|
|
|
} // namespace
|
|
|
|
|
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:
|
|
|
|
void SetUp() override { ASSERT_TRUE(server_.Start(kTestServerPort)); }
|
|
|
|
|
|
|
|
private:
|
|
|
|
TestWebServer server_;
|
|
|
|
};
|
|
|
|
|
|
|
|
TEST_F(HttpKeyFetcherTest, HttpGet) {
|
2023-07-13 22:55:36 +00:00
|
|
|
HttpKeyFetcher fetcher;
|
|
|
|
std::string response;
|
|
|
|
ASSERT_OK(fetcher.Get(kTestUrl, &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;
|
|
|
|
ASSERT_OK(fetcher.Post(kTestUrl, "", &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;
|
|
|
|
ASSERT_OK(fetcher.FetchKeys(kTestUrl, "foo=62&type=mp4", &response));
|
|
|
|
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;
|
|
|
|
Status status = fetcher.FetchKeys(kTestUrl404, "", &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-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;
|
|
|
|
Status status = fetcher.FetchKeys(kTestUrlDelayTwoSecs, "", &response);
|
|
|
|
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;
|
|
|
|
Status status = fetcher.FetchKeys(kTestUrlDelayTwoSecs, "", &response);
|
|
|
|
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
|