Skip to content
This repository was archived by the owner on Jun 21, 2019. It is now read-only.

Wiremock #5

Merged
merged 24 commits into from
Oct 7, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@
* 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/>.
*/
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'org.junit.platform:junit-platform-gradle-plugin:1.0.1'
}
}

plugins {
id "com.diffplug.gradle.spotless" version "3.5.2"
Expand All @@ -27,6 +35,7 @@ version = '0.0.2'

apply plugin: 'java'
apply plugin: 'maven'
apply plugin: 'org.junit.platform.gradle.plugin'

configurations {
deployerJars
Expand All @@ -43,7 +52,9 @@ dependencies {
compile 'org.apache.httpcomponents:fluent-hc:4.5.3'
compile 'com.google.code.gson:gson:2.8.0'

testCompile 'junit:junit:4.12'
testCompile "org.junit.jupiter:junit-jupiter-api:5.0.0"
testRuntime "org.junit.jupiter:junit-jupiter-engine:5.0.0"
testCompile "com.github.tomakehurst:wiremock:2.8.0"
testRuntimeOnly 'org.slf4j:slf4j-simple:1.7.25'

deployerJars "org.apache.maven.wagon:wagon-ssh:2.2"
Expand Down Expand Up @@ -86,3 +97,7 @@ spotless {
importOrderFile 'spotless.importorder'
}
}

junitPlatform {
enableStandardTestTask true
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public class MatrixHttpContent extends AMatrixHttpClient implements _MatrixConte

private Logger log = LoggerFactory.getLogger(MatrixHttpContent.class);

private final Pattern filenamePattern = Pattern.compile("filename=\"?(?<filename>.+)\"?;?");
private final Pattern filenamePattern = Pattern.compile("filename=\"?(?<filename>[^\";]+)");

private URI address;
private String type;
Expand Down
184 changes: 184 additions & 0 deletions src/test/java/io/kamax/matrix/client/MatrixHttpContentTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,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);
}

}
Loading