Welcome to mirror list, hosted at ThFree Co, Russian Federation.

parse_test.cpp « tests - github.com/marian-nmt/Simple-WebSocket-Server.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 45f5476fa86aea220b48b69738f045e3629e64ee (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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#include "assert.hpp"
#include "client_ws.hpp"
#include "server_ws.hpp"
#include <iostream>

using namespace std;
using namespace SimpleWeb;

class SocketServerTest : public SocketServerBase<WS> {
public:
  SocketServerTest() : SocketServerBase<WS>::SocketServerBase(8080) {}

  void accept() {}

  void parse_request_test() {
    std::shared_ptr<Connection> connection(new Connection(handler_runner, 0, *io_service));

    ostream ss(&connection->streambuf);
    ss << "GET /test/ HTTP/1.1\r\n";
    ss << "TestHeader: test\r\n";
    ss << "TestHeader2:test2\r\n";
    ss << "TestHeader3:test3a\r\n";
    ss << "TestHeader3:test3b\r\n";
    ss << "\r\n";

    std::istream stream(&connection->streambuf);
    ASSERT(RequestMessage::parse(stream, connection->method, connection->path, connection->query_string, connection->http_version, connection->header));

    ASSERT(connection->method == "GET");
    ASSERT(connection->path == "/test/");
    ASSERT(connection->http_version == "1.1");

    ASSERT(connection->header.size() == 4);
    auto header_it = connection->header.find("TestHeader");
    ASSERT(header_it != connection->header.end() && header_it->second == "test");
    header_it = connection->header.find("TestHeader2");
    ASSERT(header_it != connection->header.end() && header_it->second == "test2");

    header_it = connection->header.find("testheader");
    ASSERT(header_it != connection->header.end() && header_it->second == "test");
    header_it = connection->header.find("testheader2");
    ASSERT(header_it != connection->header.end() && header_it->second == "test2");

    auto range = connection->header.equal_range("testheader3");
    auto first = range.first;
    auto second = first;
    ++second;
    ASSERT(range.first != connection->header.end() && range.second != connection->header.end() &&
           ((first->second == "test3a" && second->second == "test3b") ||
            (first->second == "test3b" && second->second == "test3a")));
  }
};

class SocketClientTest : public SocketClientBase<WS> {
public:
  SocketClientTest(const std::string &server_port_path) : SocketClientBase<WS>::SocketClientBase(server_port_path, 80) {}

  void connect() {}

  void constructor_parse_test1() {
    ASSERT(path == "/test");
    ASSERT(host == "test.org");
    ASSERT(port == 8080);
  }

  void constructor_parse_test2() {
    ASSERT(path == "/test");
    ASSERT(host == "test.org");
    ASSERT(port == 80);
  }

  void constructor_parse_test3() {
    ASSERT(path == "/");
    ASSERT(host == "test.org");
    ASSERT(port == 80);
  }

  void constructor_parse_test4() {
    ASSERT(path == "/");
    ASSERT(host == "test.org");
    ASSERT(port == 8080);
  }

  void parse_response_header_test() {
    auto connection = std::shared_ptr<Connection>(new Connection(handler_runner, config.timeout_idle, *io_service));
    connection->in_message = std::shared_ptr<InMessage>(new InMessage());

    ostream stream(&connection->in_message->streambuf);
    stream << "HTTP/1.1 200 OK\r\n";
    stream << "TestHeader: test\r\n";
    stream << "TestHeader2:test2\r\n";
    stream << "TestHeader3:test3a\r\n";
    stream << "TestHeader3:test3b\r\n";
    stream << "\r\n";

    ASSERT(ResponseMessage::parse(*connection->in_message, connection->http_version, connection->status_code, connection->header));

    ASSERT(connection->header.size() == 4);
    auto header_it = connection->header.find("TestHeader");
    ASSERT(header_it != connection->header.end() && header_it->second == "test");
    header_it = connection->header.find("TestHeader2");
    ASSERT(header_it != connection->header.end() && header_it->second == "test2");

    header_it = connection->header.find("testheader");
    ASSERT(header_it != connection->header.end() && header_it->second == "test");
    header_it = connection->header.find("testheader2");
    ASSERT(header_it != connection->header.end() && header_it->second == "test2");

    auto range = connection->header.equal_range("testheader3");
    auto first = range.first;
    auto second = first;
    ++second;
    ASSERT(range.first != connection->header.end() && range.second != connection->header.end() &&
           ((first->second == "test3a" && second->second == "test3b") ||
            (first->second == "test3b" && second->second == "test3a")));

    connection.reset();
  }
};

int main() {
  ASSERT(case_insensitive_equal("Test", "tesT"));
  ASSERT(case_insensitive_equal("tesT", "test"));
  ASSERT(!case_insensitive_equal("test", "tseT"));
  CaseInsensitiveEqual equal;
  ASSERT(equal("Test", "tesT"));
  ASSERT(equal("tesT", "test"));
  ASSERT(!equal("test", "tset"));
  CaseInsensitiveHash hash;
  ASSERT(hash("Test") == hash("tesT"));
  ASSERT(hash("tesT") == hash("test"));
  ASSERT(hash("test") != hash("tset"));

  SocketServerTest serverTest;
  serverTest.io_service = std::make_shared<io_context>();

  serverTest.parse_request_test();

  SocketClientTest clientTest("test.org:8080/test");
  clientTest.constructor_parse_test1();

  SocketClientTest clientTest2("test.org/test");
  clientTest2.constructor_parse_test2();

  SocketClientTest clientTest3("test.org");
  clientTest3.constructor_parse_test3();

  SocketClientTest clientTest4("test.org:8080");
  clientTest4.io_service = std::make_shared<io_context>();
  clientTest4.constructor_parse_test4();

  clientTest4.parse_response_header_test();
}