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>
|
2023-05-01 23:56:46 +00:00
|
|
|
#include <chrono>
|
|
|
|
#include <thread>
|
2022-11-04 22:46:41 +00:00
|
|
|
|
|
|
|
#include "glog/logging.h"
|
|
|
|
#include "packager/status/status_test_util.h"
|
2013-12-16 17:13:29 +00:00
|
|
|
|
2023-05-01 23:56:46 +00:00
|
|
|
namespace shaka {
|
|
|
|
namespace media {
|
|
|
|
|
2013-12-16 17:13:29 +00:00
|
|
|
namespace {
|
2023-05-01 23:56:46 +00:00
|
|
|
|
2022-11-04 22:46:41 +00:00
|
|
|
const char kTestUrl[] = "https://httpbin.org/anything";
|
|
|
|
const char kTestUrl404[] = "https://httpbin.org/status/404";
|
|
|
|
const char kTestUrlWithPort[] = "https://httpbin.org:443/anything";
|
|
|
|
const char kTestUrlDelayTwoSecs[] = "https://httpbin.org/delay/2";
|
2013-12-16 17:13:29 +00:00
|
|
|
|
2023-05-01 23:56:46 +00:00
|
|
|
// Tests using httpbin can sometimes be flaky. We get HTTP 502 errors when it
|
|
|
|
// is overloaded. This will retry a test with delays, up to a limit, if the
|
|
|
|
// HTTP status code is 502.
|
|
|
|
void RetryTest(
|
|
|
|
std::function<Status(HttpKeyFetcher&, std::string*)> make_request,
|
|
|
|
std::function<void(Status, std::string&)> check_response,
|
|
|
|
int32_t timeout_in_seconds = 0) {
|
2013-12-16 17:13:29 +00:00
|
|
|
std::string response;
|
2023-05-01 23:56:46 +00:00
|
|
|
Status status;
|
|
|
|
|
|
|
|
for (int i = 0; i < 3; ++i) {
|
|
|
|
HttpKeyFetcher fetcher(timeout_in_seconds);
|
|
|
|
|
|
|
|
response.clear();
|
|
|
|
status = make_request(fetcher, &response);
|
|
|
|
if (testing::Test::HasFailure()) return;
|
|
|
|
|
|
|
|
if (fetcher.http_status_code() != 502) {
|
|
|
|
// Not a 502 error, so take this result.
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delay with exponential increase (1s, 2s, 4s), then loop try again.
|
|
|
|
int delay = 1 << i;
|
|
|
|
LOG(WARNING) << "httpbin failure (" << fetcher.http_status_code() << "): "
|
|
|
|
<< "Delaying " << delay << " seconds and retrying.";
|
|
|
|
std::this_thread::sleep_for(std::chrono::seconds(delay));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Out of retries? Check what we have.
|
|
|
|
check_response(status, response);
|
2013-12-16 17:13:29 +00:00
|
|
|
}
|
|
|
|
|
2023-05-01 23:56:46 +00:00
|
|
|
} // namespace
|
|
|
|
|
|
|
|
TEST(HttpKeyFetcherTest, HttpGet) {
|
|
|
|
RetryTest(
|
|
|
|
// make_request
|
|
|
|
[](HttpKeyFetcher& fetcher, std::string* response) -> Status {
|
|
|
|
return fetcher.Get(kTestUrl, response);
|
|
|
|
},
|
|
|
|
// check_response
|
|
|
|
[](Status status, std::string& response) -> void {
|
|
|
|
ASSERT_OK(status);
|
|
|
|
EXPECT_NE(std::string::npos, response.find("\"method\": \"GET\""));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(HttpKeyFetcherTest, HttpPost) {
|
|
|
|
RetryTest(
|
|
|
|
// make_request
|
|
|
|
[](HttpKeyFetcher& fetcher, std::string* response) -> Status {
|
|
|
|
return fetcher.Post(kTestUrl, "", response);
|
|
|
|
},
|
|
|
|
// check_response
|
|
|
|
[](Status status, std::string& response) -> void {
|
|
|
|
ASSERT_OK(status);
|
|
|
|
EXPECT_NE(std::string::npos, response.find("\"method\": \"POST\""));
|
|
|
|
});
|
2013-12-16 17:13:29 +00:00
|
|
|
}
|
|
|
|
|
2022-11-04 22:46:41 +00:00
|
|
|
TEST(HttpKeyFetcherTest, HttpFetchKeys) {
|
2023-05-01 23:56:46 +00:00
|
|
|
RetryTest(
|
|
|
|
// make_request
|
|
|
|
[](HttpKeyFetcher& fetcher, std::string* response) -> Status {
|
|
|
|
return fetcher.FetchKeys(kTestUrl, "foo=62&type=mp4", response);
|
|
|
|
},
|
|
|
|
// check_response
|
|
|
|
[](Status status, std::string& response) -> void {
|
|
|
|
ASSERT_OK(status);
|
|
|
|
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
|
|
|
|
2022-11-04 22:46:41 +00:00
|
|
|
TEST(HttpKeyFetcherTest, InvalidUrl) {
|
2023-05-01 23:56:46 +00:00
|
|
|
RetryTest(
|
|
|
|
// make_request
|
|
|
|
[](HttpKeyFetcher& fetcher, std::string* response) -> Status {
|
|
|
|
return fetcher.FetchKeys(kTestUrl404, "", response);
|
|
|
|
},
|
|
|
|
// check_response
|
|
|
|
[](Status status, std::string&) -> void {
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2022-11-04 22:46:41 +00:00
|
|
|
TEST(HttpKeyFetcherTest, UrlWithPort) {
|
2023-05-01 23:56:46 +00:00
|
|
|
RetryTest(
|
|
|
|
// make_request
|
|
|
|
[](HttpKeyFetcher& fetcher, std::string* response) -> Status {
|
|
|
|
return fetcher.FetchKeys(kTestUrlWithPort, "", response);
|
|
|
|
},
|
|
|
|
// check_response
|
|
|
|
[](Status status, std::string&) -> void {
|
|
|
|
ASSERT_OK(status);
|
|
|
|
});
|
2013-12-16 17:13:29 +00:00
|
|
|
}
|
|
|
|
|
2022-11-04 22:46:41 +00:00
|
|
|
TEST(HttpKeyFetcherTest, SmallTimeout) {
|
2021-08-04 18:56:44 +00:00
|
|
|
const int32_t kTimeoutInSeconds = 1;
|
2023-05-01 23:56:46 +00:00
|
|
|
|
|
|
|
RetryTest(
|
|
|
|
// make_request
|
|
|
|
[](HttpKeyFetcher& fetcher, std::string* response) -> Status {
|
|
|
|
return fetcher.FetchKeys(kTestUrlDelayTwoSecs, "", response);
|
|
|
|
},
|
|
|
|
// check_response
|
|
|
|
[](Status status, std::string&) -> void {
|
|
|
|
EXPECT_EQ(error::TIME_OUT, status.error_code());
|
|
|
|
},
|
|
|
|
// timeout_in_seconds
|
|
|
|
kTimeoutInSeconds);
|
2014-06-18 01:33:07 +00:00
|
|
|
}
|
|
|
|
|
2022-11-04 22:46:41 +00:00
|
|
|
TEST(HttpKeyFetcherTest, BigTimeout) {
|
2023-05-01 23:56:46 +00:00
|
|
|
const int32_t kTimeoutInSeconds = 10;
|
|
|
|
|
|
|
|
RetryTest(
|
|
|
|
// make_request
|
|
|
|
[](HttpKeyFetcher& fetcher, std::string* response) -> Status {
|
|
|
|
return fetcher.FetchKeys(kTestUrlDelayTwoSecs, "", response);
|
|
|
|
},
|
|
|
|
// check_response
|
|
|
|
[](Status status, std::string&) -> void {
|
|
|
|
ASSERT_OK(status);
|
|
|
|
},
|
|
|
|
// timeout_in_seconds
|
|
|
|
kTimeoutInSeconds);
|
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
|