2014-02-14 23:21:05 +00:00
|
|
|
// Copyright 2014 Google Inc. All rights reserved.
|
|
|
|
//
|
|
|
|
// 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-02-20 22:38:28 +00:00
|
|
|
#include "media/base/http_fetcher.h"
|
2013-12-16 17:13:29 +00:00
|
|
|
|
|
|
|
#include "base/logging.h"
|
|
|
|
#include "base/strings/string_number_conversions.h"
|
|
|
|
#include "base/strings/string_util.h"
|
|
|
|
#include "media/base/status_test_util.h"
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
const int kHttpOK = 200;
|
|
|
|
const int kHttpNotFound = 404;
|
|
|
|
|
|
|
|
const char kTestUrl[] = "http://packager-test.appspot.com/http_test";
|
|
|
|
const char kTestUrlWithPort[] = "http://packager-test.appspot.com:80/http_test";
|
|
|
|
const char kExpectedGetResponse[] =
|
|
|
|
"<html><head><title>http_test</title></head><body><pre>"
|
|
|
|
"Arguments()</pre></body></html>";
|
|
|
|
const char kPostData[] = "foo=62&type=mp4";
|
|
|
|
const char kExpectedPostResponse[] =
|
|
|
|
"<html><head><title>http_test</title></head><body><pre>"
|
|
|
|
"Arguments([foo]=>62[type]=>mp4)</pre></body></html>";
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
namespace media {
|
|
|
|
|
|
|
|
static void CheckHttpGet(const std::string& url,
|
|
|
|
const std::string& expected_response) {
|
2014-02-20 22:38:28 +00:00
|
|
|
SimpleHttpFetcher fetcher;
|
2013-12-16 17:13:29 +00:00
|
|
|
std::string response;
|
|
|
|
ASSERT_OK(fetcher.Get(url, &response));
|
2014-02-26 23:55:01 +00:00
|
|
|
base::RemoveChars(response, "\r\n\t ", &response);
|
2013-12-16 17:13:29 +00:00
|
|
|
EXPECT_EQ(expected_response, response);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void CheckHttpPost(const std::string& url, const std::string& data,
|
|
|
|
const std::string& expected_response) {
|
2014-02-20 22:38:28 +00:00
|
|
|
SimpleHttpFetcher fetcher;
|
2013-12-16 17:13:29 +00:00
|
|
|
std::string response;
|
|
|
|
ASSERT_OK(fetcher.Post(url, data, &response));
|
2014-02-26 23:55:01 +00:00
|
|
|
base::RemoveChars(response, "\r\n\t ", &response);
|
2013-12-16 17:13:29 +00:00
|
|
|
EXPECT_EQ(expected_response, response);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-02-20 22:38:28 +00:00
|
|
|
TEST(HttpFetcherTest, HttpGet) {
|
2013-12-16 17:13:29 +00:00
|
|
|
CheckHttpGet(kTestUrl, kExpectedGetResponse);
|
|
|
|
}
|
|
|
|
|
2014-02-20 22:38:28 +00:00
|
|
|
TEST(HttpFetcherTest, HttpPost) {
|
2013-12-16 17:13:29 +00:00
|
|
|
CheckHttpPost(kTestUrl, kPostData, kExpectedPostResponse);
|
|
|
|
}
|
|
|
|
|
2014-02-20 22:38:28 +00:00
|
|
|
TEST(HttpFetcherTest, InvalidUrl) {
|
|
|
|
SimpleHttpFetcher fetcher;
|
2013-12-16 17:13:29 +00:00
|
|
|
std::string response;
|
|
|
|
const std::string invalid_url(kTestUrl, sizeof(kTestUrl) - 2);
|
|
|
|
Status status = fetcher.Get(invalid_url, &response);
|
|
|
|
EXPECT_EQ(error::HTTP_FAILURE, status.error_code());
|
|
|
|
EXPECT_TRUE(
|
|
|
|
EndsWith(status.error_message(), base::IntToString(kHttpNotFound), true));
|
|
|
|
}
|
|
|
|
|
2014-02-20 22:38:28 +00:00
|
|
|
TEST(HttpFetcherTest, UrlWithPort) {
|
2013-12-16 17:13:29 +00:00
|
|
|
CheckHttpGet(kTestUrlWithPort, kExpectedGetResponse);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace media
|
|
|
|
|