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;
40 import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
43 public class OnapHttpConnectionTest {
45 OnapHttpConnection con = null;
49 mockHttpRequest(null);
50 inp = new HttpInput();
53 Map<String, String> map1 = new HashMap<>();
54 map1.put("header1", "value1");
55 inp.setReqHeaders(map1);
56 Map<String, String> map2 = new HashMap<>();
57 map2.put("query1", "value1");
58 inp.setReqQueries(map2);
59 Map<String, String> map = new HashMap<>();
60 map.put("cookie1", "value1");
61 inp.setReqCookies(map);
62 inp.setUri("http://192.168.99.10:80");
65 @Test(expected = OnapCommandHttpFailure.class)
66 public void httpUnSecuredGetExceptionTest() throws OnapCommandHttpFailure {
67 new MockUp<CloseableHttpClient>() {
69 public CloseableHttpResponse execute(HttpUriRequest request, HttpContext context)
70 throws IOException, ClientProtocolException {
72 throw new IOException("IO Exception");
76 con = new OnapHttpConnection();
81 @Test(expected = OnapCommandHttpFailure.class)
82 public void httpUnSecuredPostExceptionTest() throws OnapCommandHttpFailure {
83 new MockUp<CloseableHttpClient>() {
85 public CloseableHttpResponse execute(HttpUriRequest request, HttpContext context)
86 throws IOException, ClientProtocolException {
88 throw new IOException("IO Exception");
92 inp.setMethod("post");
93 con = new OnapHttpConnection();
98 @Test(expected = OnapCommandHttpFailure.class)
99 public void httpUnSecuredPostExceptionTest1() throws OnapCommandHttpFailure {
100 new MockUp<CloseableHttpClient>() {
102 public CloseableHttpResponse execute(HttpUriRequest request, HttpContext context)
103 throws IOException, ClientProtocolException {
105 throw new IOException("IO Exception");
109 inp.setMethod("post");
110 inp.setBinaryData(true);
111 con = new OnapHttpConnection();
115 @Test(expected = OnapCommandHttpFailure.class)
116 public void httpUnSecuredPutExceptionTest() throws OnapCommandHttpFailure {
117 new MockUp<CloseableHttpClient>() {
119 public CloseableHttpResponse execute(HttpUriRequest request, HttpContext context)
120 throws IOException, ClientProtocolException {
122 throw new IOException("IO Exception");
125 inp.setMethod("put");
126 con = new OnapHttpConnection();
130 @Test(expected = OnapCommandHttpFailure.class)
131 public void httpUnSecuredDeleteExceptionTest() throws OnapCommandHttpFailure {
132 new MockUp<CloseableHttpClient>() {
134 public CloseableHttpResponse execute(HttpUriRequest request, HttpContext context)
135 throws IOException, ClientProtocolException {
137 throw new IOException("IO Exception");
140 inp.setMethod("delete");
141 con = new OnapHttpConnection();
145 @Test(expected = IllegalArgumentException.class)
146 public void httpUnSecuredOtherExceptionTest() throws OnapCommandHttpFailure {
147 new MockUp<CloseableHttpClient>() {
149 public CloseableHttpResponse execute(HttpUriRequest request, HttpContext context)
150 throws IOException, ClientProtocolException {
152 throw new IOException("IO Exception");
155 inp.setMethod("other");
156 con = new OnapHttpConnection();
160 @Test(expected = OnapCommandHttpFailure.class)
161 public void testGetMultipartEntityWithoutMultipartEntityName() throws OnapCommandHttpFailure {
162 new MockUp<CloseableHttpClient>() {
164 public CloseableHttpResponse execute(HttpUriRequest request, HttpContext context)
165 throws IOException, ClientProtocolException {
167 throw new IOException("IO Exception");
170 new MockUp<HttpInput>() {
173 public boolean isBinaryData() {
177 Map<String, String> reqHeaders = new HashMap<>();
178 reqHeaders.put("Content-Disposition","form-data");
179 reqHeaders.put("name","upload");
180 reqHeaders.put("filename","upload.txt");
181 reqHeaders.put("Content-Type","application/octet-stream");
182 reqHeaders.put("Content-Transfer-Encoding","binary");
183 inp.setReqHeaders(reqHeaders);
184 inp.setMethod("post");
185 con = new OnapHttpConnection();
189 @Test(expected = OnapCommandHttpFailure.class)
190 public void testGetMultipartEntityWithMultipartEntityName() throws OnapCommandHttpFailure {
191 new MockUp<CloseableHttpClient>() {
193 public CloseableHttpResponse execute(HttpUriRequest request, HttpContext context)
194 throws IOException, ClientProtocolException {
196 throw new IOException("IO Exception");
199 new MockUp<HttpInput>() {
202 public boolean isBinaryData() {
206 Map<String, String> reqHeaders = new HashMap<>();
207 reqHeaders.put("Content-Disposition","form-data");
208 reqHeaders.put("name","upload");
209 reqHeaders.put("filename","upload.txt");
210 reqHeaders.put("Content-Type","application/octet-stream");
211 reqHeaders.put("Content-Transfer-Encoding","binary");
212 inp.setReqHeaders(reqHeaders);
213 inp.setMethod("post");
214 inp.setMultipartEntityName("test");
215 con = new OnapHttpConnection();
220 public void httpUnSecuredCloseExceptionTest() throws IOException {
221 inp.setMethod("other");
222 con = new OnapHttpConnection();
223 assertDoesNotThrow(() -> con.close());
227 public void httpSecuredGetExceptionTest() {
229 // ProtocolVersion p = new ProtocolVersion("http",1,0);
230 // HttpResponse hr = DefaultHttpResponseFactory.INSTANCE.newHttpResponse(p, 200 , null) ;
232 new MockUp<CloseableHttpClient>() {
234 public CloseableHttpResponse execute(HttpUriRequest request, HttpContext context)
235 throws IOException, ClientProtocolException {
237 throw new IOException("IO Exception");
241 HttpInput inp = new HttpInput();
242 inp.setMethod("get");
244 inp.setReqHeaders(new HashMap<String, String>());
245 inp.setReqQueries(new HashMap<String, String>());
246 inp.setUri("https://192.168.99.10:80");
247 OnapHttpConnection con = new OnapHttpConnection();
249 } catch (OnapCommandHttpFailure e) {
250 assertEquals("0x3001::IO Exception", e.getMessage());
254 private static void mockHttpRequest(HttpResult result) {
255 new MockUp<OnapHttpConnection>() {
256 boolean isMock = false;
259 public HttpResult request(Invocation inv, HttpInput input) throws OnapCommandHttpFailure {
263 return inv.proceed(input);