Skip to content

Commit 33af963

Browse files
committed
improve http request parameter display
1 parent 37c514d commit 33af963

File tree

2 files changed

+84
-10
lines changed

2 files changed

+84
-10
lines changed

api/handler_events_test.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,57 @@ func TestHandler_Events(t *testing.T) {
7777
event.Time.Format(time.RFC3339Nano))))
7878
},
7979
},
80+
{
81+
name: "get http event with header parameter as string",
82+
fn: func(t *testing.T, h http.Handler, sm *events.StoreManager) {
83+
sm.SetStore(1, events.NewTraits().WithNamespace("http"))
84+
85+
r := httptest.NewRequest("get", "http://localhost/foo", nil)
86+
r.Header.Set("foo", "bar")
87+
88+
params := &openapi.RequestParameters{Header: map[string]openapi.RequestParameterValue{}}
89+
v := "bar"
90+
params.Header["Foo"] = openapi.RequestParameterValue{
91+
Value: "bar",
92+
Raw: &v,
93+
}
94+
r = r.WithContext(openapi.NewContext(context.Background(), params))
95+
96+
_, err := openapi.NewLogEventContext(r, false, sm, events.NewTraits().WithNamespace("http"))
97+
require.NoError(t, err)
98+
try.Handler(t,
99+
http.MethodGet,
100+
"http://foo.api/api/events?namespace=http",
101+
nil,
102+
"",
103+
h,
104+
try.HasStatusCode(200),
105+
try.AssertBody(func(t *testing.T, body string) {
106+
var m []map[string]any
107+
require.NoError(t, json.Unmarshal([]byte(body), &m))
108+
require.Equal(t, map[string]interface{}{
109+
"actions": interface{}(nil),
110+
"api": "",
111+
"deprecated": false,
112+
"duration": float64(0),
113+
"path": "",
114+
"request": map[string]interface{}{
115+
"method": "get",
116+
"parameters": []interface{}{
117+
map[string]interface{}{
118+
"name": "Foo",
119+
"raw": "bar",
120+
"type": "header",
121+
"value": "\"bar\"",
122+
},
123+
},
124+
"url": "http://localhost/foo",
125+
},
126+
"response": map[string]interface{}{"body": "", "size": float64(0), "statusCode": float64(0)}},
127+
m[0]["data"])
128+
}))
129+
},
130+
},
80131
{
81132
name: "get http event with header parameter as object",
82133
fn: func(t *testing.T, h http.Handler, sm *events.StoreManager) {
Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<script setup lang="ts">
2-
import { computed, type PropType } from 'vue';
2+
import { computed, ref, type PropType } from 'vue';
33
44
const props = defineProps({
55
parameters: { type: Object as PropType<HttpEventParameter[]>, required: true },
@@ -15,24 +15,44 @@ const sorted = computed(() => {
1515
return 1
1616
})
1717
})
18+
const showRaw = ref<{[name: string]: boolean}>({})
19+
function renderJsonValue(value: any) {
20+
try {
21+
const parsed = typeof value === 'string' ? JSON.parse(value) : value;
22+
if (typeof parsed === 'string') {
23+
return parsed;
24+
}
25+
return JSON.stringify(parsed, null, 2);
26+
} catch {
27+
return value
28+
}
29+
}
1830
</script>
1931

2032
<template>
21-
<table class="table dataTable">
33+
<table class="table table.sm dataTable">
2234
<thead>
2335
<tr>
24-
<th scope="col" class="text-left w-25">Name</th>
25-
<th scope="col" class="text-left w-10">Type</th>
26-
<th scope="col" class="text-center w-10">OpenAPI</th>
27-
<th scope="col" class="text-left">Value</th>
36+
<th scope="col" style="width:40px"></th>
37+
<th scope="col" class="text-left w-20">Name</th>
38+
<th scope="col" class="text-left" style="width:100px;">Type</th>
39+
<th scope="col" class="text-center" style="width: 130px;">OpenAPI</th>
40+
<th scope="col" class="text-left" style="width:70%">Value</th>
2841
</tr>
2942
</thead>
3043
<tbody>
3144
<tr v-for="p in sorted">
32-
<td>{{ p.name }}</td>
33-
<td>{{ p.type }}</td>
34-
<td class="text-center">{{ p.value ? 'yes' : 'no' }}</td>
35-
<td>{{ p.value ? p.value : p.raw }}</td>
45+
<td>
46+
<button class="btn btn-sm btn-outline-secondary" style="--bs-btn-padding-y: .1rem; --bs-btn-padding-x: .25rem; --bs-btn-font-size: .75rem;"
47+
@click="showRaw[p.name] = !showRaw[p.name]">
48+
<i v-if="showRaw[p.name]" class="bi bi-layout-text-sidebar" title="Show parsed value"></i>
49+
<i v-else class="bi bi-code" title="Show raw value"></i>
50+
</button>
51+
</td>
52+
<td class="align-middle">{{ p.name }}</td>
53+
<td class="align-middle">{{ p.type }}</td>
54+
<td class="text-center align-middle">{{ p.value ? 'yes' : 'no' }}</td>
55+
<td class="align-middle">{{ p.value ? (showRaw[p.name] ? p.raw : renderJsonValue(p.value)) : p.raw }}</td>
3656
</tr>
3757
</tbody>
3858
</table>
@@ -42,4 +62,7 @@ const sorted = computed(() => {
4262
.w-10{
4363
width: 10%;
4464
}
65+
.w-20{
66+
width: 20%;
67+
}
4568
</style>

0 commit comments

Comments
 (0)