• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 ylong_http::body::async_impl::Body as AsyncBody;
20 
21 use crate::common::init_test_work_runtime;
22 
23 /// SDV test cases for `async::Client`.
24 ///
25 /// # Brief
26 /// 1. Starts a hyper server with the tokio coroutine.
27 /// 2. Creates an async::Client.
28 /// 3. The client sends a request message.
29 /// 4. Verifies the received request on the server.
30 /// 5. The server sends a response message.
31 /// 6. Verifies the received response on the client.
32 /// 7. Shuts down the server.
33 /// 8. Repeats the preceding operations to start the next test case.
34 #[test]
sdv_async_client_send_request()35 fn sdv_async_client_send_request() {
36     // `GET` request
37     async_client_test_case!(
38         HTTP;
39         ServeFnName: ylong_server_fn,
40         RuntimeThreads: 1,
41         Request: {
42             Method: "GET",
43             Host: "http://127.0.0.1",
44             Header: "Content-Length", "6",
45             Body: "Hello!",
46         },
47         Response: {
48             Status: 200,
49             Version: "HTTP/1.1",
50             Header: "Content-Length", "3",
51             Body: "Hi!",
52         },
53     );
54 
55     // `HEAD` request.
56     async_client_test_case!(
57         HTTP;
58         ServeFnName: ylong_server_fn,
59         RuntimeThreads: 1,
60         Request: {
61             Method: "HEAD",
62             Host: "http://127.0.0.1",
63             Header: "Content-Length", "6",
64             Body: "Hello!",
65         },
66         Response: {
67             Status: 200,
68             Version: "HTTP/1.1",
69             Body: "",
70         },
71     );
72 
73     // `Post` Request.
74     async_client_test_case!(
75         HTTP;
76         ServeFnName: ylong_server_fn,
77         RuntimeThreads: 1,
78         Request: {
79             Method: "POST",
80             Host: "http://127.0.0.1",
81             Header: "Content-Length", "6",
82             Body: "Hello!",
83         },
84         Response: {
85             Status: 201,
86             Version: "HTTP/1.1",
87             Header: "Content-Length", "3",
88             Body: "Hi!",
89         },
90     );
91 
92     // `HEAD` request without body.
93     async_client_test_case!(
94         HTTP;
95         ServeFnName: ylong_server_fn,
96         RuntimeThreads: 1,
97         Request: {
98             Method: "HEAD",
99             Host: "http://127.0.0.1",
100             Body: "",
101         },
102         Response: {
103             Status: 200,
104             Version: "HTTP/1.1",
105             Body: "",
106         },
107     );
108 
109     // `PUT` request.
110     async_client_test_case!(
111         HTTP;
112         ServeFnName: ylong_server_fn,
113         RuntimeThreads: 1,
114         Request: {
115             Method: "PUT",
116             Host: "http://127.0.0.1",
117             Header: "Content-Length", "6",
118             Body: "Hello!",
119         },
120         Response: {
121             Status: 200,
122             Version: "HTTP/1.1",
123             Header: "Content-Length", "3",
124             Body: "Hi!",
125         },
126     );
127 }
128 
129 /// SDV test cases for `async::Client`.
130 ///
131 /// # Brief
132 /// 1. Creates a hyper server with the tokio coroutine.
133 /// 2. Creates an async::Client.
134 /// 3. The client repeatedly sends requests to the the server.
135 /// 4. Verifies each response returned by the server.
136 /// 5. Shuts down the server.
137 #[test]
sdv_client_send_request_repeatedly()138 fn sdv_client_send_request_repeatedly() {
139     async_client_test_case!(
140         HTTP;
141         ServeFnName: ylong_server_fn,
142         RuntimeThreads: 2,
143         Request: {
144             Method: "GET",
145             Host: "http://127.0.0.1",
146             Header: "Content-Length", "6",
147             Body: "Hello!",
148         },
149         Response: {
150             Status: 201,
151             Version: "HTTP/1.1",
152             Header: "Content-Length", "11",
153             Body: "METHOD GET!",
154         },
155         Request: {
156             Method: "POST",
157             Host: "http://127.0.0.1",
158             Header: "Content-Length", "6",
159             Body: "Hello!",
160         },
161         Response: {
162             Status: 201,
163             Version: "HTTP/1.1",
164             Header: "Content-Length", "12",
165             Body: "METHOD POST!",
166         },
167     );
168 }
169 
170 /// SDV test cases for `async::Client`.
171 ///
172 /// # Brief
173 /// 1. Creates an async::Client.
174 /// 2. Creates five servers and five coroutine sequentially.
175 /// 3. The client sends requests to the created servers in five coroutines.
176 /// 4. Verifies the responses returned by each server.
177 /// 5. Shuts down the servers.
178 #[test]
sdv_client_making_multiple_connections()179 fn sdv_client_making_multiple_connections() {
180     async_client_test_case!(
181         HTTP;
182         ServeFnName: ylong_server_fn,
183         RuntimeThreads: 2,
184         ClientNum: 5,
185         Request: {
186             Method: "GET",
187             Host: "http://127.0.0.1",
188             Header: "Content-Length", "6",
189             Body: "Hello!",
190         },
191         Response: {
192             Status: 201,
193             Version: "HTTP/1.1",
194             Header: "Content-Length", "11",
195             Body: "METHOD GET!",
196         },
197     );
198 }
199