2 * Copyright 2017 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.onap.cli.fw.http;
19 import static org.junit.Assert.assertEquals;
21 import java.io.IOException;
22 import java.util.HashMap;
25 import mockit.Invocation;
29 import org.apache.http.client.ClientProtocolException;
30 import org.apache.http.client.methods.CloseableHttpResponse;
31 import org.apache.http.client.methods.HttpUriRequest;
32 import org.apache.http.impl.client.CloseableHttpClient;
33 import org.apache.http.protocol.HttpContext;
34 import org.junit.Before;
35 import org.junit.Test;
36 import org.onap.cli.fw.http.connect.HttpInput;
37 import org.onap.cli.fw.http.connect.HttpResult;
38 import org.onap.cli.fw.http.connect.OnapHttpConnection;
39 import org.onap.cli.fw.http.error.OnapCommandHttpFailure;
42 public class OnapHttpConnectionTest {
44 OnapHttpConnection con = null;
48 mockHttpRequest(null);
49 inp = new HttpInput();
52 Map<String, String> map1 = new HashMap<>();
53 map1.put("header1", "value1");
54 inp.setReqHeaders(map1);
55 Map<String, String> map2 = new HashMap<>();
56 map2.put("query1", "value1");
57 inp.setReqQueries(map2);
58 Map<String, String> map = new HashMap<>();
59 map.put("cookie1", "value1");
60 inp.setReqCookies(map);
61 inp.setUri("http://192.168.99.10:80");
64 @Test(expected = OnapCommandHttpFailure.class)
65 public void httpUnSecuredGetExceptionTest() throws OnapCommandHttpFailure {
66 new MockUp<CloseableHttpClient>() {
68 public CloseableHttpResponse execute(HttpUriRequest request, HttpContext context)
69 throws IOException, ClientProtocolException {
71 throw new IOException("IO Exception");
75 con = new OnapHttpConnection();
80 @Test(expected = OnapCommandHttpFailure.class)
81 public void httpUnSecuredPostExceptionTest() throws OnapCommandHttpFailure {
82 new MockUp<CloseableHttpClient>() {
84 public CloseableHttpResponse execute(HttpUriRequest request, HttpContext context)
85 throws IOException, ClientProtocolException {
87 throw new IOException("IO Exception");
91 inp.setMethod("post");
92 con = new OnapHttpConnection();
97 @Test(expected = OnapCommandHttpFailure.class)
98 public void httpUnSecuredPostExceptionTest1() throws OnapCommandHttpFailure {
99 new MockUp<CloseableHttpClient>() {
101 public CloseableHttpResponse execute(HttpUriRequest request, HttpContext context)
102 throws IOException, ClientProtocolException {
104 throw new IOException("IO Exception");
108 inp.setMethod("post");
109 inp.setBinaryData(true);
110 con = new OnapHttpConnection();
114 @Test(expected = OnapCommandHttpFailure.class)
115 public void httpUnSecuredPutExceptionTest() throws OnapCommandHttpFailure {
116 new MockUp<CloseableHttpClient>() {
118 public CloseableHttpResponse execute(HttpUriRequest request, HttpContext context)
119 throws IOException, ClientProtocolException {
121 throw new IOException("IO Exception");
124 inp.setMethod("put");
125 con = new OnapHttpConnection();
129 @Test(expected = OnapCommandHttpFailure.class)
130 public void httpUnSecuredDeleteExceptionTest() throws OnapCommandHttpFailure {
131 new MockUp<CloseableHttpClient>() {
133 public CloseableHttpResponse execute(HttpUriRequest request, HttpContext context)
134 throws IOException, ClientProtocolException {
136 throw new IOException("IO Exception");
139 inp.setMethod("delete");
140 con = new OnapHttpConnection();
144 @Test(expected = IllegalArgumentException.class)
145 public void httpUnSecuredOtherExceptionTest() throws OnapCommandHttpFailure {
146 new MockUp<CloseableHttpClient>() {
148 public CloseableHttpResponse execute(HttpUriRequest request, HttpContext context)
149 throws IOException, ClientProtocolException {
151 throw new IOException("IO Exception");
154 inp.setMethod("other");
155 con = new OnapHttpConnection();
159 @Test(expected = OnapCommandHttpFailure.class)
160 public void testGetMultipartEntityWithoutMultipartEntityName() throws OnapCommandHttpFailure {
161 new MockUp<CloseableHttpClient>() {
163 public CloseableHttpResponse execute(HttpUriRequest request, HttpContext context)
164 throws IOException, ClientProtocolException {
166 throw new IOException("IO Exception");
169 new MockUp<HttpInput>() {
172 public boolean isBinaryData() {
176 Map<String, String> reqHeaders = new HashMap<>();
177 reqHeaders.put("Content-Disposition","form-data");
178 reqHeaders.put("name","upload");
179 reqHeaders.put("filename","upload.txt");
180 reqHeaders.put("Content-Type","application/octet-stream");
181 reqHeaders.put("Content-Transfer-Encoding","binary");
182 inp.setReqHeaders(reqHeaders);
183 inp.setMethod("post");
184 con = new OnapHttpConnection();
188 @Test(expected = OnapCommandHttpFailure.class)
189 public void testGetMultipartEntityWithMultipartEntityName() throws OnapCommandHttpFailure {
190 new MockUp<CloseableHttpClient>() {
192 public CloseableHttpResponse execute(HttpUriRequest request, HttpContext context)
193 throws IOException, ClientProtocolException {
195 throw new IOException("IO Exception");
198 new MockUp<HttpInput>() {
201 public boolean isBinaryData() {
205 Map<String, String> reqHeaders = new HashMap<>();
206 reqHeaders.put("Content-Disposition","form-data");
207 reqHeaders.put("name","upload");
208 reqHeaders.put("filename","upload.txt");
209 reqHeaders.put("Content-Type","application/octet-stream");
210 reqHeaders.put("Content-Transfer-Encoding","binary");
211 inp.setReqHeaders(reqHeaders);
212 inp.setMethod("post");
213 inp.setMultipartEntityName("test");
214 con = new OnapHttpConnection();
219 public void httpUnSecuredCloseExceptionTest() throws OnapCommandHttpFailure {
220 inp.setMethod("other");
221 con = new OnapHttpConnection();
226 public void httpSecuredGetExceptionTest() {
228 // ProtocolVersion p = new ProtocolVersion("http",1,0);
229 // HttpResponse hr = DefaultHttpResponseFactory.INSTANCE.newHttpResponse(p, 200 , null) ;
231 new MockUp<CloseableHttpClient>() {
233 public CloseableHttpResponse execute(HttpUriRequest request, HttpContext context)
234 throws IOException, ClientProtocolException {
236 throw new IOException("IO Exception");
240 HttpInput inp = new HttpInput();
241 inp.setMethod("get");
243 inp.setReqHeaders(new HashMap<String, String>());
244 inp.setReqQueries(new HashMap<String, String>());
245 inp.setUri("https://192.168.99.10:80");
246 OnapHttpConnection con = new OnapHttpConnection();
248 } catch (OnapCommandHttpFailure e) {
249 assertEquals("0x3001::IO Exception", e.getMessage());
253 private static void mockHttpRequest(HttpResult result) {
254 new MockUp<OnapHttpConnection>() {
255 boolean isMock = false;
258 public HttpResult request(Invocation inv, HttpInput input) throws OnapCommandHttpFailure {
262 return inv.proceed(input);