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;
18  
19  import static org.apache.juneau.TestUtils.*;
20  import static org.apache.juneau.common.utils.StringUtils.*;
21  import static org.apache.juneau.http.HttpHeaders.*;
22  import static org.apache.juneau.http.HttpResources.*;
23  import static org.junit.jupiter.api.Assertions.*;
24  
25  import java.io.*;
26  import java.nio.file.*;
27  
28  import org.apache.juneau.*;
29  import org.apache.juneau.http.header.*;
30  import org.junit.jupiter.api.*;
31  
32  class BasicHttpResource_Test extends TestBase {
33  
34  	@Test void a01_basic() throws Exception {
35  		var f = Files.createTempFile("test","txt").toFile();
36  
37  		var x = stringResource((String)null);
38  		assertNull(x.getContentType());
39  		assertEquals("", toUtf8(x.getContent()));
40  		assertNull(x.getContentEncoding());
41  		assertEquals(0, x.getHeaders().size());
42  
43  		var x2 = stringResource("foo");
44  		assertEquals("foo", toUtf8(x2.getContent()));
45  		assertTrue(x2.isRepeatable());
46  		assertFalse(x2.isStreaming());
47  
48  		var x3 = readerResource(reader("foo"));
49  		assertEquals("foo", toUtf8(x3.getContent()));
50  		assertFalse(x3.isRepeatable());
51  		assertTrue(x3.isStreaming());
52  
53  		var x4 = byteArrayResource("foo".getBytes());
54  		assertEquals("foo", toUtf8(x4.getContent()));
55  		assertTrue(x4.isRepeatable());
56  		assertFalse(x4.isStreaming());
57  
58  		var x5 = streamResource(inputStream("foo"));
59  		assertEquals("foo", toUtf8(x5.getContent()));
60  		assertFalse(x5.isRepeatable());
61  		assertTrue(x5.isStreaming());
62  
63  		var x6 = fileResource(f);
64  		assertEquals("", toUtf8(x6.getContent()));
65  		assertTrue(x6.isRepeatable());
66  		assertFalse(x6.isStreaming());
67  
68  		var x7 = stringResource("foo").setCached();
69  		assertEquals("foo", toUtf8(x7.getContent()));
70  		assertEquals("foo", toUtf8(x7.getContent()));
71  		assertTrue(x7.isRepeatable());
72  
73  		var x8 = readerResource(reader("foo")).setCached();
74  		assertEquals("foo", toUtf8(x8.getContent()));
75  		assertEquals("foo", toUtf8(x8.getContent()));
76  		assertTrue(x8.isRepeatable());
77  
78  		var x9 = byteArrayResource("foo".getBytes()).setCached();
79  		assertEquals("foo", toUtf8(x9.getContent()));
80  		assertEquals("foo", toUtf8(x9.getContent()));
81  		assertTrue(x9.isRepeatable());
82  
83  		var x10 = streamResource(inputStream("foo")).setCached();
84  		assertEquals("foo", toUtf8(x10.getContent()));
85  		assertEquals("foo", toUtf8(x10.getContent()));
86  		assertTrue(x10.isRepeatable());
87  
88  		var x11 = stringResource((String)null).setCached();
89  		assertEquals("", toUtf8(x11.getContent()));
90  		assertTrue(x11.isRepeatable());
91  		x11.writeTo(new ByteArrayOutputStream());
92  
93  		var x12 = fileResource(f).setCached();
94  		assertEquals("", toUtf8(x12.getContent()));
95  		assertTrue(x12.isRepeatable());
96  		x12.writeTo(new ByteArrayOutputStream());
97  
98  		assertEquals(3L, stringResource("foo").getContentLength());
99  		assertEquals(3L, byteArrayResource("foo".getBytes()).getContentLength());
100 		assertEquals(0L, fileResource(f).getContentLength());
101 
102 		assertEquals(-1L, readerResource(reader("foo")).getContentLength());
103 		assertEquals(3L, readerResource(reader("foo")).setContentLength(3).getContentLength());
104 
105 		var x13 = stringResource("foo", contentType("text/plain")).setContentEncoding("identity");
106 		assertEquals("text/plain", x13.getContentType().getValue());
107 		assertEquals("identity", x13.getContentEncoding().getValue());
108 
109 		var x14 = stringResource("foo", null).setContentEncoding((String)null);
110 		assertNull(x14.getContentType());
111 		assertNull(x14.getContentEncoding());
112 	}
113 
114 	@Test void a02_header_String_Object() {
115 		var x = stringResource("foo").addHeader("Foo","bar").addHeader("Foo","baz").addHeader(null,"bar").addHeader("foo",null).getHeaders();
116 		assertEquals("Foo: bar", x.getFirst("Foo").get().toString());
117 		assertEquals("Foo: baz", x.getLast("Foo").get().toString());
118 		assertEmpty(x.getFirst("Bar"));
119 		assertEmpty(x.getLast("Bar"));
120 		assertList(x.getAll(), "Foo: bar", "Foo: baz");
121 	}
122 
123 	@Test void a03_header_Header() {
124 		var x = stringResource("foo").addHeaders(header("Foo","bar")).addHeaders(header("Foo","baz")).addHeaders(header("Bar",null)).getHeaders();
125 		assertEquals("Foo: bar", x.getFirst("Foo").get().toString());
126 		assertEquals("Foo: baz", x.getLast("Foo").get().toString());
127 		assertNull(x.getFirst("Bar").get().getValue());
128 		assertNull(x.getLast("Bar").get().getValue());
129 		assertList(x.getAll(), "Foo: bar", "Foo: baz", "Bar: null");
130 	}
131 
132 	@Test void a04_headers_List() {
133 		var x = stringResource("foo").addHeaders(header("Foo","bar"),header("Foo","baz"),header("Bar",null),null).getHeaders();
134 		assertEquals("Foo: bar", x.getFirst("Foo").get().toString());
135 		assertEquals("Foo: baz", x.getLast("Foo").get().toString());
136 		assertNull(x.getFirst("Bar").get().getValue());
137 		assertNull(x.getLast("Bar").get().getValue());
138 		assertList(x.getAll(), "Foo: bar", "Foo: baz", "Bar: null");
139 	}
140 
141 	@Test void a05_headers_array() {
142 		var x = stringResource("foo").addHeaders(header("Foo","bar"),header("Foo","baz"),header("Bar",null),null).getHeaders();
143 		assertEquals("Foo: bar", x.getFirst("Foo").get().toString());
144 		assertEquals("Foo: baz", x.getLast("Foo").get().toString());
145 		assertNull(x.getFirst("Bar").get().getValue());
146 		assertNull(x.getLast("Bar").get().getValue());
147 		assertList(x.getAll(), "Foo: bar", "Foo: baz", "Bar: null");
148 	}
149 
150 	@Test void a06_chunked() {
151 		var x1 = stringResource("foo").setChunked();
152 		assertTrue(x1.isChunked());
153 		var x2 = stringResource("foo");
154 		assertFalse(x2.isChunked());
155 	}
156 
157 	@Test void a07_chunked_boolean() {
158 		var x1 = stringResource("foo").setChunked(true);
159 		assertTrue(x1.isChunked());
160 		var x2 = stringResource("foo").setChunked(false);
161 		assertFalse(x2.isChunked());
162 	}
163 
164 	@Test void a08_contentType_String() {
165 		var x1 = stringResource("foo").setContentType("text/plain");
166 		assertEquals("text/plain", x1.getContentType().getValue());
167 		var x2 = stringResource("foo").setContentType((String)null);
168 		assertNull(x2.getContentType());
169 	}
170 
171 	@Test void a09_contentEncoding_String() {
172 		var x1 = stringResource("foo").setContentEncoding("identity");
173 		assertEquals("identity", x1.getContentEncoding().getValue());
174 		var x2 = stringResource("foo").setContentEncoding((String)null);
175 		assertNull(x2.getContentEncoding());
176 	}
177 
178 	//------------------------------------------------------------------------------------------------------------------
179 	// Utility methods
180 	//------------------------------------------------------------------------------------------------------------------
181 
182 	private BasicHeader header(String name, Object val) {
183 		return new BasicHeader(name, val);
184 	}
185 }