void HTTPServerTest::testLoleafletPost()
{
    std::unique_ptr<Poco::Net::HTTPClientSession> session(helpers::createSession(_uri));

    Poco::Net::HTTPRequest request(Poco::Net::HTTPRequest::HTTP_POST, "/loleaflet/dist/loleaflet.html");
    Poco::Net::HTMLForm form;
    form.set("access_token", "2222222222");
    form.prepareSubmit(request);
    std::ostream& ostr = session->sendRequest(request);
    form.write(ostr);

    Poco::Net::HTTPResponse response;
    std::istream& rs = session->receiveResponse(response);
    CPPUNIT_ASSERT_EQUAL(Poco::Net::HTTPResponse::HTTP_OK, response.getStatus());

    std::string html;
    Poco::StreamCopier::copyToString(rs, html);

    CPPUNIT_ASSERT(html.find(form["access_token"]) != std::string::npos);
    CPPUNIT_ASSERT(html.find(_uri.getHost()) != std::string::npos);
}
Exemple #2
0
Poco::Int64 Twitter::update(const std::string& status)
{
    Poco::Net::HTMLForm form;
    form.set("status", status);
    Poco::AutoPtr<Poco::XML::Document> pResult = invoke(Poco::Net::HTTPRequest::HTTP_POST, "update", form);
    Poco::AutoPtr<Poco::XML::NodeList> pList = pResult->getElementsByTagName("id");
    if (pList->length() > 0)
    {
        return Poco::NumberParser::parse64(pList->item(0)->innerText());
    }
    else return 0;
}
void OAuthPrivate::resourceFile(const std::string method, const std::string url, const std::string & filename, const std::string status){
	std::string authStr = buildAuthHeader(method, url, Params());
	authStr = "OAuth " + authStr;

	URI uri(url);
	std::string path(uri.getPathAndQuery());
	if (path.empty()) path = "/";

	const Poco::Net::Context::Ptr context( new Poco::Net::Context(Poco::Net::Context::CLIENT_USE, "", "", "cacert.pem"));
	HTTPSClientSession session(uri.getHost(), uri.getPort(), context);
	HTTPRequest request(HTTPRequest::HTTP_POST, path, HTTPMessage::HTTP_1_1);
	HTTPResponse response;

	Poco::Net::HTMLForm form;
	form.setEncoding(Poco::Net::HTMLForm::ENCODING_MULTIPART);

	form.set("status", status);
	
	form.addPart("media[]", new Poco::Net::FilePartSource(filename, filename, "application/octet-stream"));
	form.prepareSubmit(request);

	request.set("Authorization", authStr);
	
	std::ostream & ostr = session.sendRequest(request);
	form.write(ostr);

	std::istream& rs = session.receiveResponse(response);
	std::cout << response.getStatus() << " " << response.getReason() << std::endl;

	if (response.getStatus() != Poco::Net::HTTPResponse::HTTP_UNAUTHORIZED)
	{
		StreamCopier::copyStream(rs, std::cout);
	}
	else
	{
		Poco::NullOutputStream null;
		StreamCopier::copyStream(rs, null);
	}
}
Exemple #4
0
void HTTPPostTest::testConvertTo()
{
    const auto srcPath = Util::getTempFilePath(TDOC, "hello.odt");

    Poco::URI uri("https://127.0.0.1:" + std::to_string(ClientPortNumber));
    Poco::Net::HTTPSClientSession session(uri.getHost(), uri.getPort());

    Poco::Net::HTTPRequest request(Poco::Net::HTTPRequest::HTTP_POST, "/convert-to");
    Poco::Net::HTMLForm form;
    form.setEncoding(Poco::Net::HTMLForm::ENCODING_MULTIPART);
    form.set("format", "txt");
    form.addPart("data", new Poco::Net::FilePartSource(srcPath));
    form.prepareSubmit(request);
    // If this results in a Poco::Net::ConnectionRefusedException, loolwsd is not running.
    form.write(session.sendRequest(request));

    Poco::Net::HTTPResponse response;
    std::stringstream actualStream;
    // receiveResponse() resulted in a Poco::Net::NoMessageException.
    std::istream& responseStream = session.receiveResponse(response);
    Poco::StreamCopier::copyStream(responseStream, actualStream);

    std::ifstream fileStream(TDOC "/hello.txt");
    std::stringstream expectedStream;
    expectedStream << fileStream.rdbuf();

    // Remove the temp files.
    Util::removeFile(srcPath);

    // In some cases the result is prefixed with (the UTF-8 encoding of) the Unicode BOM
    // (U+FEFF). Skip that.
    std::string actualString = actualStream.str();
    if (actualString.size() > 3 && actualString[0] == '\xEF' && actualString[1] == '\xBB' && actualString[2] == '\xBF')
        actualString = actualString.substr(3);
    CPPUNIT_ASSERT_EQUAL(expectedStream.str(), actualString);
}