aboutsummaryrefslogtreecommitdiffstats
path: root/yql/essentials/utils/test_http_server/test_http_server.h
blob: 385cfaf9706fbbd215348cfa064a1797dca5bdf2 (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
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
#pragma once

#include <library/cpp/http/misc/httpcodes.h>

#include <util/generic/maybe.h>
#include <util/generic/ptr.h>
#include <util/generic/string.h>

#include <functional>

namespace NYql {

class TTestHttpServer {
public:
    struct TReply {
        int Code = 0;
        TString ETag;
        TString LastModified;
        TString Content;
        TMaybe<int> ContentLength;

        static TReply Ok(const TString& content, TMaybe<int> contentLength = {}) {
            TReply r;
            r.Code = HTTP_OK;
            r.Content = content;
            r.ContentLength = contentLength;
            return r;
        }

        static TReply OkETag(const TString& content, const TString& etag, TMaybe<int> contentLength = {}) {
            TReply r = Ok(content, contentLength);
            r.ETag = etag;
            return r;
        }

        static TReply OkLastModified(const TString& content, const TString& lastModified, TMaybe<int> contentLength = {}) {
            TReply r = Ok(content, contentLength);
            r.LastModified = lastModified;
            return r;
        }

        static TReply NotModified(const TString& etag = {}, const TString& lastModified = {}) {
            TReply r;
            r.Code = HTTP_NOT_MODIFIED;
            r.ETag = etag;
            r.LastModified = lastModified;
            return r;
        }

        static TReply Forbidden() {
            TReply r;
            r.Code = HTTP_FORBIDDEN;
            return r;
        }
    };

    struct TRequest {
        TString OAuthToken;
        TString IfNoneMatch;
        TString IfModifiedSince;
    };

    typedef std::function<TReply(const TRequest& request)> TRequestHandler;

public:
    explicit TTestHttpServer(int port);
    ~TTestHttpServer();
    void Start();
    TString GetUrl() const;
    void SetRequestHandler(TRequestHandler handler);

private:
    class TImpl;
    THolder<TImpl> Impl_;
};

}