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 = "sync", feature = "http1_1", feature = "tokio_base"))]
15 
16 #[macro_use]
17 mod common;
18 
19 use std::sync::Arc;
20 
21 use ylong_http_client::sync_impl::Body;
22 
23 use crate::common::init_test_work_runtime;
24 
25 /// SDV test cases for `sync::Client`.
26 ///
27 /// # Brief
28 /// 1. Creates a runtime to host the server.
29 /// 2. Creates a server within the runtime coroutine.
30 /// 3. Creates a sync::Client.
31 /// 4. The client sends a request to the the server.
32 /// 5. Verifies the response returned by the server.
33 /// 6. Shuts down the server.
34 #[test]
sdv_synchronized_client_send_request()35 fn sdv_synchronized_client_send_request() {
36     // `PUT` request.
37     sync_client_test_case!(
38         HTTP;
39         RuntimeThreads: 2,
40         Request: {
41             Method: "PUT",
42             Host: "http://127.0.0.1",
43             Header: "Content-Length", "6",
44             Body: "Hello!",
45         },
46         Response: {
47             Status: 200,
48             Version: "HTTP/1.1",
49             Header: "Content-Length", "3",
50             Body: "Hi!",
51         },
52     );
53 }
54 
55 /// SDV test cases for `sync::Client`.
56 ///
57 /// # Brief
58 /// 1. Creates a runtime to host the server.
59 /// 2. Creates a server within the runtime coroutine.
60 /// 3. Creates a sync::Client.
61 /// 4. The client sends requests to the the server repeatedly.
62 /// 5. Verifies each response returned by the server.
63 /// 6. Shuts down the server.
64 #[test]
sdv_synchronized_client_send_request_repeatedly()65 fn sdv_synchronized_client_send_request_repeatedly() {
66     sync_client_test_case!(
67         HTTP;
68         RuntimeThreads: 2,
69         Request: {
70             Method: "GET",
71             Host: "http://127.0.0.1",
72             Header: "Content-Length", "6",
73             Body: "Hello!",
74         },
75         Response: {
76             Status: 201,
77             Version: "HTTP/1.1",
78             Header: "Content-Length", "11",
79             Body: "METHOD GET!",
80         },
81         Request: {
82             Method: "POST",
83             Host: "http://127.0.0.1",
84             Header: "Content-Length", "6",
85             Body: "Hello!",
86         },
87         Response: {
88             Status: 201,
89             Version: "HTTP/1.1",
90             Header: "Content-Length", "12",
91             Body: "METHOD POST!",
92         },
93     );
94 }
95