View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.juneau.http.response;
18  
19  import static org.apache.juneau.http.HttpResponses.*;
20  import static org.junit.jupiter.api.Assertions.*;
21  
22  import java.lang.reflect.*;
23  
24  import org.apache.juneau.*;
25  import org.apache.juneau.rest.annotation.*;
26  import org.apache.juneau.rest.mock.*;
27  import org.junit.jupiter.api.*;
28  
29  class HttpException_Test extends TestBase {
30  
31  	@Rest
32  	public static class A {
33  		@RestGet
34  		public void f1() throws BasicHttpException {
35  			throw new BasicHttpException(225, new RuntimeException("foo"), "bar {0}", "baz");
36  		}
37  		@RestGet
38  		public void f2() throws BasicHttpException {
39  			throw new BasicHttpException(225, "foo");
40  		}
41  		@RestGet
42  		public void f3() throws BasicHttpException {
43  			throw new BasicHttpException(225, new RuntimeException("baz"));
44  		}
45  		@RestGet
46  		public void f4() throws BasicHttpException {
47  			throw new BasicHttpException(225, "bar {0}", "baz");
48  		}
49  		@RestGet
50  		public void f5() throws BasicHttpException {
51  			throw httpException().setStatusCode2(225).setHeader2("Foo", "bar");
52  		}
53  	}
54  
55  	@Test void a01_basic() throws Exception {
56  		var c = MockRestClient.create(A.class).ignoreErrors().build();
57  
58  		c.get("/f1").run()
59  			.assertStatus().asCode().is(225)
60  			.assertContent("bar baz");
61  		c.get("/f2").run()
62  			.assertStatus().asCode().is(225)
63  			.assertContent("foo");
64  		c.get("/f3").run()
65  			.assertStatus().asCode().is(225)
66  			.assertContent("java.lang.RuntimeException: baz");
67  		c.get("/f4").run()
68  			.assertStatus().asCode().is(225)
69  			.assertContent("bar baz");
70  		c.get("/f5").run()
71  			.assertStatus().asCode().is(225)
72  			.assertContent("")
73  			.assertHeader("Foo").is("bar");
74  	}
75  
76  	@Test void a02_getRootCause() {
77  		var x = new BasicHttpException(100, null);
78  		assertNull(x.getRootCause());
79  
80  		x = new BasicHttpException(100, new BasicHttpException(100,"foo"));
81  		assertNull(x.getRootCause());
82  
83  		x = new BasicHttpException(100, new RuntimeException("foo"));
84  		assertInstanceOf(RuntimeException.class, x.getRootCause());
85  
86  		x = new BasicHttpException(100, new BasicHttpException(100, new RuntimeException("foo")));
87  		assertInstanceOf(RuntimeException.class, x.getRootCause());
88  
89  		x = new BasicHttpException(100, new InvocationTargetException(new RuntimeException("foo")));
90  		assertInstanceOf(RuntimeException.class, x.getRootCause());
91  	}
92  
93  	@Test void a03_getFullStackMessage() {
94  		var x = new BasicHttpException(100, null);
95  		assertEquals("Continue", x.getFullStackMessage(false));
96  		assertEquals("Continue", x.getFullStackMessage(true));
97  
98  		x = new BasicHttpException(100, "foo<bar>&baz");
99  		assertEquals("foo<bar>&baz", x.getFullStackMessage(false));
100 		assertEquals("foo bar  baz", x.getFullStackMessage(true));
101 
102 		x = new BasicHttpException(100, new RuntimeException("foo<bar>&qux"), "foo{0}","<bar>&baz");
103 		assertEquals("foo<bar>&baz\nCaused by (RuntimeException): foo<bar>&qux", x.getFullStackMessage(false));
104 		assertEquals("foo bar  baz\nCaused by (RuntimeException): foo bar  qux", x.getFullStackMessage(true));
105 
106 		x = new BasicHttpException(100, new RuntimeException(), "foo{0}","<bar>&baz");
107 		assertEquals("foo<bar>&baz\nCaused by (RuntimeException)", x.getFullStackMessage(false));
108 		assertEquals("foo bar  baz\nCaused by (RuntimeException)", x.getFullStackMessage(true));
109 	}
110 }