public async Task CopyToHttpListenerResponseAsync_StructuredMode() { var cloudEvent = new CloudEvent { Data = "plain text", DataContentType = "text/plain" }.PopulateRequiredAttributes(); var formatter = new JsonEventFormatter(); var response = await GetResponseAsync( async context => await cloudEvent.CopyToHttpListenerResponseAsync(context.Response, ContentMode.Structured, formatter)); response.EnsureSuccessStatusCode(); var content = response.Content; Assert.Equal(MimeUtilities.MediaType + "+json", content.Headers.ContentType.MediaType); var bytes = await content.ReadAsByteArrayAsync(); var parsed = new JsonEventFormatter().DecodeStructuredModeMessage(bytes, MimeUtilities.ToContentType(content.Headers.ContentType), extensionAttributes: null); AssertCloudEventsEqual(cloudEvent, parsed); Assert.Equal(cloudEvent.Data, parsed.Data); // We populate headers even though we don't strictly need to; let's validate that. Assert.Equal("1.0", response.Headers.GetValues("ce-specversion").Single()); Assert.Equal(cloudEvent.Type, response.Headers.GetValues("ce-type").Single()); Assert.Equal(cloudEvent.Id, response.Headers.GetValues("ce-id").Single()); Assert.Equal(CloudEventAttributeType.UriReference.Format(cloudEvent.Source !), response.Headers.GetValues("ce-source").Single()); // We don't populate the data content type header Assert.False(response.Headers.Contains("ce-datacontenttype")); }
public async Task CopyToListenerResponseAsync_BadContentMode() { var cloudEvent = new CloudEvent().PopulateRequiredAttributes(); var formatter = new JsonEventFormatter(); await GetResponseAsync( async context => await Assert.ThrowsAsync <ArgumentException>(() => cloudEvent.CopyToHttpListenerResponseAsync(context.Response, (ContentMode)100, formatter))); }
public async Task CopyToListenerResponseAsync_BinaryDataButNoDataContentType() { var cloudEvent = new CloudEvent { Data = new byte[10], }.PopulateRequiredAttributes(); var formatter = new JsonEventFormatter(); await GetResponseAsync( async context => await Assert.ThrowsAsync <ArgumentException>(() => cloudEvent.CopyToHttpListenerResponseAsync(context.Response, ContentMode.Binary, formatter))); }
public async Task CopyToListenerResponseAsync_NonBinaryDataButNoDataContentType_ContentTypeIsInferred() { var cloudEvent = new CloudEvent { Data = "plain text", }.PopulateRequiredAttributes(); var formatter = new JsonEventFormatter(); var response = await GetResponseAsync( async context => await cloudEvent.CopyToHttpListenerResponseAsync(context.Response, ContentMode.Binary, formatter)); var content = response.Content; Assert.Equal("application/json", content.Headers.ContentType.MediaType); Assert.Equal("\"plain text\"", await content.ReadAsStringAsync()); }
public async Task HttpBinaryClientReceiveTest() { string ctx = Guid.NewGuid().ToString(); PendingRequests.TryAdd(ctx, async context => { var cloudEvent = new CloudEvent() { Type = "com.github.pull.create", Source = new Uri("https://github.com/cloudevents/spec/pull/123"), Id = "A234-1234-1234", Time = SampleTimestamp, DataContentType = MediaTypeNames.Text.Xml, // TODO: This isn't JSON, so maybe we shouldn't be using a JSON formatter? // Further thought: separate out payload formatting from event formatting. Data = "<much wow=\"xml\"/>", ["comexampleextension1"] = "value", ["utf8examplevalue"] = "æøå" }; await cloudEvent.CopyToHttpListenerResponseAsync(context.Response, ContentMode.Binary, new JsonEventFormatter()); context.Response.StatusCode = (int)HttpStatusCode.OK; }); var httpClient = new HttpClient(); httpClient.DefaultRequestHeaders.Add(TestContextHeader, ctx); var result = await httpClient.GetAsync(new Uri(ListenerAddress + "ep")); Assert.Equal(HttpStatusCode.OK, result.StatusCode); // The non-ASCII attribute value should have been URL-encoded using UTF-8 for the header. Assert.True(result.Headers.TryGetValues("ce-utf8examplevalue", out var utf8ExampleValues)); Assert.Equal("%C3%A6%C3%B8%C3%A5", utf8ExampleValues.Single()); var receivedCloudEvent = await result.ToCloudEventAsync(new JsonEventFormatter()); Assert.Equal(CloudEventsSpecVersion.V1_0, receivedCloudEvent.SpecVersion); Assert.Equal("com.github.pull.create", receivedCloudEvent.Type); Assert.Equal(new Uri("https://github.com/cloudevents/spec/pull/123"), receivedCloudEvent.Source); Assert.Equal("A234-1234-1234", receivedCloudEvent.Id); AssertTimestampsEqual(SampleTimestamp, receivedCloudEvent.Time.Value); Assert.Equal(MediaTypeNames.Text.Xml, receivedCloudEvent.DataContentType); Assert.Equal("<much wow=\"xml\"/>", receivedCloudEvent.Data); Assert.Equal("value", receivedCloudEvent["comexampleextension1"]); Assert.Equal("æøå", receivedCloudEvent["utf8examplevalue"]); }
public async Task HttpStructuredClientReceiveTest() { string ctx = Guid.NewGuid().ToString(); PendingRequests.TryAdd(ctx, async context => { var cloudEvent = new CloudEvent { Type = "com.github.pull.create", Source = new Uri("https://github.com/cloudevents/spec/pull/123"), Id = "A234-1234-1234", Time = SampleTimestamp, DataContentType = MediaTypeNames.Text.Xml, Data = "<much wow=\"xml\"/>", ["comexampleextension1"] = "value", ["utf8examplevalue"] = "æøå" }; await cloudEvent.CopyToHttpListenerResponseAsync(context.Response, ContentMode.Structured, new JsonEventFormatter()); context.Response.StatusCode = (int)HttpStatusCode.OK; }); var httpClient = new HttpClient(); httpClient.DefaultRequestHeaders.Add(TestContextHeader, ctx); var result = await httpClient.GetAsync(new Uri(ListenerAddress + "ep")); Assert.Equal(HttpStatusCode.OK, result.StatusCode); Assert.True(result.IsCloudEvent()); var receivedCloudEvent = await result.ToCloudEventAsync(new JsonEventFormatter()); Assert.Equal(CloudEventsSpecVersion.V1_0, receivedCloudEvent.SpecVersion); Assert.Equal("com.github.pull.create", receivedCloudEvent.Type); Assert.Equal(new Uri("https://github.com/cloudevents/spec/pull/123"), receivedCloudEvent.Source); Assert.Equal("A234-1234-1234", receivedCloudEvent.Id); AssertTimestampsEqual(SampleTimestamp, receivedCloudEvent.Time.Value); Assert.Equal(MediaTypeNames.Text.Xml, receivedCloudEvent.DataContentType); Assert.Equal("<much wow=\"xml\"/>", receivedCloudEvent.Data); Assert.Equal("value", receivedCloudEvent["comexampleextension1"]); Assert.Equal("æøå", receivedCloudEvent["utf8examplevalue"]); }
public async Task CopyToHttpListenerResponseAsync_BinaryMode() { var cloudEvent = new CloudEvent { Data = "plain text", DataContentType = "text/plain" }.PopulateRequiredAttributes(); var formatter = new JsonEventFormatter(); var response = await GetResponseAsync( async context => await cloudEvent.CopyToHttpListenerResponseAsync(context.Response, ContentMode.Binary, formatter)); response.EnsureSuccessStatusCode(); var content = response.Content; Assert.Equal("text/plain", content.Headers.ContentType.MediaType); Assert.Equal("plain text", await content.ReadAsStringAsync()); Assert.Equal("1.0", response.Headers.GetValues("ce-specversion").Single()); Assert.Equal(cloudEvent.Type, response.Headers.GetValues("ce-type").Single()); Assert.Equal(cloudEvent.Id, response.Headers.GetValues("ce-id").Single()); Assert.Equal(CloudEventAttributeType.UriReference.Format(cloudEvent.Source !), response.Headers.GetValues("ce-source").Single()); // There's no data content type header; the content type itself is used for that. Assert.False(response.Headers.Contains("ce-datacontenttype")); }