• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

tu6ge / oss-rs / 5863951548

pending completion
5863951548

Pull #24

github

tu6ge
feat(client): Reduce constraints on generic param
Pull Request #24: Branch0.12

39 of 39 new or added lines in 4 files covered. (100.0%)

6180 of 6422 relevant lines covered (96.23%)

9.24 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

98.84
/src/tests/client.rs
1
use std::env::{remove_var, set_var};
2

3
use http::header::CONTENT_TYPE;
4
use http::{HeaderValue, Method};
5

6
use crate::builder::ClientWithMiddleware;
7
use crate::file::AlignBuilder;
8
use crate::{client::Client, types::CanonicalizedResource, EndPoint};
9

10
#[test]
11
fn test_get_bucket_url() {
2✔
12
    let client = Client::<ClientWithMiddleware>::new(
1✔
13
        "foo1".into(),
1✔
14
        "foo2".into(),
1✔
15
        EndPoint::CN_QINGDAO,
1✔
16
        "foo4".parse().unwrap(),
1✔
17
    );
18
    let url = client.get_bucket_url();
1✔
19
    assert_eq!(url.as_str(), "https://foo4.oss-cn-qingdao.aliyuncs.com/");
1✔
20
}
2✔
21

22
#[test]
23
fn test_from_env() {
2✔
24
    set_var("ALIYUN_KEY_ID", "foo1");
1✔
25
    set_var("ALIYUN_KEY_SECRET", "foo2");
1✔
26
    set_var("ALIYUN_ENDPOINT", "qingdao");
1✔
27
    set_var("ALIYUN_BUCKET", "foo4");
1✔
28
    remove_var("ALIYUN_OSS_INTERNAL");
1✔
29
    let client = Client::<ClientWithMiddleware>::from_env().unwrap();
1✔
30
    let url = client.get_bucket_url();
1✔
31
    assert_eq!(url.as_str(), "https://foo4.oss-cn-qingdao.aliyuncs.com/");
1✔
32

33
    set_var("ALIYUN_OSS_INTERNAL", "true");
1✔
34
    let client = Client::<ClientWithMiddleware>::from_env().unwrap();
1✔
35
    let url = client.get_bucket_url();
1✔
36
    assert_eq!(
1✔
37
        url.as_str(),
1✔
38
        "https://foo4.oss-cn-qingdao-internal.aliyuncs.com/"
39
    );
40

41
    set_var("ALIYUN_OSS_INTERNAL", "foo4");
1✔
42
    let client = Client::<ClientWithMiddleware>::from_env().unwrap();
1✔
43
    let url = client.get_bucket_url();
1✔
44
    assert_eq!(url.as_str(), "https://foo4.oss-cn-qingdao.aliyuncs.com/");
1✔
45

46
    set_var("ALIYUN_OSS_INTERNAL", "1");
1✔
47
    let client = Client::<ClientWithMiddleware>::from_env().unwrap();
1✔
48
    let url = client.get_bucket_url();
1✔
49
    assert_eq!(
1✔
50
        url.as_str(),
1✔
51
        "https://foo4.oss-cn-qingdao-internal.aliyuncs.com/"
52
    );
53

54
    set_var("ALIYUN_OSS_INTERNAL", "yes");
1✔
55
    let client = Client::<ClientWithMiddleware>::from_env().unwrap();
1✔
56
    let url = client.get_bucket_url();
1✔
57
    assert_eq!(
1✔
58
        url.as_str(),
1✔
59
        "https://foo4.oss-cn-qingdao-internal.aliyuncs.com/"
60
    );
61

62
    set_var("ALIYUN_OSS_INTERNAL", "Y");
1✔
63
    let client = Client::<ClientWithMiddleware>::from_env().unwrap();
1✔
64
    let url = client.get_bucket_url();
1✔
65
    assert_eq!(
1✔
66
        url.as_str(),
1✔
67
        "https://foo4.oss-cn-qingdao-internal.aliyuncs.com/"
68
    );
69
}
2✔
70

