1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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 }