xref: /haiku/src/tests/kits/net/service/TestServer.h (revision 2ac34dee51d95edbdf045837704466274740378d)
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 public:
21 						RandomTCPServerPort();
22 						~RandomTCPServerPort();
23 
24 	status_t			InitCheck()							const;
25 	int					FileDescriptor()					const;
26 	uint16_t			Port()								const;
27 
28 private:
29 	status_t			fInitStatus;
30 	int					fSocketFd;
31 	uint16_t			fServerPort;
32 };
33 
34 
35 class ChildProcess {
36 public:
37 						ChildProcess();
38 						~ChildProcess();
39 
40 	status_t			Start(const std::vector<std::string>& args);
41 private:
42 	pid_t				fChildPid;
43 };
44 
45 
46 enum TestServerMode {
47 	TEST_SERVER_MODE_HTTP,
48 	TEST_SERVER_MODE_HTTPS,
49 };
50 
51 
52 class TestServer {
53 public:
54 						TestServer(TestServerMode mode);
55 
56 	status_t			Start();
57 	BUrl				BaseUrl()							const;
58 
59 private:
60 	TestServerMode		fMode;
61 	ChildProcess		fChildProcess;
62 	RandomTCPServerPort fPort;
63 };
64 
65 
66 class TestProxyServer {
67 public:
68 	status_t			Start();
69 	uint16_t			Port()								const;
70 
71 private:
72 	ChildProcess		fChildProcess;
73 	RandomTCPServerPort	fPort;
74 };
75 
76 
77 #endif // TEST_SERVER_H
78