|
1 | 1 | using DebugProbe.AspNetCore.Internal.Rendering; |
2 | 2 | using DebugProbe.AspNetCore.Internal.Resources; |
3 | 3 | using DebugProbe.AspNetCore.Models; |
| 4 | +using DebugProbe.AspNetCore.Options; |
4 | 5 |
|
5 | 6 | namespace DebugProbe.AspNetCore.Tests.Rendering; |
6 | 7 |
|
@@ -242,6 +243,111 @@ public void Details_page_renders_csharp_copy_attributes_and_buttons() |
242 | 243 | Assert.Equal(2, occurrences); |
243 | 244 | } |
244 | 245 |
|
| 246 | + [Fact] |
| 247 | + public void Details_page_renders_markdown_copy_attributes_and_buttons() |
| 248 | + { |
| 249 | + var entry = CreateEntry(); |
| 250 | + entry.OutgoingRequests.Add(new DebugOutgoingRequest |
| 251 | + { |
| 252 | + Method = "PUT", |
| 253 | + Url = "https://external-api.test/v1/update", |
| 254 | + StatusCode = 200, |
| 255 | + DurationMs = 120, |
| 256 | + RequestBody = "{\"name\":\"John\"}", |
| 257 | + RequestHeaders = new Dictionary<string, string> { ["Authorization"] = "Bearer token" } |
| 258 | + }); |
| 259 | + |
| 260 | + var html = HtmlRenderer.RenderDetailsPage( |
| 261 | + entry, |
| 262 | + CreateEnvironment(), |
| 263 | + "{\"request\":true}", |
| 264 | + "{\"response\":true}"); |
| 265 | + |
| 266 | + // 1. Verify Incoming Request Card attributes and button |
| 267 | + Assert.Contains("data-method=\"POST\"", html); |
| 268 | + Assert.Contains("data-url=\"http://example.test/orders?id=10\"", html); |
| 269 | + Assert.Contains("data-headers=\"{"X-Test":"yes"}\"", html); |
| 270 | + Assert.Contains("data-body=\"{"request":true}\"", html); |
| 271 | + Assert.Contains("class=\"markdown-copy-btn\"", html); |
| 272 | + |
| 273 | + // 2. Verify Outgoing Request Card attributes and button |
| 274 | + Assert.Contains("data-method=\"PUT\"", html); |
| 275 | + Assert.Contains("data-url=\"https://external-api.test/v1/update\"", html); |
| 276 | + Assert.Contains("data-headers=\"{"Authorization":"Bearer token"}\"", html); |
| 277 | + Assert.Contains("data-body=\"{"name":"John"}\"", html); |
| 278 | + |
| 279 | + // 3. Verify Response Card has no markdown copy button (only 2 copy markdown buttons in total should exist in HTML markup) |
| 280 | + var occurrences = (html.Length - html.Replace("class=\"markdown-copy-btn\"", "").Length) / "class=\"markdown-copy-btn\"".Length; |
| 281 | + Assert.Equal(2, occurrences); |
| 282 | + } |
| 283 | + |
| 284 | + [Fact] |
| 285 | + public void Render_index_page_with_slow_badge() |
| 286 | + { |
| 287 | + var items = new List<DebugEntry> |
| 288 | + { |
| 289 | + new DebugEntry { Id = "1", Method = "GET", Path = "/1", DurationMs = 500, StatusCode = 200 }, |
| 290 | + new DebugEntry { Id = "2", Method = "GET", Path = "/2", DurationMs = 1500, StatusCode = 200 } |
| 291 | + }; |
| 292 | + |
| 293 | + // Case 1: Threshold = 1000 (Default) |
| 294 | + var htmlDefault = HtmlRenderer.RenderIndexPage(items, new DebugProbeOptions { SlowRequestThresholdMs = 1000 }); |
| 295 | + Assert.Contains("/debug/1", htmlDefault); |
| 296 | + Assert.Contains("/debug/2", htmlDefault); |
| 297 | + |
| 298 | + Assert.Contains("500 ms</td>", htmlDefault); |
| 299 | + Assert.Contains("1500 ms <span class=\"dbp-badge dbp-badge-slow\" title=\"Exceeds 1000ms threshold\">SLOW</span>", htmlDefault); |
| 300 | + |
| 301 | + // Case 2: Threshold = 0 (disabled) |
| 302 | + var htmlDisabled = HtmlRenderer.RenderIndexPage(items, new DebugProbeOptions { SlowRequestThresholdMs = 0 }); |
| 303 | + Assert.DoesNotContain("<span class=\"dbp-badge dbp-badge-slow\"", htmlDisabled); |
| 304 | + |
| 305 | + // Case 3: Threshold = 2000 |
| 306 | + var htmlHigh = HtmlRenderer.RenderIndexPage(items, new DebugProbeOptions { SlowRequestThresholdMs = 2000 }); |
| 307 | + Assert.DoesNotContain("<span class=\"dbp-badge dbp-badge-slow\"", htmlHigh); |
| 308 | + } |
| 309 | + |
| 310 | + [Fact] |
| 311 | + public void Render_details_page_with_slow_badge() |
| 312 | + { |
| 313 | + var entry = CreateEntry(); |
| 314 | + entry.DurationMs = 1200; // > 1000 |
| 315 | + entry.OutgoingRequests.Add(new DebugOutgoingRequest |
| 316 | + { |
| 317 | + Method = "GET", |
| 318 | + Url = "https://external-api.test", |
| 319 | + StatusCode = 200, |
| 320 | + DurationMs = 1500, // > 1000 |
| 321 | + TimestampUtc = entry.Timestamp.UtcDateTime.AddMilliseconds(200), |
| 322 | + IsSuccessStatusCode = true |
| 323 | + }); |
| 324 | + entry.OutgoingRequests.Add(new DebugOutgoingRequest |
| 325 | + { |
| 326 | + Method = "GET", |
| 327 | + Url = "https://external-api2.test", |
| 328 | + StatusCode = 200, |
| 329 | + DurationMs = 200, // < 1000 |
| 330 | + TimestampUtc = entry.Timestamp.UtcDateTime.AddMilliseconds(400), |
| 331 | + IsSuccessStatusCode = true |
| 332 | + }); |
| 333 | + |
| 334 | + // Case 1: Threshold = 1000 |
| 335 | + var html = HtmlRenderer.RenderDetailsPage(entry, CreateEnvironment(), "{}", "{}", new DebugProbeOptions { SlowRequestThresholdMs = 1000 }); |
| 336 | + |
| 337 | + // Detail Header should have badge |
| 338 | + Assert.Contains("1200 ms <span class=\"dbp-badge dbp-badge-slow\" title=\"Exceeds 1000ms threshold\">SLOW</span>", html); |
| 339 | + |
| 340 | + // Outgoing Request 1 (1500ms) should have badge in waterfall |
| 341 | + Assert.Contains("1500 ms <span class=\"dbp-badge dbp-badge-slow\" title=\"Exceeds 1000ms threshold\">SLOW</span>", html); |
| 342 | + |
| 343 | + // Outgoing Request 2 (200ms) should NOT have badge in waterfall |
| 344 | + Assert.Contains("200 ms</div>", html); |
| 345 | + |
| 346 | + // Case 2: Threshold = 0 |
| 347 | + var htmlDisabled = HtmlRenderer.RenderDetailsPage(entry, CreateEnvironment(), "{}", "{}", new DebugProbeOptions { SlowRequestThresholdMs = 0 }); |
| 348 | + Assert.DoesNotContain("<span class=\"dbp-badge dbp-badge-slow\"", htmlDisabled); |
| 349 | + } |
| 350 | + |
245 | 351 | private static DebugEntry CreateEntry() |
246 | 352 | { |
247 | 353 | return new DebugEntry |
@@ -284,4 +390,137 @@ private static DebugEnvironment CreateEnvironment() |
284 | 390 | AssemblyVersion = "1.0.0" |
285 | 391 | }; |
286 | 392 | } |
| 393 | + |
| 394 | + [Fact] |
| 395 | + public void Render_slow_badge_boundary_exact_threshold() |
| 396 | + { |
| 397 | + var items = new List<DebugEntry> |
| 398 | + { |
| 399 | + new DebugEntry { Id = "1", Method = "GET", Path = "/exact", DurationMs = 1000, StatusCode = 200 } |
| 400 | + }; |
| 401 | + var html = HtmlRenderer.RenderIndexPage(items, new DebugProbeOptions { SlowRequestThresholdMs = 1000 }); |
| 402 | + Assert.Contains("1000 ms <span class=\"dbp-badge dbp-badge-slow\" title=\"Exceeds 1000ms threshold\">SLOW</span>", html); |
| 403 | + } |
| 404 | + |
| 405 | + [Fact] |
| 406 | + public void Render_slow_badge_boundary_threshold_minus_one() |
| 407 | + { |
| 408 | + var items = new List<DebugEntry> |
| 409 | + { |
| 410 | + new DebugEntry { Id = "1", Method = "GET", Path = "/minus-one", DurationMs = 999, StatusCode = 200 } |
| 411 | + }; |
| 412 | + var html = HtmlRenderer.RenderIndexPage(items, new DebugProbeOptions { SlowRequestThresholdMs = 1000 }); |
| 413 | + Assert.Contains("999 ms</td>", html); |
| 414 | + Assert.DoesNotContain("<span class=\"dbp-badge dbp-badge-slow\"", html); |
| 415 | + } |
| 416 | + |
| 417 | + [Fact] |
| 418 | + public void Render_slow_badge_boundary_threshold_plus_one() |
| 419 | + { |
| 420 | + var items = new List<DebugEntry> |
| 421 | + { |
| 422 | + new DebugEntry { Id = "1", Method = "GET", Path = "/plus-one", DurationMs = 1001, StatusCode = 200 } |
| 423 | + }; |
| 424 | + var html = HtmlRenderer.RenderIndexPage(items, new DebugProbeOptions { SlowRequestThresholdMs = 1000 }); |
| 425 | + Assert.Contains("1001 ms <span class=\"dbp-badge dbp-badge-slow\" title=\"Exceeds 1000ms threshold\">SLOW</span>", html); |
| 426 | + } |
| 427 | + |
| 428 | + [Fact] |
| 429 | + public void Render_slow_badge_boundary_zero_threshold_disabled() |
| 430 | + { |
| 431 | + var items = new List<DebugEntry> |
| 432 | + { |
| 433 | + new DebugEntry { Id = "1", Method = "GET", Path = "/zero", DurationMs = 5000, StatusCode = 200 } |
| 434 | + }; |
| 435 | + var html = HtmlRenderer.RenderIndexPage(items, new DebugProbeOptions { SlowRequestThresholdMs = 0 }); |
| 436 | + Assert.Contains("5000 ms</td>", html); |
| 437 | + Assert.DoesNotContain("<span class=\"dbp-badge dbp-badge-slow\"", html); |
| 438 | + } |
| 439 | + |
| 440 | + [Fact] |
| 441 | + public void Render_slow_badge_boundary_negative_threshold_disabled() |
| 442 | + { |
| 443 | + var items = new List<DebugEntry> |
| 444 | + { |
| 445 | + new DebugEntry { Id = "1", Method = "GET", Path = "/negative", DurationMs = 5000, StatusCode = 200 } |
| 446 | + }; |
| 447 | + var html = HtmlRenderer.RenderIndexPage(items, new DebugProbeOptions { SlowRequestThresholdMs = -100 }); |
| 448 | + Assert.Contains("5000 ms</td>", html); |
| 449 | + Assert.DoesNotContain("<span class=\"dbp-badge dbp-badge-slow\"", html); |
| 450 | + } |
| 451 | + |
| 452 | + [Fact] |
| 453 | + public void Render_slow_badge_large_duration_formatting() |
| 454 | + { |
| 455 | + var items = new List<DebugEntry> |
| 456 | + { |
| 457 | + new DebugEntry { Id = "1", Method = "GET", Path = "/large", DurationMs = 120500, StatusCode = 200 } |
| 458 | + }; |
| 459 | + var html = HtmlRenderer.RenderIndexPage(items, new DebugProbeOptions { SlowRequestThresholdMs = 1000 }); |
| 460 | + Assert.Contains("120500 ms <span class=\"dbp-badge dbp-badge-slow\" title=\"Exceeds 1000ms threshold\">SLOW</span>", html); |
| 461 | + } |
| 462 | + |
| 463 | + [Fact] |
| 464 | + public void Render_details_page_independent_outgoing_call_badge_behavior() |
| 465 | + { |
| 466 | + var entry = CreateEntry(); |
| 467 | + entry.DurationMs = 500; // < 1000 (not slow) |
| 468 | + entry.OutgoingRequests.Add(new DebugOutgoingRequest |
| 469 | + { |
| 470 | + Method = "GET", |
| 471 | + Url = "https://external-api.test", |
| 472 | + StatusCode = 200, |
| 473 | + DurationMs = 1500, // > 1000 (slow) |
| 474 | + TimestampUtc = entry.Timestamp.UtcDateTime.AddMilliseconds(100), |
| 475 | + IsSuccessStatusCode = true |
| 476 | + }); |
| 477 | + |
| 478 | + var html = HtmlRenderer.RenderDetailsPage(entry, CreateEnvironment(), "{}", "{}", new DebugProbeOptions { SlowRequestThresholdMs = 1000 }); |
| 479 | + |
| 480 | + // Root request should not have the slow badge |
| 481 | + Assert.Contains("<strong>500 ms</strong>", html); |
| 482 | + Assert.DoesNotContain(" 500 ms <span class=\"dbp-badge dbp-badge-slow\"", html); |
| 483 | + |
| 484 | + // Outgoing request should independently have the slow badge |
| 485 | + Assert.Contains("1500 ms <span class=\"dbp-badge dbp-badge-slow\" title=\"Exceeds 1000ms threshold\">SLOW</span>", html); |
| 486 | + } |
| 487 | + |
| 488 | + [Fact] |
| 489 | + public void Render_details_page_outgoing_call_boundary_exact() |
| 490 | + { |
| 491 | + var entry = CreateEntry(); |
| 492 | + entry.DurationMs = 200; |
| 493 | + entry.OutgoingRequests.Add(new DebugOutgoingRequest |
| 494 | + { |
| 495 | + Method = "GET", |
| 496 | + Url = "https://external-api.test", |
| 497 | + StatusCode = 200, |
| 498 | + DurationMs = 1000, // exact |
| 499 | + TimestampUtc = entry.Timestamp.UtcDateTime.AddMilliseconds(100), |
| 500 | + IsSuccessStatusCode = true |
| 501 | + }); |
| 502 | + |
| 503 | + var html = HtmlRenderer.RenderDetailsPage(entry, CreateEnvironment(), "{}", "{}", new DebugProbeOptions { SlowRequestThresholdMs = 1000 }); |
| 504 | + Assert.Contains("1000 ms <span class=\"dbp-badge dbp-badge-slow\" title=\"Exceeds 1000ms threshold\">SLOW</span>", html); |
| 505 | + } |
| 506 | + |
| 507 | + [Fact] |
| 508 | + public void Render_details_page_outgoing_call_boundary_minus_one() |
| 509 | + { |
| 510 | + var entry = CreateEntry(); |
| 511 | + entry.DurationMs = 200; |
| 512 | + entry.OutgoingRequests.Add(new DebugOutgoingRequest |
| 513 | + { |
| 514 | + Method = "GET", |
| 515 | + Url = "https://external-api.test", |
| 516 | + StatusCode = 200, |
| 517 | + DurationMs = 999, // below threshold |
| 518 | + TimestampUtc = entry.Timestamp.UtcDateTime.AddMilliseconds(100), |
| 519 | + IsSuccessStatusCode = true |
| 520 | + }); |
| 521 | + |
| 522 | + var html = HtmlRenderer.RenderDetailsPage(entry, CreateEnvironment(), "{}", "{}", new DebugProbeOptions { SlowRequestThresholdMs = 1000 }); |
| 523 | + Assert.Contains("999 ms</div>", html); |
| 524 | + Assert.DoesNotContain(" 999 ms <span class=\"dbp-badge dbp-badge-slow\"", html); |
| 525 | + } |
287 | 526 | } |
0 commit comments