1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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
43
44
45 final class JsonResponseTest {
46
47
48
49
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
69
70
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
85
86
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
109
110
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
137
138
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 }