2 * Copyright (c) 2016, Huawei Technologies Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package org.openo.baseservice.roa.util.clientsdk;
19 import java.io.UnsupportedEncodingException;
20 import java.net.URLEncoder;
21 import java.util.Arrays;
22 import java.util.Date;
24 import org.junit.After;
25 import org.junit.AfterClass;
26 import org.junit.Assert;
27 import org.junit.Before;
28 import org.junit.BeforeClass;
29 import org.junit.Ignore;
30 import org.junit.Test;
33 import mockit.NonStrictExpectations;
41 * @version SDNO 0.5 13-Jun-2016
43 public class TestHttpUtil {
48 * @throws java.lang.Exception
52 public static void setUpBeforeClass() throws Exception {
58 * @throws java.lang.Exception
62 public static void tearDownAfterClass() throws Exception {
68 * @throws java.lang.Exception
72 public void setUp() throws Exception {
78 * @throws java.lang.Exception
82 public void tearDown() throws Exception {
91 public void testContainsIgnoreCase() {
92 final String[] array = {"hello", "how", "are", "you", "?"};
93 final String toFind = "Hello";
94 Assert.assertTrue(HttpUtil.containsIgnoreCase(array, toFind));
103 public void testContainsIgnoreCaseNull() {
104 final String[] array = {"hello", "how", "are", "you", "?"};
105 final String toFind = "Hello";
106 Assert.assertFalse(HttpUtil.containsIgnoreCase(array, null));
109 Assert.assertFalse(HttpUtil.containsIgnoreCase(array, toFind));
111 Assert.assertTrue(HttpUtil.containsIgnoreCase(array, null));
113 array[array.length - 1] = null;
114 Assert.assertTrue(HttpUtil.containsIgnoreCase(array, null));
123 public void testJoin() {
124 final String[] array = {"hello", "how", "are", "you", "?"};
125 String actual = HttpUtil.join(array, ",");
126 String expected = "hello,how,are,you,?";
127 Assert.assertEquals(actual, expected);
129 actual = HttpUtil.join(array, "#");
130 expected = expected.replaceAll(",", "#");
131 Assert.assertEquals(actual, expected);
132 actual = HttpUtil.join(new String[] {}, ",");
133 Assert.assertEquals(actual, "");
142 public void testParameterToString() {
143 // with param string.
144 Object param = new String("String Param");
145 String actual = HttpUtil.parameterToString(param);
146 String expected = "String Param";
147 Assert.assertEquals(expected, actual);
150 final Date date = new Date();
152 expected = "" + date.getTime();
153 actual = HttpUtil.parameterToString(param);
154 Assert.assertEquals(expected, actual);
156 // with param collection.
157 final String[] array = {"hello", "how", "are", "you", "?"};
158 param = Arrays.asList(array);
159 expected = HttpUtil.join(array, ",");
160 actual = HttpUtil.parameterToString(param);
161 Assert.assertEquals(expected, actual);
164 param = new Object() {
167 public String toString() {
168 return "test object";
171 expected = "test object";
172 actual = HttpUtil.parameterToString(param);
173 Assert.assertEquals(expected, actual);
177 actual = HttpUtil.parameterToString(null);
178 Assert.assertEquals(expected, actual);
188 public void testSelectHeaderAccept() {
189 final String[] accepts = {"application/json", "text/plain", "application/xml"};
190 String expected = "application/json";
191 String actual = HttpUtil.selectHeaderAccept(accepts);
192 Assert.assertEquals(expected, actual);
194 accepts[0] = "application/x-www-form-urlencoded";
195 expected = HttpUtil.join(accepts, ",");
196 actual = HttpUtil.selectHeaderAccept(accepts);
197 Assert.assertEquals(expected, actual);
200 actual = HttpUtil.selectHeaderAccept(new String[] {});
201 Assert.assertEquals(expected, actual);
211 public void testSelectHeaderContentType() {
212 final String[] accepts = {"application/json", "text/plain", "application/xml"};
213 String expected = "application/json";
214 String actual = HttpUtil.selectHeaderContentType(accepts);
215 Assert.assertEquals(expected, actual);
217 accepts[0] = "application/x-www-form-urlencoded";
218 expected = "application/x-www-form-urlencoded";
219 actual = HttpUtil.selectHeaderContentType(accepts);
220 Assert.assertEquals(expected, actual);
222 expected = "application/json";
223 actual = HttpUtil.selectHeaderContentType(new String[] {});
224 Assert.assertEquals(expected, actual);
234 public void testEscapeString() throws Exception {
235 final String str = "/this/url/to be encoded";
236 final String actual = HttpUtil.escapeString(str);
237 final String expected = "%2Fthis%2Furl%2Fto%20be%20encoded";
238 Assert.assertEquals(expected, actual);
249 public void testEscapeStringException() throws Exception {
251 final String str = "/this/url/to be encoded";
252 new NonStrictExpectations() {
258 URLEncoder.encode(str, "utf8");
259 result = new UnsupportedEncodingException();
263 final String actual = HttpUtil.escapeString(str);
264 Assert.assertEquals(str, actual);