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