1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927
|
/*
* Copyright (C) 2015-2018 Team Kodi
* This file is part of Kodi - https://kodi.tv
*
* SPDX-License-Identifier: GPL-2.0-or-later
* See LICENSES/README.md for more information.
*/
#if defined(TARGET_WINDOWS)
# include <windows.h>
#endif
#include <errno.h>
#include <stdlib.h>
#include <gtest/gtest.h>
#include "URL.h"
#include "filesystem/CurlFile.h"
#include "filesystem/File.h"
#include "interfaces/json-rpc/JSONRPC.h"
#include "network/WebServer.h"
#include "network/httprequesthandler/HTTPVfsHandler.h"
#include "network/httprequesthandler/HTTPJsonRpcHandler.h"
#include "settings/MediaSourceSettings.h"
#include "test/TestUtils.h"
#include "utils/JSONVariantParser.h"
#include "utils/StringUtils.h"
#include "utils/URIUtils.h"
#include "utils/Variant.h"
#include <random>
using namespace XFILE;
#define WEBSERVER_HOST "localhost"
#define TEST_URL_JSONRPC "jsonrpc"
#define TEST_FILES_DATA "test"
#define TEST_FILES_DATA_RANGES "range1;range2;range3"
#define TEST_FILES_HTML TEST_FILES_DATA ".html"
#define TEST_FILES_RANGES TEST_FILES_DATA "-ranges.txt"
class TestWebServer : public testing::Test
{
protected:
TestWebServer()
: webserver(),
sourcePath(XBMC_REF_FILE_PATH("xbmc/network/test/data/webserver/"))
{
static uint16_t port;
if (port == 0)
{
std::random_device rd;
std::mt19937 mt(rd());
std::uniform_int_distribution<uint16_t> dist(49152, 65535);
port = dist(mt);
}
webserverPort = port;
baseUrl = StringUtils::Format("http://" WEBSERVER_HOST ":{}", webserverPort);
}
~TestWebServer() override = default;
protected:
void SetUp() override
{
SetupMediaSources();
webserver.Start(webserverPort, "", "");
webserver.RegisterRequestHandler(&m_jsonRpcHandler);
webserver.RegisterRequestHandler(&m_vfsHandler);
}
void TearDown() override
{
if (webserver.IsStarted())
webserver.Stop();
webserver.UnregisterRequestHandler(&m_vfsHandler);
webserver.UnregisterRequestHandler(&m_jsonRpcHandler);
TearDownMediaSources();
}
void SetupMediaSources()
{
CMediaSource source;
source.strName = "WebServer Share";
source.strPath = sourcePath;
source.vecPaths.push_back(sourcePath);
source.m_allowSharing = true;
source.m_iDriveType = CMediaSource::SOURCE_TYPE_LOCAL;
source.m_iLockMode = LOCK_MODE_EVERYONE;
source.m_ignore = true;
CMediaSourceSettings::GetInstance().AddShare("videos", source);
}
void TearDownMediaSources()
{
CMediaSourceSettings::GetInstance().Clear();
}
std::string GetUrl(const std::string& path)
{
if (path.empty())
return baseUrl;
return URIUtils::AddFileToFolder(baseUrl, path);
}
std::string GetUrlOfTestFile(const std::string& testFile)
{
if (testFile.empty())
return "";
std::string path = URIUtils::AddFileToFolder(sourcePath, testFile);
path = CURL::Encode(path);
path = URIUtils::AddFileToFolder("vfs", path);
return GetUrl(path);
}
bool GetLastModifiedOfTestFile(const std::string& testFile, CDateTime& lastModified)
{
CFile file;
if (!file.Open(URIUtils::AddFileToFolder(sourcePath, testFile), READ_NO_CACHE))
return false;
struct __stat64 statBuffer;
if (file.Stat(&statBuffer) != 0)
return false;
struct tm *time;
#ifdef HAVE_LOCALTIME_R
struct tm result = {};
time = localtime_r((time_t*)&statBuffer.st_mtime, &result);
#else
time = localtime((time_t *)&statBuffer.st_mtime);
#endif
if (time == NULL)
return false;
lastModified = *time;
return lastModified.IsValid();
}
void CheckHtmlTestFileResponse(const CCurlFile& curl)
{
// get the HTTP header details
const CHttpHeader& httpHeader = curl.GetHttpHeader();
// Content-Type must be "text/html"
EXPECT_STREQ("text/html", httpHeader.GetMimeType().c_str());
// Must be only one "Content-Length" header
ASSERT_EQ(1U, httpHeader.GetValues(MHD_HTTP_HEADER_CONTENT_LENGTH).size());
// Content-Length must be "4"
// FIXME it is "0" actually
// EXPECT_STREQ("4", httpHeader.GetValue(MHD_HTTP_HEADER_CONTENT_LENGTH).c_str());
// Accept-Ranges must be "bytes"
EXPECT_STREQ("bytes", httpHeader.GetValue(MHD_HTTP_HEADER_ACCEPT_RANGES).c_str());
// check Last-Modified
CDateTime lastModified;
ASSERT_TRUE(GetLastModifiedOfTestFile(TEST_FILES_HTML, lastModified));
ASSERT_STREQ(lastModified.GetAsRFC1123DateTime().c_str(), httpHeader.GetValue(MHD_HTTP_HEADER_LAST_MODIFIED).c_str());
// Cache-Control must contain "mag-age=0" and "no-cache"
std::string cacheControl = httpHeader.GetValue(MHD_HTTP_HEADER_CACHE_CONTROL);
EXPECT_TRUE(cacheControl.find("max-age=0") != std::string::npos);
EXPECT_TRUE(cacheControl.find("no-cache") != std::string::npos);
}
void CheckRangesTestFileResponse(const CCurlFile& curl, int httpStatus = MHD_HTTP_OK, bool empty = false)
{
// get the HTTP header details
const CHttpHeader& httpHeader = curl.GetHttpHeader();
// Only zero or one "Content-Length" headers
ASSERT_GE(1U, httpHeader.GetValues(MHD_HTTP_HEADER_CONTENT_LENGTH).size());
// check the protocol line for the expected HTTP status
std::string httpStatusString = StringUtils::Format(" {} ", httpStatus);
std::string protocolLine = httpHeader.GetProtoLine();
ASSERT_TRUE(protocolLine.find(httpStatusString) != std::string::npos);
// Content-Type must be "text/html"
EXPECT_STREQ("text/plain", httpHeader.GetMimeType().c_str());
// check Content-Length
if (!empty)
{
ASSERT_EQ(1U, httpHeader.GetValues(MHD_HTTP_HEADER_CONTENT_LENGTH).size());
EXPECT_STREQ("20", httpHeader.GetValue(MHD_HTTP_HEADER_CONTENT_LENGTH).c_str());
}
// Accept-Ranges must be "bytes"
EXPECT_STREQ("bytes", httpHeader.GetValue(MHD_HTTP_HEADER_ACCEPT_RANGES).c_str());
// check Last-Modified
CDateTime lastModified;
ASSERT_TRUE(GetLastModifiedOfTestFile(TEST_FILES_RANGES, lastModified));
ASSERT_STREQ(lastModified.GetAsRFC1123DateTime().c_str(), httpHeader.GetValue(MHD_HTTP_HEADER_LAST_MODIFIED).c_str());
// Cache-Control must contain "mag-age=0" and "no-cache"
std::string cacheControl = httpHeader.GetValue(MHD_HTTP_HEADER_CACHE_CONTROL);
EXPECT_TRUE(cacheControl.find("max-age=31536000") != std::string::npos);
EXPECT_TRUE(cacheControl.find("public") != std::string::npos);
}
void CheckRangesTestFileResponse(const CCurlFile& curl, const std::string& result, const CHttpRanges& ranges)
{
// get the HTTP header details
const CHttpHeader& httpHeader = curl.GetHttpHeader();
// Only zero or one "Content-Length" headers
ASSERT_GE(1U, httpHeader.GetValues(MHD_HTTP_HEADER_CONTENT_LENGTH).size());
// check the protocol line for the expected HTTP status
std::string httpStatusString = StringUtils::Format(" {} ", MHD_HTTP_PARTIAL_CONTENT);
std::string protocolLine = httpHeader.GetProtoLine();
ASSERT_TRUE(protocolLine.find(httpStatusString) != std::string::npos);
// Accept-Ranges must be "bytes"
EXPECT_STREQ("bytes", httpHeader.GetValue(MHD_HTTP_HEADER_ACCEPT_RANGES).c_str());
// check Last-Modified
CDateTime lastModified;
ASSERT_TRUE(GetLastModifiedOfTestFile(TEST_FILES_RANGES, lastModified));
ASSERT_STREQ(lastModified.GetAsRFC1123DateTime().c_str(), httpHeader.GetValue(MHD_HTTP_HEADER_LAST_MODIFIED).c_str());
// Cache-Control must contain "mag-age=0" and "no-cache"
std::string cacheControl = httpHeader.GetValue(MHD_HTTP_HEADER_CACHE_CONTROL);
EXPECT_TRUE(cacheControl.find("max-age=31536000") != std::string::npos);
EXPECT_TRUE(cacheControl.find("public") != std::string::npos);
// If there's no range Content-Length must be "20"
if (ranges.IsEmpty())
{
ASSERT_EQ(1U, httpHeader.GetValues(MHD_HTTP_HEADER_CONTENT_LENGTH).size());
EXPECT_STREQ("20", httpHeader.GetValue(MHD_HTTP_HEADER_CONTENT_LENGTH).c_str());
EXPECT_STREQ(TEST_FILES_DATA_RANGES, result.c_str());
return;
}
// check Content-Range
uint64_t firstPosition, lastPosition;
ASSERT_TRUE(ranges.GetFirstPosition(firstPosition));
ASSERT_TRUE(ranges.GetLastPosition(lastPosition));
EXPECT_STREQ(HttpRangeUtils::GenerateContentRangeHeaderValue(firstPosition, lastPosition, 20).c_str(), httpHeader.GetValue(MHD_HTTP_HEADER_CONTENT_RANGE).c_str());
std::string expectedContent = TEST_FILES_DATA_RANGES;
const std::string expectedContentType = "text/plain";
if (ranges.Size() == 1)
{
// Content-Type must be "text/html"
EXPECT_STREQ(expectedContentType.c_str(), httpHeader.GetMimeType().c_str());
// check the content
CHttpRange firstRange;
ASSERT_TRUE(ranges.GetFirst(firstRange));
expectedContent = expectedContent.substr(static_cast<size_t>(firstRange.GetFirstPosition()), static_cast<size_t>(firstRange.GetLength()));
EXPECT_STREQ(expectedContent.c_str(), result.c_str());
// and Content-Length
ASSERT_EQ(1U, httpHeader.GetValues(MHD_HTTP_HEADER_CONTENT_LENGTH).size());
EXPECT_STREQ(std::to_string(static_cast<unsigned int>(expectedContent.size())).c_str(),
httpHeader.GetValue(MHD_HTTP_HEADER_CONTENT_LENGTH).c_str());
return;
}
// Content-Type contains the multipart boundary
const std::string expectedMimeType = "multipart/byteranges";
std::string mimeType = httpHeader.GetMimeType();
ASSERT_STREQ(expectedMimeType.c_str(), mimeType.c_str());
std::string contentType = httpHeader.GetValue(MHD_HTTP_HEADER_CONTENT_TYPE);
std::string contentTypeStart = expectedMimeType + "; boundary=";
// it must start with "multipart/byteranges; boundary=" followed by the boundary
ASSERT_EQ(0U, contentType.find(contentTypeStart));
ASSERT_GT(contentType.size(), contentTypeStart.size());
// extract the boundary
std::string multipartBoundary = contentType.substr(contentTypeStart.size());
ASSERT_FALSE(multipartBoundary.empty());
multipartBoundary = "--" + multipartBoundary;
ASSERT_EQ(0U, result.find(multipartBoundary));
std::vector<std::string> rangeParts = StringUtils::Split(result, multipartBoundary);
// the first part is not really a part and is therefore empty (the place before the first boundary)
ASSERT_TRUE(rangeParts.front().empty());
rangeParts.erase(rangeParts.begin());
// the last part is the end of the end multipart boundary
ASSERT_STREQ("--", rangeParts.back().c_str());
rangeParts.erase(rangeParts.begin() + rangeParts.size() - 1);
ASSERT_EQ(ranges.Size(), rangeParts.size());
for (size_t i = 0; i < rangeParts.size(); ++i)
{
std::string data = rangeParts.at(i);
StringUtils::Trim(data, " \r\n");
// find the separator between header and data
size_t pos = data.find("\r\n\r\n");
ASSERT_NE(std::string::npos, pos);
std::string header = data.substr(0, pos + 4);
data = data.substr(pos + 4);
// get the expected range
CHttpRange range;
ASSERT_TRUE(ranges.Get(i, range));
// parse the header of the range part
CHttpHeader rangeHeader;
rangeHeader.Parse(header);
// check Content-Type
EXPECT_STREQ(expectedContentType.c_str(), rangeHeader.GetMimeType().c_str());
// parse and check Content-Range
std::string contentRangeHeader = rangeHeader.GetValue(MHD_HTTP_HEADER_CONTENT_RANGE);
std::vector<std::string> contentRangeHeaderParts = StringUtils::Split(contentRangeHeader, "/");
ASSERT_EQ(2U, contentRangeHeaderParts.size());
// check the length of the range
EXPECT_TRUE(StringUtils::IsNaturalNumber(contentRangeHeaderParts.back()));
uint64_t contentRangeLength = str2uint64(contentRangeHeaderParts.back());
EXPECT_EQ(range.GetLength(), contentRangeLength);
// remove the leading "bytes " string from the range definition
std::string contentRangeDefinition = contentRangeHeaderParts.front();
ASSERT_EQ(0U, contentRangeDefinition.find("bytes "));
contentRangeDefinition = contentRangeDefinition.substr(6);
// check the start and end positions of the range
std::vector<std::string> contentRangeParts = StringUtils::Split(contentRangeDefinition, "-");
ASSERT_EQ(2U, contentRangeParts.size());
EXPECT_TRUE(StringUtils::IsNaturalNumber(contentRangeParts.front()));
uint64_t contentRangeStart = str2uint64(contentRangeParts.front());
EXPECT_EQ(range.GetFirstPosition(), contentRangeStart);
EXPECT_TRUE(StringUtils::IsNaturalNumber(contentRangeParts.back()));
uint64_t contentRangeEnd = str2uint64(contentRangeParts.back());
EXPECT_EQ(range.GetLastPosition(), contentRangeEnd);
// make sure the length of the content matches the one of the expected range
EXPECT_EQ(range.GetLength(), data.size());
EXPECT_STREQ(expectedContent.substr(static_cast<size_t>(range.GetFirstPosition()), static_cast<size_t>(range.GetLength())).c_str(), data.c_str());
}
}
std::string GenerateRangeHeaderValue(unsigned int start, unsigned int end)
{
return StringUtils::Format("bytes={}-{}", start, end);
}
CWebServer webserver;
CHTTPJsonRpcHandler m_jsonRpcHandler;
CHTTPVfsHandler m_vfsHandler;
std::string baseUrl;
std::string sourcePath;
uint16_t webserverPort;
};
TEST_F(TestWebServer, IsStarted)
{
ASSERT_TRUE(webserver.IsStarted());
}
TEST_F(TestWebServer, CanGetJsonRpcApiDescriptionWithHttpGet)
{
std::string result;
CCurlFile curl;
ASSERT_TRUE(curl.Get(GetUrl(TEST_URL_JSONRPC), result));
ASSERT_FALSE(result.empty());
// get the HTTP header details
const CHttpHeader& httpHeader = curl.GetHttpHeader();
// Content-Length header must be present
ASSERT_EQ(1U, httpHeader.GetValues(MHD_HTTP_HEADER_CONTENT_LENGTH).size());
// Content-Type must be "application/json"
EXPECT_STREQ("application/json", httpHeader.GetMimeType().c_str());
// Accept-Ranges must be "none"
EXPECT_STREQ("none", httpHeader.GetValue(MHD_HTTP_HEADER_ACCEPT_RANGES).c_str());
// Cache-Control must contain "mag-age=0" and "no-cache"
std::string cacheControl = httpHeader.GetValue(MHD_HTTP_HEADER_CACHE_CONTROL);
EXPECT_TRUE(cacheControl.find("max-age=0") != std::string::npos);
EXPECT_TRUE(cacheControl.find("no-cache") != std::string::npos);
}
TEST_F(TestWebServer, CanReadDataOverJsonRpcWithHttpGet)
{
// initialized JSON-RPC
JSONRPC::CJSONRPC::Initialize();
std::string result;
CCurlFile curl;
ASSERT_TRUE(curl.Get(GetUrl(TEST_URL_JSONRPC "?request=" + CURL::Encode("{ \"jsonrpc\": \"2.0\", \"method\": \"JSONRPC.Version\", \"id\": 1 }")), result));
ASSERT_FALSE(result.empty());
// parse the JSON-RPC response
CVariant resultObj;
ASSERT_TRUE(CJSONVariantParser::Parse(result, resultObj));
// make sure it's an object
ASSERT_TRUE(resultObj.isObject());
// get the HTTP header details
const CHttpHeader& httpHeader = curl.GetHttpHeader();
// Content-Length header must be present
ASSERT_EQ(1U, httpHeader.GetValues(MHD_HTTP_HEADER_CONTENT_LENGTH).size());
// Content-Type must be "application/json"
EXPECT_STREQ("application/json", httpHeader.GetMimeType().c_str());
// Accept-Ranges must be "none"
EXPECT_STREQ("none", httpHeader.GetValue(MHD_HTTP_HEADER_ACCEPT_RANGES).c_str());
// Cache-Control must contain "mag-age=0" and "no-cache"
std::string cacheControl = httpHeader.GetValue(MHD_HTTP_HEADER_CACHE_CONTROL);
EXPECT_TRUE(cacheControl.find("max-age=0") != std::string::npos);
EXPECT_TRUE(cacheControl.find("no-cache") != std::string::npos);
// uninitialize JSON-RPC
JSONRPC::CJSONRPC::Cleanup();
}
TEST_F(TestWebServer, CannotModifyOverJsonRpcWithHttpGet)
{
// initialized JSON-RPC
JSONRPC::CJSONRPC::Initialize();
std::string result;
CCurlFile curl;
ASSERT_TRUE(curl.Get(GetUrl(TEST_URL_JSONRPC "?request=" + CURL::Encode("{ \"jsonrpc\": \"2.0\", \"method\": \"Input.Left\", \"id\": 1 }")), result));
ASSERT_FALSE(result.empty());
// parse the JSON-RPC response
CVariant resultObj;
ASSERT_TRUE(CJSONVariantParser::Parse(result, resultObj));
// make sure it's an object
ASSERT_TRUE(resultObj.isObject());
// it must contain the "error" property with the "Bad client permission" error code
ASSERT_TRUE(resultObj.isMember("error") && resultObj["error"].isObject());
ASSERT_TRUE(resultObj["error"].isMember("code") && resultObj["error"]["code"].isInteger());
ASSERT_EQ(JSONRPC::BadPermission, resultObj["error"]["code"].asInteger());
// get the HTTP header details
const CHttpHeader& httpHeader = curl.GetHttpHeader();
// Content-Length header must be present
ASSERT_EQ(1U, httpHeader.GetValues(MHD_HTTP_HEADER_CONTENT_LENGTH).size());
// Content-Type must be "application/json"
EXPECT_STREQ("application/json", httpHeader.GetMimeType().c_str());
// Accept-Ranges must be "none"
EXPECT_STREQ("none", httpHeader.GetValue(MHD_HTTP_HEADER_ACCEPT_RANGES).c_str());
// Cache-Control must contain "mag-age=0" and "no-cache"
std::string cacheControl = httpHeader.GetValue(MHD_HTTP_HEADER_CACHE_CONTROL);
EXPECT_TRUE(cacheControl.find("max-age=0") != std::string::npos);
EXPECT_TRUE(cacheControl.find("no-cache") != std::string::npos);
// uninitialize JSON-RPC
JSONRPC::CJSONRPC::Cleanup();
}
TEST_F(TestWebServer, CanReadDataOverJsonRpcWithHttpPost)
{
// initialized JSON-RPC
JSONRPC::CJSONRPC::Initialize();
std::string result;
CCurlFile curl;
curl.SetMimeType("application/json");
ASSERT_TRUE(curl.Post(GetUrl(TEST_URL_JSONRPC), "{ \"jsonrpc\": \"2.0\", \"method\": \"JSONRPC.Version\", \"id\": 1 }", result));
ASSERT_FALSE(result.empty());
// parse the JSON-RPC response
CVariant resultObj;
ASSERT_TRUE(CJSONVariantParser::Parse(result, resultObj));
// make sure it's an object
ASSERT_TRUE(resultObj.isObject());
// get the HTTP header details
const CHttpHeader& httpHeader = curl.GetHttpHeader();
// Content-Length header must be present
ASSERT_EQ(1U, httpHeader.GetValues(MHD_HTTP_HEADER_CONTENT_LENGTH).size());
// Content-Type must be "application/json"
EXPECT_STREQ("application/json", httpHeader.GetMimeType().c_str());
// Accept-Ranges must be "none"
EXPECT_STREQ("none", httpHeader.GetValue(MHD_HTTP_HEADER_ACCEPT_RANGES).c_str());
// Cache-Control must contain "mag-age=0" and "no-cache"
std::string cacheControl = httpHeader.GetValue(MHD_HTTP_HEADER_CACHE_CONTROL);
EXPECT_TRUE(cacheControl.find("max-age=0") != std::string::npos);
EXPECT_TRUE(cacheControl.find("no-cache") != std::string::npos);
// uninitialize JSON-RPC
JSONRPC::CJSONRPC::Cleanup();
}
TEST_F(TestWebServer, CanModifyOverJsonRpcWithHttpPost)
{
// initialized JSON-RPC
JSONRPC::CJSONRPC::Initialize();
std::string result;
CCurlFile curl;
curl.SetMimeType("application/json");
ASSERT_TRUE(curl.Post(GetUrl(TEST_URL_JSONRPC), "{ \"jsonrpc\": \"2.0\", \"method\": \"Input.Left\", \"id\": 1 }", result));
ASSERT_FALSE(result.empty());
// parse the JSON-RPC response
CVariant resultObj;
ASSERT_TRUE(CJSONVariantParser::Parse(result, resultObj));
// make sure it's an object
ASSERT_TRUE(resultObj.isObject());
// it must contain the "result" property with the "OK" value
ASSERT_TRUE(resultObj.isMember("result") && resultObj["result"].isString());
EXPECT_STREQ("OK", resultObj["result"].asString().c_str());
// get the HTTP header details
const CHttpHeader& httpHeader = curl.GetHttpHeader();
// Content-Length header must be present
ASSERT_EQ(1U, httpHeader.GetValues(MHD_HTTP_HEADER_CONTENT_LENGTH).size());
// Content-Type must be "application/json"
EXPECT_STREQ("application/json", httpHeader.GetMimeType().c_str());
// Accept-Ranges must be "none"
EXPECT_STREQ("none", httpHeader.GetValue(MHD_HTTP_HEADER_ACCEPT_RANGES).c_str());
// Cache-Control must contain "mag-age=0" and "no-cache"
std::string cacheControl = httpHeader.GetValue(MHD_HTTP_HEADER_CACHE_CONTROL);
EXPECT_TRUE(cacheControl.find("max-age=0") != std::string::npos);
EXPECT_TRUE(cacheControl.find("no-cache") != std::string::npos);
// uninitialize JSON-RPC
JSONRPC::CJSONRPC::Cleanup();
}
TEST_F(TestWebServer, CanNotHeadNonExistingFile)
{
CCurlFile curl;
ASSERT_FALSE(curl.Exists(CURL(GetUrlOfTestFile("file_does_not_exist"))));
}
TEST_F(TestWebServer, CanHeadFile)
{
CCurlFile curl;
ASSERT_TRUE(curl.Exists(CURL(GetUrlOfTestFile(TEST_FILES_HTML))));
CheckHtmlTestFileResponse(curl);
}
TEST_F(TestWebServer, CanNotGetNonExistingFile)
{
std::string result;
CCurlFile curl;
ASSERT_FALSE(curl.Get(GetUrlOfTestFile("file_does_not_exist"), result));
ASSERT_TRUE(result.empty());
}
TEST_F(TestWebServer, CanGetFile)
{
std::string result;
CCurlFile curl;
curl.SetRequestHeader(MHD_HTTP_HEADER_RANGE, "");
ASSERT_TRUE(curl.Get(GetUrlOfTestFile(TEST_FILES_HTML), result));
ASSERT_STREQ(TEST_FILES_DATA, result.c_str());
CheckHtmlTestFileResponse(curl);
}
TEST_F(TestWebServer, CanGetFileForcingNoCache)
{
// check non-cacheable HTML with Control-Cache: no-cache
std::string result;
CCurlFile curl_html;
curl_html.SetRequestHeader(MHD_HTTP_HEADER_RANGE, "");
curl_html.SetRequestHeader(MHD_HTTP_HEADER_CACHE_CONTROL, "no-cache");
ASSERT_TRUE(curl_html.Get(GetUrlOfTestFile(TEST_FILES_HTML), result));
EXPECT_STREQ(TEST_FILES_DATA, result.c_str());
CheckHtmlTestFileResponse(curl_html);
// check cacheable text file with Control-Cache: no-cache
result.clear();
CCurlFile curl_txt;
curl_txt.SetRequestHeader(MHD_HTTP_HEADER_RANGE, "");
curl_txt.SetRequestHeader(MHD_HTTP_HEADER_CACHE_CONTROL, "no-cache");
ASSERT_TRUE(curl_txt.Get(GetUrlOfTestFile(TEST_FILES_RANGES), result));
EXPECT_STREQ(TEST_FILES_DATA_RANGES, result.c_str());
CheckRangesTestFileResponse(curl_txt);
// check cacheable text file with deprecated Pragma: no-cache
result.clear();
CCurlFile curl_txt_pragma;
curl_txt_pragma.SetRequestHeader(MHD_HTTP_HEADER_RANGE, "");
curl_txt_pragma.SetRequestHeader(MHD_HTTP_HEADER_PRAGMA, "no-cache");
ASSERT_TRUE(curl_txt_pragma.Get(GetUrlOfTestFile(TEST_FILES_RANGES), result));
EXPECT_STREQ(TEST_FILES_DATA_RANGES, result.c_str());
CheckRangesTestFileResponse(curl_txt_pragma);
}
TEST_F(TestWebServer, CanGetCachedFileWithOlderIfModifiedSince)
{
// get the last modified date of the file
CDateTime lastModified;
ASSERT_TRUE(GetLastModifiedOfTestFile(TEST_FILES_RANGES, lastModified));
CDateTime lastModifiedOlder = lastModified - CDateTimeSpan(1, 0, 0, 0);
// get the file with an older If-Modified-Since value
std::string result;
CCurlFile curl;
curl.SetRequestHeader(MHD_HTTP_HEADER_RANGE, "");
curl.SetRequestHeader(MHD_HTTP_HEADER_IF_MODIFIED_SINCE, lastModifiedOlder.GetAsRFC1123DateTime());
ASSERT_TRUE(curl.Get(GetUrlOfTestFile(TEST_FILES_RANGES), result));
EXPECT_STREQ(TEST_FILES_DATA_RANGES, result.c_str());
CheckRangesTestFileResponse(curl);
}
TEST_F(TestWebServer, CanGetCachedFileWithExactIfModifiedSince)
{
// get the last modified date of the file
CDateTime lastModified;
ASSERT_TRUE(GetLastModifiedOfTestFile(TEST_FILES_RANGES, lastModified));
// get the file with the exact If-Modified-Since value
std::string result;
CCurlFile curl;
curl.SetRequestHeader(MHD_HTTP_HEADER_RANGE, "");
curl.SetRequestHeader(MHD_HTTP_HEADER_IF_MODIFIED_SINCE, lastModified.GetAsRFC1123DateTime());
ASSERT_TRUE(curl.Get(GetUrlOfTestFile(TEST_FILES_RANGES), result));
ASSERT_TRUE(result.empty());
CheckRangesTestFileResponse(curl, MHD_HTTP_NOT_MODIFIED, true);
}
TEST_F(TestWebServer, CanGetCachedFileWithNewerIfModifiedSince)
{
// get the last modified date of the file
CDateTime lastModified;
ASSERT_TRUE(GetLastModifiedOfTestFile(TEST_FILES_RANGES, lastModified));
CDateTime lastModifiedNewer = lastModified + CDateTimeSpan(1, 0, 0, 0);
// get the file with a newer If-Modified-Since value
std::string result;
CCurlFile curl;
curl.SetRequestHeader(MHD_HTTP_HEADER_RANGE, "");
curl.SetRequestHeader(MHD_HTTP_HEADER_IF_MODIFIED_SINCE,
lastModifiedNewer.GetAsRFC1123DateTime());
ASSERT_TRUE(curl.Get(GetUrlOfTestFile(TEST_FILES_RANGES), result));
ASSERT_TRUE(result.empty());
CheckRangesTestFileResponse(curl, MHD_HTTP_NOT_MODIFIED, true);
}
TEST_F(TestWebServer, CanGetCachedFileWithNewerIfModifiedSinceForcingNoCache)
{
// get the last modified date of the file
CDateTime lastModified;
ASSERT_TRUE(GetLastModifiedOfTestFile(TEST_FILES_RANGES, lastModified));
CDateTime lastModifiedNewer = lastModified + CDateTimeSpan(1, 0, 0, 0);
// get the file with a newer If-Modified-Since value but forcing no caching
std::string result;
CCurlFile curl;
curl.SetRequestHeader(MHD_HTTP_HEADER_RANGE, "");
curl.SetRequestHeader(MHD_HTTP_HEADER_IF_MODIFIED_SINCE, lastModifiedNewer.GetAsRFC1123DateTime());
curl.SetRequestHeader(MHD_HTTP_HEADER_CACHE_CONTROL, "no-cache");
ASSERT_TRUE(curl.Get(GetUrlOfTestFile(TEST_FILES_RANGES), result));
EXPECT_STREQ(TEST_FILES_DATA_RANGES, result.c_str());
CheckRangesTestFileResponse(curl);
}
TEST_F(TestWebServer, CanGetCachedFileWithOlderIfUnmodifiedSince)
{
// get the last modified date of the file
CDateTime lastModified;
ASSERT_TRUE(GetLastModifiedOfTestFile(TEST_FILES_RANGES, lastModified));
CDateTime lastModifiedOlder = lastModified - CDateTimeSpan(1, 0, 0, 0);
// get the file with an older If-Unmodified-Since value
std::string result;
CCurlFile curl;
curl.SetRequestHeader(MHD_HTTP_HEADER_RANGE, "");
curl.SetRequestHeader(MHD_HTTP_HEADER_IF_UNMODIFIED_SINCE, lastModifiedOlder.GetAsRFC1123DateTime());
ASSERT_FALSE(curl.Get(GetUrlOfTestFile(TEST_FILES_RANGES), result));
}
TEST_F(TestWebServer, CanGetCachedFileWithExactIfUnmodifiedSince)
{
// get the last modified date of the file
CDateTime lastModified;
ASSERT_TRUE(GetLastModifiedOfTestFile(TEST_FILES_RANGES, lastModified));
// get the file with an older If-Unmodified-Since value
std::string result;
CCurlFile curl;
curl.SetRequestHeader(MHD_HTTP_HEADER_RANGE, "");
curl.SetRequestHeader(MHD_HTTP_HEADER_IF_UNMODIFIED_SINCE, lastModified.GetAsRFC1123DateTime());
ASSERT_TRUE(curl.Get(GetUrlOfTestFile(TEST_FILES_RANGES), result));
EXPECT_STREQ(TEST_FILES_DATA_RANGES, result.c_str());
CheckRangesTestFileResponse(curl);
}
TEST_F(TestWebServer, CanGetCachedFileWithNewerIfUnmodifiedSince)
{
// get the last modified date of the file
CDateTime lastModified;
ASSERT_TRUE(GetLastModifiedOfTestFile(TEST_FILES_RANGES, lastModified));
CDateTime lastModifiedNewer = lastModified + CDateTimeSpan(1, 0, 0, 0);
// get the file with a newer If-Unmodified-Since value
std::string result;
CCurlFile curl;
curl.SetRequestHeader(MHD_HTTP_HEADER_RANGE, "");
curl.SetRequestHeader(MHD_HTTP_HEADER_IF_UNMODIFIED_SINCE, lastModifiedNewer.GetAsRFC1123DateTime());
ASSERT_TRUE(curl.Get(GetUrlOfTestFile(TEST_FILES_RANGES), result));
EXPECT_STREQ(TEST_FILES_DATA_RANGES, result.c_str());
CheckRangesTestFileResponse(curl);
}
TEST_F(TestWebServer, CanGetRangedFileRange0_)
{
const std::string rangedFileContent = TEST_FILES_DATA_RANGES;
const std::string range = "bytes=0-";
CHttpRanges ranges;
ASSERT_TRUE(ranges.Parse(range, rangedFileContent.size()));
// get the whole file but specify the beginning of the range
std::string result;
CCurlFile curl;
curl.SetRequestHeader(MHD_HTTP_HEADER_RANGE, range);
ASSERT_TRUE(curl.Get(GetUrlOfTestFile(TEST_FILES_RANGES), result));
CheckRangesTestFileResponse(curl, result, ranges);
}
TEST_F(TestWebServer, CanGetRangedFileRange0_End)
{
const std::string rangedFileContent = TEST_FILES_DATA_RANGES;
const std::string range = GenerateRangeHeaderValue(0, rangedFileContent.size());
CHttpRanges ranges;
ASSERT_TRUE(ranges.Parse(range, rangedFileContent.size()));
// get the whole file but specify the whole range
std::string result;
CCurlFile curl;
curl.SetRequestHeader(MHD_HTTP_HEADER_RANGE, range);
ASSERT_TRUE(curl.Get(GetUrlOfTestFile(TEST_FILES_RANGES), result));
CheckRangesTestFileResponse(curl, result, ranges);
}
TEST_F(TestWebServer, CanGetRangedFileRange0_2xEnd)
{
const std::string rangedFileContent = TEST_FILES_DATA_RANGES;
const std::string range = GenerateRangeHeaderValue(0, rangedFileContent.size() * 2);
CHttpRanges ranges;
ASSERT_TRUE(ranges.Parse(range, rangedFileContent.size()));
// get the whole file but specify a larger range
std::string result;
CCurlFile curl;
curl.SetRequestHeader(MHD_HTTP_HEADER_RANGE, range);
ASSERT_TRUE(curl.Get(GetUrlOfTestFile(TEST_FILES_RANGES), result));
CheckRangesTestFileResponse(curl, result, ranges);
}
TEST_F(TestWebServer, CanGetRangedFileRange0_First)
{
const std::string rangedFileContent = TEST_FILES_DATA_RANGES;
std::vector<std::string> rangedContent = StringUtils::Split(TEST_FILES_DATA_RANGES, ";");
const std::string range = GenerateRangeHeaderValue(0, rangedContent.front().size() - 1);
CHttpRanges ranges;
ASSERT_TRUE(ranges.Parse(range, rangedFileContent.size()));
// get the whole file but specify a larger range
std::string result;
CCurlFile curl;
curl.SetRequestHeader(MHD_HTTP_HEADER_RANGE, range);
ASSERT_TRUE(curl.Get(GetUrlOfTestFile(TEST_FILES_RANGES), result));
CheckRangesTestFileResponse(curl, result, ranges);
}
TEST_F(TestWebServer, CanGetRangedFileRangeFirst_Second)
{
const std::string rangedFileContent = TEST_FILES_DATA_RANGES;
std::vector<std::string> rangedContent = StringUtils::Split(TEST_FILES_DATA_RANGES, ";");
const std::string range = GenerateRangeHeaderValue(rangedContent.front().size() + 1, rangedContent.front().size() + 1 + rangedContent.at(2).size() - 1);
CHttpRanges ranges;
ASSERT_TRUE(ranges.Parse(range, rangedFileContent.size()));
// get the whole file but specify a larger range
std::string result;
CCurlFile curl;
curl.SetRequestHeader(MHD_HTTP_HEADER_RANGE, range);
ASSERT_TRUE(curl.Get(GetUrlOfTestFile(TEST_FILES_RANGES), result));
CheckRangesTestFileResponse(curl, result, ranges);
}
TEST_F(TestWebServer, CanGetRangedFileRange_Last)
{
const std::string rangedFileContent = TEST_FILES_DATA_RANGES;
std::vector<std::string> rangedContent = StringUtils::Split(TEST_FILES_DATA_RANGES, ";");
const std::string range =
StringUtils::Format("bytes=-{}", static_cast<unsigned int>(rangedContent.back().size()));
CHttpRanges ranges;
ASSERT_TRUE(ranges.Parse(range, rangedFileContent.size()));
// get the whole file but specify a larger range
std::string result;
CCurlFile curl;
curl.SetRequestHeader(MHD_HTTP_HEADER_RANGE, range);
ASSERT_TRUE(curl.Get(GetUrlOfTestFile(TEST_FILES_RANGES), result));
CheckRangesTestFileResponse(curl, result, ranges);
}
TEST_F(TestWebServer, CanGetRangedFileRangeFirstSecond)
{
const std::string rangedFileContent = TEST_FILES_DATA_RANGES;
std::vector<std::string> rangedContent = StringUtils::Split(TEST_FILES_DATA_RANGES, ";");
const std::string range = StringUtils::Format(
"bytes=0-{},{}-{}", static_cast<unsigned int>(rangedContent.front().size() - 1),
static_cast<unsigned int>(rangedContent.front().size() + 1),
static_cast<unsigned int>(rangedContent.front().size() + 1) +
static_cast<unsigned int>(rangedContent.at(1).size() - 1));
CHttpRanges ranges;
ASSERT_TRUE(ranges.Parse(range, rangedFileContent.size()));
// get the whole file but specify a larger range
std::string result;
CCurlFile curl;
curl.SetRequestHeader(MHD_HTTP_HEADER_RANGE, range);
ASSERT_TRUE(curl.Get(GetUrlOfTestFile(TEST_FILES_RANGES), result));
CheckRangesTestFileResponse(curl, result, ranges);
}
TEST_F(TestWebServer, CanGetRangedFileRangeFirstSecondLast)
{
const std::string rangedFileContent = TEST_FILES_DATA_RANGES;
std::vector<std::string> rangedContent = StringUtils::Split(TEST_FILES_DATA_RANGES, ";");
const std::string range = StringUtils::Format(
"bytes=0-{},{}-{},-{}", static_cast<unsigned int>(rangedContent.front().size() - 1),
static_cast<unsigned int>(rangedContent.front().size() + 1),
static_cast<unsigned int>(rangedContent.front().size() + 1) +
static_cast<unsigned int>(rangedContent.at(1).size() - 1),
static_cast<unsigned int>(rangedContent.back().size()));
CHttpRanges ranges;
ASSERT_TRUE(ranges.Parse(range, rangedFileContent.size()));
// get the whole file but specify a larger range
std::string result;
CCurlFile curl;
curl.SetRequestHeader(MHD_HTTP_HEADER_RANGE, range);
ASSERT_TRUE(curl.Get(GetUrlOfTestFile(TEST_FILES_RANGES), result));
CheckRangesTestFileResponse(curl, result, ranges);
}
TEST_F(TestWebServer, CanGetCachedRangedFileWithOlderIfRange)
{
const std::string rangedFileContent = TEST_FILES_DATA_RANGES;
const std::string range = "bytes=0-";
CHttpRanges ranges;
ASSERT_TRUE(ranges.Parse(range, rangedFileContent.size()));
// get the last modified date of the file
CDateTime lastModified;
ASSERT_TRUE(GetLastModifiedOfTestFile(TEST_FILES_RANGES, lastModified));
CDateTime lastModifiedOlder = lastModified - CDateTimeSpan(1, 0, 0, 0);
// get the whole file (but ranged) with an older If-Range value
std::string result;
CCurlFile curl;
curl.SetRequestHeader(MHD_HTTP_HEADER_RANGE, range);
curl.SetRequestHeader(MHD_HTTP_HEADER_IF_RANGE, lastModifiedOlder.GetAsRFC1123DateTime());
ASSERT_TRUE(curl.Get(GetUrlOfTestFile(TEST_FILES_RANGES), result));
EXPECT_STREQ(TEST_FILES_DATA_RANGES, result.c_str());
CheckRangesTestFileResponse(curl);
}
TEST_F(TestWebServer, CanGetCachedRangedFileWithExactIfRange)
{
const std::string rangedFileContent = TEST_FILES_DATA_RANGES;
const std::string range = "bytes=0-";
CHttpRanges ranges;
ASSERT_TRUE(ranges.Parse(range, rangedFileContent.size()));
// get the last modified date of the file
CDateTime lastModified;
ASSERT_TRUE(GetLastModifiedOfTestFile(TEST_FILES_RANGES, lastModified));
// get the whole file (but ranged) with an older If-Range value
std::string result;
CCurlFile curl;
curl.SetRequestHeader(MHD_HTTP_HEADER_RANGE, range);
curl.SetRequestHeader(MHD_HTTP_HEADER_IF_RANGE, lastModified.GetAsRFC1123DateTime());
ASSERT_TRUE(curl.Get(GetUrlOfTestFile(TEST_FILES_RANGES), result));
CheckRangesTestFileResponse(curl, result, ranges);
}
TEST_F(TestWebServer, CanGetCachedRangedFileWithNewerIfRange)
{
const std::string rangedFileContent = TEST_FILES_DATA_RANGES;
const std::string range = "bytes=0-";
CHttpRanges ranges;
ASSERT_TRUE(ranges.Parse(range, rangedFileContent.size()));
// get the last modified date of the file
CDateTime lastModified;
ASSERT_TRUE(GetLastModifiedOfTestFile(TEST_FILES_RANGES, lastModified));
CDateTime lastModifiedNewer = lastModified + CDateTimeSpan(1, 0, 0, 0);
// get the whole file (but ranged) with an older If-Range value
std::string result;
CCurlFile curl;
curl.SetRequestHeader(MHD_HTTP_HEADER_RANGE, range);
curl.SetRequestHeader(MHD_HTTP_HEADER_IF_RANGE, lastModifiedNewer.GetAsRFC1123DateTime());
ASSERT_TRUE(curl.Get(GetUrlOfTestFile(TEST_FILES_RANGES), result));
CheckRangesTestFileResponse(curl, result, ranges);
}
|