Skip to content

Commit 96e8684

Browse files
committed
test: Fixtures and handlers for form-urlencoded and text/plain bodies
1 parent d326115 commit 96e8684

4 files changed

Lines changed: 117 additions & 0 deletions

File tree

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.retailsvc.http.start;
2+
3+
import static java.net.HttpURLConnection.HTTP_OK;
4+
5+
import com.retailsvc.http.Request;
6+
import com.sun.net.httpserver.HttpExchange;
7+
import com.sun.net.httpserver.HttpHandler;
8+
import java.io.IOException;
9+
import java.nio.charset.StandardCharsets;
10+
11+
/** Echoes the parsed form body to the response as Map#toString. */
12+
public class FormEchoHandler implements HttpHandler {
13+
14+
@Override
15+
public void handle(HttpExchange exchange) throws IOException {
16+
Object parsed = Request.parsed();
17+
byte[] body = String.valueOf(parsed).getBytes(StandardCharsets.UTF_8);
18+
try (exchange) {
19+
exchange.getResponseHeaders().add("Content-Type", "text/plain; charset=utf-8");
20+
exchange.sendResponseHeaders(HTTP_OK, body.length);
21+
exchange.getResponseBody().write(body);
22+
}
23+
}
24+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.retailsvc.http.start;
2+
3+
import static java.net.HttpURLConnection.HTTP_OK;
4+
5+
import com.retailsvc.http.Request;
6+
import com.sun.net.httpserver.HttpExchange;
7+
import com.sun.net.httpserver.HttpHandler;
8+
import java.io.IOException;
9+
import java.nio.charset.StandardCharsets;
10+
11+
/** Echoes the parsed text/plain body back to the response. */
12+
public class TextEchoHandler implements HttpHandler {
13+
14+
@Override
15+
public void handle(HttpExchange exchange) throws IOException {
16+
String parsed = (String) Request.parsed();
17+
byte[] body = parsed.getBytes(StandardCharsets.UTF_8);
18+
try (exchange) {
19+
exchange.getResponseHeaders().add("Content-Type", "text/plain; charset=utf-8");
20+
exchange.sendResponseHeaders(HTTP_OK, body.length);
21+
exchange.getResponseBody().write(body);
22+
}
23+
}
24+
}

src/test/resources/openapi.json

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,48 @@
335335
},
336336
"responses": { "200": { "description": "OK" } }
337337
}
338+
},
339+
"/form-echo": {
340+
"post": {
341+
"operationId": "form-echo",
342+
"requestBody": {
343+
"required": true,
344+
"content": {
345+
"application/x-www-form-urlencoded": {
346+
"schema": {
347+
"type": "object",
348+
"properties": {
349+
"name": { "type": "string" },
350+
"age": { "type": "integer" },
351+
"tags": {
352+
"type": "array",
353+
"items": { "type": "string" }
354+
}
355+
}
356+
}
357+
}
358+
}
359+
},
360+
"responses": {
361+
"200": { "description": "ok" }
362+
}
363+
}
364+
},
365+
"/text-echo": {
366+
"post": {
367+
"operationId": "text-echo",
368+
"requestBody": {
369+
"required": true,
370+
"content": {
371+
"text/plain": {
372+
"schema": { "type": "string" }
373+
}
374+
}
375+
},
376+
"responses": {
377+
"200": { "description": "ok" }
378+
}
379+
}
338380
}
339381
},
340382
"components": {

src/test/resources/openapi.yaml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,33 @@ paths:
237237
responses:
238238
"200":
239239
description: OK
240+
/form-echo:
241+
post:
242+
operationId: form-echo
243+
requestBody:
244+
required: true
245+
content:
246+
application/x-www-form-urlencoded:
247+
schema:
248+
type: object
249+
properties:
250+
name: { type: string }
251+
age: { type: integer }
252+
tags:
253+
type: array
254+
items: { type: string }
255+
responses:
256+
"200": { description: ok }
257+
/text-echo:
258+
post:
259+
operationId: text-echo
260+
requestBody:
261+
required: true
262+
content:
263+
text/plain:
264+
schema: { type: string }
265+
responses:
266+
"200": { description: ok }
240267

241268
components:
242269
parameters:

0 commit comments

Comments
 (0)