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"
|
|
|
|
#include "packager/status/status_test_util.h"
|
2013-12-16 17:13:29 +00:00
|
|
|
|
|
|
|
namespace {
|
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
|
|
|
} // namespace
|
|
|
|
|
2016-05-20 21:19:33 +00:00
|
|
|
namespace shaka {
|
2013-12-16 17:13:29 +00:00
|
|
|
namespace media {
|
|
|
|
|
2022-11-04 22:46:41 +00:00
|
|
|
TEST(HttpFetcherTest, HttpGet) {
|
2014-10-07 21:33:08 +00:00
|
|
|
HttpKeyFetcher fetcher;
|
2013-12-16 17:13:29 +00:00
|
|
|
std::string response;
|
2022-11-04 22:46:41 +00:00
|
|
|
ASSERT_OK(fetcher.Get(kTestUrl, &response));
|
|
|
|
EXPECT_NE(std::string::npos, response.find("\"method\": \"GET\""));
|
2013-12-16 17:13:29 +00:00
|
|
|
}
|
|
|
|
|
2022-11-04 22:46:41 +00:00
|
|
|
TEST(HttpFetcherTest, HttpPost) {
|
2014-10-07 21:33:08 +00:00
|
|
|
HttpKeyFetcher fetcher;
|
2013-12-16 17:13:29 +00:00
|
|
|
std::string response;
|
2022-11-04 22:46:41 +00:00
|
|
|
ASSERT_OK(fetcher.Post(kTestUrl, "", &response));
|
|
|
|
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) {
|
2014-10-07 21:33:08 +00:00
|
|
|
HttpKeyFetcher fetcher;
|
|
|
|
std::string response;
|
2022-11-04 22:46:41 +00:00
|
|
|
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
|
|
|
|
2022-11-04 22:46:41 +00:00
|
|
|
TEST(HttpKeyFetcherTest, InvalidUrl) {
|
2014-10-07 21:33:08 +00:00
|
|
|
HttpKeyFetcher fetcher;
|
2013-12-16 17:13:29 +00:00
|
|
|
std::string response;
|
2022-11-04 22:46:41 +00:00
|
|
|
Status status = fetcher.FetchKeys(kTestUrl404, "", &response);
|
2013-12-16 17:13:29 +00:00
|
|
|
EXPECT_EQ(error::HTTP_FAILURE, status.error_code());
|
2022-11-04 22:46:41 +00:00
|
|
|
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) {
|
2014-10-07 21:33:08 +00:00
|
|
|
HttpKeyFetcher fetcher;
|
|
|
|
std::string response;
|
2022-11-04 22:46:41 +00:00
|
|
|
ASSERT_OK(fetcher.FetchKeys(kTestUrlWithPort, "", &response));
|
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;
|
2014-10-07 21:33:08 +00:00
|
|
|
HttpKeyFetcher fetcher(kTimeoutInSeconds);
|
2014-06-18 01:33:07 +00:00
|
|
|
std::string response;
|
2022-11-04 22:46:41 +00:00
|
|
|
Status status = fetcher.FetchKeys(kTestUrlDelayTwoSecs, "", &response);
|
2014-06-18 01:33:07 +00:00
|
|
|
EXPECT_EQ(error::TIME_OUT, status.error_code());
|
|
|
|
}
|
|
|
|
|
2022-11-04 22:46:41 +00:00
|
|
|
TEST(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;
|
2022-11-04 22:46:41 +00:00
|
|
|
Status status = fetcher.FetchKeys(kTestUrlDelayTwoSecs, "", &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
|
2013-12-16 17:13:29 +00:00
|
|
|
|