Skip to content

Commit

Permalink
Openapi wildcard 700 test implementation (#707)
Browse files Browse the repository at this point in the history
* 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
christiangoerdes and predic8 authored Aug 24, 2023
1 parent 1959838 commit 4fc1981
Show file tree
Hide file tree
Showing 5 changed files with 176 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

public abstract class Message<T> {

private static Logger log = LoggerFactory.getLogger(Message.class.getName());
private static final Logger log = LoggerFactory.getLogger(Message.class.getName());

protected Body body = new NoBody();
protected ContentType mediaType;
Expand Down Expand Up @@ -77,6 +77,12 @@ public T mediaType(String mediaType) throws ParseException {
}

public boolean isOfMediaType(String mediaType) {
// See https://datatracker.ietf.org/doc/html/rfc7231#appendix-D
// media-range = ( "*/*" / ( type "/*" ) / ( type "/" subtype ) ) *( OWS
// ";" OWS parameter )
// So */foo is illegal => We do not have to check that.
if (mediaType.startsWith("*/*"))
return true;
return this.mediaType.match(mediaType);
}

Expand Down
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"));
}
}
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 core/src/test/resources/openapi/specs/content-type-wildcards.yml
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
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@

JsonProtectionTest.class,

DefaultConfigAdminConsoleTest.class
//DefaultConfigAdminConsoleTest.class
})
public class ExampleTestsWithoutInternet {
}

0 comments on commit 4fc1981

Please sign in to comment.