camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From zregv...@apache.org
Subject [camel] 03/03: CAMEL-12855: cleanup and tests
Date Sat, 06 Oct 2018 11:01:02 GMT
This is an automated email from the ASF dual-hosted git repository.

zregvart pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/camel.git

commit a4f81d4f43d25030ed70d4bdb1979542ee31ba4c
Author: Zoran Regvart <zregvart@apache.org>
AuthorDate: Sat Oct 6 13:00:18 2018 +0200

    CAMEL-12855: cleanup and tests
---
 components/camel-swagger-java/pom.xml              |  30 ++++-
 .../apache/camel/swagger/RestSwaggerSupport.java   |  29 ++---
 .../camel/swagger/RestSwaggerSupportTest.java      | 140 +++++++++++++++++++++
 3 files changed, 179 insertions(+), 20 deletions(-)

diff --git a/components/camel-swagger-java/pom.xml b/components/camel-swagger-java/pom.xml
index 434a3c8..344fa32 100644
--- a/components/camel-swagger-java/pom.xml
+++ b/components/camel-swagger-java/pom.xml
@@ -135,10 +135,29 @@
       <scope>test</scope>
     </dependency>
     <dependency>
-      <groupId>junit</groupId>
-      <artifactId>junit</artifactId>
+      <groupId>org.junit.jupiter</groupId>
+      <artifactId>junit-jupiter-api</artifactId>
+      <version>${junit-jupiter-version}</version>
       <scope>test</scope>
-    </dependency>  
+    </dependency>
+    <dependency>
+      <groupId>org.junit.jupiter</groupId>
+      <artifactId>junit-jupiter-params</artifactId>
+      <version>${junit-jupiter-version}</version>
+      <scope>test</scope>
+    </dependency>
+    <dependency>
+      <groupId>org.junit.jupiter</groupId>
+      <artifactId>junit-jupiter-engine</artifactId>
+      <version>${junit-jupiter-version}</version>
+      <scope>test</scope>
+    </dependency>
+    <dependency>
+      <groupId>org.junit.vintage</groupId>
+      <artifactId>junit-vintage-engine</artifactId>
+      <version>${junit-jupiter-version}</version>
+      <scope>test</scope>
+    </dependency>
     <dependency>
       <groupId>org.apache.logging.log4j</groupId>
       <artifactId>log4j-api</artifactId>
@@ -160,6 +179,11 @@
       <version>${assertj-version}</version>
       <scope>test</scope>
     </dependency>
+    <dependency>
+      <groupId>org.mockito</groupId>
+      <artifactId>mockito-core</artifactId>
+      <scope>test</scope>
+    </dependency>
   </dependencies>
 
 </project>
