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.commons.utils.CollectionUtils.*;
21  import static org.apache.juneau.commons.utils.StringUtils.*;
22  import static org.apache.juneau.http.HttpEntities.*;
23  import static org.apache.juneau.httppart.HttpPartSchema.*;
24  import static org.junit.jupiter.api.Assertions.*;
25  
26  import java.io.*;
27  
28  import org.apache.juneau.*;
29  import org.apache.juneau.httppart.*;
30  import org.apache.juneau.json.*;
31  import org.apache.juneau.msgpack.*;
32  import org.apache.juneau.oapi.*;
33  import org.apache.juneau.rest.annotation.*;
34  import org.apache.juneau.rest.client.*;
35  import org.apache.juneau.rest.httppart.*;
36  import org.apache.juneau.rest.mock.*;
37  import org.apache.juneau.rest.servlet.*;
38  import org.apache.juneau.testutils.pojos.*;
39  import org.junit.jupiter.api.*;
40  
41  class SerializedHttpEntity_Test extends TestBase {
42  
43  	@Rest
44  	public static class A extends BasicRestObject {
45  		@RestPost
46  		public String[] checkHeader(org.apache.juneau.rest.RestRequest req) {
47  			return req.getHeaders().getAll(req.getHeaderParam("Check").orElse(null)).stream().map(RequestHeader::getValue).toArray(String[]::new);
48  		}
49  		@RestPost
50  		public Reader checkBody(org.apache.juneau.rest.RestRequest req) throws IOException {
51  			return req.getReader();
52  		}
53  	}
54  
55  	@Test void a01_basic() throws Exception {
56  		serializedEntity(ABean.get(),JsonSerializer.DEFAULT).assertString().is("{\"a\":1,\"b\":\"foo\"}");
57  		serializedEntity(ABean::get,JsonSerializer.DEFAULT).assertString().is("{\"a\":1,\"b\":\"foo\"}");
58  		serializedEntity(ABean.get(),null).assertString().is("{a:1,b:'foo'}");
59  		serializedEntity(null,JsonSerializer.DEFAULT).assertString().is("null");
60  	}
61  
62  	@Test void a02_schema() throws Exception {
63  		serializedEntity(l("foo","bar"),OpenApiSerializer.DEFAULT).setSchema(T_ARRAY_PIPES).assertString().is("foo|bar");
64  	}
65  
66  	@Test void a03_serializer_streaming() throws Exception {
67  		serializedEntity(ABean.get(),MsgPackSerializer.DEFAULT).assertBytes().asSpacedHex().is("82 A1 61 01 A1 62 A3 66 6F 6F");
68  	}
69  
70  	@Test void a04_serializer_bad() {
71  		assertThrowsWithMessage(Exception.class, "Required value not provided.", ()->serializedEntity(null,OpenApiSerializer.DEFAULT).setSchema(schema().required().build()).asString());
72  	}
73  
74  	@Test void a05_writeTo() throws Exception {
75  		var baos = new ByteArrayOutputStream();
76  		serializedEntity("foo", null).writeTo(baos);
77  		assertEquals("foo", toUtf8(baos.toByteArray()));
78  	}
79  
80  	@Test void a06_isRepeatable() {
81  		assertTrue(serializedEntity(ABean.get(),null).isRepeatable());
82  	}
83  
84  	@Test void a07_getContentLength() {
85  		assertEquals(-1L, serializedEntity(ABean.get(),null).getContentLength());
86  	}
87  
88  	@Test void a08_getContent() {
89  		assertEquals("foo", toUtf8(serializedEntity("foo",null).getContent()));
90  	}
91  
92  	@Test void a09_chunked() throws Exception {
93  		checkHeaderClient("Transfer-Encoding").post("/",serializedEntity(ABean.get(),null).setChunked()).run().assertContent("['chunked']");
94  	}
95  
96  	@Test void a10_contentEncoding() throws Exception {
97  		checkHeaderClient("Content-Encoding").post("/",serializedEntity(ABean.get(),null).setContentEncoding("identity")).run().assertContent("['identity']");
98  	}
99  
100 	@Test void a12_contentType() throws Exception {
101 		checkHeaderClient("Content-Type").post("/",serializedEntity(reader("foo"),null).setContentType("text/foo")).run().assertContent("['text/foo']");
102 	}
103 
104 	//------------------------------------------------------------------------------------------------------------------
105 	// Utility methods
106 	//------------------------------------------------------------------------------------------------------------------
107 
108 	private static HttpPartSchema.Builder schema() {
109 		return HttpPartSchema.create();
110 	}
111 
112 	private static RestClient checkHeaderClient(String header) {
113 		return MockRestClient.create(A.class).rootUrl("http://localhost/checkHeader").json5().header("Check",header).ignoreErrors().build();
114 	}
115 }