71
#[test]
72
fn test_builder_with_header() {
2✔
73
    let client = Client::<ClientWithMiddleware>::new(
1✔
74
        "foo1".into(),
1✔
75
        "foo2".into(),
1✔
76
        EndPoint::CN_QINGDAO,
1✔
77
        "foo4".parse().unwrap(),
1✔
78
    );
79
    let url = "http://foo.example.net/foo".parse().unwrap();
1✔
80
    let resource = CanonicalizedResource::new("bar");
1✔
81
    let headers = vec![(CONTENT_TYPE, HeaderValue::from_static("application/json"))];
1✔
82
    let builder = client.builder_with_header(Method::POST, url, resource, headers);
1✔
83

84
    assert!(builder.is_ok());
1✔
85

86
    let request = builder.unwrap().build().unwrap();
1✔
87

88
    assert_eq!(request.method(), "POST");
1✔
89
    assert!(request.url().host().is_some());
1✔
90
    assert_eq!(request.url().path(), "/foo");
1✔
91
    assert_eq!(
1✔
92
        request.headers().get("content-type"),
1✔
93
        Some(&HeaderValue::from_str("application/json").unwrap())
1✔
94
    );
95
    assert_eq!(
1✔
96
        request.headers().get("accesskeyid"),
1✔
97
        Some(&HeaderValue::from_str("foo1").unwrap())
1✔
98
    );
99
    assert!(request.headers().get("secretaccesskey").is_none());
1✔
100
    assert_eq!(
1✔
101
        request.headers().get("verb"),
1✔
102
        Some(&HeaderValue::from_str("POST").unwrap())
1✔
103
    );
104
    assert_eq!(
1✔
105
        request.headers().get("date"),
1✔
106
        Some(&HeaderValue::from_str("Thu, 06 Oct 2022 20:40:00 GMT").unwrap())
1✔
107
    );
108
    assert_eq!(
1✔
109
        request.headers().get("canonicalizedresource"),
1✔
110
        Some(&HeaderValue::from_str("bar").unwrap())
1✔
111
    );
112
    assert_eq!(
1✔
113
        request.headers().get("authorization"),
1✔
114
        Some(&HeaderValue::from_str("OSS foo1:FUrk4hgj2yIB8lJpnsSub+CTC9M=").unwrap())
1✔
115
    );
116
}
2✔
117

118
#[cfg(feature = "blocking")]
119
#[test]
120
fn test_blocking_builder_with_header() {
2✔
121
    use crate::blocking::builder::ClientWithMiddleware;
122
    use crate::client::Client;
123
    use crate::file::blocking::AlignBuilder;
124
    let client = Client::<ClientWithMiddleware>::new(
1✔
125
        "foo1".into(),
1✔
126
        "foo2".into(),
1✔
127
        EndPoint::CnQingdao,
1✔
128
        "foo4".parse().unwrap(),
1✔
129
    );
130
    let url = "http://foo.example.net/foo".parse().unwrap();
1✔
131
    let resource = CanonicalizedResource::new("bar");
1✔
132
    let headers = vec![(CONTENT_TYPE, HeaderValue::from_static("application/json"))];
1✔
133
    let builder = client.builder_with_header(Method::POST, url, resource, headers);
1✔
134

135
    assert!(builder.is_ok());
1✔
136

137
    let request = builder.unwrap().build().unwrap();
1✔
138

139
    assert_eq!(request.method(), "POST");
1✔
140
    assert!(request.url().host().is_some());
1✔
141
    assert_eq!(request.url().path(), "/foo");
1✔
142
    assert_eq!(
1✔
143
        request.headers().get("content-type"),
1✔
144
        Some(&HeaderValue::from_str("application/json").unwrap())
1✔
145
    );
146
    assert_eq!(
1✔
147
        request.headers().get("accesskeyid"),
1✔
148
        Some(&HeaderValue::from_str("foo1").unwrap())
1✔
149
    );
150
    assert!(request.headers().get("secretaccesskey").is_none());
1✔
151
    assert_eq!(
1✔
152
        request.headers().get("verb"),
1✔
153
        Some(&HeaderValue::from_str("POST").unwrap())
1✔
154
    );
155
    assert_eq!(
1✔
156
        request.headers().get("date"),
1✔
157
        Some(&HeaderValue::from_str("Thu, 06 Oct 2022 20:40:00 GMT").unwrap())
1✔
158
    );
159
    assert_eq!(
1✔
160
        request.headers().get("canonicalizedresource"),
1✔
161
        Some(&HeaderValue::from_str("bar").unwrap())
1✔
162
    );
163
    assert_eq!(
1✔
164
        request.headers().get("authorization"),
1✔
165
        Some(&HeaderValue::from_str("OSS foo1:FUrk4hgj2yIB8lJpnsSub+CTC9M=").unwrap())
1✔
166
    );
167
}
2✔
168

