xref: /haiku/src/tests/kits/net/netservices2/TestServer.h (revision 52c4471a3024d2eb81fe88e2c3982b9f8daa5e56)
1 /*
2  * Copyright 2020 Haiku, Inc. All rights reserved.
3  * Distributed under the terms of the MIT License.
4  *
5  * Authors:
6  *   Kyle Ambroff-Kao, kyle@ambroffkao.com
7  */
8 #ifndef TEST_SERVER_H
9 #define TEST_SERVER_H
10 
11 #include <string>
12 #include <vector>
13 
14 #include <os/support/SupportDefs.h>
15 #include <os/support/Url.h>
16 
17 
18 // Binds to a random unused TCP port.
19 class RandomTCPServerPort
20 {
21 public:
22 	RandomTCPServerPort();
23 	~RandomTCPServerPort();
24 
25 	status_t InitCheck() const;
26 	int FileDescriptor() const;
27 	uint16_t Port() const;
28 
29 private:
30 	status_t fInitStatus;
31 	int fSocketFd;
32 	uint16_t fServerPort;
33 };
34 
35 
36 class ChildProcess
37 {
38 public:
39 	ChildProcess();
40 	~ChildProcess();
41 
42 	status_t Start(const std::vector<std::string>& args);
43 
44 private:
45 	pid_t fChildPid;
46 };
47 
48 
49 enum class TestServerMode {
50 	Http,
51 	Https,
52 };
53 
54 
55 class TestServer
56 {
57 public:
58 	TestServer(TestServerMode mode);
59 
60 	status_t Start();
61 	BUrl BaseUrl() const;
62 
63 private:
64 	TestServerMode fMode;
65 	ChildProcess fChildProcess;
66 	RandomTCPServerPort fPort;
67 };
68 
69 
70 class TestProxyServer
71 {
72 public:
73 	status_t Start();
74 	uint16_t Port() const;
75 
76 private:
77 	ChildProcess fChildProcess;
78 	RandomTCPServerPort fPort;
79 };
80 
81 
82 #endif // TEST_SERVER_H
83