1 /*
2 * Copyright (c) 2011-2022, jcabi.com
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met: 1) Redistributions of source code must retain the above
8 * copyright notice, this list of conditions and the following
9 * disclaimer. 2) Redistributions in binary form must reproduce the above
10 * copyright notice, this list of conditions and the following
11 * disclaimer in the documentation and/or other materials provided
12 * with the distribution. 3) Neither the name of the jcabi.com nor
13 * the names of its contributors may be used to endorse or promote
14 * products derived from this software without specific prior written
15 * permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
19 * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
20 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
21 * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
22 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
28 * OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30 package com.jcabi.http.response;
31
32 import com.jcabi.http.Response;
33 import com.jcabi.http.request.FakeRequest;
34 import jakarta.json.stream.JsonParsingException;
35 import org.hamcrest.MatcherAssert;
36 import org.hamcrest.Matchers;
37 import org.junit.jupiter.api.Assertions;
38 import org.junit.jupiter.api.Test;
39 import org.junit.jupiter.api.function.Executable;
40
41 /**
42 * Test case for {@link JsonResponse}.
43 * @since 1.1
44 */
45 final class JsonResponseTest {
46
47 /**
48 * JsonResponse can read and return a JSON document.
49 * @throws Exception If something goes wrong inside
50 */
51 @Test
52 void readsJsonDocument() throws Exception {
53 final Response resp = new FakeRequest()
54 .withBody("{\n\t\r\"foo-foo\":2,\n\"bar\":\"\u20ac\"}")
55 .fetch();
56 final JsonResponse response = new JsonResponse(resp);
57 MatcherAssert.assertThat(
58 response.json().readObject().getInt("foo-foo"),
59 Matchers.equalTo(2)
60 );
61 MatcherAssert.assertThat(
62 response.json().readObject().getString("bar"),
63 Matchers.equalTo("\u20ac")
64 );
65 }
66
67 /**
68 * JsonResponse can read control characters.
69 *
70 * @throws Exception If something goes wrong inside
71 */
72 @Test
73 void readsControlCharacters() throws Exception {
74 final Response resp = new FakeRequest()
75 .withBody("{\"test\":\n\"\u001Fblah\uFFFDcwhoa\u0000!\"}").fetch();
76 final JsonResponse response = new JsonResponse(resp);
77 MatcherAssert.assertThat(
78 response.json().readObject().getString("test"),
79 Matchers.is("\u001Fblah\uFFFDcwhoa\u0000!")
80 );
81 }
82
83 /**
84 * JsonResponse logs the JSON body for JSON object parse errors.
85 *
86 * @throws Exception If something goes wrong inside
87 */
88 @Test
89 void logsForInvalidJsonObject() throws Exception {
90 final String body = "{\"test\": \"logged!\"$@%#^&%@$#}";
91 final Response resp = new FakeRequest().withBody(body).fetch();
92 MatcherAssert.assertThat(
93 Assertions.assertThrows(
94 JsonParsingException.class,
95 new Executable() {
96 @Override
97 public void execute() throws Throwable {
98 new JsonResponse(resp).json().readObject();
99 }
100 },
101 "readObject() should have thrown JsonParsingException"
102 ),
103 Matchers.hasToString(Matchers.containsString(body))
104 );
105 }
106
107 /**
108 * JsonResponse logs the JSON body for JSON array parse errors.
109 *
110 * @throws Exception If something goes wrong inside
111 */
112 @Test
113 void logsForInvalidJsonArray() throws Exception {
114 final String body = "[\"test\": \"logged!\"$@%#^&%@$#]";
115 final Response resp = new FakeRequest().withBody(body).fetch();
116 MatcherAssert.assertThat(
117 Assertions.assertThrows(
118 JsonParsingException.class,
119 new Executable() {
120 @Override
121 public void execute() throws Throwable {
122 new JsonResponse(resp).json().readArray();
123 }
124 },
125 "readArray() should have thrown JsonParsingException"
126 ),
127 Matchers.hasToString(
128 Matchers.containsString(
129 body
130 )
131 )
132 );
133 }
134
135 /**
136 * JsonResponse logs the JSON body for JSON read() parse errors.
137 *
138 * @throws Exception If something goes wrong inside
139 */
140 @Test
141 void logsForInvalidJson() throws Exception {
142 final String body = "{test:[]}}}";
143 final Response resp = new FakeRequest().withBody(body).fetch();
144 MatcherAssert.assertThat(
145 Assertions.assertThrows(
146 JsonParsingException.class,
147 new Executable() {
148 @Override
149 public void execute() throws Throwable {
150 new JsonResponse(resp).json().read();
151 }
152 },
153 "readStructure() should have thrown JsonParsingException"
154 ),
155 Matchers.<JsonParsingException>hasToString(
156 Matchers.containsString(
157 body
158 )
159 )
160 );
161 }
162
163 }