summaryrefslogtreecommitdiff
path: root/common/UnitHTTP.hpp
blob: 0343036f176183ff8953e407df8e7bf93dd3b92d (plain)
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
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * 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/.
 */
#ifndef INCLUDED_UNITHTTP_HPP
#define INCLUDED_UNITHTTP_HPP

#include <memory>
#include <sstream>

#include <Poco/Net/HTTPClientSession.h>
#include <Poco/Net/HTTPRequest.h>
#include <Poco/Net/HTTPServerParams.h>
#include <Poco/Net/HTTPServerRequest.h>
#include <Poco/Net/HTTPServerResponse.h>
#include <Poco/Net/SocketAddress.h>
#include <Poco/Version.h>

#include "Common.hpp"
#include <LOOLWebSocket.hpp>

using Poco::Net::SocketAddress;
using Poco::Net::HTTPServerParams;

/// Unit test stub for a server response
class UnitHTTPServerResponse : public Poco::Net::HTTPServerResponse
{
    bool _sent;
    std::stringstream _dummyStream;

public:
    UnitHTTPServerResponse() :
        _sent(false)
    {
    }
    virtual void sendContinue() override {}
    virtual std::ostream& send() override
    {
        _sent = true;
        return _dummyStream;
    }
    virtual void sendFile(const std::string& /* path */,
                          const std::string& /* mediaType */) override {}
    virtual void sendBuffer(const void* /* buffer */,
                            std::size_t /* length */) override {}
    virtual void redirect(const std::string& /* uri */,
                          HTTPStatus /* status = HTTP_FOUND */) override {}
    virtual void requireAuthentication(const std::string& /* realm */) override {}
    virtual bool sent() const override { return _sent; }
};

/// Unit test stub for server params with a public dtor
class UnitHTTPServerParams : public Poco::Net::HTTPServerParams
{
public:
    ~UnitHTTPServerParams() {}
};

/// Unit test stub for a server request
class UnitHTTPServerRequest : public Poco::Net::HTTPServerRequest
{
protected:
    UnitHTTPServerResponse& _response;
    Poco::Net::SocketAddress _clientAddress;
    Poco::Net::SocketAddress _serverAddress;
    std::stringstream _dummyStream;
    UnitHTTPServerParams _dummyParams;

public:
    UnitHTTPServerRequest(UnitHTTPServerResponse& inResponse,
                          const std::string& uri) :
        _response(inResponse),
        _serverAddress(MasterPortNumber)
    {
        setURI(uri);
    }
    virtual std::istream& stream() override
    {
        return _dummyStream;
    }
#if POCO_VERSION < 0x02000000
    virtual bool expectContinue() const override
    {
        return false;
    }
#endif
#if POCO_VERSION >= 0x02000000
    virtual bool secure() const override
    {
        return true;
    }
#endif
    virtual const SocketAddress& clientAddress() const override
    {
        return _clientAddress;
    }
    virtual const SocketAddress& serverAddress() const override
    {
        return _serverAddress;
    }
    virtual const HTTPServerParams& serverParams() const override
    {
        return _dummyParams;
    }
    virtual Poco::Net::HTTPServerResponse& response() const override
    {
        return _response;
    }
};

namespace UnitHTTP
{
    Poco::Net::HTTPClientSession* createSession();
}

class UnitWebSocket
{
    Poco::Net::HTTPClientSession* _session;
    LOOLWebSocket* _socket;

public:
    /// Get a websocket connected for a given URL
    UnitWebSocket(const std::string& docURL);
    ~UnitWebSocket()
    {
        delete _socket;
        delete _session;
    }

    LOOLWebSocket* getLOOLWebSocket() const;
};

#endif

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */