diff --git a/src/CloudEventsPowerShell/CloudEvents.Sdk.psm1 b/src/CloudEventsPowerShell/CloudEvents.Sdk.psm1 index 35f1696..f62b297 100644 --- a/src/CloudEventsPowerShell/CloudEvents.Sdk.psm1 +++ b/src/CloudEventsPowerShell/CloudEvents.Sdk.psm1 @@ -146,13 +146,16 @@ function Set-CloudEventData { PROCESS { - # https://github.com/cloudevents/spec/blob/main/cloudevents/spec.md#datacontenttype - $contentType = New-Object ` - -TypeName 'System.Net.Mime.ContentType' ` - -ArgumentList ($DataContentType) - $cloudEvent.Data = $Data - $cloudEvent.DataContentType = $dataContentType + + if ($PSBoundParameters.ContainsKey('DataContentType')) { + # https://github.com/cloudevents/spec/blob/main/cloudevents/spec.md#datacontenttype + $contentType = New-Object ` + -TypeName 'System.Net.Mime.ContentType' ` + -ArgumentList ($DataContentType) + + $cloudEvent.DataContentType = $contentType + } if ($PSBoundParameters.ContainsKey('DataSchema')) { $cloudEvent.DataSchema = $DataSchema diff --git a/test/unit/Set-CloudEventData.Tests.ps1 b/test/unit/Set-CloudEventData.Tests.ps1 index 222b3ee..5c38713 100644 --- a/test/unit/Set-CloudEventData.Tests.ps1 +++ b/test/unit/Set-CloudEventData.Tests.ps1 @@ -25,10 +25,20 @@ Describe "Set-CloudEventData Function Tests" { # Assert $actual | Should -Not -Be $null $actual.DataContentType.ToString() | Should -Be $expectedDataContentType + $actual.DataContentType | Should -BeOfType ([System.Net.Mime.ContentType]) $actual.Data | Should -Be $expectedData $actual.DataSchema | Should -BeNullOrEmpty } + It 'Sets data when DataContentType is omitted' { + $cloudEvent = New-CloudEvent -Id 'test-id' -Type test -Source 'urn:test' + + $actual = $cloudEvent | Set-CloudEventData -Data 'test' + + $actual.Data | Should -Be 'test' + $actual.DataContentType | Should -BeNullOrEmpty + } + It 'Sets xml text data' { # Arrange $cloudEvent = New-CloudEvent `