|
4 | 4 |
|
5 | 5 | using System; |
6 | 6 | using System.Collections.Generic; |
7 | | -using System.Diagnostics; |
8 | 7 | using System.IO; |
9 | 8 | using System.Linq; |
10 | 9 | using System.Net; |
@@ -285,6 +284,206 @@ public async Task ApmMiddleware_ShouldSkipCapturing_WhenInvalidContentType() |
285 | 284 | result.StatusCode.Should().Be(HttpStatusCode.UnsupportedMediaType); |
286 | 285 | } |
287 | 286 |
|
| 287 | + [Fact] |
| 288 | + public async Task When_CaptureBodyConfigurationToAllAndSuccessfulCall_Should_CaptureBody() |
| 289 | + { |
| 290 | + var sutEnv = StartSutEnv(CreateConfiguration()); |
| 291 | + |
| 292 | + // build test data, which we send to the sample app |
| 293 | + var data = new { Name = "John" }; |
| 294 | + |
| 295 | + var body = JsonConvert.SerializeObject(data, |
| 296 | + new JsonSerializerSettings { ContractResolver = new CamelCasePropertyNamesContractResolver() }); |
| 297 | + |
| 298 | + // send data to the sample app |
| 299 | + var result = await sutEnv.HttpClient.PostAsync("api/Home/Send", new StringContent(body, Encoding.UTF8, "application/json")); |
| 300 | + |
| 301 | + // wait for the payload sender to receive the transaction |
| 302 | + sutEnv.MockPayloadSender.WaitForTransactions(TimeSpan.FromSeconds(10)); |
| 303 | + |
| 304 | + // make sure the sample app received the data |
| 305 | + result.StatusCode.Should().Be((HttpStatusCode)200); |
| 306 | + |
| 307 | + // and make sure the data is captured by the agent |
| 308 | + sutEnv.MockPayloadSender.FirstTransaction.Should().NotBeNull(); |
| 309 | + sutEnv.MockPayloadSender.FirstTransaction.Context.Request.Body.Should().Be(body); |
| 310 | + } |
| 311 | + |
| 312 | + [Fact] |
| 313 | + public async Task When_CaptureBodyConfigurationToTransactionsAndSuccessfulCall_Should_CaptureBody() |
| 314 | + { |
| 315 | + var sutEnv = StartSutEnv(CreateConfiguration(ConfigConsts.SupportedValues.CaptureBodyTransactions)); |
| 316 | + |
| 317 | + // build test data, which we send to the sample app |
| 318 | + var data = new { Name = "John" }; |
| 319 | + |
| 320 | + var body = JsonConvert.SerializeObject(data, |
| 321 | + new JsonSerializerSettings { ContractResolver = new CamelCasePropertyNamesContractResolver() }); |
| 322 | + |
| 323 | + // send data to the sample app |
| 324 | + var result = await sutEnv.HttpClient.PostAsync("api/Home/Send", new StringContent(body, Encoding.UTF8, "application/json")); |
| 325 | + |
| 326 | + // wait for the payload sender to receive the transaction |
| 327 | + sutEnv.MockPayloadSender.WaitForTransactions(TimeSpan.FromSeconds(3)); |
| 328 | + |
| 329 | + // make sure the sample app received the data |
| 330 | + result.StatusCode.Should().Be((HttpStatusCode)200); |
| 331 | + |
| 332 | + // and make sure the data is captured by the agent |
| 333 | + sutEnv.MockPayloadSender.FirstTransaction.Should().NotBeNull(); |
| 334 | + sutEnv.MockPayloadSender.FirstTransaction.Context.Request.Body.Should().Be(body); |
| 335 | + } |
| 336 | + |
| 337 | + [Fact] |
| 338 | + public async Task When_CaptureBodyConfigurationToOffAndSuccessfulCall_ShouldNot_CaptureBody() |
| 339 | + { |
| 340 | + var sutEnv = StartSutEnv(CreateConfiguration(ConfigConsts.SupportedValues.CaptureBodyOff)); |
| 341 | + |
| 342 | + // build test data, which we send to the sample app |
| 343 | + var data = new { Name = "John" }; |
| 344 | + |
| 345 | + var body = JsonConvert.SerializeObject(data, |
| 346 | + new JsonSerializerSettings { ContractResolver = new CamelCasePropertyNamesContractResolver() }); |
| 347 | + |
| 348 | + // send data to the sample app |
| 349 | + var result = await sutEnv.HttpClient.PostAsync("api/Home/Send", new StringContent(body, Encoding.UTF8, "application/json")); |
| 350 | + |
| 351 | + // wait for the payload sender to receive the transaction |
| 352 | + sutEnv.MockPayloadSender.WaitForTransactions(TimeSpan.FromSeconds(3)); |
| 353 | + |
| 354 | + // make sure the sample app received the data |
| 355 | + result.StatusCode.Should().Be((HttpStatusCode)200); |
| 356 | + |
| 357 | + // and make sure the data is captured by the agent |
| 358 | + sutEnv.MockPayloadSender.FirstTransaction.Should().NotBeNull(); |
| 359 | + sutEnv.MockPayloadSender.FirstTransaction.Context.Request.Body.Should().Be("[REDACTED]"); |
| 360 | + } |
| 361 | + |
| 362 | + [Fact] |
| 363 | + public async Task When_CaptureBodyConfigurationToErrorsAndSuccessfulCall_ShouldNot_CaptureBody() |
| 364 | + { |
| 365 | + var sutEnv = StartSutEnv(CreateConfiguration(ConfigConsts.SupportedValues.CaptureBodyErrors)); |
| 366 | + |
| 367 | + // build test data, which we send to the sample app |
| 368 | + var data = new { Name = "John" }; |
| 369 | + |
| 370 | + var body = JsonConvert.SerializeObject(data, |
| 371 | + new JsonSerializerSettings { ContractResolver = new CamelCasePropertyNamesContractResolver() }); |
| 372 | + |
| 373 | + // send data to the sample app |
| 374 | + var result = await sutEnv.HttpClient.PostAsync("api/Home/Send", new StringContent(body, Encoding.UTF8, "application/json")); |
| 375 | + |
| 376 | + // wait for the payload sender to receive the transaction |
| 377 | + sutEnv.MockPayloadSender.WaitForTransactions(TimeSpan.FromSeconds(3)); |
| 378 | + |
| 379 | + // make sure the sample app received the data |
| 380 | + result.StatusCode.Should().Be((HttpStatusCode)200); |
| 381 | + |
| 382 | + // and make sure the data is captured by the agent |
| 383 | + sutEnv.MockPayloadSender.FirstTransaction.Should().NotBeNull(); |
| 384 | + sutEnv.MockPayloadSender.FirstTransaction.Context.Request.Body.Should().BeNull(); |
| 385 | + } |
| 386 | + |
| 387 | + [Fact] |
| 388 | + public async Task When_CaptureBodyConfigurationToAllAndFailingCall_Should_CaptureBody() |
| 389 | + { |
| 390 | + var sutEnv = StartSutEnv(CreateConfiguration()); |
| 391 | + |
| 392 | + // build test data, which we send to the sample app |
| 393 | + var data = new { Name = "John" }; |
| 394 | + |
| 395 | + var body = JsonConvert.SerializeObject(data, |
| 396 | + new JsonSerializerSettings { ContractResolver = new CamelCasePropertyNamesContractResolver() }); |
| 397 | + |
| 398 | + // send data to the sample app |
| 399 | + var result = await sutEnv.HttpClient.PostAsync("api/Home/SendError", new StringContent(body, Encoding.UTF8, "application/json")); |
| 400 | + |
| 401 | + // wait for the payload sender to receive the transaction |
| 402 | + sutEnv.MockPayloadSender.WaitForTransactions(TimeSpan.FromSeconds(3)); |
| 403 | + |
| 404 | + // make sure the sample app received the data |
| 405 | + result.StatusCode.Should().Be((HttpStatusCode)500); |
| 406 | + |
| 407 | + // and make sure the data is captured by the agent |
| 408 | + sutEnv.MockPayloadSender.FirstTransaction.Should().NotBeNull(); |
| 409 | + sutEnv.MockPayloadSender.FirstTransaction.Context.Request.Body.Should().Be(body); |
| 410 | + } |
| 411 | + |
| 412 | + [Fact] |
| 413 | + public async Task When_CaptureBodyConfigurationToErrorsAndFailingCall_Should_CaptureBody() |
| 414 | + { |
| 415 | + var sutEnv = StartSutEnv(CreateConfiguration(ConfigConsts.SupportedValues.CaptureBodyErrors)); |
| 416 | + |
| 417 | + // build test data, which we send to the sample app |
| 418 | + var data = new { Name = "John" }; |
| 419 | + |
| 420 | + var body = JsonConvert.SerializeObject(data, |
| 421 | + new JsonSerializerSettings { ContractResolver = new CamelCasePropertyNamesContractResolver() }); |
| 422 | + |
| 423 | + // send data to the sample app |
| 424 | + var result = await sutEnv.HttpClient.PostAsync("api/Home/SendError", new StringContent(body, Encoding.UTF8, "application/json")); |
| 425 | + |
| 426 | + // wait for the payload sender to receive the transaction |
| 427 | + sutEnv.MockPayloadSender.WaitForTransactions(TimeSpan.FromSeconds(3)); |
| 428 | + |
| 429 | + // make sure the sample app received the data |
| 430 | + result.StatusCode.Should().Be((HttpStatusCode)500); |
| 431 | + |
| 432 | + // and make sure the data is captured by the agent |
| 433 | + sutEnv.MockPayloadSender.FirstTransaction.Should().NotBeNull(); |
| 434 | + sutEnv.MockPayloadSender.FirstTransaction.Context.Request.Body.Should().Be(body); |
| 435 | + } |
| 436 | + |
| 437 | + [Fact] |
| 438 | + public async Task When_CaptureBodyConfigurationToTransactionsAndFailingCall_ShouldNot_CaptureBody() |
| 439 | + { |
| 440 | + var sutEnv = StartSutEnv(CreateConfiguration(ConfigConsts.SupportedValues.CaptureBodyTransactions)); |
| 441 | + |
| 442 | + // build test data, which we send to the sample app |
| 443 | + var data = new { Name = "John" }; |
| 444 | + |
| 445 | + var body = JsonConvert.SerializeObject(data, |
| 446 | + new JsonSerializerSettings { ContractResolver = new CamelCasePropertyNamesContractResolver() }); |
| 447 | + |
| 448 | + // send data to the sample app |
| 449 | + var result = await sutEnv.HttpClient.PostAsync("api/Home/SendError", new StringContent(body, Encoding.UTF8, "application/json")); |
| 450 | + |
| 451 | + // wait for the payload sender to receive the transaction |
| 452 | + sutEnv.MockPayloadSender.WaitForTransactions(TimeSpan.FromSeconds(3)); |
| 453 | + |
| 454 | + // make sure the sample app received the data |
| 455 | + result.StatusCode.Should().Be((HttpStatusCode)500); |
| 456 | + |
| 457 | + // and make sure the data is captured by the agent |
| 458 | + sutEnv.MockPayloadSender.FirstTransaction.Should().NotBeNull(); |
| 459 | + sutEnv.MockPayloadSender.FirstTransaction.Context.Request.Body.Should().Be("[REDACTED]"); |
| 460 | + } |
| 461 | + |
| 462 | + [Fact] |
| 463 | + public async Task When_CaptureBodyConfigurationToOffAndFailingCall_ShouldNot_CaptureBody() |
| 464 | + { |
| 465 | + var sutEnv = StartSutEnv(CreateConfiguration(ConfigConsts.SupportedValues.CaptureBodyOff)); |
| 466 | + |
| 467 | + // build test data, which we send to the sample app |
| 468 | + var data = new { Name = "John" }; |
| 469 | + |
| 470 | + var body = JsonConvert.SerializeObject(data, |
| 471 | + new JsonSerializerSettings { ContractResolver = new CamelCasePropertyNamesContractResolver() }); |
| 472 | + |
| 473 | + // send data to the sample app |
| 474 | + var result = await sutEnv.HttpClient.PostAsync("api/Home/SendError", new StringContent(body, Encoding.UTF8, "application/json")); |
| 475 | + |
| 476 | + // wait for the payload sender to receive the transaction |
| 477 | + sutEnv.MockPayloadSender.WaitForTransactions(TimeSpan.FromSeconds(3)); |
| 478 | + |
| 479 | + // make sure the sample app received the data |
| 480 | + result.StatusCode.Should().Be((HttpStatusCode)500); |
| 481 | + |
| 482 | + // and make sure the data is captured by the agent |
| 483 | + sutEnv.MockPayloadSender.FirstTransaction.Should().NotBeNull(); |
| 484 | + sutEnv.MockPayloadSender.FirstTransaction.Context.Request.Body.Should().Be("[REDACTED]"); |
| 485 | + } |
| 486 | + |
288 | 487 | private static IEnumerable<OptionsTestVariant> BuildOptionsTestVariants() |
289 | 488 | { |
290 | 489 | var captureBodyContentTypesVariants = new[] |
|
0 commit comments