1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
use std::path::Path;
use std::str::FromStr;
use http::HeaderValue;
use serde::{Deserialize, Serialize, Serializer};
use thiserror::Error;
use url::Url;
use uv_auth::{AuthPolicy, Credentials};
use uv_redacted::DisplaySafeUrl;
use uv_small_str::SmallString;
use crate::exclude_newer::ExcludeNewerOverride;
use crate::index_name::{IndexName, IndexNameError};
use crate::origin::Origin;
use crate::{IndexStatusCodeStrategy, IndexUrl, IndexUrlError, SerializableStatusCode};
/// Cache control configuration for an index.
#[derive(Debug, Clone, Hash, Eq, PartialEq, Ord, PartialOrd, Default)]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
pub struct IndexCacheControl {
/// Cache control header for Simple API requests.
#[cfg_attr(feature = "schemars", schemars(with = "Option<String>"))]
pub api: Option<HeaderValue>,
/// Cache control header for file downloads.
#[cfg_attr(feature = "schemars", schemars(with = "Option<String>"))]
pub files: Option<HeaderValue>,
}
impl IndexCacheControl {
/// Return the default Simple API cache control headers for the given index URL, if applicable.
pub fn simple_api_cache_control(_url: &Url) -> Option<HeaderValue> {
None
}
/// Return the default files cache control headers for the given index URL, if applicable.
pub fn artifact_cache_control(url: &Url) -> Option<HeaderValue> {
let dominated_by_pytorch_or_nvidia = url.host_str().is_some_and(|host| {
host.eq_ignore_ascii_case("download.pytorch.org")
|| host.eq_ignore_ascii_case("pypi.nvidia.com")
});
if dominated_by_pytorch_or_nvidia {
// Some wheels in the PyTorch registry were accidentally uploaded with `no-cache,no-store,must-revalidate`.
// The PyTorch team plans to correct this in the future, but in the meantime we override
// the cache control headers to allow caching of static files.
//
// See: https://github.com/pytorch/pytorch/pull/149218
//
// The same issue applies to files hosted on `pypi.nvidia.com`.
Some(HeaderValue::from_static(
"max-age=365000000, immutable, public",
))
} else {
None
}
}
}
#[derive(Serialize)]
#[serde(rename_all = "kebab-case")]
struct IndexCacheControlRef<'a> {
#[serde(skip_serializing_if = "Option::is_none")]
api: Option<&'a str>,
#[serde(skip_serializing_if = "Option::is_none")]
files: Option<&'a str>,
}
impl Serialize for IndexCacheControl {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
IndexCacheControlRef {
api: self.api.as_ref().map(|api| {
api.to_str()
.expect("cache-control.api is always parsed from a string")
}),
files: self.files.as_ref().map(|files| {
files
.to_str()
.expect("cache-control.files is always parsed from a string")
}),
}
.serialize(serializer)
}
}
#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "kebab-case")]
struct IndexCacheControlWire {
api: Option<SmallString>,
files: Option<SmallString>,
}
impl<'de> Deserialize<'de> for IndexCacheControl {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
let wire = IndexCacheControlWire::deserialize(deserializer)?;
let api = wire
.api
.map(|api| {
HeaderValue::from_str(api.as_ref()).map_err(|_| {
serde::de::Error::custom(
"`cache-control.api` must be a valid HTTP header value",
)
})
})
.transpose()?;
let files = wire
.files
.map(|files| {
HeaderValue::from_str(files.as_ref()).map_err(|_| {
serde::de::Error::custom(
"`cache-control.files` must be a valid HTTP header value",
)
})
})
.transpose()?;
Ok(Self { api, files })
}
}
#[derive(Debug, Clone, Serialize)]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
#[serde(rename_all = "kebab-case")]
pub struct Index {
/// The name of the index.
///
/// Index names can be used to reference indexes elsewhere in the configuration. For example,
/// you can pin a package to a specific index by name:
///
/// ```toml
/// [[tool.uv.index]]
/// name = "pytorch"
/// url = "https://download.pytorch.org/whl/cu121"
///
/// [tool.uv.sources]
/// torch = { index = "pytorch" }
/// ```
pub name: Option<IndexName>,
/// The URL of the index.
///
/// Expects to receive a URL (e.g., `https://pypi.org/simple`) or a local path.
pub url: IndexUrl,
/// Mark the index as explicit.
///
/// Explicit indexes will _only_ be used when explicitly requested via a `[tool.uv.sources]`
/// definition, as in:
///
/// ```toml
/// [[tool.uv.index]]
/// name = "pytorch"
/// url = "https://download.pytorch.org/whl/cu121"
/// explicit = true
///
/// [tool.uv.sources]
/// torch = { index = "pytorch" }
/// ```
#[serde(default)]
pub explicit: bool,
/// Mark the index as the default index.
///
/// By default, uv uses PyPI as the default index, such that even if additional indexes are
/// defined via `[[tool.uv.index]]`, PyPI will still be used as a fallback for packages that
/// aren't found elsewhere. To disable the PyPI default, set `default = true` on at least one
/// other index.
///
/// Marking an index as default will move it to the front of the list of indexes, such that it
/// is given the highest priority when resolving packages.
#[serde(default)]
pub default: bool,
/// The origin of the index (e.g., a CLI flag, a user-level configuration file, etc.).
#[serde(skip)]
pub origin: Option<Origin>,
/// The format used by the index.
///
/// Indexes can either be PEP 503-compliant (i.e., a PyPI-style registry implementing the Simple
/// API) or structured as a flat list of distributions (e.g., `--find-links`). In both cases,
/// indexes can point to either local or remote resources.
#[serde(default)]
pub format: IndexFormat,
/// The URL of the upload endpoint.
///
/// When using `uv publish --index <name>`, this URL is used for publishing.
///
/// A configuration for the default index PyPI would look as follows:
///
/// ```toml
/// [[tool.uv.index]]
/// name = "pypi"
/// url = "https://pypi.org/simple"
/// publish-url = "https://upload.pypi.org/legacy/"
/// ```
pub publish_url: Option<DisplaySafeUrl>,
/// When uv should use authentication for requests to the index.
///
/// ```toml
/// [[tool.uv.index]]
/// name = "my-index"
/// url = "https://<omitted>/simple"
/// authenticate = "always"
/// ```
#[serde(default)]
pub authenticate: AuthPolicy,
/// Status codes that uv should ignore when deciding whether
/// to continue searching in the next index after a failure.
///
/// ```toml
/// [[tool.uv.index]]
/// name = "my-index"
/// url = "https://<omitted>/simple"
/// ignore-error-codes = [401, 403]
/// ```
#[serde(default)]
pub ignore_error_codes: Option<Vec<SerializableStatusCode>>,
/// Cache control configuration for this index.
///
/// When set, these headers will override the server's cache control headers
/// for both package metadata requests and artifact downloads.
///
/// ```toml
/// [[tool.uv.index]]
/// name = "my-index"
/// url = "https://<omitted>/simple"
/// cache-control = { api = "max-age=600", files = "max-age=3600" }
/// ```
#[serde(default)]
pub cache_control: Option<IndexCacheControl>,
/// An index-specific `exclude-newer` cutoff.
///
/// Accepts the same date, timestamp, and duration values as the global `exclude-newer`
/// setting. Set this to `false` to disable `exclude-newer` for this index entirely.
///
/// When set to a value, packages resolved from this index will use that cutoff instead of the
/// globally-specified value, unless a package-specific `exclude-newer-package` override is
/// present.
///
/// This option is in preview and may change in any future release.
///
/// ```toml
/// [tool.uv]
/// exclude-newer = "2025-01-01T00:00:00Z"
///
/// [[tool.uv.index]]
/// name = "internal"
/// url = "https://internal.example.com/simple"
/// exclude-newer = "7 days"
/// ```
#[serde(default, skip_serializing_if = "Option::is_none")]
#[cfg_attr(feature = "schemars", schemars(with = "ExcludeNewerOverride"))]
pub exclude_newer: Option<ExcludeNewerOverride>,
}
impl PartialEq for Index {
fn eq(&self, other: &Self) -> bool {
let Self {
name,
url,
explicit,
default,
origin: _,
format,
publish_url,
authenticate,
ignore_error_codes,
cache_control,
exclude_newer,
} = self;
*url == other.url
&& *name == other.name
&& *explicit == other.explicit
&& *default == other.default
&& *format == other.format
&& *publish_url == other.publish_url
&& *authenticate == other.authenticate
&& *ignore_error_codes == other.ignore_error_codes
&& *cache_control == other.cache_control
&& *exclude_newer == other.exclude_newer
}
}
impl Eq for Index {}
impl PartialOrd for Index {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
Some(self.cmp(other))
}
}
impl Ord for Index {
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
let Self {
name,
url,
explicit,
default,
origin: _,
format,
publish_url,
authenticate,
ignore_error_codes,
cache_control,
exclude_newer,
} = self;
url.cmp(&other.url)
.then_with(|| name.cmp(&other.name))
.then_with(|| explicit.cmp(&other.explicit))
.then_with(|| default.cmp(&other.default))
.then_with(|| format.cmp(&other.format))
.then_with(|| publish_url.cmp(&other.publish_url))
.then_with(|| authenticate.cmp(&other.authenticate))
.then_with(|| ignore_error_codes.cmp(&other.ignore_error_codes))
.then_with(|| cache_control.cmp(&other.cache_control))
.then_with(|| exclude_newer.cmp(&other.exclude_newer))
}
}
impl std::hash::Hash for Index {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
let Self {
name,
url,
explicit,
default,
origin: _,
format,
publish_url,
authenticate,
ignore_error_codes,
cache_control,
exclude_newer,
} = self;
url.hash(state);
name.hash(state);
explicit.hash(state);
default.hash(state);
format.hash(state);
publish_url.hash(state);
authenticate.hash(state);
ignore_error_codes.hash(state);
cache_control.hash(state);
exclude_newer.hash(state);
}
}
#[derive(
Default,
Debug,
Copy,
Clone,
Hash,
Eq,
PartialEq,
Ord,
PartialOrd,
serde::Serialize,
serde::Deserialize,
)]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
#[serde(rename_all = "kebab-case")]
pub enum IndexFormat {
/// A PyPI-style index implementing the Simple Repository API.
#[default]
Simple,
/// A `--find-links`-style index containing a flat list of wheels and source distributions.
Flat,
}
impl Index {
/// Initialize an [`Index`] from a pip-style `--index-url`.
pub fn from_index_url(url: IndexUrl) -> Self {
Self {
url,
name: None,
explicit: false,
default: true,
origin: None,
format: IndexFormat::Simple,
publish_url: None,
authenticate: AuthPolicy::default(),
ignore_error_codes: None,
cache_control: None,
exclude_newer: None,
}
}
/// Initialize an [`Index`] from a pip-style `--extra-index-url`.
pub fn from_extra_index_url(url: IndexUrl) -> Self {
Self {
url,
name: None,
explicit: false,
default: false,
origin: None,
format: IndexFormat::Simple,
publish_url: None,
authenticate: AuthPolicy::default(),
ignore_error_codes: None,
cache_control: None,
exclude_newer: None,
}
}
/// Initialize an [`Index`] from a pip-style `--find-links`.
pub fn from_find_links(url: IndexUrl) -> Self {
Self {
url,
name: None,
explicit: false,
default: false,
origin: None,
format: IndexFormat::Flat,
publish_url: None,
authenticate: AuthPolicy::default(),
ignore_error_codes: None,
cache_control: None,
exclude_newer: None,
}
}
/// Set the [`Origin`] of the index.
#[must_use]
pub fn with_origin(mut self, origin: Origin) -> Self {
self.origin = Some(origin);
self
}
/// Return the [`IndexUrl`] of the index.
pub fn url(&self) -> &IndexUrl {
&self.url
}
/// Consume the [`Index`] and return the [`IndexUrl`].
pub fn into_url(self) -> IndexUrl {
self.url
}
/// Return the raw [`Url`] of the index.
pub fn raw_url(&self) -> &DisplaySafeUrl {
self.url.url()
}
/// Return the root [`Url`] of the index, if applicable.
///
/// For indexes with a `/simple` endpoint, this is simply the URL with the final segment
/// removed. This is useful, e.g., for credential propagation to other endpoints on the index.
pub fn root_url(&self) -> Option<DisplaySafeUrl> {
self.url.root()
}
/// If credentials are available (via the URL or environment) and [`AuthPolicy`] is
/// [`AuthPolicy::Auto`], promote to [`AuthPolicy::Always`] so that future operations
/// (e.g., `uv tool upgrade`) know that authentication is required even after the credentials
/// are stripped from the stored URL.
#[must_use]
pub fn with_promoted_auth_policy(mut self) -> Self {
if matches!(self.authenticate, AuthPolicy::Auto) && self.credentials().is_some() {
self.authenticate = AuthPolicy::Always;
}
self
}
/// Retrieve the credentials for the index, either from the environment, or from the URL itself.
pub fn credentials(&self) -> Option<Credentials> {
// If the index is named, and credentials are provided via the environment, prefer those.
if let Some(name) = self.name.as_ref() {
if let Some(credentials) = Credentials::from_env(name.to_env_var()) {
return Some(credentials);
}
}
// Otherwise, extract the credentials from the URL.
Credentials::from_url(self.url.url())
}
/// Resolve the index relative to the given root directory.
pub fn relative_to(mut self, root_dir: &Path) -> Result<Self, IndexUrlError> {
if let IndexUrl::Path(ref url) = self.url {
if let Some(given) = url.given() {
self.url = IndexUrl::parse(given, Some(root_dir))?;
}
}
Ok(self)
}
/// Return the [`IndexStatusCodeStrategy`] for this index.
pub fn status_code_strategy(&self) -> IndexStatusCodeStrategy {
if let Some(ignore_error_codes) = &self.ignore_error_codes {
IndexStatusCodeStrategy::from_ignored_error_codes(ignore_error_codes)
} else {
IndexStatusCodeStrategy::from_index_url(self.url.url())
}
}
/// Return the cache control header for file requests to this index, if any.
pub fn artifact_cache_control(&self) -> Option<HeaderValue> {
self.cache_control
.as_ref()
.and_then(|cache_control| cache_control.files.clone())
.or_else(|| IndexCacheControl::artifact_cache_control(self.url.url()))
}
/// Return the cache control header for API requests to this index, if any.
pub fn simple_api_cache_control(&self) -> Option<HeaderValue> {
self.cache_control
.as_ref()
.and_then(|cache_control| cache_control.api.clone())
.or_else(|| IndexCacheControl::simple_api_cache_control(self.url.url()))
}
/// Return the `exclude-newer` setting for this index.
pub fn exclude_newer(&self) -> Option<&ExcludeNewerOverride> {
self.exclude_newer.as_ref()
}
}
impl From<IndexUrl> for Index {
fn from(value: IndexUrl) -> Self {
Self {
name: None,
url: value,
explicit: false,
default: false,
origin: None,
format: IndexFormat::Simple,
publish_url: None,
authenticate: AuthPolicy::default(),
ignore_error_codes: None,
cache_control: None,
exclude_newer: None,
}
}
}
impl FromStr for Index {
type Err = IndexSourceError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
// Determine whether the source is prefixed with a name, as in `name=https://pypi.org/simple`.
if let Some((name, url)) = s.split_once('=') {
if !name.chars().any(|c| c == ':') {
let name = IndexName::from_str(name)?;
let url = IndexUrl::from_str(url)?;
return Ok(Self {
name: Some(name),
url,
explicit: false,
default: false,
origin: None,
format: IndexFormat::Simple,
publish_url: None,
authenticate: AuthPolicy::default(),
ignore_error_codes: None,
cache_control: None,
exclude_newer: None,
});
}
}
// Otherwise, assume the source is a URL.
let url = IndexUrl::from_str(s)?;
Ok(Self {
name: None,
url,
explicit: false,
default: false,
origin: None,
format: IndexFormat::Simple,
publish_url: None,
authenticate: AuthPolicy::default(),
ignore_error_codes: None,
cache_control: None,
exclude_newer: None,
})
}
}
/// An [`IndexUrl`] along with the metadata necessary to query the index.
#[derive(Debug, Clone, Hash, Eq, PartialEq, Ord, PartialOrd)]
pub struct IndexMetadata {
/// The URL of the index.
pub url: IndexUrl,
/// The format used by the index.
pub format: IndexFormat,
}
impl IndexMetadata {
/// Return a reference to the [`IndexMetadata`].
pub fn as_ref(&self) -> IndexMetadataRef<'_> {
let Self { url, format: kind } = self;
IndexMetadataRef { url, format: *kind }
}
/// Consume the [`IndexMetadata`] and return the [`IndexUrl`].
pub fn into_url(self) -> IndexUrl {
self.url
}
}
/// A reference to an [`IndexMetadata`].
#[derive(Debug, Copy, Clone)]
pub struct IndexMetadataRef<'a> {
/// The URL of the index.
pub url: &'a IndexUrl,
/// The format used by the index.
pub format: IndexFormat,
}
impl IndexMetadata {
/// Return the [`IndexUrl`] of the index.
pub fn url(&self) -> &IndexUrl {
&self.url
}
}
impl IndexMetadataRef<'_> {
/// Return the [`IndexUrl`] of the index.
pub fn url(&self) -> &IndexUrl {
self.url
}
}
impl<'a> From<&'a Index> for IndexMetadataRef<'a> {
fn from(value: &'a Index) -> Self {
Self {
url: &value.url,
format: value.format,
}
}
}
impl<'a> From<&'a IndexMetadata> for IndexMetadataRef<'a> {
fn from(value: &'a IndexMetadata) -> Self {
Self {
url: &value.url,
format: value.format,
}
}
}
impl From<IndexUrl> for IndexMetadata {
fn from(value: IndexUrl) -> Self {
Self {
url: value,
format: IndexFormat::Simple,
}
}
}
impl<'a> From<&'a IndexUrl> for IndexMetadataRef<'a> {
fn from(value: &'a IndexUrl) -> Self {
Self {
url: value,
format: IndexFormat::Simple,
}
}
}
/// Wire type for deserializing an [`Index`] with validation.
#[derive(Deserialize)]
#[serde(rename_all = "kebab-case")]
struct IndexWire {
name: Option<IndexName>,
url: IndexUrl,
#[serde(default)]
explicit: bool,
#[serde(default)]
default: bool,
#[serde(default)]
format: IndexFormat,
publish_url: Option<DisplaySafeUrl>,
#[serde(default)]
authenticate: AuthPolicy,
#[serde(default)]
ignore_error_codes: Option<Vec<SerializableStatusCode>>,
#[serde(default)]
cache_control: Option<IndexCacheControl>,
#[serde(default)]
exclude_newer: Option<ExcludeNewerOverride>,
}
impl<'de> Deserialize<'de> for Index {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
let wire = IndexWire::deserialize(deserializer)?;
if wire.explicit && wire.name.is_none() {
return Err(serde::de::Error::custom(format!(
"An index with `explicit = true` requires a `name`: {}",
wire.url
)));
}
Ok(Self {
name: wire.name,
url: wire.url,
explicit: wire.explicit,
default: wire.default,
origin: None,
format: wire.format,
publish_url: wire.publish_url,
authenticate: wire.authenticate,
ignore_error_codes: wire.ignore_error_codes,
cache_control: wire.cache_control,
exclude_newer: wire.exclude_newer,
})
}
}
/// An error that can occur when parsing an [`Index`].
#[derive(Error, Debug)]
pub enum IndexSourceError {
#[error(transparent)]
Url(#[from] IndexUrlError),
#[error(transparent)]
IndexName(#[from] IndexNameError),
#[error("Index included a name, but the name was empty")]
EmptyName,
}
#[cfg(test)]
mod tests {
use super::*;
use http::HeaderValue;
#[test]
fn test_index_cache_control_headers() {
// Test that cache control headers are properly parsed from TOML
let toml_str = r#"
name = "test-index"
url = "https://test.example.com/simple"
cache-control = { api = "max-age=600", files = "max-age=3600" }
"#;
let index: Index = toml::from_str(toml_str).unwrap();
assert_eq!(index.name.as_ref().unwrap().as_ref(), "test-index");
assert!(index.cache_control.is_some());
assert_eq!(index.exclude_newer, None);
let cache_control = index.cache_control.as_ref().unwrap();
assert_eq!(
cache_control.api,
Some(HeaderValue::from_static("max-age=600"))
);
assert_eq!(
cache_control.files,
Some(HeaderValue::from_static("max-age=3600"))
);
}
#[test]
fn test_index_without_cache_control() {
// Test that indexes work without cache control headers
let toml_str = r#"
name = "test-index"
url = "https://test.example.com/simple"
"#;
let index: Index = toml::from_str(toml_str).unwrap();
assert_eq!(index.name.as_ref().unwrap().as_ref(), "test-index");
assert_eq!(index.cache_control, None);
assert_eq!(index.exclude_newer, None);
}
#[test]
fn test_index_partial_cache_control() {
// Test that cache control can have just one field
let toml_str = r#"
name = "test-index"
url = "https://test.example.com/simple"
cache-control = { api = "max-age=300" }
"#;
let index: Index = toml::from_str(toml_str).unwrap();
assert_eq!(index.name.as_ref().unwrap().as_ref(), "test-index");
assert!(index.cache_control.is_some());
assert_eq!(index.exclude_newer, None);
let cache_control = index.cache_control.as_ref().unwrap();
assert_eq!(
cache_control.api,
Some(HeaderValue::from_static("max-age=300"))
);
assert_eq!(cache_control.files, None);
}
#[test]
fn test_index_invalid_api_cache_control() {
let toml_str = r#"
name = "test-index"
url = "https://test.example.com/simple"
cache-control = { api = "max-age=600\n" }
"#;
let err = toml::from_str::<Index>(toml_str).unwrap_err();
assert!(
err.to_string()
.contains("`cache-control.api` must be a valid HTTP header value")
);
}
#[test]
fn test_index_invalid_files_cache_control() {
let toml_str = r#"
name = "test-index"
url = "https://test.example.com/simple"
cache-control = { files = "max-age=3600\n" }
"#;
let err = toml::from_str::<Index>(toml_str).unwrap_err();
assert!(
err.to_string()
.contains("`cache-control.files` must be a valid HTTP header value")
);
}
#[test]
fn test_index_exclude_newer_disable() {
let toml_str = r#"
name = "internal"
url = "https://internal.example.com/simple"
exclude-newer = false
"#;
let index: Index = toml::from_str(toml_str).unwrap();
assert_eq!(index.name.as_ref().unwrap().as_ref(), "internal");
assert_eq!(index.exclude_newer, Some(ExcludeNewerOverride::Disabled));
}
#[test]
fn test_index_exclude_newer_relative() {
let toml_str = r#"
name = "internal"
url = "https://internal.example.com/simple"
exclude-newer = "7 days"
"#;
let index: Index = toml::from_str(toml_str).unwrap();
assert_eq!(index.name.as_ref().unwrap().as_ref(), "internal");
assert!(matches!(
index.exclude_newer,
Some(ExcludeNewerOverride::Enabled(_))
));
}
}