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.resource;
18  
19  import static org.apache.juneau.commons.utils.StringUtils.*;
20  import static org.apache.juneau.http.HttpHeaders.*;
21  import static org.junit.jupiter.api.Assertions.*;
22  
23  import org.apache.http.*;
24  import org.apache.juneau.*;
25  import org.apache.juneau.http.header.*;
26  import org.junit.jupiter.api.*;
27  
28  class StringResource_Test extends TestBase {
29  
30  	@Test void a01_basic() throws Exception {
31  		var x = new StringResource();
32  		assertEquals("", toUtf8(x.getContent()));
33  		assertTrue(x.isRepeatable());
34  		assertFalse(x.isStreaming());
35  
36  		var x2 = new StringResource(contentType("text/plain"), "foo");
37  		assertEquals("foo", toUtf8(x2.getContent()));
38  		assertEquals("text/plain", x2.getContentType().getValue());
39  		assertTrue(x2.isRepeatable());
40  		assertFalse(x2.isStreaming());
41  
42  		var x3 = new StringResource(null, null);
43  		assertEquals("", toUtf8(x3.getContent()));
44  	}
45  
46  	@Test void a02_caching() throws Exception {
47  		var x = new StringResource(null, "foo").setCached();
48  		assertEquals("foo", toUtf8(x.getContent()));
49  		assertEquals("foo", toUtf8(x.getContent()));
50  		assertTrue(x.isRepeatable());
51  	}
52  
53  	@Test void a03_headers() {
54  		var x = new StringResource(null, "foo")
55  			.addHeader("Foo", "bar")
56  			.addHeader("Foo", "baz")
57  			.setHeader("Qux", "quux");
58  
59  		HeaderList headers = x.getHeaders();
60  		assertEquals("Foo: bar", headers.getFirst("Foo").get().toString());
61  		assertEquals("Foo: baz", headers.getLast("Foo").get().toString());
62  		assertEquals("Qux: quux", headers.getFirst("Qux").get().toString());
63  	}
64  
65  	@Test void a04_contentType() {
66  		var x = new StringResource(null, "foo")
67  			.setContentType("text/plain");
68  		assertEquals("text/plain", x.getContentType().getValue());
69  
70  		var x2 = new StringResource(null, "foo")
71  			.setContentType(contentType("text/html"));
72  		assertEquals("text/html", x2.getContentType().getValue());
73  	}
74  
75  	@Test void a05_contentEncoding() {
76  		var x = new StringResource(null, "foo")
77  			.setContentEncoding("identity");
78  		assertEquals("identity", x.getContentEncoding().getValue());
79  
80  		var x2 = new StringResource(null, "foo")
81  			.setContentEncoding(contentEncoding("gzip"));
82  		assertEquals("gzip", x2.getContentEncoding().getValue());
83  	}
84  
85  	@Test void a06_chunked() {
86  		var x = new StringResource(null, "foo")
87  			.setChunked();
88  		assertTrue(x.isChunked());
89  
90  		var x2 = new StringResource(null, "foo")
91  			.setChunked(true);
92  		assertTrue(x2.isChunked());
93  
94  		var x3 = new StringResource(null, "foo")
95  			.setChunked(false);
96  		assertFalse(x3.isChunked());
97  	}
98  
99  	@Test void a07_fluentSetters() {
100 		var x = new StringResource(null, "foo");
101 
102 		// Test setHeader returns correct type
103 		assertSame(x, x.setHeader("X-Test", "value"));
104 		assertEquals("value", x.getHeaders().getFirst("X-Test").get().getValue());
105 
106 		// Test addHeader returns correct type
107 		assertSame(x, x.addHeader("X-Test2", "value2"));
108 		assertEquals("value2", x.getHeaders().getFirst("X-Test2").get().getValue());
109 
110 		// Test setHeaders returns correct type
111 		assertSame(x, x.setHeaders(header("X-Test3", "value3")));
112 		assertEquals("value3", x.getHeaders().getFirst("X-Test3").get().getValue());
113 
114 		// Test addHeaders returns correct type
115 		assertSame(x, x.addHeaders(header("X-Test4", "value4")));
116 		assertEquals("value4", x.getHeaders().getFirst("X-Test4").get().getValue());
117 	}
118 
119 	@Test void a08_copy() throws Exception {
120 		var x = new StringResource(contentType("text/plain"), "foo")
121 			.setHeader("Foo", "bar");
122 
123 		StringResource x2 = x.copy();
124 		assertNotSame(x, x2);
125 		assertEquals("foo", toUtf8(x2.getContent()));
126 		assertEquals("text/plain", x2.getContentType().getValue());
127 		assertEquals("bar", x2.getHeaders().getFirst("Foo").get().getValue());
128 	}
129 
130 	@Test void a09_contentLength() {
131 		var x = new StringResource(null, "foo");
132 		assertEquals(3L, x.getContentLength());
133 
134 		// Content length can be overridden for StringResource
135 		var x2 = new StringResource(null, "foo").setContentLength(10);
136 		assertEquals(10L, x2.getContentLength());
137 	}
138 
139 	//------------------------------------------------------------------------------------------------------------------
140 	// Utility methods
141 	//------------------------------------------------------------------------------------------------------------------
142 
143 	private static Header header(String name, Object val) {
144 		return BasicHeader.of(name, val);
145 	}
146 }