Skip to content

Commit 5550f5a

Browse files
authored
Merge pull request #4 from webhkp/FIX-3-method-checkbox-data
Fix checkbox data submission for method
2 parents 492cc36 + c29d5a9 commit 5550f5a

File tree

3 files changed

+66
-20
lines changed

3 files changed

+66
-20
lines changed

app/main.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from fastapi.staticfiles import StaticFiles
77
from datetime import date
88
import json
9+
from typing import List
910

1011
app = FastAPI()
1112

@@ -56,7 +57,7 @@ def add_route(
5657
endpoint: str = Form(...),
5758
response_message: str = Form(...),
5859
response_status: int = Form(...),
59-
methods: list = Form([]),
60+
methods: List[str] = Form(...),
6061
):
6162
if endpoint.startswith("/"):
6263
endpoint = endpoint[1:]

app/templates/index.html

Lines changed: 51 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
66
<link rel="icon" href="/static/favicon.png" type="image/x-icon" />
77

8-
<title>Mock Server Config Viewer</title>
8+
<title>Mock Server Config</title>
99

1010
<script src="/static/alpine.js" defer></script>
1111

@@ -153,24 +153,57 @@ <h1>Mock Server Config Viewer</h1>
153153
<label>Select HTTP Methods:</label><br />
154154
<div>
155155
<label
156-
><input type="checkbox" value="GET" x-model="methods" /> GET</label
156+
><input
157+
type="checkbox"
158+
value="GET"
159+
name="methods"
160+
x-model="methods"
161+
/>
162+
GET</label
157163
><br />
158164
<label
159-
><input type="checkbox" value="POST" x-model="methods" /> POST</label
165+
><input
166+
type="checkbox"
167+
value="POST"
168+
name="methods"
169+
x-model="methods"
170+
/>
171+
POST</label
160172
><br />
161173
<label
162-
><input type="checkbox" value="PUT" x-model="methods" /> PUT</label
174+
><input
175+
type="checkbox"
176+
value="PUT"
177+
name="methods"
178+
x-model="methods"
179+
/>
180+
PUT</label
163181
><br />
164182
<label
165-
><input type="checkbox" value="DELETE" x-model="methods" />
183+
><input
184+
type="checkbox"
185+
value="DELETE"
186+
name="methods"
187+
x-model="methods"
188+
/>
166189
DELETE</label
167190
><br />
168191
<label
169-
><input type="checkbox" value="PATCH" x-model="methods" />
192+
><input
193+
type="checkbox"
194+
value="PATCH"
195+
name="methods"
196+
x-model="methods"
197+
/>
170198
PATCH</label
171199
><br />
172200
<label
173-
><input type="checkbox" value="OPTIONS" x-model="methods" />
201+
><input
202+
type="checkbox"
203+
value="OPTIONS"
204+
name="methods"
205+
x-model="methods"
206+
/>
174207
OPTIONS</label
175208
>
176209
</div>
@@ -221,6 +254,7 @@ <h2>Current Route Config</h2>
221254
<thead>
222255
<tr>
223256
<th>Endpoint</th>
257+
<th>Methods</th>
224258
<th>Response</th>
225259
<th>Status</th>
226260
</tr>
@@ -229,6 +263,7 @@ <h2>Current Route Config</h2>
229263
{% for route_data in routes_data %}
230264
<tr>
231265
<td>{{ route_data.get('endpoint') }}</td>
266+
<td>{{ route_data.get('methods') }}</td>
232267
<td>{{ route_data.get('response') }}</td>
233268
<td>{{ route_data.get('status') }}</td>
234269
</tr>
@@ -244,17 +279,23 @@ <h2>Current Route Config</h2>
244279
function validateJson() {
245280
const form = document.getElementById("jsonForm");
246281
const jsonData = form.response_message.value;
282+
const selectedMethods = [
283+
...form.querySelectorAll('input[name="methods"]:checked'),
284+
].map((cb) => cb.value);
247285

248-
if (isValid(jsonData)) {
249-
form.submit();
250-
} else {
286+
if (!isValid(jsonData)) {
251287
alert("Invalid JSON format. Please correct it.");
288+
} else if (selectedMethods.length === 0) {
289+
alert("Please select at least one method.");
290+
} else {
291+
form.submit();
252292
}
253293
}
254294

255295
function isValid(jsonString) {
256296
try {
257297
JSON.parse(jsonString);
298+
258299
return true;
259300
} catch {
260301
return false;

config.yml

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
routes:
2-
- endpoint: /hello
3-
response:
4-
message: Hello, Mock Server!
5-
status: 200
6-
- endpoint: /status
7-
response:
8-
message: available
9-
status: ok
10-
status: 200
2+
- endpoint: hello
3+
methods:
4+
- GET
5+
response:
6+
message: Hello, Mock Server!
7+
status: 200
8+
- endpoint: status
9+
methods:
10+
- GET
11+
response:
12+
message: available
13+
status: ok
14+
status: 200

0 commit comments

Comments
 (0)