Skip to content

Commit

Permalink
refactor tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ryber committed Feb 17, 2024
1 parent aa4989c commit bed07fe
Show file tree
Hide file tree
Showing 11 changed files with 269 additions and 38 deletions.
10 changes: 9 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
<junit.version>5.10.0</junit.version>
<mockito.version>5.6.0</mockito.version>
<maven-checkstyle-plugin.version>3.3.0</maven-checkstyle-plugin.version>
<assertj.version>3.25.3</assertj.version>
</properties>

<scm>
Expand Down Expand Up @@ -313,7 +314,14 @@
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>3.25.1</version>
<version>${assertj.version}</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.assertj/assertj-guava -->
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-guava</artifactId>
<version>${assertj.version}</version>
<scope>test</scope>
</dependency>
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ void setCharSetAfterBody() {
.charset(StandardCharsets.US_ASCII)
.asObject(RequestCapture.class)
.getBody()
.assertContentType("text/plain; charset=US-ASCII");
.assertContentType("text/plain", "charset", "US-ASCII");
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ void testChangingEncodingToForms(){
.field("foo", "bar")
.asObject(RequestCapture.class)
.getBody()
.assertContentType("application/x-www-form-urlencoded; charset=US-ASCII")
.assertRawContentType("application/x-www-form-urlencoded; charset=US-ASCII")
.assertParam("foo", "bar")
.assertCharset(StandardCharsets.US_ASCII);
}
Expand All @@ -166,7 +166,7 @@ void testChangingEncodingAfterMovingToForm(){
.charset(StandardCharsets.US_ASCII)
.asObject(RequestCapture.class)
.getBody()
.assertContentType("application/x-www-form-urlencoded; charset=US-ASCII")
.assertContentType("application/x-www-form-urlencoded", "charset", "US-ASCII")
.assertParam("foo", "bar")
.assertCharset(StandardCharsets.US_ASCII);
}
Expand Down
165 changes: 165 additions & 0 deletions unirest-bdd-tests/src/test/java/BehaviorTests/HeaderAsserts.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
/**
* The MIT License
*
* Copyright for portions of unirest-java are held by Kong Inc (c) 2013.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

package BehaviorTests;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.common.base.Splitter;
import com.google.common.collect.*;
import io.javalin.http.Context;
import org.assertj.core.data.MapEntry;
import org.assertj.guava.api.MultimapAssert;

import java.util.Base64;
import java.util.Collections;
import java.util.List;
import java.util.Map;


import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.*;

public class HeaderAsserts {
public ArrayListMultimap<String, HeaderValue> headers = ArrayListMultimap.create();

public HeaderAsserts(){}

public HeaderAsserts(Context req) {
Collections.list(req.req().getHeaderNames())
.forEach(name -> Collections.list(req.req().getHeaders(name))
.forEach(value -> headers.put(name, new HeaderValue(value))));
}

public HeaderAsserts(Map<String, String> values){
values.forEach((k,v) -> headers.put(k, new HeaderValue(v)));
}

public void assertNoHeader(String s) {
assertFalse(headers.containsKey(s), "Should Have No Header " + s);
}

public void assertHeader(String key, String... value) {
assertMultiMap(headers).containsKeys(key);
assertThat(headers.get(key))
.extracting(HeaderValue::getValue)
.contains(value);

}

public void assertBasicAuth(String username, String password) {
assertMultiMap(headers).containsKeys("Authorization");
var value = headers.get("Authorization").get(0);
assertThat(value.getValue()).as("Missing value scope of Basic").contains("Basic ");
String decoded = new String(Base64.getDecoder().decode(value.getValue().replace("Basic ", "")));
assertThat(decoded).isEqualTo(username + ":" + password);
}

public void assertHeaderSize(String name, int size) {
assertEquals(size, headers.get(name).size());
}

public void assertMultiPartContentType() {
List<HeaderValue> h = headers.get("Content-Type");
assertEquals(1, h.size(), "Expected exactly 1 Content-Type header");
HeaderValue value = h.get(0);
value.assertMainValue("multipart/form-data");
value.assertHasParam("boundary");
value.assertParam("charset", "UTF-8");
}

public void assertHeaderWithParam(String headerKey, String headerValue, String paramKey, String paramValue) {
assertMultiMap(headers).containsKeys(headerKey);
var value = headers.get(headerKey).get(0);
value.assertMainValue(headerValue);
value.assertParam(paramKey, paramValue);
}

public void assertRawValue(String key, String value) {
assertThat(headers.get(key))
.isNotEmpty()
.extracting(HeaderValue::rawValue)
.contains(value);
}

private static class HeaderValue {
@JsonIgnore private static final Splitter partSplitter = Splitter.on(";").trimResults().omitEmptyStrings();
@JsonIgnore private static final Splitter paramSplitter = Splitter.on("=").trimResults().omitEmptyStrings();
@JsonProperty("rawValue")
private String rawValue;
@JsonProperty("value")
private String value;
@JsonProperty("params")
private ArrayListMultimap<String, String> params = ArrayListMultimap.create();

public HeaderValue(){}

public HeaderValue(String value) {
this.rawValue = value;
if(value.contains(";")){
int pos=0;
for(String part : partSplitter.split(value)){
if(pos == 0){
this.value = part;
} else if (part.contains("=")) {
var param = paramSplitter.splitToList(part);
params.put(param.get(0), param.get(1));
}
pos++;
}
} else {
this.value = value;
}
}

public String getValue() {
return value;
}

public void assertHasParam(String name) {
assertMultiMap(params)
.as("Header Param")
.containsKeys(name);
}

public void assertMainValue(String expectedValue) {
assertEquals(expectedValue, value);
}

public void assertParam(String name, String value) {
assertMultiMap(params)
.as("Header Param")
.contains(MapEntry.entry(name, value));
}

public String rawValue() {
return rawValue;
}
}

public static <K, V> MultimapAssert<K, V> assertMultiMap(final Multimap<K, V> actual) {
return org.assertj.guava.api.Assertions.assertThat(actual);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/**
* The MIT License
*
* Copyright for portions of unirest-java are held by Kong Inc (c) 2013.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

package BehaviorTests;

import org.junit.jupiter.api.Test;

import java.util.Map;

class HeaderAssertsTest {

@Test
void canAssertHeaderWithParts() {
HeaderAsserts a = new HeaderAsserts(Map.of("foo", "bar; baz=qux; zip=zap;;"));
a.assertHeader("foo", "bar");
a.assertHeaderWithParam("foo", "bar", "baz", "qux");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,7 @@ void defaultContentTypes() {
.asObject(RequestCapture.class)
.getBody()
.assertHeaderSize("Content-Type", 1)
.assertContentType("application/x-www-form-urlencoded; charset=UTF-8");
.assertRawContentType("application/x-www-form-urlencoded; charset=UTF-8");
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,16 @@
package BehaviorTests;

import kong.unirest.core.ContentType;
import kong.unirest.core.HttpResponse;
import kong.unirest.core.MultipartMode;
import kong.unirest.core.Unirest;
import org.junit.jupiter.api.Test;

import java.io.*;
import java.net.URISyntaxException;

import static BehaviorTests.TestUtil.*;
import static java.util.Arrays.asList;
import static BehaviorTests.TestUtil.getFileBytes;
import static BehaviorTests.TestUtil.rezFile;
import static org.junit.jupiter.api.Assertions.assertEquals;

class MultiPartFormPostingTest extends BddTest {
Expand Down Expand Up @@ -119,7 +119,7 @@ void testMultipartInputStreamContentType() throws Exception {
.asObject(RequestCapture.class)
.getBody()
.assertMultiPartContentType()
.assertHeader("Accept", ContentType.MULTIPART_FORM_DATA.toString())
.assertHeader("Accept", "multipart/form-data")
.assertParam("name", "Mark")
.getFile("image.jpg")
.assertFileType("application/octet-stream");
Expand Down Expand Up @@ -423,4 +423,19 @@ void rawInspection() {
assertEquals(expected, body);
}

@Test
void mediaTypesForParts() {
Unirest.post(MockServer.POST)
.field("content",
rezInput("/spidey.pdf"),
ContentType.APPLICATION_PDF,
"spiderman")
.field("metadata",
"{\"foo\": 1}",
ContentType.APPLICATION_JSON.getMimeType())
.asObject(RequestCapture.class)
.getBody()
.assertContentType("multipart/form-data");

}
}
Loading

0 comments on commit bed07fe

Please sign in to comment.