Skip to content
This repository was archived by the owner on May 28, 2018. It is now read-only.

Commit d34cee6

Browse files
committed
Adding testcase for FormParam validation.
Change-Id: I8e684c15e21658d0ac233ae6e9487c26e454ebcb (cherry picked from commit 9f017bf)
1 parent 1c41f5c commit d34cee6

File tree

1 file changed

+44
-13
lines changed

1 file changed

+44
-13
lines changed

core-server/src/test/java/org/glassfish/jersey/server/internal/inject/ParamExceptionMappingTest.java

Lines changed: 44 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
33
*
4-
* Copyright (c) 2012-2015 Oracle and/or its affiliates. All rights reserved.
4+
* Copyright (c) 2012-2016 Oracle and/or its affiliates. All rights reserved.
55
*
66
* The contents of this file are subject to the terms of either the GNU
77
* General Public License Version 2 only ("GPL") or the Common Development
@@ -54,6 +54,7 @@
5454
import javax.ws.rs.Path;
5555
import javax.ws.rs.PathParam;
5656
import javax.ws.rs.QueryParam;
57+
import javax.ws.rs.container.ContainerResponseContext;
5758
import javax.ws.rs.core.Cookie;
5859
import javax.ws.rs.core.Form;
5960
import javax.ws.rs.core.MediaType;
@@ -64,9 +65,9 @@
6465
import org.glassfish.jersey.server.ContainerResponse;
6566
import org.glassfish.jersey.server.ParamException;
6667
import org.glassfish.jersey.server.RequestContextBuilder;
67-
6868
import org.junit.Test;
6969
import static org.junit.Assert.assertEquals;
70+
import static org.junit.Assert.assertTrue;
7071

7172
/**
7273
* Not sure whether this is relevant anymore.
@@ -81,8 +82,14 @@ public abstract static class BaseExceptionMapper<T extends ParamException> imple
8182

8283
public Response toResponse(T exception, String entity) {
8384
assertEquals("x", exception.getParameterName());
84-
if (exception.getParameterType() != PathParam.class) {
85+
86+
// path param and form param can be integers in this test, thus different default value
87+
if (!exception.getParameterType().equals(PathParam.class)
88+
&& !exception.getParameterType().equals(FormParam.class)) {
89+
8590
assertEquals("default", exception.getDefaultStringValue());
91+
} else {
92+
assertTrue(exception.getDefaultStringValue().equals("default") || exception.getDefaultStringValue().equals("1"));
8693
}
8794
return Response.fromResponse(exception.getResponse()).entity(entity).build();
8895
}
@@ -124,7 +131,7 @@ public Response toResponse(ParamException.QueryParamException exception) {
124131
}
125132

126133
public static class
127-
CookieExceptionMapper extends BaseExceptionMapper<ParamException.CookieParamException> {
134+
CookieExceptionMapper extends BaseExceptionMapper<ParamException.CookieParamException> {
128135

129136
public Response toResponse(ParamException.CookieParamException exception) {
130137
return toResponse(exception, "cookie");
@@ -150,7 +157,7 @@ public static class ParamExceptionMapperResource {
150157

151158
@Path("path/{x}")
152159
@GET
153-
public String getPath(@PathParam("x") URI x) {
160+
public String getPath(@DefaultValue("1") @PathParam("x") int x) {
154161
return "";
155162
}
156163

@@ -184,19 +191,26 @@ public String getHeader(@DefaultValue("default") @HeaderParam("x") URI x) {
184191
public String postForm(@DefaultValue("default") @FormParam("x") URI x) {
185192
return "";
186193
}
194+
195+
@Path("form-int")
196+
@POST
197+
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
198+
public String postForm(@DefaultValue("1") @FormParam("x") int x) {
199+
return "";
200+
}
187201
}
188202

189203
@Test
190204
public void testParamException() throws ExecutionException, InterruptedException {
191205
initiateWebApplication(ParamExceptionMapperResource.class,
192-
PathExceptionMapper.class,
193-
MatrixExceptionMapper.class,
194-
QueryExceptionMapper.class,
195-
CookieExceptionMapper.class,
196-
HeaderExceptionMapper.class,
197-
FormExceptionMapper.class);
198-
199-
ContainerResponse responseContext = getResponseContext(UriBuilder.fromPath("/").path("path/ 123").build().toString());
206+
PathExceptionMapper.class,
207+
MatrixExceptionMapper.class,
208+
QueryExceptionMapper.class,
209+
CookieExceptionMapper.class,
210+
HeaderExceptionMapper.class,
211+
FormExceptionMapper.class);
212+
213+
ContainerResponse responseContext = getResponseContext(UriBuilder.fromPath("/").path("path/ test").build().toString());
200214
assertEquals("path", responseContext.getEntity());
201215

202216
responseContext = getResponseContext(UriBuilder.fromPath("/").path("matrix;x= 123").build().toString());
@@ -226,6 +240,23 @@ public void testParamException() throws ExecutionException, InterruptedException
226240
assertEquals("form", responseContext.getEntity());
227241
}
228242

243+
@Test
244+
public void testFormParamPrimitiveValidation() throws ExecutionException, InterruptedException {
245+
initiateWebApplication(ParamExceptionMapperResource.class,
246+
FormExceptionMapper.class);
247+
248+
Form f = new Form();
249+
f.param("x", "http://oracle.com");
250+
ContainerResponseContext responseContext = apply(
251+
RequestContextBuilder.from("/form-int", "POST")
252+
.type(MediaType.APPLICATION_FORM_URLENCODED_TYPE)
253+
.entity(f)
254+
.build()
255+
);
256+
257+
assertEquals("form", responseContext.getEntity());
258+
}
259+
229260
@Test
230261
public void testGeneralParamException() throws ExecutionException, InterruptedException {
231262
initiateWebApplication(ParamExceptionMapperResource.class,

0 commit comments

Comments
 (0)