Skip to content

Commit 10256b7

Browse files
Merge pull request #31 from georgidhristov/feature/payload-type-badges
Feature/payload type badges
2 parents 76f0a2a + 3f55c00 commit 10256b7

6 files changed

Lines changed: 351 additions & 127 deletions

File tree

DebugProbe.AspNetCore/Internal/HtmlRenderer.cs

Lines changed: 67 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -48,20 +48,14 @@ public static string RenderDetailsPage(DebugEntry x, DebugEnvironment e, string
4848
? x.Path
4949
: $"{x.Path}{x.Query}";
5050

51-
var statusClass = x.StatusCode switch
52-
{
53-
>= 200 and < 300 => "status-200",
54-
>= 300 and < 400 => "status-300",
55-
>= 400 and < 500 => "status-400",
56-
>= 500 => "status-500",
57-
_ => ""
58-
};
51+
var statusClass = GetStatusClass(x.StatusCode);
5952

6053
var content = EmbeddedResources.Details
6154
.Replace("{{method}}", Encode(x.Method))
6255
.Replace("{{path}}", Encode(pathWithQuery))
63-
.Replace("{{status}}", string.Format($"{x.StatusCode} {((HttpStatusCode)x.StatusCode)}"))
56+
.Replace("{{status}}", GetStatusText(x.StatusCode))
6457
.Replace("{{statusClass}}", statusClass)
58+
.Replace("{{responseStatusCode}}", x.StatusCode.ToString())
6559
.Replace("{{traceId}}", x.Id.ToString())
6660

6761
.Replace("{{time}}", x.Timestamp.ToString("yyyy-MM-dd HH:mm:ss.fff"))
@@ -80,9 +74,15 @@ public static string RenderDetailsPage(DebugEntry x, DebugEnvironment e, string
8074
.Replace("{{dateFormat}}", e.DateFormat ?? "")
8175
.Replace("{{assemblyVersion}}", Encode(e.AssemblyVersion))
8276

83-
.Replace("{{requestUrl}}", Encode(string.IsNullOrEmpty(x.RequestUrl) ? "(empty)" : x.RequestUrl))
84-
.Replace("{{request}}", Encode(string.IsNullOrEmpty(req) ? "(empty)" : req))
85-
.Replace("{{response}}", Encode(string.IsNullOrEmpty(res) ? "(empty)" : res))
77+
.Replace("{{requestUrl}}", Encode(string.IsNullOrEmpty(x.RequestUrl) ? "" : x.RequestUrl))
78+
.Replace("{{requestType}}", GetPayloadType(req))
79+
.Replace("{{requestTypeClass}}", GetPayloadTypeClass(req))
80+
.Replace("{{request}}", Encode(string.IsNullOrEmpty(req) ? "" : req))
81+
82+
.Replace("{{responseType}}", GetPayloadType(res))
83+
.Replace("{{responseTypeClass}}", GetPayloadTypeClass(res))
84+
.Replace("{{response}}", Encode(string.IsNullOrEmpty(res) ? "" : res))
85+
8686
.Replace("{{headers}}", headers);
8787

8888
return BuildLayout(content);
@@ -92,4 +92,59 @@ private static string Encode(string? value)
9292
{
9393
return WebUtility.HtmlEncode(value ?? "");
9494
}
95+
96+
private static string GetStatusText(int statusCode)
97+
{
98+
return $"{statusCode} {((HttpStatusCode)statusCode)}";
99+
}
100+
101+
private static string GetStatusClass(int statusCode)
102+
{
103+
return statusCode switch
104+
{
105+
>= 200 and < 300 => "status-200",
106+
>= 300 and < 400 => "status-300",
107+
>= 400 and < 500 => "status-400",
108+
>= 500 => "status-500",
109+
_ => ""
110+
};
111+
}
112+
113+
private static string GetPayloadType(string value)
114+
{
115+
if (string.IsNullOrWhiteSpace(value))
116+
{
117+
return "Empty";
118+
}
119+
120+
if (JsonUtils.IsValidJson(value))
121+
{
122+
return "JSON";
123+
}
124+
125+
return LooksLikeJson(value) ? "Invalid JSON" : "Plain Text";
126+
}
127+
128+
private static string GetPayloadTypeClass(string value)
129+
{
130+
if (string.IsNullOrWhiteSpace(value))
131+
{
132+
return "payload-empty";
133+
}
134+
135+
if (JsonUtils.IsValidJson(value))
136+
{
137+
return "payload-json";
138+
}
139+
140+
return LooksLikeJson(value) ? "payload-invalid-json" : "payload-text";
141+
}
142+
143+
private static bool LooksLikeJson(string value)
144+
{
145+
var trimmed = value.TrimStart();
146+
147+
return trimmed.StartsWith('{') || trimmed.StartsWith('[');
148+
}
149+
95150
}

DebugProbe.AspNetCore/Internal/JsonUtils.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,22 @@ public static string Format(string json)
2727
return json;
2828
}
2929
}
30+
31+
public static bool IsValidJson(string value)
32+
{
33+
if (string.IsNullOrWhiteSpace(value))
34+
{
35+
return false;
36+
}
37+
38+
try
39+
{
40+
JsonDocument.Parse(value);
41+
return true;
42+
}
43+
catch
44+
{
45+
return false;
46+
}
47+
}
3048
}

0 commit comments

Comments
 (0)