webapi: Use raw whitespace to format JSON.

No need to manually construct HTML in this formatting func, it can simply deal with whitespace and then use <pre> tags to display.

Add some tests to validate behaviour.
This commit is contained in:
jholdstock 2023-06-26 14:26:48 +01:00 committed by Jamie Holdstock
parent 78cf99bc2c
commit 4dc2cc4d8d
4 changed files with 69 additions and 10 deletions

View File

@ -4,7 +4,6 @@ import (
"bytes" "bytes"
"encoding/json" "encoding/json"
"fmt" "fmt"
"html/template"
"strings" "strings"
"time" "time"
@ -40,16 +39,19 @@ func stripWss(input string) string {
return input return input
} }
func indentJSON(log slog.Logger) func(string) template.HTML { // indentJSON returns a func which uses whitespace to format a provided JSON
return func(input string) template.HTML { // string. If the parameter is invalid JSON, an error will be logged and the
// param will be returned unaltered.
func indentJSON(log slog.Logger) func(string) string {
return func(input string) string {
var indented bytes.Buffer var indented bytes.Buffer
err := json.Indent(&indented, []byte(input), "<br/>", "&nbsp;&nbsp;&nbsp;&nbsp;") err := json.Indent(&indented, []byte(input), "", " ")
if err != nil { if err != nil {
log.Errorf("Failed to indent JSON: %w", err) log.Errorf("Failed to indent JSON: %w", err)
return template.HTML(input) return input
} }
return template.HTML(indented.String()) return indented.String()
} }
} }

51
webapi/formatting_test.go Normal file
View File

@ -0,0 +1,51 @@
package webapi
import (
"testing"
"github.com/decred/slog"
)
func TestIndentJSON(t *testing.T) {
t.Parallel()
// Get the actual invokable func by passing a noop logger.
indentJSONFunc := indentJSON(slog.Disabled)
tests := map[string]struct {
input string
expected string
}{
"nothing": {
input: "",
expected: "",
},
"empty": {
input: "{}",
expected: "{}",
},
"one line JSON": {
input: "{\"key\":\"value\"}",
expected: "{\n \"key\": \"value\"\n}",
},
"nested JSON": {
input: "{\"key\":{\"key2\":\"value\"}}",
expected: "{\n \"key\": {\n \"key2\": \"value\"\n }\n}",
},
"invalid JSON": {
input: "this is not valid json",
expected: "this is not valid json",
},
}
for testName, test := range tests {
test := test
t.Run(testName, func(t *testing.T) {
t.Parallel()
actual := indentJSONFunc(test.input)
if actual != test.expected {
t.Fatalf("expected %q, got %q", test.expected, actual)
}
})
}
}

View File

@ -158,6 +158,12 @@ footer .code {
font-size: 12px; font-size: 12px;
} }
#ticket-table pre {
color: #3D5873;
font-size: 12px;
white-space: pre-wrap;
}
.vsp-status { .vsp-status {
padding: 10px; padding: 10px;
} }

View File

@ -128,7 +128,7 @@
<table class="my-2"> <table class="my-2">
<tr> <tr>
<th>Request</th> <th>Request</th>
<td>{{ indentJSON $value.Request }}</td> <td><pre>{{ indentJSON $value.Request }}</pre></td>
</tr> </tr>
<tr> <tr>
<th>Request<br />Signature</th> <th>Request<br />Signature</th>
@ -136,7 +136,7 @@
</tr> </tr>
<tr> <tr>
<th>Response</th> <th>Response</th>
<td>{{ indentJSON $value.Response }}</td> <td><pre>{{ indentJSON $value.Response }}</pre></td>
</tr> </tr>
<tr> <tr>
<th>Response<br />Signature</th> <th>Response<br />Signature</th>
@ -172,7 +172,7 @@
<table class="my-2"> <table class="my-2">
<tr> <tr>
<th>Request</th> <th>Request</th>
<td>{{ indentJSON .AltSignAddrData.Req }}</td> <td><pre>{{ indentJSON .AltSignAddrData.Req }}</pre></td>
</tr> </tr>
<tr> <tr>
<th>Request<br />Signature</th> <th>Request<br />Signature</th>
@ -180,7 +180,7 @@
</tr> </tr>
<tr> <tr>
<th>Response</th> <th>Response</th>
<td>{{ indentJSON .AltSignAddrData.Resp }}</td> <td><pre>{{ indentJSON .AltSignAddrData.Resp }}</pre></td>
</tr> </tr>
<tr> <tr>
<th>Response<br />Signature</th> <th>Response<br />Signature</th>