aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/testing/mock_server/server.h
blob: 6d57cebf401955e507e489d05ee759093aaeb12d (plain) (blame)
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
#pragma once

#include <library/cpp/http/misc/parsed_request.h>
#include <library/cpp/http/server/http.h>
#include <library/cpp/http/server/response.h>
#include <library/cpp/testing/unittest/tests_data.h>

#include <util/system/event.h>

#include <functional>

class TAutoEvent;
class TThread;

namespace NMock {
    class TMockServer {
    public:
        using TGenerator = std::function<TRequestReplier*()>;

        TMockServer(ui16 port, TGenerator generator);
        TMockServer(const THttpServerOptions& options, TGenerator generator);

        ~TMockServer();

        size_t GetClientCount() const;
        void SetGenerator(TGenerator generator);

    private:
        static void* Worker(void* arg);

        class TCallBack;

        THolder<TCallBack> Cb_;
        THolder<THttpServer> Server_;
        THolder<TThread> Thread_;
        THolder<TAutoEvent> Ev_;
    };

    class TPong: public TRequestReplier {
    public:
        bool DoReply(const TReplyParams& params) override {
            const TParsedHttpFull parsed(params.Input.FirstLine());

            const HttpCodes code = parsed.Path == "/ping" ? HTTP_OK : HTTP_NOT_FOUND;

            THttpResponse resp(code);
            resp.OutTo(params.Output);

            return true;
        }
    };
}