From 81080e01eb2ceb3ff2ec4d943295681bd4ef7fe1 Mon Sep 17 00:00:00 2001 From: Torben Burchgart Date: Mon, 31 Jul 2023 15:55:38 +0200 Subject: [PATCH] Updated links and paths --- .../OpenAPIPublisherInterceptor.java | 7 +- .../OpenAPIPublisherInterceptorTest.java | 69 ++++++++++++++++--- 2 files changed, 61 insertions(+), 15 deletions(-) diff --git a/core/src/main/java/com/predic8/membrane/core/openapi/serviceproxy/OpenAPIPublisherInterceptor.java b/core/src/main/java/com/predic8/membrane/core/openapi/serviceproxy/OpenAPIPublisherInterceptor.java index b550281e4..b9a866d10 100644 --- a/core/src/main/java/com/predic8/membrane/core/openapi/serviceproxy/OpenAPIPublisherInterceptor.java +++ b/core/src/main/java/com/predic8/membrane/core/openapi/serviceproxy/OpenAPIPublisherInterceptor.java @@ -51,7 +51,7 @@ public class OpenAPIPublisherInterceptor extends AbstractInterceptor { public static final String PATH_UI = "/api-docs/ui"; private static final Pattern patternMeta = Pattern.compile("/api-docs?/(.*)"); - private static final Pattern patternUI = Pattern.compile("/api-docs?/(.*)"); + private static final Pattern patternUI = Pattern.compile("/api-docs?/ui(.*)"); protected Map apis; @@ -75,8 +75,7 @@ public Outcome handleRequest(Exchange exc) throws Exception { if (!exc.getRequest().getUri().startsWith("/api-doc")) return CONTINUE; - // Should be called with /api-doc/ui and /api-docs/ui Pattern Match? - if (exc.getRequest().getUri().startsWith(PATH_UI)) { + if (exc.getRequest().getUri().matches("/api-docs?/ui")) { return handleSwaggerUi(exc); } @@ -131,7 +130,7 @@ private Outcome handleSwaggerUi(Exchange exc) { // No id specified if (!m.matches()) { Map details = new HashMap<>(); - details.put("message", "Please specify an id of an OpenAPI document. Path should match this pattern: /api-doc/ui/<>"); + details.put("message", "Please specify an id of an OpenAPI document. Path should match this pattern: /api-docs/ui/<>"); exc.setResponse(createProblemDetails(404, "/openapi/wrong-id", "No OpenAPI document id", details)); return RETURN; } diff --git a/core/src/test/java/com/predic8/membrane/core/openapi/serviceproxy/OpenAPIPublisherInterceptorTest.java b/core/src/test/java/com/predic8/membrane/core/openapi/serviceproxy/OpenAPIPublisherInterceptorTest.java index 553154468..1100eeb2c 100644 --- a/core/src/test/java/com/predic8/membrane/core/openapi/serviceproxy/OpenAPIPublisherInterceptorTest.java +++ b/core/src/test/java/com/predic8/membrane/core/openapi/serviceproxy/OpenAPIPublisherInterceptorTest.java @@ -38,6 +38,9 @@ public class OpenAPIPublisherInterceptorTest { private final ObjectMapper omYaml = ObjectMapperFactory.createYaml(); private final ObjectMapper om = new ObjectMapper(); + private static final String metaOld = "/api-doc"; + private static final String uiOld = "/api-doc/ui"; + OpenAPIRecordFactory openAPIRecordFactory; OpenAPIPublisherInterceptor interceptor; Map records; @@ -74,46 +77,82 @@ public void constuctor() { @Test public void getApiDirectoryOld() throws Exception { - get.getRequest().setUri("/api-doc"); + get.getRequest().setUri(metaOld); assertEquals( RETURN, interceptor.handleRequest(get)); assertTrue(TestUtils.getMapFromResponse(get).size() >= 27); } - // TODO Same Test with /api-docs + @Test + public void getApiDirectory() throws Exception { + get.getRequest().setUri(OpenAPIPublisherInterceptor.PATH); + assertEquals( RETURN, interceptor.handleRequest(get)); + assertTrue(TestUtils.getMapFromResponse(get).size() >= 27); + } @Test public void getHTMLOverviewOld() throws Exception { - get.getRequest().setUri("/api-doc"); + get.getRequest().setUri(metaOld); + Header header = new Header(); + header.setAccept("html"); + get.getRequest().setHeader(header); + assertEquals( RETURN, interceptor.handleRequest(get)); + assertTrue(get.getResponse().getBodyAsStringDecoded().contains("Servers 1 API")); + } + + @Test + public void getHTMLOverview() throws Exception { + get.getRequest().setUri(OpenAPIPublisherInterceptor.PATH); Header header = new Header(); header.setAccept("html"); get.getRequest().setHeader(header); assertEquals( RETURN, interceptor.handleRequest(get)); - assertTrue(get.getResponse().getBodyAsStringDecoded().contains("Servers 1 API")); + assertTrue(get.getResponse().getBodyAsStringDecoded().contains("Servers 1 API")); } - // TODO ditto + @Test + public void getSwaggerUIOld() throws Exception { + get.getRequest().setUri(uiOld + "/nested-objects-and-arrays-test-api-v1-0"); + assertEquals( RETURN, interceptor.handleRequest(get)); + assertTrue(get.getResponse().getBodyAsStringDecoded().contains("html")); + } @Test public void getSwaggerUI() throws Exception { - get.getRequest().setUri("/api-doc/ui/nested-objects-and-arrays-test-api-v1-0"); + get.getRequest().setUri(OpenAPIPublisherInterceptor.PATH_UI + "/nested-objects-and-arrays-test-api-v1-0"); assertEquals( RETURN, interceptor.handleRequest(get)); assertTrue(get.getResponse().getBodyAsStringDecoded().contains("html")); } @Test public void getSwaggerUIWrongIdOld() throws Exception { - get.getRequest().setUri("/api-doc/ui/wrong-id-0"); + get.getRequest().setUri(uiOld + "/wrong-id-0"); assertEquals( RETURN, interceptor.handleRequest(get)); assertEquals( 404, get.getResponse().getStatusCode()); - checkHasValidProblemJSOM(get); + checkHasValidProblemJSON(get); + } + + @Test + public void getSwaggerUIWrongId() throws Exception { + get.getRequest().setUri(OpenAPIPublisherInterceptor.PATH_UI + "/wrong-id-0"); + assertEquals( RETURN, interceptor.handleRequest(get)); + assertEquals( 404, get.getResponse().getStatusCode()); + checkHasValidProblemJSON(get); + } + + @Test + public void getSwaggerUINoIdOld() throws Exception { + get.getRequest().setUri(uiOld); + assertEquals( RETURN, interceptor.handleRequest(get)); + assertEquals( 404, get.getResponse().getStatusCode()); + checkHasValidProblemJSON(get); } @Test public void getSwaggerUINoId() throws Exception { - get.getRequest().setUri("/api-doc"); + get.getRequest().setUri(OpenAPIPublisherInterceptor.PATH_UI); assertEquals( RETURN, interceptor.handleRequest(get)); assertEquals( 404, get.getResponse().getStatusCode()); - checkHasValidProblemJSOM(get); + checkHasValidProblemJSON(get); } private void checkHasValidProblemJSON(Exchange exc) throws IOException { @@ -126,9 +165,17 @@ private void checkHasValidProblemJSON(Exchange exc) throws IOException { assertTrue(json.has("type")); } + @Test + public void getApiByIdOld() throws Exception { + get.getRequest().setUri(metaOld + "/nested-objects-and-arrays-test-api-v1-0"); + assertEquals( RETURN, interceptor.handleRequest(get)); + assertEquals("application/x-yaml", get.getResponse().getHeader().getContentType()); + assertEquals("Nested Objects and Arrays Test API", getJsonFromYamlResponse(get).get("info").get("title").textValue()); + } + @Test public void getApiById() throws Exception { - get.getRequest().setUri("/api-doc/nested-objects-and-arrays-test-api-v1-0"); + get.getRequest().setUri(OpenAPIPublisherInterceptor.PATH + "/nested-objects-and-arrays-test-api-v1-0"); assertEquals( RETURN, interceptor.handleRequest(get)); assertEquals("application/x-yaml", get.getResponse().getHeader().getContentType()); assertEquals("Nested Objects and Arrays Test API", getJsonFromYamlResponse(get).get("info").get("title").textValue());