forked from kamax-matrix/matrix-java-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMatrixHttpContentTest.java
184 lines (152 loc) · 7.86 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
/*
* 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.hamcrest.core.IsEqual;
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;
import static com.github.tomakehurst.wiremock.client.WireMock.*;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.*;
/*
* TODO As the spec is outdated, I'm not sure if the error 403 can really happen in these test cases. This class has
* to be checked for correctness, when matrix's spec is updated.
*/
public class MatrixHttpContentTest extends MatrixHttpTest {
private String bodyFilename = "textfile.txt";
private URI address = new URI("mxc://localhost/testpath/" + bodyFilename);
private String downloadUrl = "/_matrix/media/v1/download/" + address.getHost() + address.getPath() + tokenParameter;
public MatrixHttpContentTest() throws URISyntaxException {
}
@Test
public void isValid() throws URISyntaxException {
stubFor(get(urlEqualTo(downloadUrl)).willReturn(
aResponse().withStatus(200).withBodyFile(bodyFilename).withHeader("Content-Type", "text/plain")));
assertTrue(createContentObject().isValid());
}
@Test
public void isValidMissingContentType() throws URISyntaxException {
stubFor(get(urlEqualTo(downloadUrl)).willReturn(aResponse().withStatus(200).withBodyFile(bodyFilename)));
assertFalse(createContentObject().isValid());
}
@Test
public void isValidError404() throws URISyntaxException {
stubFor(get(urlEqualTo(downloadUrl)).willReturn(aResponse().withStatus(404).withBody(error404Response)));
assertFalse(createContentObject().isValid());
}
@Test
public void isValidError403() throws URISyntaxException {
stubFor(get(urlEqualTo(downloadUrl)).willReturn(aResponse().withStatus(403).withBody(error403Response)));
assertFalse(createContentObject().isValid());
}
@Test
public void getType() throws URISyntaxException, IOException {
stubFor(get(urlEqualTo(downloadUrl)).willReturn(
aResponse().withStatus(200).withBodyFile(bodyFilename).withHeader("Content-Type", "text/plain")));
assertEquals("text/plain", createContentObject().getType());
}
@Test
public void getTypeMissingContentType() throws URISyntaxException {
stubFor(get(urlEqualTo(downloadUrl)).willReturn(aResponse().withStatus(200).withBodyFile(bodyFilename)));
assertNull(createContentObject().getType());
}
@Test
public void getTypeError404() throws URISyntaxException, IOException {
stubFor(get(urlEqualTo(downloadUrl))
.willReturn(aResponse().withStatus(404).withStatus(404).withBody(error404Response)));
assertNull(createContentObject().getType());
}
@Test
public void getTypeError403() throws URISyntaxException, IOException {
stubFor(get(urlEqualTo(downloadUrl))
.willReturn(aResponse().withStatus(403).withStatus(403).withBody(error403Response)));
assertNull(createContentObject().getType());
}
@Test
public void getData() throws URISyntaxException, IOException {
stubFor(get(urlEqualTo(downloadUrl)).willReturn(
aResponse().withStatus(200).withBodyFile(bodyFilename).withHeader("Content-Type", "text/plain")));
byte[] expectedResult = Files.readAllBytes(Paths.get(ClassLoader
.getSystemResource("wiremock" + File.separator + "__files" + File.separator + bodyFilename).toURI()));
assertThat(createContentObject().getData(), IsEqual.equalTo(expectedResult));
}
@Test
public void getDataMissingContentType() throws URISyntaxException {
stubFor(get(urlEqualTo(downloadUrl)).willReturn(aResponse().withStatus(200).withBodyFile(bodyFilename)));
assertNull(createContentObject().getData());
}
@Test
public void getDataError404() throws URISyntaxException, IOException {
stubFor(get(urlEqualTo(downloadUrl)).willReturn(aResponse().withStatus(404).withBody(error404Response)));
assertNull(createContentObject().getData());
}
@Test
public void getDataError403() throws URISyntaxException, IOException {
stubFor(get(urlEqualTo(downloadUrl)).willReturn(aResponse().withStatus(403).withBody(error403Response)));
assertNull(createContentObject().getData());
}
@Test
public void getFilename() throws URISyntaxException, IOException {
stubFor(get(urlEqualTo(downloadUrl)).willReturn(
aResponse().withStatus(200).withBodyFile(bodyFilename).withHeader("Content-Type", "text/plain")
.withHeader("Content-Disposition", String.format("filename=%s;", bodyFilename))));
assertEquals(Optional.of(bodyFilename), createContentObject().getFilename());
reset();
stubFor(get(urlEqualTo(downloadUrl)).willReturn(
aResponse().withStatus(200).withBodyFile(bodyFilename).withHeader("Content-Type", "text/plain")
.withHeader("Content-Disposition", String.format("filename=\"%s\";", bodyFilename))));
assertEquals(Optional.of(bodyFilename), createContentObject().getFilename());
reset();
stubFor(get(urlEqualTo(downloadUrl)).willReturn(
aResponse().withStatus(200).withBodyFile(bodyFilename).withHeader("Content-Type", "text/plain")
.withHeader("Content-Disposition", String.format("filename=\"%s\"", bodyFilename))));
assertEquals(Optional.of(bodyFilename), createContentObject().getFilename());
reset();
stubFor(get(urlEqualTo(downloadUrl)).willReturn(
aResponse().withStatus(200).withBodyFile(bodyFilename).withHeader("Content-Type", "text/plain")
.withHeader("Content-Disposition", String.format("filename=%s", bodyFilename))));
assertEquals(Optional.of(bodyFilename), createContentObject().getFilename());
}
@Test
public void getFilenameMissingContentType() throws URISyntaxException {
stubFor(get(urlEqualTo(downloadUrl)).willReturn(
aResponse().withStatus(200).withBodyFile(bodyFilename).withHeader("Content-Type", "text/plain")));
assertEquals(Optional.empty(), createContentObject().getFilename());
}
@Test
public void getFilenameError404() throws URISyntaxException, IOException {
stubFor(get(urlEqualTo(downloadUrl)).willReturn(aResponse().withStatus(404).withBody(error404Response)));
assertEquals(Optional.empty(), createContentObject().getFilename());
}
@Test
public void getFilenameError403() throws URISyntaxException, IOException {
stubFor(get(urlEqualTo(downloadUrl)).willReturn(aResponse().withStatus(403).withBody(error403Response)));
assertEquals(Optional.empty(), createContentObject().getFilename());
}
private MatrixHttpContent createContentObject() throws URISyntaxException {
MatrixClientContext context = createClientContext();
return new MatrixHttpContent(context, address);
}
}