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.apache.juneau.http.response.LengthRequired.*;
21  
22  import org.apache.juneau.*;
23  import org.apache.juneau.rest.annotation.*;
24  import org.apache.juneau.rest.mock.*;
25  import org.junit.jupiter.api.*;
26  
27  class LengthRequired_Test extends TestBase {
28  
29  	@Rest
30  	public static class A {
31  		@RestGet
32  		public void f1() throws LengthRequired {
33  			throw new LengthRequired();
34  		}
35  		@RestGet
36  		public void f2() throws LengthRequired {
37  			throw new LengthRequired("foo {0}", "bar");
38  		}
39  		@RestGet
40  		public void f3() throws LengthRequired {
41  			throw new LengthRequired(new RuntimeException("baz"));
42  		}
43  		@RestGet
44  		public void f4() throws LengthRequired {
45  			throw new LengthRequired(new RuntimeException("baz"), "foo {0}", "bar");
46  		}
47  		@RestGet
48  		public void f5() throws LengthRequired {
49  			throw lengthRequired().setHeader2("Foo", "bar");
50  		}
51  		@RestGet
52  		public void f6() throws LengthRequired {
53  			throw new LengthRequired("foo");
54  		}
55  	}
56  
57  	@Test void a01_basic() throws Exception {
58  		var c = MockRestClient.create(A.class).ignoreErrors().noTrace().build();
59  
60  		c.get("/f1").run()
61  			.assertStatus().asCode().is(STATUS_CODE)
62  			.assertContent(REASON_PHRASE);
63  		c.get("/f2").run()
64  			.assertStatus().asCode().is(STATUS_CODE)
65  			.assertContent("foo bar");
66  		c.get("/f3").run()
67  			.assertStatus().asCode().is(STATUS_CODE)
68  			.assertContent("baz");
69  		c.get("/f4").run()
70  			.assertStatus().asCode().is(STATUS_CODE)
71  			.assertContent("foo bar");
72  		c.get("/f5").run()
73  			.assertStatus().asCode().is(STATUS_CODE)
74  			.assertContent(REASON_PHRASE)
75  			.assertHeader("Foo").is("bar");
76  		c.get("/f6").run()
77  			.assertStatus().asCode().is(STATUS_CODE)
78  			.assertContent("foo");
79  	}
80  }