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