Skip to content

Commit 4cf299f

Browse files
committed
Add a crude test to verify #16
1 parent 70471a3 commit 4cf299f

File tree

2 files changed

+68
-1
lines changed

2 files changed

+68
-1
lines changed

json/src/test/java/com/fasterxml/jackson/jakarta/rs/json/dw/ResourceTestBase.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,4 +90,15 @@ protected Server startServer(int port, Class<? extends Application> appClass,
9090
}
9191
return server;
9292
}
93+
94+
/*
95+
/**********************************************************
96+
/* Other helper methods
97+
/**********************************************************
98+
*/
99+
100+
protected String a2q(String json) {
101+
return json.replace("'", "\"");
102+
}
103+
93104
}

json/src/test/java/com/fasterxml/jackson/jakarta/rs/json/dw/SimpleEndpointTestBase.java

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,15 @@ public Point maxPoint(MappingIterator<Point> points) throws IOException
205205
return max;
206206
}
207207

208+
@Path("/echo")
209+
@POST
210+
@Consumes(MediaType.APPLICATION_JSON)
211+
@Produces(MediaType.APPLICATION_JSON)
212+
public Point echoPoint(Point point) throws IOException
213+
{
214+
return point;
215+
}
216+
208217
private int _distance(Point p) {
209218
return (p.x * p.x) + (p.y * p.y);
210219
}
@@ -478,7 +487,54 @@ public void testMappingIterator() throws Exception
478487
assertEquals(4, p.y);
479488
}
480489

481-
// [Issue#34] Verify that Untouchables act the way as they should
490+
// [jakarta-rs-providers#16]
491+
public void testPointNoTrailingContent() throws Exception
492+
{
493+
final ObjectMapper mapper = new ObjectMapper();
494+
Server server = startServer(TEST_PORT, SimpleResourceApp.class);
495+
Point p;
496+
497+
try {
498+
URL url = new URL("http://localhost:"+TEST_PORT+"/point/echo");
499+
500+
// First, content with no trailing stuff
501+
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
502+
conn.setRequestProperty("Accept", MediaType.APPLICATION_JSON);
503+
conn.setRequestProperty("Content-Type", MediaType.APPLICATION_JSON);
504+
conn.setDoOutput(true);
505+
conn.setRequestMethod("POST");
506+
OutputStream out = conn.getOutputStream();
507+
out.write(a2q("{'x':1,'y':1}").getBytes("UTF-8"));
508+
out.close();
509+
assertEquals(200, conn.getResponseCode());
510+
InputStream in = conn.getInputStream();
511+
p = mapper.readValue(in, Point.class);
512+
in.close();
513+
// ensure we got a valid Point
514+
assertNotNull(p);
515+
assertEquals(1, p.x);
516+
assertEquals(1, p.y);
517+
518+
// Then try with trailing token; not allowed
519+
conn = (HttpURLConnection) url.openConnection();
520+
conn.setRequestProperty("Accept", MediaType.APPLICATION_JSON);
521+
conn.setRequestProperty("Content-Type", MediaType.APPLICATION_JSON);
522+
conn.setDoOutput(true);
523+
conn.setRequestMethod("POST");
524+
out = conn.getOutputStream();
525+
out.write(a2q("{'x':1,'y':1} 123 ").getBytes("UTF-8"));
526+
out.close();
527+
528+
// Hmmh. Typically would be mapped to 400 but apparently JAX-RS default is 500
529+
assertEquals(500, conn.getResponseCode());
530+
in.close();
531+
532+
} finally {
533+
server.stop();
534+
}
535+
}
536+
537+
// Verify that Untouchables act the way as they should
482538
@SuppressWarnings("resource")
483539
public void testUntouchables() throws Exception
484540
{

0 commit comments

Comments
 (0)