|
8 | 8 | using Newtonsoft.Json.Linq; |
9 | 9 | using System; |
10 | 10 | using System.Collections.Generic; |
11 | | -using System.Globalization; |
12 | 11 | using System.Linq; |
13 | 12 | using System.Net; |
14 | 13 | using System.Net.Http; |
@@ -40,6 +39,20 @@ public class HttpClientExtensionsTest : HttpTestBase |
40 | 39 | "Structured", |
41 | 40 | new StringContent("content is ignored", Encoding.UTF8, "application/cloudevents+json"), |
42 | 41 | null |
| 42 | + }, |
| 43 | + { |
| 44 | + "Binary with header in content", |
| 45 | + new StringContent("header is in the content", Encoding.UTF8, "application/json") |
| 46 | + { |
| 47 | + Headers = |
| 48 | + { |
| 49 | + { "ce-specversion", "1.0" }, |
| 50 | + { "ce-type", "test-type" }, |
| 51 | + { "ce-id", "test-id" }, |
| 52 | + { "ce-source", "//test" } |
| 53 | + } |
| 54 | + }, |
| 55 | + null |
43 | 56 | } |
44 | 57 | }; |
45 | 58 |
|
@@ -203,7 +216,7 @@ public async Task HttpBinaryClientReceiveTest() |
203 | 216 | }; |
204 | 217 |
|
205 | 218 | await cloudEvent.CopyToHttpListenerResponseAsync(context.Response, ContentMode.Binary, new JsonEventFormatter()); |
206 | | - context.Response.StatusCode = (int)HttpStatusCode.OK; |
| 219 | + context.Response.StatusCode = (int) HttpStatusCode.OK; |
207 | 220 | }); |
208 | 221 |
|
209 | 222 | var httpClient = new HttpClient(); |
@@ -270,7 +283,7 @@ public async Task HttpBinaryClientSendTest() |
270 | 283 | Assert.Equal("value", receivedCloudEvent["comexampleextension1"]); |
271 | 284 | // The non-ASCII attribute value should have been correctly URL-decoded. |
272 | 285 | Assert.Equal("æøå", receivedCloudEvent["utf8examplevalue"]); |
273 | | - context.Response.StatusCode = (int)HttpStatusCode.NoContent; |
| 286 | + context.Response.StatusCode = (int) HttpStatusCode.NoContent; |
274 | 287 | return Task.CompletedTask; |
275 | 288 | }); |
276 | 289 |
|
@@ -301,7 +314,7 @@ public async Task HttpStructuredClientReceiveTest() |
301 | 314 | }; |
302 | 315 |
|
303 | 316 | await cloudEvent.CopyToHttpListenerResponseAsync(context.Response, ContentMode.Structured, new JsonEventFormatter()); |
304 | | - context.Response.StatusCode = (int)HttpStatusCode.OK; |
| 317 | + context.Response.StatusCode = (int) HttpStatusCode.OK; |
305 | 318 | }); |
306 | 319 |
|
307 | 320 | var httpClient = new HttpClient(); |
@@ -371,7 +384,7 @@ public async Task HttpStructuredClientSendTest() |
371 | 384 |
|
372 | 385 | Assert.Equal("value", receivedCloudEvent["comexampleextension1"]); |
373 | 386 | Assert.Equal("æøå", receivedCloudEvent["utf8examplevalue"]); |
374 | | - context.Response.StatusCode = (int)HttpStatusCode.NoContent; |
| 387 | + context.Response.StatusCode = (int) HttpStatusCode.NoContent; |
375 | 388 | return Task.CompletedTask; |
376 | 389 | }); |
377 | 390 |
|
@@ -438,6 +451,32 @@ public async Task ToHttpContent_Batch() |
438 | 451 | AssertBatchesEqual(batch, parsedBatch); |
439 | 452 | } |
440 | 453 |
|
| 454 | + [Theory] |
| 455 | + [InlineData(ContentMode.Binary)] |
| 456 | + [InlineData(ContentMode.Structured)] |
| 457 | + public async Task RoundtripRequest(ContentMode contentMode) |
| 458 | + { |
| 459 | + var cloudEvent = new CloudEvent().PopulateRequiredAttributes(); |
| 460 | + var formatter = new JsonEventFormatter(); |
| 461 | + var content = cloudEvent.ToHttpContent(contentMode, formatter); |
| 462 | + var request = new HttpRequestMessage { Content = content }; |
| 463 | + var parsed = await request.ToCloudEventAsync(formatter); |
| 464 | + AssertCloudEventsEqual(cloudEvent, parsed); |
| 465 | + } |
| 466 | + |
| 467 | + [Theory] |
| 468 | + [InlineData(ContentMode.Binary)] |
| 469 | + [InlineData(ContentMode.Structured)] |
| 470 | + public async Task RoundtripResponse(ContentMode contentMode) |
| 471 | + { |
| 472 | + var cloudEvent = new CloudEvent().PopulateRequiredAttributes(); |
| 473 | + var formatter = new JsonEventFormatter(); |
| 474 | + var content = cloudEvent.ToHttpContent(contentMode, formatter); |
| 475 | + var request = new HttpResponseMessage { Content = content }; |
| 476 | + var parsed = await request.ToCloudEventAsync(formatter); |
| 477 | + AssertCloudEventsEqual(cloudEvent, parsed); |
| 478 | + } |
| 479 | + |
441 | 480 | internal static void CopyHeaders(IDictionary<string, string>? source, HttpHeaders target) |
442 | 481 | { |
443 | 482 | if (source is null) |
|
0 commit comments