Skip to content

Commit 6a8b301

Browse files
authored
Merge pull request #7 from cmbrose/continuation-token-fix
Fix continuation tokens on PS 7+
2 parents 0b8c17d + 4f735bc commit 6a8b301

File tree

4 files changed

+127
-5
lines changed

4 files changed

+127
-5
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
# Release Notes
22
All notable changes and release history of the "cosmos-db" module will be documented in this file.
33

4+
## 1.8
5+
* Bugfix for continuation tokens on Powershell version 7+
6+
47
## 1.7
58
* Minor interface improvements
69

cosmos-db/cosmos-db.psd1

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,15 @@
33
#
44
# Generated by: cmbrose
55
#
6-
# Generated on: 12/27/2020
7-
#
6+
# Generated on: 1/1/2021
87

98
@{
109

1110
# Script module or binary module file associated with this manifest.
1211
# RootModule = ''
1312

1413
# Version number of this module.
15-
ModuleVersion = '1.7'
14+
ModuleVersion = '1.8'
1615

1716
# Supported PSEditions
1817
# CompatiblePSEditions = @()

cosmos-db/cosmos-db.psm1

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -187,19 +187,43 @@ Function Invoke-CosmosDbApiRequest([string]$verb, [string]$url, $headers, $body=
187187
Invoke-WebRequest -Method $verb -Uri $url -Body $body -Headers $headers
188188
}
189189

190+
Function Get-ContinuationToken($response)
191+
{
192+
$value = $response.Headers["x-ms-continuation"]
193+
194+
if ($PSVersionTable.PSEdition -eq "Core")
195+
{
196+
if (-not $value)
197+
{
198+
return $null
199+
}
200+
201+
# Headers were changed to arrays in version 7
202+
# https://docs.microsoft.com/en-us/powershell/scripting/whats-new/breaking-changes-ps6?view=powershell-7.1#changes-to-web-cmdlets
203+
$value[0]
204+
}
205+
else
206+
{
207+
$value
208+
}
209+
}
210+
190211
Function Invoke-CosmosDbApiRequestWithContinuation([string]$verb, [string]$url, $headers, $body=$null)
191212
{
192213
process
193214
{
194215
$response = Invoke-CosmosDbApiRequest -Verb $verb -Url $url -Body $body -Headers $headers
195216
$response
196217

197-
while ($response.Headers["x-ms-continuation"])
218+
$continuationToken = Get-ContinuationToken $response
219+
while ($continuationToken)
198220
{
199-
$headers["x-ms-continuation"] = $response.Headers["x-ms-continuation"];
221+
$headers["x-ms-continuation"] = $continuationToken
200222

201223
$response = Invoke-CosmosDbApiRequest -Verb $verb -Url $url -Body $body -Headers $headers
202224
$response
225+
226+
$continuationToken = Get-ContinuationToken $response
203227
}
204228
}
205229
}

tests/Invoke-CosmosDbApiRequestWithContinuation.Tests.ps1

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@ InModuleScope cosmos-db {
66
Use-CosmosDbInternalFlag -EnableCaching $false
77

88
. $PSScriptRoot\Utils.ps1
9+
10+
$ORIG_PS_EDITION = $PSVersionTable.PSEdition
11+
}
12+
13+
AfterAll {
14+
$PSVersionTable.PSEdition = $ORIG_PS_EDITION
915
}
1016

1117
Describe "Invoke-CosmosDbApiRequestWithContinuation" {
@@ -46,6 +52,45 @@ InModuleScope cosmos-db {
4652
Assert-MockCalled Invoke-CosmosDbApiRequest -Times 1
4753
}
4854

55+
It "Handles responses without continuation header for Powershell 7" {
56+
$PSVersionTable.PSEdition = "Core"
57+
58+
$MOCK_VERB = "MOCK_VERB"
59+
$MOCK_URL = "MOCK_URL"
60+
$MOCK_BODY = @{
61+
Mock = "Mock"
62+
}
63+
$MOCK_HEADERS = @{
64+
Mock = "Mock";
65+
}
66+
67+
$response = @{
68+
StatusCode = 200;
69+
Content = "{}";
70+
Headers = @{};
71+
}
72+
73+
Mock Invoke-CosmosDbApiRequest {
74+
param($verb, $url, $body, $headers)
75+
76+
$headers["x-ms-continuation"] | Should -BeNullOrEmpty | Out-Null
77+
78+
$verb | Should -Be $MOCK_VERB | Out-Null
79+
$url | Should -Be $MOCK_URL | Out-Null
80+
$body | Should -Be $MOCK_BODY | Out-Null
81+
$headers | Should -Be $MOCK_HEADERS | Out-Null
82+
83+
$response
84+
}
85+
86+
$result = Invoke-CosmosDbApiRequestWithContinuation -Verb $MOCK_VERB -Url $MOCK_URL -Body $MOCK_BODY -Headers $MOCK_HEADERS
87+
88+
$result | Should -BeExactly $response
89+
@($result).Count | Should -Be 1
90+
91+
Assert-MockCalled Invoke-CosmosDbApiRequest -Times 1
92+
}
93+
4994
It "Handles continuation response headers" {
5095
$continuationTokens = @($null, "1", "2", "3")
5196

@@ -92,5 +137,56 @@ InModuleScope cosmos-db {
92137

93138
Assert-MockCalled Invoke-CosmosDbApiRequest -Times $continuationTokens.Count
94139
}
140+
141+
It "Handles continuation response headers for Powershell 7" {
142+
$continuationTokens = @($null, "100", "200", "300")
143+
144+
$PSVersionTable.PSEdition = "Core"
145+
146+
$MOCK_VERB = "MOCK_VERB"
147+
$MOCK_URL = "MOCK_URL"
148+
$MOCK_BODY = @{
149+
Mock = "Mock"
150+
}
151+
$MOCK_HEADERS = @{
152+
Mock = "Mock";
153+
}
154+
155+
$global:idx = 0
156+
$global:expectedResponses = @()
157+
158+
Mock Invoke-CosmosDbApiRequest {
159+
param($verb, $url, $body, $headers)
160+
161+
$verb | Should -Be $MOCK_VERB | Out-Null
162+
$url | Should -Be $MOCK_URL | Out-Null
163+
$body | Should -Be $MOCK_BODY | Out-Null
164+
165+
$headers["x-ms-continuation"] | Should -Be $continuationTokens[$global:idx] | Out-Null
166+
$global:idx = $global:idx + 1
167+
168+
$headers.Remove("x-ms-continuation")
169+
AssertHashtablesEqual $MOCK_HEADERS $headers
170+
171+
$response = @{
172+
StatusCode = 200;
173+
Content = "$global:idx";
174+
Headers = @{
175+
# The empty here is to trick powershell into no automatically converting the single item array into just a value
176+
"x-ms-continuation" = @($continuationTokens[$global:idx], "")
177+
};
178+
}
179+
180+
$global:expectedResponses += $response
181+
$response
182+
}
183+
184+
$result = Invoke-CosmosDbApiRequestWithContinuation -Verb $MOCK_VERB -Url $MOCK_URL -Body $MOCK_BODY -Headers $MOCK_HEADERS
185+
186+
$result | Should -BeExactly $global:expectedResponses
187+
@($result).Count | Should -Be $continuationTokens.Count
188+
189+
Assert-MockCalled Invoke-CosmosDbApiRequest -Times $continuationTokens.Count
190+
}
95191
}
96192
}

0 commit comments

Comments
 (0)