Merge pull request #43 from dmilov/topic/no-content-type

fix: Incorrect parsing of Binary Content Mode cloud events
This commit is contained in:
dmilov 2021-08-30 12:05:03 +03:00 committed by GitHub
commit a6b7df670c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 526 additions and 408 deletions

View File

@ -9,7 +9,7 @@
RootModule = 'CloudEvents.Sdk.psm1' RootModule = 'CloudEvents.Sdk.psm1'
# Version number of this module. # Version number of this module.
ModuleVersion = '0.3.0' ModuleVersion = '0.3.1'
# Supported PSEditions # Supported PSEditions
CompatiblePSEditions = @('Core') CompatiblePSEditions = @('Core')

View File

@ -7,7 +7,7 @@ $xmlDataSerilizationLibPath = Join-Path (Join-Path $PSScriptRoot 'dataserializat
. $xmlDataSerilizationLibPath . $xmlDataSerilizationLibPath
function New-CloudEvent { function New-CloudEvent {
<# <#
.SYNOPSIS .SYNOPSIS
This function creates a new cloud event. This function creates a new cloud event.
@ -35,8 +35,8 @@ function New-CloudEvent {
Creates a cloud event with Type, Source, Id, and Time Creates a cloud event with Type, Source, Id, and Time
#> #>
[CmdletBinding()] [CmdletBinding()]
param( param(
[Parameter(Mandatory = $true)] [Parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()] [ValidateNotNullOrEmpty()]
[string] [string]
@ -56,9 +56,9 @@ param(
[ValidateNotNullOrEmpty()] [ValidateNotNullOrEmpty()]
[DateTime] [DateTime]
$Time $Time
) )
PROCESS { PROCESS {
$cloudEvent = New-Object ` $cloudEvent = New-Object `
-TypeName 'CloudNative.CloudEvents.CloudEvent' ` -TypeName 'CloudNative.CloudEvents.CloudEvent' `
-ArgumentList @( -ArgumentList @(
@ -69,12 +69,12 @@ PROCESS {
@()) @())
Write-Output $cloudEvent Write-Output $cloudEvent
} }
} }
#region Set Data Functions #region Set Data Functions
function Set-CloudEventData { function Set-CloudEventData {
<# <#
.SYNOPSIS .SYNOPSIS
This function sets data in a cloud event. This function sets data in a cloud event.
@ -98,8 +98,8 @@ function Set-CloudEventData {
Sets xml data to the cloud event Sets xml data to the cloud event
#> #>
[CmdletBinding()] [CmdletBinding()]
param( param(
[Parameter(Mandatory = $true, [Parameter(Mandatory = $true,
ValueFromPipeline = $true)] ValueFromPipeline = $true)]
[ValidateNotNullOrEmpty()] [ValidateNotNullOrEmpty()]
@ -120,7 +120,7 @@ param(
[string] [string]
$DataContentType) $DataContentType)
PROCESS { PROCESS {
# https://github.com/cloudevents/spec/blob/master/spec.md#datacontenttype # https://github.com/cloudevents/spec/blob/master/spec.md#datacontenttype
$contentType = New-Object ` $contentType = New-Object `
@ -131,12 +131,12 @@ PROCESS {
$cloudEvent.DataContentType = $dataContentType $cloudEvent.DataContentType = $dataContentType
Write-Output $CloudEvent Write-Output $CloudEvent
} }
} }
function Set-CloudEventJsonData { function Set-CloudEventJsonData {
<# <#
.SYNOPSIS .SYNOPSIS
This function sets JSON format data in a cloud event. This function sets JSON format data in a cloud event.
@ -163,8 +163,8 @@ function Set-CloudEventJsonData {
Sets JSON data to the cloud event Sets JSON data to the cloud event
#> #>
[CmdletBinding()] [CmdletBinding()]
param( param(
[Parameter(Mandatory = $true, [Parameter(Mandatory = $true,
ValueFromPipeline = $true)] ValueFromPipeline = $true)]
[ValidateNotNullOrEmpty()] [ValidateNotNullOrEmpty()]
@ -182,7 +182,7 @@ param(
[int] [int]
$Depth = 3) $Depth = 3)
PROCESS { PROCESS {
# DataContentType is set to 'application/json' # DataContentType is set to 'application/json'
# https://github.com/cloudevents/spec/blob/master/spec.md#datacontenttype # https://github.com/cloudevents/spec/blob/master/spec.md#datacontenttype
@ -194,12 +194,12 @@ PROCESS {
$cloudEvent.Data = ConvertTo-Json -InputObject $Data -Depth $Depth $cloudEvent.Data = ConvertTo-Json -InputObject $Data -Depth $Depth
Write-Output $CloudEvent Write-Output $CloudEvent
} }
} }
function Set-CloudEventXmlData { function Set-CloudEventXmlData {
<# <#
.SYNOPSIS .SYNOPSIS
This function sets XML format data in a cloud event. This function sets XML format data in a cloud event.
@ -229,8 +229,8 @@ function Set-CloudEventXmlData {
Sets XML data in the cloud event Sets XML data in the cloud event
#> #>
[CmdletBinding()] [CmdletBinding()]
param( param(
[Parameter(Mandatory = $true, [Parameter(Mandatory = $true,
ValueFromPipeline = $true)] ValueFromPipeline = $true)]
[ValidateNotNullOrEmpty()] [ValidateNotNullOrEmpty()]
@ -247,7 +247,7 @@ param(
[bool] [bool]
$AttributesKeysInElementAttributes) $AttributesKeysInElementAttributes)
PROCESS { PROCESS {
# DataContentType is set to 'application/xml' # DataContentType is set to 'application/xml'
$dataContentType = New-Object ` $dataContentType = New-Object `
@ -258,14 +258,14 @@ PROCESS {
$cloudEvent.Data = ConvertTo-CEDataXml -InputObject $Data -AttributesKeysInElementAttributes $AttributesKeysInElementAttributes $cloudEvent.Data = ConvertTo-CEDataXml -InputObject $Data -AttributesKeysInElementAttributes $AttributesKeysInElementAttributes
Write-Output $CloudEvent Write-Output $CloudEvent
} }
} }
#endregion Set Data Functions #endregion Set Data Functions
#region Read Data Functions #region Read Data Functions
function Read-CloudEventData { function Read-CloudEventData {
<# <#
.SYNOPSIS .SYNOPSIS
This function gets the data from a cloud event. This function gets the data from a cloud event.
@ -282,23 +282,23 @@ function Read-CloudEventData {
Reads data from a cloud event received on the http response Reads data from a cloud event received on the http response
#> #>
[CmdletBinding()] [CmdletBinding()]
param( param(
[Parameter(Mandatory = $true, [Parameter(Mandatory = $true,
ValueFromPipeline = $true)] ValueFromPipeline = $true)]
[ValidateNotNullOrEmpty()] [ValidateNotNullOrEmpty()]
[CloudNative.CloudEvents.CloudEvent] [CloudNative.CloudEvents.CloudEvent]
$CloudEvent $CloudEvent
) )
PROCESS { PROCESS {
Write-Output $CloudEvent.Data Write-Output $CloudEvent.Data
} }
} }
function Read-CloudEventJsonData { function Read-CloudEventJsonData {
<# <#
.SYNOPSIS .SYNOPSIS
This function gets JSON fromat data from a cloud event as a PowerShell hashtable. This function gets JSON fromat data from a cloud event as a PowerShell hashtable.
@ -320,14 +320,14 @@ function Read-CloudEventJsonData {
#> #>
<# <#
.DESCRIPTION .DESCRIPTION
Returns PowerShell hashtable that represents the CloudEvent Json Data Returns PowerShell hashtable that represents the CloudEvent Json Data
if the data content type is 'application/json', otherwise otherwise non-terminating error and no result if the data content type is 'application/json', otherwise otherwise non-terminating error and no result
#> #>
[CmdletBinding()] [CmdletBinding()]
param( param(
[Parameter(Mandatory = $true, [Parameter(Mandatory = $true,
ValueFromPipeline = $true)] ValueFromPipeline = $true)]
[ValidateNotNullOrEmpty()] [ValidateNotNullOrEmpty()]
@ -338,9 +338,9 @@ param(
ValueFromPipeline = $false)] ValueFromPipeline = $false)]
[int] [int]
$Depth = 3 $Depth = 3
) )
PROCESS { PROCESS {
# DataContentType is expected to be 'application/json' # DataContentType is expected to be 'application/json'
# https://github.com/cloudevents/spec/blob/master/spec.md#datacontenttype # https://github.com/cloudevents/spec/blob/master/spec.md#datacontenttype
@ -349,7 +349,7 @@ PROCESS {
-ArgumentList ([System.Net.Mime.MediaTypeNames+Application]::Json) -ArgumentList ([System.Net.Mime.MediaTypeNames+Application]::Json)
if ($CloudEvent.DataContentType -eq $dataContentType -or ` if ($CloudEvent.DataContentType -eq $dataContentType -or `
($CloudEvent.DataContentType -eq $null -and ` # Datacontent Type is Optional, if it is not specified we assume it is JSON as per https://github.com/cloudevents/spec/blob/v1.0.1/spec.md#datacontenttype ($CloudEvent.DataContentType -eq $null -and # Datacontent Type is Optional, if it is not specified we assume it is JSON as per https://github.com/cloudevents/spec/blob/v1.0.1/spec.md#datacontenttype
$cloudEvent.Data -is [Newtonsoft.Json.Linq.JObject])) { $cloudEvent.Data -is [Newtonsoft.Json.Linq.JObject])) {
$data = $cloudEvent.Data $data = $cloudEvent.Data
@ -361,15 +361,16 @@ PROCESS {
$result = $data.ToString() | ConvertFrom-Json -AsHashtable -Depth $Depth $result = $data.ToString() | ConvertFrom-Json -AsHashtable -Depth $Depth
Write-Output $result Write-Output $result
} else { }
else {
Write-Error "Cloud Event '$($cloudEvent.Id)' has no json data" Write-Error "Cloud Event '$($cloudEvent.Id)' has no json data"
} }
} }
} }
function Read-CloudEventXmlData { function Read-CloudEventXmlData {
<# <#
.SYNOPSIS .SYNOPSIS
This function gets XML fromat data from a cloud event as a PowerShell hashtable. This function gets XML fromat data from a cloud event as a PowerShell hashtable.
@ -430,14 +431,14 @@ function Read-CloudEventXmlData {
#> #>
<# <#
.DESCRIPTION .DESCRIPTION
Returns PowerShell hashtable that represents the CloudEvent Xml Data Returns PowerShell hashtable that represents the CloudEvent Xml Data
if the data content type is 'application/xml', otherwise non-terminating error and no result if the data content type is 'application/xml', otherwise non-terminating error and no result
#> #>
[CmdletBinding()] [CmdletBinding()]
param( param(
[Parameter(Mandatory = $true, [Parameter(Mandatory = $true,
ValueFromPipeline = $true)] ValueFromPipeline = $true)]
[ValidateNotNullOrEmpty()] [ValidateNotNullOrEmpty()]
@ -448,9 +449,9 @@ param(
[ValidateSet("SkipAttributes", "AlwaysAttrValue", "AttrValueWhenAttributes")] [ValidateSet("SkipAttributes", "AlwaysAttrValue", "AttrValueWhenAttributes")]
[string] [string]
$ConvertMode $ConvertMode
) )
PROCESS { PROCESS {
# DataContentType is expected to be 'application/xml' # DataContentType is expected to be 'application/xml'
$dataContentType = New-Object ` $dataContentType = New-Object `
@ -468,17 +469,18 @@ PROCESS {
$result = $data.ToString() | ConvertFrom-CEDataXml -ConvertMode $ConvertMode $result = $data.ToString() | ConvertFrom-CEDataXml -ConvertMode $ConvertMode
Write-Output $result Write-Output $result
} else { }
else {
Write-Error "Cloud Event '$($cloudEvent.Id)' has no xml data" Write-Error "Cloud Event '$($cloudEvent.Id)' has no xml data"
} }
} }
} }
#endregion Read Data Functions #endregion Read Data Functions
#region HTTP Protocol Binding Conversion Functions #region HTTP Protocol Binding Conversion Functions
function ConvertTo-HttpMessage { function ConvertTo-HttpMessage {
<# <#
.SYNOPSIS .SYNOPSIS
This function converts a cloud event object to a Http Message. This function converts a cloud event object to a Http Message.
@ -519,8 +521,8 @@ function ConvertTo-HttpMessage {
Sends a cloud event http requests to a server Sends a cloud event http requests to a server
#> #>
[CmdletBinding()] [CmdletBinding()]
param( param(
[Parameter( [Parameter(
Mandatory = $true, Mandatory = $true,
ValueFromPipeline = $true, ValueFromPipeline = $true,
@ -536,7 +538,7 @@ param(
[CloudNative.CloudEvents.ContentMode] [CloudNative.CloudEvents.ContentMode]
$ContentMode) $ContentMode)
PROCESS { PROCESS {
# Output Object # Output Object
$result = New-Object -TypeName PSCustomObject $result = New-Object -TypeName PSCustomObject
@ -561,8 +563,7 @@ PROCESS {
elseif ($attribute.Value -is [Uri] -or $attribute.Value -is [int]) { elseif ($attribute.Value -is [Uri] -or $attribute.Value -is [int]) {
$headers.Add(($HttpHeaderPrefix + $attribute.Key), $attribute.Value.ToString()) $headers.Add(($HttpHeaderPrefix + $attribute.Key), $attribute.Value.ToString())
} }
else else {
{
$headers.Add(($HttpHeaderPrefix + $attribute.Key), $headers.Add(($HttpHeaderPrefix + $attribute.Key),
[System.Text.Encoding]::UTF8.GetString($cloudEventFormatter.EncodeAttribute($cloudEvent.SpecVersion, $attribute.Key, [System.Text.Encoding]::UTF8.GetString($cloudEventFormatter.EncodeAttribute($cloudEvent.SpecVersion, $attribute.Key,
$attribute.Value, $attribute.Value,
@ -609,16 +610,17 @@ PROCESS {
try { try {
$read = 0 $read = 0
while (($read = $cloudEvent.Data.Read($buffer, 0, 1024)) -gt 0) while (($read = $cloudEvent.Data.Read($buffer, 0, 1024)) -gt 0) {
{
$ms.Write($buffer, 0, $read); $ms.Write($buffer, 0, $read);
} }
$bodyData = $ms.ToArray() $bodyData = $ms.ToArray()
} finally { }
finally {
$ms.Dispose() $ms.Dispose()
} }
} else { }
else {
$bodyData = $cloudEventFormatter.EncodeAttribute($cloudEvent.SpecVersion, $bodyData = $cloudEventFormatter.EncodeAttribute($cloudEvent.SpecVersion,
[CloudNative.CloudEvents.CloudEventAttributes]::DataAttributeName($cloudEvent.SpecVersion), [CloudNative.CloudEvents.CloudEventAttributes]::DataAttributeName($cloudEvent.SpecVersion),
$cloudEvent.Data, $cloudEvent.Extensions.Values) $cloudEvent.Data, $cloudEvent.Extensions.Values)
@ -629,11 +631,11 @@ PROCESS {
} }
Write-Output $result Write-Output $result
} }
} }
function ConvertFrom-HttpMessage { function ConvertFrom-HttpMessage {
<# <#
.SYNOPSIS .SYNOPSIS
This function converts a Http Message to a cloud event object This function converts a Http Message to a cloud event object
@ -655,8 +657,8 @@ function ConvertFrom-HttpMessage {
Converts a http response to a cloud event object Converts a http response to a cloud event object
#> #>
[CmdletBinding()] [CmdletBinding()]
param( param(
[Parameter( [Parameter(
Mandatory = $true, Mandatory = $true,
ValueFromPipeline = $false, ValueFromPipeline = $false,
@ -672,10 +674,9 @@ param(
[ValidateNotNull()] [ValidateNotNull()]
$Body) $Body)
PROCESS { PROCESS {
$HttpHeaderPrefix = "ce-"; $HttpHeaderPrefix = "ce-";
$SpecVersionHttpHeader1 = $HttpHeaderPrefix + "cloudEventsVersion"; $SpecVersionHttpHeader = $HttpHeaderPrefix + "specversion";
$SpecVersionHttpHeader2 = $HttpHeaderPrefix + "specversion";
$result = $null $result = $null
@ -687,7 +688,7 @@ PROCESS {
$Body = [System.Text.Encoding]::UTF8.GetBytes($Body) $Body = [System.Text.Encoding]::UTF8.GetBytes($Body)
} }
if ($Headers['Content-Type'] -ne $null) { if ($null -ne $Headers['Content-Type']) {
$ContentType = $Headers['Content-Type'] $ContentType = $Headers['Content-Type']
if ($ContentType -is [array]) { if ($ContentType -is [array]) {
# Get the first content-type value # Get the first content-type value
@ -714,25 +715,25 @@ PROCESS {
$result = $formatter.DecodeJObject($jObject, $null) $result = $formatter.DecodeJObject($jObject, $null)
$result.Data = $result.Data $result.Data = $result.Data
} else { }
else {
# Throw error for unsupported encoding # Throw error for unsupported encoding
throw "Unsupported CloudEvents encoding" throw "Unsupported CloudEvents encoding"
} }
} else { }
else {
# Handle Binary Mode # Handle Binary Mode
$version = [CloudNative.CloudEvents.CloudEventsSpecVersion]::Default $version = $null
if ($Headers.Contains($SpecVersionHttpHeader1)) { if ($Headers.Contains($SpecVersionHttpHeader) -and `
$version = [CloudNative.CloudEvents.CloudEventsSpecVersion]::V0_1 $null -ne $Headers[$SpecVersionHttpHeader] -and `
} ($Headers[$SpecVersionHttpHeader] | Select-Object -First 1).StartsWith('1.0')) {
# We do support the 1.0 cloud event version
if ($Headers.Contains($SpecVersionHttpHeader2)) { $version = [CloudNative.CloudEvents.CloudEventsSpecVersion]::V1_0
if ($Headers[$SpecVersionHttpHeader2][0] -eq "0.2") {
$version = [CloudNative.CloudEvents.CloudEventsSpecVersion]::V0_2
} elseif ($Headers[$SpecVersionHttpHeader2][0] -eq "0.3") {
$version = [CloudNative.CloudEvents.CloudEventsSpecVersion]::V0_3
}
} }
if ($null -ne $version) {
# SpecVersion is REQUIRED attribute, it it is not specified this is not a CloudEvent
# https://github.com/cloudevents/spec/blob/v1.0.1/spec.md#specversion
$cloudEvent = New-Object ` $cloudEvent = New-Object `
-TypeName 'CloudNative.CloudEvents.CloudEvent' ` -TypeName 'CloudNative.CloudEvents.CloudEvent' `
-ArgumentList @($version, $null); -ArgumentList @($version, $null);
@ -741,8 +742,7 @@ PROCESS {
# Get attributes from HTTP Headers # Get attributes from HTTP Headers
foreach ($httpHeader in $Headers.GetEnumerator()) { foreach ($httpHeader in $Headers.GetEnumerator()) {
if ($httpHeader.Key.Equals($SpecVersionHttpHeader1, [StringComparison]::InvariantCultureIgnoreCase) -or ` if ($httpHeader.Key.Equals($SpecVersionHttpHeader, [StringComparison]::InvariantCultureIgnoreCase)) {
$httpHeader.Key.Equals($SpecVersionHttpHeader2, [StringComparison]::InvariantCultureIgnoreCase)) {
continue continue
} }
@ -755,8 +755,7 @@ PROCESS {
$name = $httpHeader.Key.Substring(3); $name = $httpHeader.Key.Substring(3);
# Abolished structures in headers in 1.0 # Abolished structures in headers in 1.0
if ($version -ne [CloudNative.CloudEvents.CloudEventsSpecVersion]::V0_1 -and ` if ( $null -ne $headerValue -and `
$headerValue -ne $null -and `
$headerValue.StartsWith('"') -and ` $headerValue.StartsWith('"') -and `
$headerValue.EndsWith('"') -or ` $headerValue.EndsWith('"') -or `
$headerValue.StartsWith("'") -and $headerValue.EndsWith("'") -or ` $headerValue.StartsWith("'") -and $headerValue.EndsWith("'") -or `
@ -767,24 +766,41 @@ PROCESS {
$attributes[$name] = $jsonFormatter.DecodeAttribute($version, $name, $attributes[$name] = $jsonFormatter.DecodeAttribute($version, $name,
[System.Text.Encoding]::UTF8.GetBytes($headerValue), $null); [System.Text.Encoding]::UTF8.GetBytes($headerValue), $null);
} else { }
else {
$attributes[$name] = $headerValue $attributes[$name] = $headerValue
} }
} }
} }
if ($Headers['Content-Type'] -ne $null -and $Headers['Content-Type'][0] -is [string]) { # Verify parsed attributes from HTTP Headers
if ($null -ne $attributes['datacontenttype']) {
# ce-datatype is prohibitted by the protocol -> throw error
# https://github.com/cloudevents/spec/blob/v1.0.1/http-protocol-binding.md#311-http-content-type
throw "'ce-datacontenttype' HTTP header is prohibited for Binary ContentMode CloudEvent"
}
if ($Headers['Content-Type'] -is [string]) {
$cloudEvent.DataContentType = New-Object 'System.Net.Mime.ContentType' -ArgumentList @($Headers['Content-Type'])
} elseif ($Headers['Content-Type'][0] -is [string]) {
$cloudEvent.DataContentType = New-Object 'System.Net.Mime.ContentType' -ArgumentList @($Headers['Content-Type'][0]) $cloudEvent.DataContentType = New-Object 'System.Net.Mime.ContentType' -ArgumentList @($Headers['Content-Type'][0])
} }
# Id, Type, and Source are reuiqred to be non-empty strings otherwise consider this is not a CloudEvent
# https://github.com/cloudevents/spec/blob/v1.0.1/spec.md#required-attributes
if ( -not [string]::IsNullOrEmpty($cloudEvent.Id) -and `
-not [string]::IsNullOrEmpty($cloudEvent.Source) -and `
-not [string]::IsNullOrEmpty($cloudEvent.Type)) {
# Get Data from HTTP Body # Get Data from HTTP Body
$cloudEvent.Data = $Body $cloudEvent.Data = $Body
$result = $cloudEvent $result = $cloudEvent
} }
} }
}
}
Write-Output $result Write-Output $result
} }
} }
#endregion HTTP Protocol Binding Conversion Functions #endregion HTTP Protocol Binding Conversion Functions

View File

@ -4,9 +4,9 @@
# ************************************************************************** # **************************************************************************
Describe "ConvertFrom-HttpMessage Function Tests" { Describe "ConvertFrom-HttpMessage Function Tests" {
BeforeAll { BeforeEach {
$expectedSpecVersion = '1.0' $script:expectedSpecVersion = '1.0'
$expectedStructuredContentType = 'application/cloudevents+json' $script:expectedStructuredContentType = 'application/cloudevents+json'
} }
Context "Converts CloudEvent in Binary Content Mode" { Context "Converts CloudEvent in Binary Content Mode" {
@ -73,6 +73,43 @@ Describe "ConvertFrom-HttpMessage Function Tests" {
} }
It 'Converts a CloudEvent with required properties and application/xml format data' { It 'Converts a CloudEvent with required properties and application/xml format data' {
# Arrange
$expectedType = 'test'
$expectedSource = 'urn:test'
$expectedDataContentType = 'application/xml'
$expectedId = 'test-id-2'
$expectedData = [Text.Encoding]::UTF8.GetBytes('<much wow="xml"/>')
$headers = @{
'Content-Type' = @($expectedDataContentType, 'charset=utf-8')
'ce-specversion' = $expectedSpecVersion
'ce-type' = $expectedType
'ce-source' = $expectedSource
'ce-id' = $expectedId
}
$body = $expectedData
# Act
$actual = ConvertFrom-HttpMessage `
-Headers $headers `
-Body $body
# Assert
$actual | Should -Not -Be $null
$actual.Id | Should -Be $expectedId
$actual.Type | Should -Be $expectedType
$actual.Source | Should -Be $expectedSource
$actual.DataContentType | Should -Be $expectedDataContentType
$actual.Data | Should -Be $expectedData
## Assert Data obtained by Read-CloudEventData
$actualData = $actual | Read-CloudEventData
$actualData | Should -Be $expectedData
}
It 'Returns null when ce-id is not specified' {
# Arrange # Arrange
$expectedType = 'test' $expectedType = 'test'
$expectedSource = 'urn:test' $expectedSource = 'urn:test'
@ -94,16 +131,57 @@ Describe "ConvertFrom-HttpMessage Function Tests" {
-Body $body -Body $body
# Assert # Assert
$actual | Should -Not -Be $null $actual | Should -Be $null
$actual.Type | Should -Be $expectedType }
$actual.Source | Should -Be $expectedSource
$actual.DataContentType | Should -Be $expectedDataContentType
$actual.Data | Should -Be $expectedData
## Assert Data obtained by Read-CloudEventData It 'Returns null when ce-source is not specified' {
$actualData = $actual | Read-CloudEventData # Arrange
$expectedType = 'test'
$expectedDataContentType = 'application/xml'
$expectedId = 'test-id-3'
$expectedData = [Text.Encoding]::UTF8.GetBytes('<much wow="xml"/>')
$actualData | Should -Be $expectedData $headers = @{
'Content-Type' = @($expectedDataContentType, 'charset=utf-8')
'ce-specversion' = $expectedSpecVersion
'ce-type' = $expectedType
'ce-id' = $expectedId
}
$body = $expectedData
# Act
$actual = ConvertFrom-HttpMessage `
-Headers $headers `
-Body $body
# Assert
$actual | Should -Be $null
}
It 'Returns null when ce-type is not specified' {
# Arrange
$expectedSource = 'urn:test'
$expectedDataContentType = 'application/xml'
$expectedId = 'test-id-4'
$expectedData = [Text.Encoding]::UTF8.GetBytes('<much wow="xml"/>')
$headers = @{
'Content-Type' = @($expectedDataContentType, 'charset=utf-8')
'ce-specversion' = $expectedSpecVersion
'ce-source' = $expectedSource
'ce-id' = $expectedId
}
$body = $expectedData
# Act
$actual = ConvertFrom-HttpMessage `
-Headers $headers `
-Body $body
# Assert
$actual | Should -Be $null
} }
} }
@ -245,5 +323,29 @@ Describe "ConvertFrom-HttpMessage Function Tests" {
-Body $body } | ` -Body $body } | `
Should -Throw "*Unsupported CloudEvents encoding*" Should -Throw "*Unsupported CloudEvents encoding*"
} }
It 'Returns null when no Content-Type header' {
# Arrange
$expectedType = 'test'
$expectedSource = 'urn:test'
$expectedDataContentType = 'application/xml'
$expectedData = [Text.Encoding]::UTF8.GetBytes('<much wow="xml"/>')
$structuredJsonBody = @{
'specversion' = $expectedSpecVersion
'type' = $expectedType
'source' = $expectedSource
'datacontenttype' = $expectedDataContentType
'data' = $expectedData
}
$body = [Text.Encoding]::UTF8.GetBytes(($structuredJsonBody | ConvertTo-Json))
# Act & Assert
$ce = ConvertFrom-HttpMessage `
-Headers @{} `
-Body $body
$ce | Should -Be $null
}
} }
} }