diff --git a/components/camel-swagger-java/src/main/java/org/apache/camel/swagger/RestSwaggerSupport.java
b/components/camel-swagger-java/src/main/java/org/apache/camel/swagger/RestSwaggerSupport.java
index 7a90ab3..33cd20a 100644
--- a/components/camel-swagger-java/src/main/java/org/apache/camel/swagger/RestSwaggerSupport.java
+++ b/components/camel-swagger-java/src/main/java/org/apache/camel/swagger/RestSwaggerSupport.java
@@ -59,11 +59,11 @@ import static org.apache.camel.swagger.SwaggerHelper.clearVendorExtensions;
  * such as servlet/jetty/netty4-http to offer Swagger API listings with minimal effort.
  */
 public class RestSwaggerSupport {
-	
-    private static final String HEADER_X_FORWARDED_PREFIX = "X-Forwarded-Prefix";
-    private static final String HEADER_X_FORWARDED_HOST = "X-Forwarded-Host";
-    private static final String HEADER_X_FORWARDED_PROTO = "X-Forwarded-Proto";
-    private static final String HEADER_HOST = "Host";
+
+    static final String HEADER_X_FORWARDED_PREFIX = "X-Forwarded-Prefix";
+    static final String HEADER_X_FORWARDED_HOST = "X-Forwarded-Host";
+    static final String HEADER_X_FORWARDED_PROTO = "X-Forwarded-Proto";
+    static final String HEADER_HOST = "Host";
 
     private static final Logger LOG = LoggerFactory.getLogger(RestSwaggerSupport.class);
     private RestSwaggerReader reader = new RestSwaggerReader();
@@ -355,37 +355,32 @@ public class RestSwaggerSupport {
         response.setHeader("Access-Control-Max-Age", maxAge);
     }
 
-    private void setupXForwardedHeaders(Swagger swagger, Map<String, Object> headers)
{
+    static void setupXForwardedHeaders(Swagger swagger, Map<String, Object> headers)
{
 
         String host = (String) headers.get(HEADER_HOST);
-        if(ObjectHelper.isNotEmpty(host)) {
+        if (ObjectHelper.isNotEmpty(host)) {
             swagger.setHost(host);
         }
 
         String forwardedPrefix = (String) headers.get(HEADER_X_FORWARDED_PREFIX);
         if (ObjectHelper.isNotEmpty(forwardedPrefix)) {
-            String prefixedBasePath = "/" + URISupport.stripPrefix(forwardedPrefix, "/")
+
-                    (!forwardedPrefix.endsWith("/") ? "/" : "") + URISupport.stripPrefix(swagger.getBasePath(),
"/");
-            swagger.setBasePath(prefixedBasePath);
+            swagger.setBasePath(URISupport.joinPaths(forwardedPrefix, swagger.getBasePath()));
         }
 
         String forwardedHost = (String) headers.get(HEADER_X_FORWARDED_HOST);
-        if(ObjectHelper.isNotEmpty(forwardedHost)) {
+        if (ObjectHelper.isNotEmpty(forwardedHost)) {
             swagger.setHost(forwardedHost);
         }
 
         String proto = (String) headers.get(HEADER_X_FORWARDED_PROTO);
         if(ObjectHelper.isNotEmpty(proto)) {
             String[] schemes = proto.split(",");
-            List<Scheme> schs = new ArrayList<>();
             for(String scheme : schemes) {
                 String trimmedScheme = scheme.trim();
-                schs.add(Scheme.forValue(trimmedScheme));
+                if (ObjectHelper.isNotEmpty(trimmedScheme)) {
+                    swagger.addScheme(Scheme.forValue(trimmedScheme));
+                }
             }
-            swagger.setSchemes(schs);
-        }
-        else {
-            swagger.setSchemes(null);
         }
     }
 
diff --git a/components/camel-swagger-java/src/test/java/org/apache/camel/swagger/RestSwaggerSupportTest.java
b/components/camel-swagger-java/src/test/java/org/apache/camel/swagger/RestSwaggerSupportTest.java
new file mode 100644
index 0000000..3498a8e
--- /dev/null
+++ b/components/camel-swagger-java/src/test/java/org/apache/camel/swagger/RestSwaggerSupportTest.java
@@ -0,0 +1,140 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.swagger;
+
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.stream.Stream;
+
+import io.swagger.models.Scheme;
+import io.swagger.models.Swagger;
+
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.Arguments;
+import org.junit.jupiter.params.provider.MethodSource;
+
+import static org.junit.jupiter.params.provider.Arguments.arguments;
+import static org.mockito.Mockito.spy;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.verifyNoMoreInteractions;
+import static org.mockito.Mockito.verifyZeroInteractions;
+
+public class RestSwaggerSupportTest {
+
+    @Test
+    public void shouldAdaptFromXForwardHeaders() {
+        final Swagger swagger = spy(new Swagger().basePath("/base"));
+
+        final Map<String, Object> headers = new HashMap<>();
+        headers.put(RestSwaggerSupport.HEADER_X_FORWARDED_PREFIX, "/prefix");
+        headers.put(RestSwaggerSupport.HEADER_X_FORWARDED_HOST, "host");
+        headers.put(RestSwaggerSupport.HEADER_X_FORWARDED_PROTO, "http, HTTPS ");
+        RestSwaggerSupport.setupXForwardedHeaders(swagger, headers);
+
+        verify(swagger).getBasePath();
+        verify(swagger).setBasePath("/prefix/base");
+        verify(swagger).setHost("host");
+        verify(swagger).addScheme(Scheme.HTTP);
+        verify(swagger).addScheme(Scheme.HTTPS);
+        verifyNoMoreInteractions(swagger);
+    }
+
+    @ParameterizedTest
+    @MethodSource("basePathAndPrefixVariations")
+    public void shouldAdaptWithVaryingBasePathsAndPrefixes(final String prefix, final String
basePath,
+        final String expected) {
+        final Swagger swagger = spy(new Swagger().basePath(basePath));
+
+        final Map<String, Object> headers = new HashMap<>();
+        headers.put(RestSwaggerSupport.HEADER_X_FORWARDED_PREFIX, prefix);
+        RestSwaggerSupport.setupXForwardedHeaders(swagger, headers);
+
+        verify(swagger).getBasePath();
+        verify(swagger).setBasePath(expected);
+        verifyNoMoreInteractions(swagger);
+    }
+
+    @ParameterizedTest
+    @MethodSource("schemeVariations")
+    public void shouldAdaptWithVaryingSchemes(final String xForwardedScheme, final Scheme[]
expected) {
+        final Swagger swagger = spy(new Swagger());
+
+        RestSwaggerSupport.setupXForwardedHeaders(swagger,
+            Collections.singletonMap(RestSwaggerSupport.HEADER_X_FORWARDED_PROTO, xForwardedScheme));
+
+        for (final Scheme scheme : expected) {
+            verify(swagger).addScheme(scheme);
+        }
+
+        verifyNoMoreInteractions(swagger);
+    }
+
+    @Test
+    public void shouldNotAdaptFromXForwardHeadersWhenNoHeadersSpecified() {
+        final Swagger swagger = spy(new Swagger());
+
+        RestSwaggerSupport.setupXForwardedHeaders(swagger, Collections.emptyMap());
+
+        verifyZeroInteractions(swagger);
+    }
+
+    static Stream<Arguments> basePathAndPrefixVariations() {
+        return Stream.of(//
+            arguments("/prefix", "/base", "/prefix/base"), //
+            arguments("/prefix", "/base/", "/prefix/base/"), //
+            arguments("/prefix", "base", "/prefix/base"), //
+            arguments("/prefix", "base/", "/prefix/base/"), //
+            arguments("/prefix", "", "/prefix"), //
+            arguments("/prefix", null, "/prefix"), //
+            arguments("/prefix/", "/base", "/prefix/base"), //
+            arguments("/prefix/", "/base/", "/prefix/base/"), //
+            arguments("/prefix/", "base", "/prefix/base"), //
+            arguments("/prefix/", "base/", "/prefix/base/"), //
+            arguments("/prefix/", "", "/prefix/"), //
+            arguments("/prefix/", null, "/prefix/"), //
+            arguments("prefix", "/base", "prefix/base"), //
+            arguments("prefix", "/base/", "prefix/base/"), //
+            arguments("prefix", "base", "prefix/base"), //
+            arguments("prefix", "base/", "prefix/base/"), //
+            arguments("prefix", "", "prefix"), //
+            arguments("prefix", null, "prefix"), //
+            arguments("prefix/", "/base", "prefix/base"), //
+            arguments("prefix/", "/base/", "prefix/base/"), //
+            arguments("prefix/", "base", "prefix/base"), //
+            arguments("prefix/", "base/", "prefix/base/"), //
+            arguments("prefix/", "", "prefix/"), //
+            arguments("prefix/", null, "prefix/") //
+        );
+    }
+
+    static Stream<Arguments> schemeVariations() {
+        final Scheme[] none = new Scheme[0];
+
+        return Stream.of(//
+            arguments(null, none), //
+            arguments("", none), //
+            arguments(",", none), //
+            arguments(" , ", none), //
+            arguments("HTTPS,http", new Scheme[] {Scheme.HTTPS, Scheme.HTTP}), //
+            arguments(" HTTPS,  http ", new Scheme[] {Scheme.HTTPS, Scheme.HTTP}), //
+            arguments(",http,", new Scheme[] {Scheme.HTTP}), //
+            arguments("hTtpS", new Scheme[] {Scheme.HTTPS})//
+        );
+    }
+}


Mime
View raw message