2020-01-17 01:50:15 -06:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
|
|
|
|
/*
|
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
*/
|
|
|
|
|
2021-03-25 08:47:10 -05:00
|
|
|
#include <config.h>
|
|
|
|
|
2021-04-05 12:00:26 -05:00
|
|
|
#include <net/HttpRequest.hpp>
|
|
|
|
|
2020-01-17 01:50:15 -06:00
|
|
|
#include <memory>
|
|
|
|
#include <ostream>
|
|
|
|
#include <set>
|
|
|
|
#include <string>
|
|
|
|
|
|
|
|
#include <Poco/DOM/AutoPtr.h>
|
|
|
|
#include <Poco/DOM/DOMParser.h>
|
|
|
|
#include <Poco/DOM/Document.h>
|
|
|
|
#include <Poco/DOM/NodeList.h>
|
|
|
|
#include <Poco/Exception.h>
|
|
|
|
#include <Poco/RegularExpression.h>
|
|
|
|
#include <Poco/URI.h>
|
2020-01-20 10:45:53 -06:00
|
|
|
#include <test/lokassert.hpp>
|
2020-01-17 01:50:15 -06:00
|
|
|
|
|
|
|
#include <Png.hpp>
|
|
|
|
#include <Unit.hpp>
|
|
|
|
#include <helpers.hpp>
|
|
|
|
|
2021-11-18 06:08:14 -06:00
|
|
|
class COOLWebSocket;
|
2020-01-17 01:50:15 -06:00
|
|
|
|
|
|
|
/// Test suite for /hosting, etc.
|
|
|
|
class UnitHosting : public UnitWSD
|
|
|
|
{
|
|
|
|
TestResult testDiscovery();
|
|
|
|
TestResult testCapabilities();
|
|
|
|
|
|
|
|
public:
|
2021-05-01 17:37:04 -05:00
|
|
|
UnitHosting()
|
|
|
|
: UnitWSD("UnitHosting")
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2020-12-24 11:15:37 -06:00
|
|
|
void invokeWSDTest() override;
|
2020-01-17 01:50:15 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
UnitBase::TestResult UnitHosting::testDiscovery()
|
|
|
|
{
|
2021-05-01 17:37:04 -05:00
|
|
|
LOG_TST("Getting /hosting/discovery the first time.");
|
2021-04-05 16:51:33 -05:00
|
|
|
const std::shared_ptr<const http::Response> httpResponse
|
2021-04-05 18:44:41 -05:00
|
|
|
= http::get(helpers::getTestServerURI(), "/hosting/discovery");
|
2021-04-05 12:00:26 -05:00
|
|
|
|
|
|
|
LOK_ASSERT(httpResponse->done());
|
|
|
|
LOK_ASSERT(httpResponse->state() == http::Response::State::Complete);
|
|
|
|
|
|
|
|
LOK_ASSERT(!httpResponse->statusLine().httpVersion().empty());
|
|
|
|
LOK_ASSERT(!httpResponse->statusLine().reasonPhrase().empty());
|
|
|
|
LOK_ASSERT_EQUAL(200U, httpResponse->statusLine().statusCode());
|
|
|
|
LOK_ASSERT(httpResponse->statusLine().statusCategory()
|
|
|
|
== http::StatusLine::StatusCodeClass::Successful);
|
|
|
|
LOK_ASSERT_EQUAL(std::string("HTTP/1.1"), httpResponse->statusLine().httpVersion());
|
|
|
|
LOK_ASSERT_EQUAL(std::string("OK"), httpResponse->statusLine().reasonPhrase());
|
|
|
|
LOK_ASSERT_EQUAL(std::string("text/xml"), httpResponse->header().getContentType());
|
|
|
|
|
|
|
|
// Repeat, with a trailing foreslash in the URL.
|
2021-05-01 17:37:04 -05:00
|
|
|
LOG_TST("Getting /hosting/discovery the second time.");
|
2021-04-05 16:51:33 -05:00
|
|
|
const std::shared_ptr<const http::Response> httpResponse2
|
2021-04-05 18:44:41 -05:00
|
|
|
= http::get(helpers::getTestServerURI(), "/hosting/discovery/");
|
2020-01-17 01:50:15 -06:00
|
|
|
|
2021-04-05 12:00:26 -05:00
|
|
|
LOK_ASSERT(httpResponse2->done());
|
|
|
|
LOK_ASSERT(httpResponse2->state() == http::Response::State::Complete);
|
2020-01-17 01:50:15 -06:00
|
|
|
|
2021-04-05 12:00:26 -05:00
|
|
|
LOK_ASSERT(!httpResponse2->statusLine().httpVersion().empty());
|
|
|
|
LOK_ASSERT(!httpResponse2->statusLine().reasonPhrase().empty());
|
|
|
|
LOK_ASSERT_EQUAL(200U, httpResponse2->statusLine().statusCode());
|
|
|
|
LOK_ASSERT(httpResponse2->statusLine().statusCategory()
|
|
|
|
== http::StatusLine::StatusCodeClass::Successful);
|
|
|
|
LOK_ASSERT_EQUAL(std::string("HTTP/1.1"), httpResponse2->statusLine().httpVersion());
|
|
|
|
LOK_ASSERT_EQUAL(std::string("OK"), httpResponse2->statusLine().reasonPhrase());
|
|
|
|
LOK_ASSERT_EQUAL(std::string("text/xml"), httpResponse2->header().getContentType());
|
2020-01-17 01:50:15 -06:00
|
|
|
|
2021-05-01 17:37:04 -05:00
|
|
|
LOG_TST("Comparing /hosting/discovery from both requests.");
|
2021-04-05 12:00:26 -05:00
|
|
|
LOK_ASSERT_EQUAL(httpResponse2->getBody(), httpResponse->getBody());
|
2020-01-17 01:50:15 -06:00
|
|
|
|
|
|
|
return TestResult::Ok;
|
|
|
|
}
|
|
|
|
|
|
|
|
UnitBase::TestResult UnitHosting::testCapabilities()
|
|
|
|
{
|
2021-04-06 21:45:32 -05:00
|
|
|
auto httpSession = http::Session::create(helpers::getTestServerURI());
|
|
|
|
std::shared_ptr<const http::Response> httpResponse
|
|
|
|
= httpSession->syncRequest(http::Request("/hosting/discovery"));
|
|
|
|
|
|
|
|
LOK_ASSERT(httpResponse->done());
|
|
|
|
LOK_ASSERT(httpResponse->state() == http::Response::State::Complete);
|
2020-01-17 01:50:15 -06:00
|
|
|
|
|
|
|
// Get discovery first and extract the urlsrc of the capabilities end point
|
|
|
|
std::string capabilitiesURI;
|
|
|
|
{
|
2021-04-06 21:45:32 -05:00
|
|
|
LOK_ASSERT_EQUAL(200U, httpResponse->statusLine().statusCode());
|
|
|
|
LOK_ASSERT_EQUAL(std::string("text/xml"), httpResponse->header().getContentType());
|
2020-01-17 01:50:15 -06:00
|
|
|
|
2021-04-06 21:45:32 -05:00
|
|
|
const std::string discoveryXML = httpResponse->getBody();
|
2020-01-17 01:50:15 -06:00
|
|
|
|
|
|
|
Poco::XML::DOMParser parser;
|
|
|
|
Poco::XML::AutoPtr<Poco::XML::Document> docXML = parser.parseString(discoveryXML);
|
|
|
|
Poco::XML::AutoPtr<Poco::XML::NodeList> listNodes = docXML->getElementsByTagName("action");
|
|
|
|
bool foundCapabilities = false;
|
|
|
|
for (unsigned long index = 0; index < listNodes->length(); ++index)
|
|
|
|
{
|
|
|
|
Poco::XML::Element* elem = static_cast<Poco::XML::Element*>(listNodes->item(index));
|
|
|
|
Poco::XML::Element* parent = elem->parentNode()
|
|
|
|
? static_cast<Poco::XML::Element*>(elem->parentNode())
|
|
|
|
: nullptr;
|
|
|
|
if (parent && parent->getAttribute("name") == "Capabilities")
|
|
|
|
{
|
|
|
|
foundCapabilities = true;
|
|
|
|
capabilitiesURI = elem->getAttribute("urlsrc");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-20 10:45:53 -06:00
|
|
|
LOK_ASSERT(foundCapabilities);
|
2021-04-06 21:45:32 -05:00
|
|
|
LOK_ASSERT_EQUAL(helpers::getTestServerURI() + CAPABILITIES_END_POINT, capabilitiesURI);
|
2020-01-17 01:50:15 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Then get the capabilities json
|
|
|
|
{
|
2021-04-06 21:45:32 -05:00
|
|
|
httpResponse = httpSession->syncRequest(http::Request(CAPABILITIES_END_POINT));
|
|
|
|
|
|
|
|
LOK_ASSERT(httpResponse->done());
|
|
|
|
LOK_ASSERT(httpResponse->state() == http::Response::State::Complete);
|
2020-01-17 01:50:15 -06:00
|
|
|
|
2021-04-06 21:45:32 -05:00
|
|
|
LOK_ASSERT_EQUAL(200U, httpResponse->statusLine().statusCode());
|
|
|
|
LOK_ASSERT_EQUAL(std::string("application/json"), httpResponse->header().getContentType());
|
2020-01-17 01:50:15 -06:00
|
|
|
|
2021-04-06 21:45:32 -05:00
|
|
|
const std::string responseString = httpResponse->getBody();
|
2020-01-17 01:50:15 -06:00
|
|
|
|
|
|
|
Poco::JSON::Parser parser;
|
|
|
|
Poco::Dynamic::Var jsonFile = parser.parse(responseString);
|
|
|
|
Poco::JSON::Object::Ptr features = jsonFile.extract<Poco::JSON::Object::Ptr>();
|
2020-01-20 10:45:53 -06:00
|
|
|
LOK_ASSERT(features);
|
|
|
|
LOK_ASSERT(features->has("convert-to"));
|
2020-01-17 01:50:15 -06:00
|
|
|
|
|
|
|
Poco::JSON::Object::Ptr convert_to
|
|
|
|
= features->get("convert-to").extract<Poco::JSON::Object::Ptr>();
|
2020-01-20 10:45:53 -06:00
|
|
|
LOK_ASSERT(convert_to->has("available"));
|
|
|
|
LOK_ASSERT(convert_to->get("available"));
|
2020-01-17 01:50:15 -06:00
|
|
|
}
|
|
|
|
return TestResult::Ok;
|
|
|
|
}
|
|
|
|
|
2020-12-24 11:15:37 -06:00
|
|
|
void UnitHosting::invokeWSDTest()
|
2020-01-17 01:50:15 -06:00
|
|
|
{
|
|
|
|
UnitBase::TestResult result = testDiscovery();
|
|
|
|
if (result != TestResult::Ok)
|
|
|
|
exitTest(result);
|
|
|
|
|
|
|
|
result = testCapabilities();
|
|
|
|
if (result != TestResult::Ok)
|
|
|
|
exitTest(result);
|
|
|
|
|
|
|
|
exitTest(TestResult::Ok);
|
|
|
|
}
|
|
|
|
|
|
|
|
UnitBase* unit_create_wsd(void) { return new UnitHosting(); }
|
|
|
|
|
|
|
|
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|