forked from kamax-matrix/matrix-java-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMatrixHttpContentTest.java
215 lines (174 loc) · 8.51 KB
/
MatrixHttpContentTest.java
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
/*
* matrix-java-sdk - Matrix Client SDK for Java
* Copyright (C) 2017 Arne Augenstein
*
* https://max.kamax.io/
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package io.kamax.matrix.client;
import org.junit.Test;
import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Optional;
/*
* TODO As the spec is outdated, I'm not sure, if the error 403 can really happen in this case. Has to be re-checked,
* when the spec is up to date, again.
*/
public class MatrixHttpContentTest extends MatrixHttpTest {
private URI address = new URI("mxc://localhost/testAddress.txt");
public MatrixHttpContentTest() throws URISyntaxException {
}
@Test
public void isValid() throws URISyntaxException {
isValidSuccessful(true, 200);
}
@Test
public void isValidMissingContentType() throws URISyntaxException {
String url = createDownloadUrl();
String bodyFile = "textfile.txt";
TestResponseBuilder responseBuilder = new TestResponseBuilder(200).setBodyFile(bodyFile);
new TestRunnerGet<Boolean>(new TestRequestBuilder(url), responseBuilder).runTest(createContentObject()::isValid,
false);
}
@Test
public void isValidError404() throws URISyntaxException {
isValidSuccessful(false, 404);
}
@Test
public void isValidError403() throws URISyntaxException {
isValidSuccessful(false, 403);
}
private void isValidSuccessful(boolean expectedResult, int responseStatus) throws URISyntaxException {
String url = createDownloadUrl();
String bodyFile = "textfile.txt";
TestResponseBuilder responseBuilder = new TestResponseBuilder(responseStatus).setBodyFile(bodyFile)//
.setContentType("text/plain");
new TestRunnerGet<Boolean>(new TestRequestBuilder(url), responseBuilder).runTest(createContentObject()::isValid,
expectedResult);
}
@Test
public void getType() throws URISyntaxException, IOException {
getTypeSuccessful(200, "text/plain");
}
@Test
public void getTypeMissingContentType() throws URISyntaxException {
String url = createDownloadUrl();
String bodyFile = "textfile.txt";
TestResponseBuilder responseBuilder = new TestResponseBuilder(200).setBodyFile(bodyFile);
new TestRunnerGet<String>(new TestRequestBuilder(url), responseBuilder).runTest(createContentObject()::getType,
null);
}
@Test
public void getTypeError404() throws URISyntaxException, IOException {
getTypeSuccessful(404, null);
}
@Test
public void getTypeError403() throws URISyntaxException, IOException {
getTypeSuccessful(403, null);
}
private void getTypeSuccessful(int responseStatus, String contentType) throws URISyntaxException {
String url = createDownloadUrl();
String bodyFile = "textfile.txt";
TestResponseBuilder responseBuilder = new TestResponseBuilder(responseStatus).setBodyFile(bodyFile)
.setContentType("text/plain");//
new TestRunnerGet<String>(new TestRequestBuilder(url), responseBuilder).runTest(createContentObject()::getType,
contentType);
}
@Test
public void getData() throws URISyntaxException, IOException {
byte[] expectedResult = Files.readAllBytes(Paths.get(ClassLoader
.getSystemResource("wiremock" + File.separator + "__files" + File.separator + "textfile.txt").toURI()));
getDataSuccessful(200, expectedResult, "textfile.txt");
}
@Test
public void getDataMissingContentType() throws URISyntaxException {
String url = createDownloadUrl();
String bodyFile = "textfile.txt";
TestResponseBuilder responseBuilder = new TestResponseBuilder(200).setBodyFile(bodyFile);
new TestRunnerGet<byte[]>(new TestRequestBuilder(url), responseBuilder).runTest(createContentObject()::getData,
null);
}
@Test
public void getDataError404() throws URISyntaxException, IOException {
getDataSuccessful(404, null, "textfile.txt");
}
@Test
public void getDataError403() throws URISyntaxException, IOException {
getDataSuccessful(403, null, "textfile.txt");
}
private void getDataSuccessful(int responseStatus, byte[] expectedResult, String bodyFile)
throws URISyntaxException {
String url = createDownloadUrl();
TestResponseBuilder responseBuilder = new TestResponseBuilder(responseStatus).setBodyFile(bodyFile)//
.setContentType("text/plain");
new TestRunnerGet<byte[]>(new TestRequestBuilder(url), responseBuilder).runTest(createContentObject()::getData,
expectedResult);
}
@Test
public void getFilename() throws URISyntaxException, IOException {
String url = createDownloadUrl();
String bodyFile = "textfile.txt";
TestResponseBuilder responseBuilder = new TestResponseBuilder(200).setBodyFile(bodyFile)//
.setContentType("text/plain")//
.putHeader("Content-Disposition", String.format("filename=" + bodyFile + ";"));
new TestRunnerGet<Optional<String>>(new TestRequestBuilder(url), responseBuilder)
.runTest(createContentObject()::getFilename, Optional.of(bodyFile));
responseBuilder.putHeader("Content-Disposition", String.format("filename=\"%s\";", bodyFile));
new TestRunnerGet<Optional<String>>(new TestRequestBuilder(url), responseBuilder)
.runTest(createContentObject()::getFilename, Optional.of(bodyFile));
responseBuilder.putHeader("Content-Disposition", String.format("filename=\"%s\"", bodyFile));
new TestRunnerGet<Optional<String>>(new TestRequestBuilder(url), responseBuilder)
.runTest(createContentObject()::getFilename, Optional.of(bodyFile));
responseBuilder.putHeader("Content-Disposition", String.format("filename=%s", bodyFile));
new TestRunnerGet<Optional<String>>(new TestRequestBuilder(url), responseBuilder)
.runTest(createContentObject()::getFilename, Optional.of(bodyFile));
}
@Test
public void getFilenameMissingContentType() throws URISyntaxException {
String url = createDownloadUrl();
String bodyFile = "textfile.txt";
TestResponseBuilder responseBuilder = new TestResponseBuilder(200).setBodyFile(bodyFile);
new TestRunnerGet<Optional<String>>(new TestRequestBuilder(url), responseBuilder)
.runTest(createContentObject()::getFilename, Optional.empty());
}
@Test
public void getFilenameError404() throws URISyntaxException, IOException {
getFilenameSuccessful(404, "textfile.txt", Optional.empty());
}
@Test
public void getFilenameError403() throws URISyntaxException, IOException {
getFilenameSuccessful(404, "textfile.txt", Optional.empty());
}
private void getFilenameSuccessful(int responseStatus, String bodyFile, Optional<String> expectedResult)
throws URISyntaxException {
String url = createDownloadUrl();
TestResponseBuilder responseBuilder = new TestResponseBuilder(responseStatus).setBodyFile(bodyFile)//
.setContentType("text/plain")//
.putHeader("Content-Disposition", String.format("filename=%s;", bodyFile));
new TestRunnerGet<Optional<String>>(new TestRequestBuilder(url), responseBuilder)
.runTest(createContentObject()::getFilename, expectedResult);
}
private MatrixHttpContent createContentObject() throws URISyntaxException {
MatrixClientContext context = createClientContext();
return new MatrixHttpContent(context, address);
}
private String createDownloadUrl() {
return "/_matrix/media/v1/download/" + address.getHost() + address.getPath() + getAcessTokenParameter();
}
}