Repository: cxf Updated Branches: refs/heads/master 7988c4a97 -> 4306144c1
[CXF-6478]: Added JUnit test Project: http://git-wip-us.apache.org/repos/asf/cxf/repo Commit: http://git-wip-us.apache.org/repos/asf/cxf/commit/4306144c Tree: http://git-wip-us.apache.org/repos/asf/cxf/tree/4306144c Diff: http://git-wip-us.apache.org/repos/asf/cxf/diff/4306144c Branch: refs/heads/master Commit: 4306144c1704d2cf085d387cf0be8d9cbdd5f5b8 Parents: 7988c4a Author: Andrei Shakirin <[email protected]> Authored: Sun Jul 5 15:07:06 2015 +0200 Committer: Andrei Shakirin <[email protected]> Committed: Sun Jul 5 15:07:06 2015 +0200 ---------------------------------------------------------------------- .../org/apache/cxf/jaxrs/utils/FormUtils.java | 7 +- .../apache/cxf/jaxrs/utils/FormUtilsTest.java | 104 +++++++++++++++++++ 2 files changed, 110 insertions(+), 1 deletion(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/cxf/blob/4306144c/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/FormUtils.java ---------------------------------------------------------------------- diff --git a/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/FormUtils.java b/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/FormUtils.java index afee3bc..e91e60a 100644 --- a/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/FormUtils.java +++ b/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/FormUtils.java @@ -51,6 +51,7 @@ import org.apache.cxf.message.MessageUtils; import org.apache.cxf.phase.PhaseInterceptorChain; public final class FormUtils { + public static final String FORM_PARAMS_FROM_HTTP_PARAMS = "set.form.parameters.from.http.parameters"; public static final String FORM_PARAM_MAP = "org.apache.cxf.form_data"; private static final Logger LOG = LogUtils.getL7dLogger(FormUtils.class); @@ -156,7 +157,7 @@ public final class FormUtils { if (!StringUtils.isEmpty(postBody)) { populateMapFromString(params, m, postBody, enc, decode); } else if (request != null - && MessageUtils.getContextualBoolean(m, "set.form.parameters.from.http.parameters", true)) { + && MessageUtils.getContextualBoolean(m, FORM_PARAMS_FROM_HTTP_PARAMS, true)) { for (Enumeration<String> en = request.getParameterNames(); en.hasMoreElements();) { String paramName = en.nextElement(); String[] values = request.getParameterValues(paramName); @@ -167,6 +168,10 @@ public final class FormUtils { } public static void logRequestParametersIfNeeded(Map<String, List<String>> params, String enc) { + if ((PhaseInterceptorChain.getCurrentMessage() == null) + || (PhaseInterceptorChain.getCurrentMessage().getInterceptorChain() == null)) { + return; + } String chain = PhaseInterceptorChain.getCurrentMessage().getInterceptorChain().toString(); if (chain.contains(LoggingInInterceptor.class.getSimpleName())) { ByteArrayOutputStream bos = new ByteArrayOutputStream(); http://git-wip-us.apache.org/repos/asf/cxf/blob/4306144c/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/utils/FormUtilsTest.java ---------------------------------------------------------------------- diff --git a/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/utils/FormUtilsTest.java b/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/utils/FormUtilsTest.java new file mode 100644 index 0000000..ac90b1e --- /dev/null +++ b/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/utils/FormUtilsTest.java @@ -0,0 +1,104 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.cxf.jaxrs.utils; + +import java.util.Arrays; +import java.util.Collections; +import java.util.Enumeration; + +import javax.servlet.http.HttpServletRequest; +import javax.ws.rs.core.MultivaluedMap; + +import org.apache.cxf.jaxrs.impl.MetadataMap; +import org.apache.cxf.message.Message; +import org.easymock.EasyMock; + +import org.junit.Assert; +import org.junit.Test; + +public class FormUtilsTest extends Assert { + + private static final String HTTP_PARAM1 = "httpParam1"; + private static final String HTTP_PARAM2 = "httpParam2"; + private static final String HTTP_PARAM_VALUE1 = "httpValue1"; + private static final String HTTP_PARAM_VALUE2 = "httpValue2"; + + private static final String FORM_PARAM1 = "formParam1"; + private static final String FORM_PARAM2 = "formParam2"; + private static final String FORM_PARAM_VALUE1 = "formValue1"; + private static final String FORM_PARAM_VALUE2 = "formValue2"; + + private Message mockMessage; + private HttpServletRequest mockRequest; + + @Test + public void populateMapFromStringFromHTTP() { + mockObjects(null); + EasyMock.replay(mockMessage, mockRequest); + + MultivaluedMap<String, String> params = new MetadataMap<String, String>(); + FormUtils.populateMapFromString(params, mockMessage, null, "UTF-8", false, mockRequest); + + assertEquals(2, params.size()); + assertEquals(HTTP_PARAM_VALUE1, params.get(HTTP_PARAM1).iterator().next()); + assertEquals(HTTP_PARAM_VALUE2, params.get(HTTP_PARAM2).iterator().next()); + } + + public void populateMapFromStringFromHTTPWithProp() { + mockObjects("false"); + EasyMock.replay(mockMessage, mockRequest); + + MultivaluedMap<String, String> params = new MetadataMap<String, String>(); + FormUtils.populateMapFromString(params, mockMessage, null, "UTF-8", false, mockRequest); + + assertEquals(0, params.size()); + } + + @Test + public void populateMapFromStringFromBody() { + mockObjects(null); + EasyMock.replay(mockMessage, mockRequest); + + MultivaluedMap<String, String> params = new MetadataMap<String, String>(); + String postBody = FORM_PARAM1 + "=" + FORM_PARAM_VALUE1 + "&" + FORM_PARAM2 + "=" + FORM_PARAM_VALUE2; + FormUtils.populateMapFromString(params, mockMessage, postBody, "UTF-8", false, mockRequest); + + assertEquals(2, params.size()); + assertEquals(FORM_PARAM_VALUE1, params.get(FORM_PARAM1).iterator().next()); + assertEquals(FORM_PARAM_VALUE2, params.get(FORM_PARAM2).iterator().next()); + } + + + private void mockObjects(String formPropertyValue) { + mockMessage = EasyMock.createMock(Message.class); + EasyMock.expect(mockMessage.getContextualProperty(FormUtils.FORM_PARAMS_FROM_HTTP_PARAMS)) + .andReturn(formPropertyValue).anyTimes(); + EasyMock.expect(mockMessage.getExchange()).andReturn(null).anyTimes(); + + mockRequest = EasyMock.createMock(HttpServletRequest.class); + String[] httpParamNames = {HTTP_PARAM1, HTTP_PARAM2}; + Enumeration<String> httpParamsEnum = Collections.enumeration(Arrays.asList(httpParamNames)); + EasyMock.expect(mockRequest.getParameterNames()).andReturn(httpParamsEnum).anyTimes(); + EasyMock.expect(mockRequest.getParameterValues(HTTP_PARAM1)).andReturn(new String[] {HTTP_PARAM_VALUE1}) + .anyTimes(); + EasyMock.expect(mockRequest.getParameterValues(HTTP_PARAM2)).andReturn(new String[] {HTTP_PARAM_VALUE2}) + .anyTimes(); + } +}
