|
| 1 | +/* |
| 2 | + * matrix-java-sdk - Matrix Client SDK for Java |
| 3 | + * Copyright (C) 2017 Arne Augenstein |
| 4 | + * |
| 5 | + * https://max.kamax.io/ |
| 6 | + * |
| 7 | + * This program is free software: you can redistribute it and/or modify |
| 8 | + * it under the terms of the GNU Affero General Public License as |
| 9 | + * published by the Free Software Foundation, either version 3 of the |
| 10 | + * License, or (at your option) any later version. |
| 11 | + * |
| 12 | + * This program is distributed in the hope that it will be useful, |
| 13 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | + * GNU Affero General Public License for more details. |
| 16 | + * |
| 17 | + * You should have received a copy of the GNU Affero General Public License |
| 18 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 19 | + */ |
| 20 | + |
| 21 | +package io.kamax.matrix.client; |
| 22 | + |
| 23 | +import org.hamcrest.core.IsEqual; |
| 24 | +import org.junit.Test; |
| 25 | + |
| 26 | +import java.io.File; |
| 27 | +import java.io.IOException; |
| 28 | +import java.net.URI; |
| 29 | +import java.net.URISyntaxException; |
| 30 | +import java.nio.file.Files; |
| 31 | +import java.nio.file.Paths; |
| 32 | +import java.util.Optional; |
| 33 | + |
| 34 | +import static com.github.tomakehurst.wiremock.client.WireMock.*; |
| 35 | + |
| 36 | +import static org.hamcrest.MatcherAssert.assertThat; |
| 37 | +import static org.junit.jupiter.api.Assertions.*; |
| 38 | + |
| 39 | +/* |
| 40 | + * TODO As the spec is outdated, I'm not sure if the error 403 can really happen in these test cases. This class has |
| 41 | + * to be checked for correctness, when matrix's spec is updated. |
| 42 | + */ |
| 43 | +public class MatrixHttpContentTest extends MatrixHttpTest { |
| 44 | + private String bodyFilename = "textfile.txt"; |
| 45 | + private URI address = new URI("mxc://localhost/testpath/" + bodyFilename); |
| 46 | + private String downloadUrl = "/_matrix/media/v1/download/" + address.getHost() + address.getPath() + tokenParameter; |
| 47 | + |
| 48 | + public MatrixHttpContentTest() throws URISyntaxException { |
| 49 | + } |
| 50 | + |
| 51 | + @Test |
| 52 | + public void isValid() throws URISyntaxException { |
| 53 | + stubFor(get(urlEqualTo(downloadUrl)).willReturn( |
| 54 | + aResponse().withStatus(200).withBodyFile(bodyFilename).withHeader("Content-Type", "text/plain"))); |
| 55 | + assertTrue(createContentObject().isValid()); |
| 56 | + } |
| 57 | + |
| 58 | + @Test |
| 59 | + public void isValidMissingContentType() throws URISyntaxException { |
| 60 | + stubFor(get(urlEqualTo(downloadUrl)).willReturn(aResponse().withStatus(200).withBodyFile(bodyFilename))); |
| 61 | + assertFalse(createContentObject().isValid()); |
| 62 | + } |
| 63 | + |
| 64 | + @Test |
| 65 | + public void isValidError404() throws URISyntaxException { |
| 66 | + stubFor(get(urlEqualTo(downloadUrl)).willReturn(aResponse().withStatus(404).withBody(error404Response))); |
| 67 | + assertFalse(createContentObject().isValid()); |
| 68 | + } |
| 69 | + |
| 70 | + @Test |
| 71 | + public void isValidError403() throws URISyntaxException { |
| 72 | + stubFor(get(urlEqualTo(downloadUrl)).willReturn(aResponse().withStatus(403).withBody(error403Response))); |
| 73 | + assertFalse(createContentObject().isValid()); |
| 74 | + } |
| 75 | + |
| 76 | + @Test |
| 77 | + public void getType() throws URISyntaxException, IOException { |
| 78 | + stubFor(get(urlEqualTo(downloadUrl)).willReturn( |
| 79 | + aResponse().withStatus(200).withBodyFile(bodyFilename).withHeader("Content-Type", "text/plain"))); |
| 80 | + assertEquals("text/plain", createContentObject().getType()); |
| 81 | + } |
| 82 | + |
| 83 | + @Test |
| 84 | + public void getTypeMissingContentType() throws URISyntaxException { |
| 85 | + stubFor(get(urlEqualTo(downloadUrl)).willReturn(aResponse().withStatus(200).withBodyFile(bodyFilename))); |
| 86 | + assertNull(createContentObject().getType()); |
| 87 | + } |
| 88 | + |
| 89 | + @Test |
| 90 | + public void getTypeError404() throws URISyntaxException, IOException { |
| 91 | + stubFor(get(urlEqualTo(downloadUrl)) |
| 92 | + .willReturn(aResponse().withStatus(404).withStatus(404).withBody(error404Response))); |
| 93 | + assertNull(createContentObject().getType()); |
| 94 | + } |
| 95 | + |
| 96 | + @Test |
| 97 | + public void getTypeError403() throws URISyntaxException, IOException { |
| 98 | + stubFor(get(urlEqualTo(downloadUrl)) |
| 99 | + .willReturn(aResponse().withStatus(403).withStatus(403).withBody(error403Response))); |
| 100 | + assertNull(createContentObject().getType()); |
| 101 | + } |
| 102 | + |
| 103 | + @Test |
| 104 | + public void getData() throws URISyntaxException, IOException { |
| 105 | + stubFor(get(urlEqualTo(downloadUrl)).willReturn( |
| 106 | + aResponse().withStatus(200).withBodyFile(bodyFilename).withHeader("Content-Type", "text/plain"))); |
| 107 | + |
| 108 | + byte[] expectedResult = Files.readAllBytes(Paths.get(ClassLoader |
| 109 | + .getSystemResource("wiremock" + File.separator + "__files" + File.separator + bodyFilename).toURI())); |
| 110 | + assertThat(createContentObject().getData(), IsEqual.equalTo(expectedResult)); |
| 111 | + } |
| 112 | + |
| 113 | + @Test |
| 114 | + public void getDataMissingContentType() throws URISyntaxException { |
| 115 | + stubFor(get(urlEqualTo(downloadUrl)).willReturn(aResponse().withStatus(200).withBodyFile(bodyFilename))); |
| 116 | + assertNull(createContentObject().getData()); |
| 117 | + } |
| 118 | + |
| 119 | + @Test |
| 120 | + public void getDataError404() throws URISyntaxException, IOException { |
| 121 | + stubFor(get(urlEqualTo(downloadUrl)).willReturn(aResponse().withStatus(404).withBody(error404Response))); |
| 122 | + assertNull(createContentObject().getData()); |
| 123 | + } |
| 124 | + |
| 125 | + @Test |
| 126 | + public void getDataError403() throws URISyntaxException, IOException { |
| 127 | + stubFor(get(urlEqualTo(downloadUrl)).willReturn(aResponse().withStatus(403).withBody(error403Response))); |
| 128 | + assertNull(createContentObject().getData()); |
| 129 | + } |
| 130 | + |
| 131 | + @Test |
| 132 | + public void getFilename() throws URISyntaxException, IOException { |
| 133 | + stubFor(get(urlEqualTo(downloadUrl)).willReturn( |
| 134 | + aResponse().withStatus(200).withBodyFile(bodyFilename).withHeader("Content-Type", "text/plain") |
| 135 | + .withHeader("Content-Disposition", String.format("filename=%s;", bodyFilename)))); |
| 136 | + assertEquals(Optional.of(bodyFilename), createContentObject().getFilename()); |
| 137 | + |
| 138 | + reset(); |
| 139 | + |
| 140 | + stubFor(get(urlEqualTo(downloadUrl)).willReturn( |
| 141 | + aResponse().withStatus(200).withBodyFile(bodyFilename).withHeader("Content-Type", "text/plain") |
| 142 | + .withHeader("Content-Disposition", String.format("filename=\"%s\";", bodyFilename)))); |
| 143 | + assertEquals(Optional.of(bodyFilename), createContentObject().getFilename()); |
| 144 | + |
| 145 | + reset(); |
| 146 | + |
| 147 | + stubFor(get(urlEqualTo(downloadUrl)).willReturn( |
| 148 | + aResponse().withStatus(200).withBodyFile(bodyFilename).withHeader("Content-Type", "text/plain") |
| 149 | + .withHeader("Content-Disposition", String.format("filename=\"%s\"", bodyFilename)))); |
| 150 | + assertEquals(Optional.of(bodyFilename), createContentObject().getFilename()); |
| 151 | + |
| 152 | + reset(); |
| 153 | + |
| 154 | + stubFor(get(urlEqualTo(downloadUrl)).willReturn( |
| 155 | + aResponse().withStatus(200).withBodyFile(bodyFilename).withHeader("Content-Type", "text/plain") |
| 156 | + .withHeader("Content-Disposition", String.format("filename=%s", bodyFilename)))); |
| 157 | + assertEquals(Optional.of(bodyFilename), createContentObject().getFilename()); |
| 158 | + } |
| 159 | + |
| 160 | + @Test |
| 161 | + public void getFilenameMissingContentType() throws URISyntaxException { |
| 162 | + stubFor(get(urlEqualTo(downloadUrl)).willReturn( |
| 163 | + aResponse().withStatus(200).withBodyFile(bodyFilename).withHeader("Content-Type", "text/plain"))); |
| 164 | + assertEquals(Optional.empty(), createContentObject().getFilename()); |
| 165 | + } |
| 166 | + |
| 167 | + @Test |
| 168 | + public void getFilenameError404() throws URISyntaxException, IOException { |
| 169 | + stubFor(get(urlEqualTo(downloadUrl)).willReturn(aResponse().withStatus(404).withBody(error404Response))); |
| 170 | + assertEquals(Optional.empty(), createContentObject().getFilename()); |
| 171 | + } |
| 172 | + |
| 173 | + @Test |
| 174 | + public void getFilenameError403() throws URISyntaxException, IOException { |
| 175 | + stubFor(get(urlEqualTo(downloadUrl)).willReturn(aResponse().withStatus(403).withBody(error403Response))); |
| 176 | + assertEquals(Optional.empty(), createContentObject().getFilename()); |
| 177 | + } |
| 178 | + |
| 179 | + private MatrixHttpContent createContentObject() throws URISyntaxException { |
| 180 | + MatrixClientContext context = createClientContext(); |
| 181 | + return new MatrixHttpContent(context, address); |
| 182 | + } |
| 183 | + |
| 184 | +} |
0 commit comments