-
Notifications
You must be signed in to change notification settings - Fork 138
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Openapi wildcard 700 test implementation (#707)
* Init * implemented tests * code optimization * code optimization * added response tests * code optimization * code optimization * removed unused imports * added required changes * added constants * added constants --------- Co-authored-by: Thomas Bayer <[email protected]>
- Loading branch information
1 parent
1959838
commit 4fc1981
Showing
5 changed files
with
176 additions
and
2 deletions.
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
36 changes: 36 additions & 0 deletions
36
core/src/test/java/com/predic8/membrane/core/openapi/model/MessageTest.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,36 @@ | ||
package com.predic8.membrane.core.openapi.model; | ||
|
||
|
||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import javax.validation.constraints.AssertTrue; | ||
import java.net.URISyntaxException; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertFalse; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
class MessageTest { | ||
|
||
private Request request; | ||
|
||
@BeforeEach | ||
void setup() throws URISyntaxException { | ||
request = Request.post().json().path("/star-star").body("{}"); | ||
} | ||
|
||
@Test | ||
void starStarTest() { | ||
assertTrue(request.isOfMediaType("*/*")); | ||
} | ||
|
||
@Test | ||
void typeStarTest() { | ||
assertTrue(request.isOfMediaType("application/*")); | ||
} | ||
|
||
@Test | ||
void starTypeTest() { | ||
assertFalse(request.isOfMediaType("*/json")); | ||
} | ||
} |
80 changes: 80 additions & 0 deletions
80
.../src/test/java/com/predic8/membrane/core/openapi/validators/ContentTypeWildcardTests.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,80 @@ | ||
package com.predic8.membrane.core.openapi.validators; | ||
|
||
import com.predic8.membrane.core.openapi.model.Request; | ||
import com.predic8.membrane.core.openapi.model.Response; | ||
import jakarta.mail.internet.ContentType; | ||
import jakarta.mail.internet.ParseException; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import static com.predic8.membrane.core.http.MimeType.APPLICATION_JSON; | ||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
public class ContentTypeWildcardTests extends AbstractValidatorTest { | ||
|
||
|
||
private static final Request STAR_STAR = Request.post().json().path("/star-star").body("{}"); | ||
private static final Request STAR_JSON = Request.post().json().path("/star-json").body("{}"); | ||
private static final Request APPLICATION_STAR = Request.post().json().path("/application-star").body("{}"); | ||
|
||
@Override | ||
String getOpenAPIFileName() { | ||
return "/openapi/specs/content-type-wildcards.yml"; | ||
} | ||
|
||
// See https://datatracker.ietf.org/doc/html/rfc7231#appendix-D | ||
// media-range = ( "*/*" / ( type "/*" ) / ( type "/" subtype ) ) *( OWS | ||
// ";" OWS parameter ) | ||
// For that reason java.jakarta.ContentType does not match for type = * | ||
// In Message.isOfMediaType we fix that for Membrane | ||
@Test | ||
void contentTypeMatching() throws ParseException { | ||
assertFalse(new ContentType("*/*").match(APPLICATION_JSON)); | ||
} | ||
|
||
@Test | ||
void contentTypeMatchingSwitch() throws ParseException { | ||
assertTrue(new ContentType(APPLICATION_JSON).match("application/*")); | ||
} | ||
|
||
void validateAndAssert(Request request, boolean expectedResult) { | ||
ValidationErrors errors = validator.validate(request); | ||
assertEquals(expectedResult, errors.isEmpty()); | ||
} | ||
|
||
@Test | ||
void starStarTest() { | ||
validateAndAssert(STAR_STAR, true); | ||
} | ||
|
||
@Test | ||
void starTypeTest() { | ||
validateAndAssert(STAR_JSON, false); | ||
} | ||
|
||
@Test | ||
void typeStarTest() { | ||
validateAndAssert(APPLICATION_STAR, true); | ||
} | ||
|
||
void responseTest(Request request, boolean expectedResult) throws ParseException { | ||
ValidationErrors errors = validator.validateResponse( | ||
request, | ||
Response.statusCode(200).json().body("{}")); | ||
assertEquals(expectedResult, errors.isEmpty()); | ||
} | ||
|
||
@Test | ||
void starStarResponseTest() throws ParseException { | ||
responseTest(STAR_STAR, true); | ||
} | ||
|
||
@Test | ||
void starTypeResponseTest() throws ParseException { | ||
responseTest(STAR_JSON, false); | ||
} | ||
|
||
@Test | ||
void typeStarResponseTest() throws ParseException { | ||
responseTest(APPLICATION_STAR, true); | ||
} | ||
} |
52 changes: 52 additions & 0 deletions
52
core/src/test/resources/openapi/specs/content-type-wildcards.yml
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,52 @@ | ||
# TODO Also make examples for responses | ||
openapi: '3.0.2' | ||
info: | ||
title: Wildcard API | ||
version: '1.0' | ||
servers: | ||
- url: https://api.server.test | ||
paths: | ||
/star-star: | ||
post: | ||
requestBody: | ||
content: | ||
'*/*': | ||
schema: | ||
type: string | ||
responses: | ||
'200': | ||
description: OK | ||
content: | ||
'*/*': | ||
schema: | ||
type: string | ||
|
||
/application-star: | ||
post: | ||
requestBody: | ||
content: | ||
'application/*': | ||
schema: | ||
type: object | ||
responses: | ||
'200': | ||
description: OK | ||
content: | ||
'application/*': | ||
schema: | ||
type: object | ||
|
||
/star-json: | ||
post: | ||
requestBody: | ||
content: | ||
'*/json': | ||
schema: | ||
type: string | ||
responses: | ||
'200': | ||
description: OK | ||
content: | ||
'*/json': | ||
schema: | ||
type: string |
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