summaryrefslogtreecommitdiff
path: root/test/tgbot/net/HttpParser.cpp
blob: 25326235be631cced602bbc6817fbef380e9562e (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
#include <boost/test/unit_test.hpp>

#include <tgbot/net/HttpParser.h>

#include "utils.h"

using namespace std;
using namespace TgBot;

BOOST_AUTO_TEST_SUITE(tHttpParser)

BOOST_AUTO_TEST_CASE(generateRequest) {
    vector<HttpReqArg> args = { HttpReqArg("email", "test@example.com"), HttpReqArg("text", "Hello, world!") };
    string t = HttpParser().generateRequest(Url("http://example.com/index.html"), args, true);
    string e = ""
        "POST /index.html HTTP/1.1\r\n"
        "Host: example.com\r\n"
        "Connection: keep-alive\r\n"
        "Content-Type: application/x-www-form-urlencoded\r\n"
        "Content-Length: 49\r\n"
        "\r\n"
        "email=test%40example.com&text=Hello%2C%20world%21";
    BOOST_CHECK_MESSAGE(t == e, diffS(t, e));
}

BOOST_AUTO_TEST_CASE(generateMultipartFormData) {
    vector<HttpReqArg> args = { HttpReqArg("email", "test@example.com"), HttpReqArg("text", "Hello, world!", true) };
    string boundary = HttpParser().generateMultipartBoundary(args);
    string t = HttpParser().generateMultipartFormData(args, boundary);
    string e = ""
        "--" + boundary + "\r\n"
        "Content-Disposition: form-data; name=\"email\"\r\n"
        "\r\n"
        "test@example.com\r\n"
        "--" + boundary + "\r\n"
        "Content-Disposition: form-data; name=\"text\"; filename=\"\"\r\n"
        "Content-Type: text/plain\r\n"
        "\r\n"
        "Hello, world!\r\n"
        "--" + boundary + "--\r\n";
    BOOST_CHECK_MESSAGE(t == e, diffS(t, e));
}

BOOST_AUTO_TEST_CASE(generateWwwFormUrlencoded) {
    vector<HttpReqArg> args = { HttpReqArg("email", "test@example.com"), HttpReqArg("text", "Hello, world!") };
    string t = HttpParser().generateWwwFormUrlencoded(args);
    string e = "email=test%40example.com&text=Hello%2C%20world%21";
    BOOST_CHECK_MESSAGE(t == e, diffS(t, e));
}

BOOST_AUTO_TEST_CASE(generateResponse) {
    string t = HttpParser().generateResponse("testdata", "text/plain", 200, "OK", false);
    string e = ""
        "HTTP/1.1 200 OK\r\n"
        "Content-Type: text/plain\r\n"
        "Content-Length: 8\r\n"
        "Connection: close\r\n"
        "\r\n"
        "testdata";
    BOOST_CHECK_MESSAGE(t == e, diffS(t, e));
}

BOOST_AUTO_TEST_CASE(parseRequest) {
    string data = ""
        "POST /index.html HTTP/1.1\r\n"
        "Host: example.com\r\n"
        "Connection: keep-alive\r\n"
        "Content-Type: text/plain\r\n"
        "Content-Length: 8\r\n"
        "\r\n"
        "testdata";

    unordered_map<string, string> tHeaders = HttpParser().parseHeader(data.substr(0, data.rfind("\r\n")), true);
    string tBody = HttpParser().extractBody(data);

    unordered_map<string, string> eHeaders = {
        { "_method", "POST" },
        { "_path", "/index.html" },
        { "Host", "example.com" },
        { "Connection", "keep-alive" },
        { "Content-Type", "text/plain" },
        { "Content-Length", "8" }
    };
    string eBody = "testdata";

    BOOST_CHECK_MESSAGE(tBody == eBody, diffS(tBody, eBody));
    BOOST_CHECK_MESSAGE(tHeaders == eHeaders, diffMSS(tHeaders, eHeaders));
}

BOOST_AUTO_TEST_CASE(parseResponse) {
    string data = ""
        "HTTP/1.1 200 OK\r\n"
        "Content-Type: text/plain\r\n"
        "Content-Length: 8\r\n"
        "\r\n"
        "testdata";

    unordered_map<string, string> tHeaders = HttpParser().parseHeader(data.substr(0, data.rfind("\r\n")), false);
    string tBody = HttpParser().extractBody(data);

    unordered_map<string, string> eHeaders = {
        { "_status", "200" },
        { "Content-Type", "text/plain" },
        { "Content-Length", "8" }
    };
    string eBody = "testdata";

    BOOST_CHECK_MESSAGE(tBody == eBody, diffS(tBody, eBody));
    BOOST_CHECK_MESSAGE(tHeaders == eHeaders, diffMSS(tHeaders, eHeaders));
}

BOOST_AUTO_TEST_SUITE_END()