169
mod handle_error {
170
    use crate::builder::{check_http_status, BuilderError, BuilderErrorKind};
171
    use crate::errors::OssService;
172
    use http::Response as HttpResponse;
173
    use reqwest::Response;
174

175
    #[tokio::test]
3✔
176
    async fn test_async_has_error() {
2✔
177
        let http = HttpResponse::builder()
1✔
178
            .status(302)
179
            //.header("X-Custom-Foo", "Bar")
180
            .body(
181
                r#"<?xml version=\"1.0\" encoding=\"UTF-8\"?>
182
            <Error>
183
                <Code>foo_code</Code>
184
                <Message>bar</Message>
185
                <RequestId>63145DB90BFD85303279D56B</RequestId>
186
                <HostId>honglei123.oss-cn-shanghai.aliyuncs.com</HostId>
187
                <MaxAllowedSkewMilliseconds>900000</MaxAllowedSkewMilliseconds>
188
                <RequestTime>2022-09-04T07:11:33.000Z</RequestTime>
189
                <ServerTime>2022-09-04T08:11:37.000Z</ServerTime>
190
            </Error>
191
            "#,
192
            )
193
            .unwrap();
194
        let response: Response = http.into();
1✔
195

196
        let res = check_http_status(response).await;
1✔
197

198
        let BuilderError { kind } = res.unwrap_err();
1✔
199
        match kind {
2✔
200
            BuilderErrorKind::OssService(b) => {
1✔
201
                let OssService { code, .. } = *b;
1✔
202
                assert!(code == "foo_code");
1✔
203
            }
1✔
204
            _ => unreachable!(),
×
205
        }
1✔
206

207
        //mock.checkpoint();
208
    }
3✔
209

210
    #[tokio::test]
3✔
211
    async fn test_async_ok() {
2✔
212
        let http = HttpResponse::builder()
1✔
213
            .status(200)
214
            //.header("X-Custom-Foo", "Bar")
215
            .body("body_abc")
216
            .unwrap();
217
        let response: Response = http.into();
1✔
218

219
        let res = check_http_status(response).await;
1✔
220
        assert!(res.is_ok());
1✔
221
        let ok = res.unwrap();
1✔
222
        assert_eq!(ok.status(), 200);
1✔
223
        assert_eq!(&ok.text().await.unwrap(), "body_abc");
1✔
224

225
        let http = HttpResponse::builder()
1✔
226
            .status(204)
227
            //.header("X-Custom-Foo", "Bar")
228
            .body("body_abc")
229
            .unwrap();
230
        let response: Response = http.into();
1✔
231

232
        let res = check_http_status(response).await;
1✔
233
        assert!(res.is_ok());
1✔
234
        let ok = res.unwrap();
1✔
235
        assert_eq!(ok.status(), 204);
1✔
236
        assert_eq!(&ok.text().await.unwrap(), "body_abc");
2✔
237
    }
2✔
238

239
    #[cfg(feature = "blocking")]
240
    #[test]
241
    fn test_blocking_has_error() {
2✔
242
        use crate::blocking::builder::check_http_status as blocking_check_http_status;
243
        use reqwest::blocking::Response as BlockingResponse;
244

245
        let http = HttpResponse::builder()
1✔
246
            .status(302)
247
            //.header("X-Custom-Foo", "Bar")
248
            .body(
249
                r#"<?xml version=\"1.0\" encoding=\"UTF-8\"?>
250
            <Error>
251
                <Code>foo_code</Code>
252
                <Message>bar</Message>
253
                <RequestId>63145DB90BFD85303279D56B</RequestId>
254
                <HostId>honglei123.oss-cn-shanghai.aliyuncs.com</HostId>
255
                <MaxAllowedSkewMilliseconds>900000</MaxAllowedSkewMilliseconds>
256
                <RequestTime>2022-09-04T07:11:33.000Z</RequestTime>
257
                <ServerTime>2022-09-04T08:11:37.000Z</ServerTime>
258
            </Error>
259
            "#,
260
            )
261
            .unwrap();
262
        let response: BlockingResponse = http.into();
1✔
263

264
        let res = blocking_check_http_status(response);
1✔
265

266
        let BuilderError { kind } = res.unwrap_err();
1✔
267
        match kind {
1✔
268
            BuilderErrorKind::OssService(b) => {
1✔
269
                let OssService { code, .. } = *b;
1✔
270
                assert!(code == "foo_code");
1✔
271
            }
1✔
272
            _ => unreachable!(),
×
273
        }
274
    }
2✔
275

276
    #[cfg(feature = "blocking")]
277
    #[test]
278
    fn test_blocking_ok() {
2✔
279
        use crate::blocking::builder::check_http_status as blocking_check_http_status;
280
        use reqwest::blocking::Response as BlockingResponse;
281

282
        let http = HttpResponse::builder()
1✔
283
            .status(200)
284
            //.header("X-Custom-Foo", "Bar")
285
            .body("body_abc")
286
            .unwrap();
287
        let response: BlockingResponse = http.into();
1✔
288

289
        let res = blocking_check_http_status(response);
1✔
290
        assert!(res.is_ok());
1✔
291
        let ok = res.unwrap();
1✔
292
        assert_eq!(ok.status(), 200);
1✔
293
        assert_eq!(&ok.text().unwrap(), "body_abc");
1✔
294

295
        let http = HttpResponse::builder()
1✔
296
            .status(204)
297
            //.header("X-Custom-Foo", "Bar")
298
            .body("body_abc")
299
            .unwrap();
300
        let response: BlockingResponse = http.into();
1✔
301

302
        let res = blocking_check_http_status(response);
1✔
303
        assert!(res.is_ok());
1✔
304
        let ok = res.unwrap();
1✔
305
        assert_eq!(ok.status(), 204);
1✔
306
        assert_eq!(&ok.text().unwrap(), "body_abc");
1✔
307
    }
2✔
308
}
309

310
// blocking mock 有错误
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc