Skip to content

Commit 3225f32

Browse files
Copilottvaron3
andcommitted
Add unit tests for pkranges link URL encoding fix
Add three tests in partition_key_range_cache.rs that verify: - item_by_rid() preserves '=' in RIDs like "pLLZAIuPigw=" (no %3D) - item() incorrectly URL-encodes '=' to '%3D' (documenting the bug) - item_by_rid() also preserves '+' and '/' in base64 RIDs Co-authored-by: tvaron3 <70857381+tvaron3@users.noreply.github.com> Agent-Logs-Url: https://github.com/Azure/azure-sdk-for-rust/sessions/f515482f-d87e-4105-9421-6ff7dac1508b
1 parent 0fc1e76 commit 3225f32

File tree

1 file changed

+57
-0
lines changed

1 file changed

+57
-0
lines changed

sdk/cosmos/azure_data_cosmos/src/routing/partition_key_range_cache.rs

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -684,4 +684,61 @@ mod tests {
684684
assert!(range.target_throughput.is_some());
685685
assert_eq!(range.target_throughput.unwrap(), 1000.0);
686686
}
687+
688+
// Tests verifying that the pkranges resource link uses item_by_rid() so that
689+
// collection RIDs (which are base64-encoded and can contain '=', '+', '/') are
690+
// not URL-percent-encoded. Using item() would encode '=' to '%3D', causing 404s.
691+
692+
#[test]
693+
fn pkranges_link_rid_with_equals_is_not_encoded() {
694+
// RIDs like "pLLZAIuPigw=" contain '=' which item() would encode to '%3D'.
695+
// item_by_rid() must preserve it as-is.
696+
let collection_rid = "pLLZAIuPigw=";
697+
let database_link = ResourceLink::root(ResourceType::Databases).item("perfdb");
698+
let pk_range_link = database_link
699+
.feed(ResourceType::Containers)
700+
.item_by_rid(collection_rid)
701+
.feed(ResourceType::PartitionKeyRanges);
702+
703+
// Correct: '=' preserved, not encoded to '%3D'
704+
assert_eq!(
705+
"dbs/perfdb/colls/pLLZAIuPigw=/pkranges",
706+
pk_range_link.path()
707+
);
708+
}
709+
710+
#[test]
711+
fn pkranges_link_item_encodes_equals_incorrectly() {
712+
// Demonstrates the bug: item() URL-encodes '=' to '%3D', producing a path
713+
// that Cosmos DB cannot find (404).
714+
let collection_rid = "pLLZAIuPigw=";
715+
let database_link = ResourceLink::root(ResourceType::Databases).item("perfdb");
716+
let pk_range_link_wrong = database_link
717+
.feed(ResourceType::Containers)
718+
.item(collection_rid)
719+
.feed(ResourceType::PartitionKeyRanges);
720+
721+
// Wrong: '=' is encoded to '%3D', causing 404 from Cosmos DB
722+
assert!(
723+
pk_range_link_wrong.path().contains("%3D"),
724+
"item() should URL-encode '=' to '%3D'"
725+
);
726+
assert_eq!(
727+
"dbs/perfdb/colls/pLLZAIuPigw%3D/pkranges",
728+
pk_range_link_wrong.path()
729+
);
730+
}
731+
732+
#[test]
733+
fn pkranges_link_rid_with_plus_is_not_encoded() {
734+
// RIDs may also contain '+' (base64 char). item_by_rid() must preserve it.
735+
let collection_rid = "AB+CD/EF==";
736+
let database_link = ResourceLink::root(ResourceType::Databases).item("mydb");
737+
let pk_range_link = database_link
738+
.feed(ResourceType::Containers)
739+
.item_by_rid(collection_rid)
740+
.feed(ResourceType::PartitionKeyRanges);
741+
742+
assert_eq!("dbs/mydb/colls/AB+CD/EF==/pkranges", pk_range_link.path());
743+
}
687744
}

0 commit comments

Comments
 (0)