1 // Copyright (c) 2023 Huawei Device Co., Ltd.
2 // Licensed under the Apache License, Version 2.0 (the "License");
3 // you may not use this file except in compliance with the License.
4 // You may obtain a copy of the License at
5 //
6 //     http://www.apache.org/licenses/LICENSE-2.0
7 //
8 // Unless required by applicable law or agreed to in writing, software
9 // distributed under the License is distributed on an "AS IS" BASIS,
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 // See the License for the specific language governing permissions and
12 // limitations under the License.
13 
14 #![cfg(all(feature = "async", feature = "http1_1", feature = "tokio_base"))]
15 
16 #[macro_use]
17 mod common;
18 
19 use crate::common::init_test_work_runtime;
20 
21 /// SDV test cases for `async::Client`.
22 ///
23 /// # Brief
24 /// 1. Starts a hyper server with the tokio coroutine.
25 /// 2. Creates an async::Client.
26 /// 3. The client sends a request message.
27 /// 4. Verifies the received request on the server.
28 /// 5. The server sends a response message.
29 /// 6. Verifies the received response on the client.
30 /// 7. Shuts down the server.
31 /// 8. Repeats the preceding operations to start the next test case.
32 #[test]
sdv_async_client_send_request()33 fn sdv_async_client_send_request() {
34     // `GET` request
35     async_client_test_case!(
36         HTTP;
37         ServeFnName: ylong_server_fn,
38         RuntimeThreads: 1,
39         Request: {
40             Method: "GET",
41             Host: "http://127.0.0.1",
42             Header: "Content-Length", "6",
43             Body: "Hello!",
44         },
45         Response: {
46             Status: 200,
47             Version: "HTTP/1.1",
48             Header: "Content-Length", "3",
49             Body: "Hi!",
50         },
51     );
52 
53     // `HEAD` request.
54     async_client_test_case!(
55         HTTP;
56         ServeFnName: ylong_server_fn,
57         RuntimeThreads: 1,
58         Request: {
59             Method: "HEAD",
60             Host: "http://127.0.0.1",
61             Header: "Content-Length", "6",
62             Body: "Hello!",
63         },
64         Response: {
65             Status: 200,
66             Version: "HTTP/1.1",
67             Body: "",
68         },
69     );
70 
71     // `Post` Request.
72     async_client_test_case!(
73         HTTP;
74         ServeFnName: ylong_server_fn,
75         RuntimeThreads: 1,
76         Request: {
77             Method: "POST",
78             Host: "http://127.0.0.1",
79             Header: "Content-Length", "6",
80             Body: "Hello!",
81         },
82         Response: {
83             Status: 201,
84             Version: "HTTP/1.1",
85             Header: "Content-Length", "3",
86             Body: "Hi!",
87         },
88     );
89 
90     // `HEAD` request without body.
91     async_client_test_case!(
92         HTTP;
93         ServeFnName: ylong_server_fn,
94         RuntimeThreads: 1,
95         Request: {
96             Method: "HEAD",
97             Host: "http://127.0.0.1",
98             Body: "",
99         },
100         Response: {
101             Status: 200,
102             Version: "HTTP/1.1",
103             Body: "",
104         },
105     );
106 
107     // `PUT` request.
108     async_client_test_case!(
109         HTTP;
110         ServeFnName: ylong_server_fn,
111         RuntimeThreads: 1,
112         Request: {
113             Method: "PUT",
114             Host: "http://127.0.0.1",
115             Header: "Content-Length", "6",
116             Body: "Hello!",
117         },
118         Response: {
119             Status: 200,
120             Version: "HTTP/1.1",
121             Header: "Content-Length", "3",
122             Body: "Hi!",
123         },
124     );
125 }
126 
127 /// SDV test cases for `async::Client`.
128 ///
129 /// # Brief
130 /// 1. Creates a hyper server with the tokio coroutine.
131 /// 2. Creates an async::Client.
132 /// 3. The client repeatedly sends requests to the the server.
133 /// 4. Verifies each response returned by the server.
134 /// 5. Shuts down the server.
135 #[test]
sdv_client_send_request_repeatedly()136 fn sdv_client_send_request_repeatedly() {
137     async_client_test_case!(
138         HTTP;
139         ServeFnName: ylong_server_fn,
140         RuntimeThreads: 2,
141         Request: {
142             Method: "GET",
143             Host: "http://127.0.0.1",
144             Header: "Content-Length", "6",
145             Body: "Hello!",
146         },
147         Response: {
148             Status: 201,
149             Version: "HTTP/1.1",
150             Header: "Content-Length", "11",
151             Body: "METHOD GET!",
152         },
153         Request: {
154             Method: "POST",
155             Host: "http://127.0.0.1",
156             Header: "Content-Length", "6",
157             Body: "Hello!",
158         },
159         Response: {
160             Status: 201,
161             Version: "HTTP/1.1",
162             Header: "Content-Length", "12",
163             Body: "METHOD POST!",
164         },
165     );
166 }
167 
168 /// SDV test cases for `async::Client`.
169 ///
170 /// # Brief
171 /// 1. Creates an async::Client.
172 /// 2. Creates five servers and five coroutine sequentially.
173 /// 3. The client sends requests to the created servers in five coroutines.
174 /// 4. Verifies the responses returned by each server.
175 /// 5. Shuts down the servers.
176 #[test]
sdv_client_making_multiple_connections()177 fn sdv_client_making_multiple_connections() {
178     async_client_test_case!(
179         HTTP;
180         ServeFnName: ylong_server_fn,
181         RuntimeThreads: 2,
182         ClientNum: 5,
183         Request: {
184             Method: "GET",
185             Host: "http://127.0.0.1",
186             Header: "Content-Length", "6",
187             Body: "Hello!",
188         },
189         Response: {
190             Status: 201,
191             Version: "HTTP/1.1",
192             Header: "Content-Length", "11",
193             Body: "METHOD GET!",
194         },
195     );
196 }
197