This repository was archived by the owner on Jun 21, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
Wiremock #5
Merged
Merged
Wiremock #5
Changes from 18 commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
4a21b25
Create first wiremock tests
42dce74
Add tests for MatrixApplicationServiceClient
5e0df5b
Refactor test class structure
56a05cd
Create builders and add some more tests
2378055
Fix regular expression
04912b8
Fix error in runner
72ae867
Merge remote-tracking branch 'origin/master' into wiremock
e8d9970
Merge branch 'master' into wiremock
c6d245a
Add more tests for MatrixHttpContent
12a0019
Refactor test runners
2524f6f
Add more tests
2b422aa
Add sendText test cases
5a86b86
Add tests for getting joined users
ff38b37
Refactor handling of strings
9f9ec10
Rename builder classes
82c133a
Fix formatting issue
9839132
Refactor topic tests
9beb59e
Refactor test of getName
b125c81
Correct regular expression
dd63582
Correct comment
0dc7493
Move to JUnit 5
8881e0a
Revert to JUnit 5.0.0
611fac3
Port MatrixHttpRoomTest to JUnit 5
ee285bc
Port the rest of the tests to JUnit 5
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
215 changes: 215 additions & 0 deletions
215
src/test/java/io/kamax/matrix/client/MatrixHttpContentTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Left over code? |
||
} | ||
|
||
@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(); | ||
} | ||
|
||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't the pattern be
filename=\"?(?<filename>[^\";]+)
instead?