Skip to content

Commit

Permalink
Add more platform-http tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ppalaga committed Oct 23, 2019
1 parent 041c41d commit c13bf45
Show file tree
Hide file tree
Showing 5 changed files with 137 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,7 @@ Message toCamelMessage(RoutingContext ctx, Exchange exchange) {
final Message result = new DefaultMessage(exchange);

final HeaderFilterStrategy headerFilterStrategy = getEndpoint().getHeaderFilterStrategy();
System.out.println("headerFilterStrategy = "+ headerFilterStrategy);
populateCamelHeaders(ctx, result.getHeaders(), exchange, headerFilterStrategy );
final String mimeType = ctx.parsedHeaders().contentType().value();
final boolean isMultipartFormData = "multipart/form-data".equals(mimeType);
Expand Down
1 change: 1 addition & 0 deletions integration-tests/platform-http/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@
<enableJni>true</enableJni>
<enableAllSecurityServices>true</enableAllSecurityServices>
<disableReports>true</disableReports>
<addAllCharsets>true</addAllCharsets><!-- Required by the encoding() test -->
</configuration>
</execution>
</executions>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,13 @@
package org.apache.camel.quarkus.component.platform.http.it;

import java.io.ByteArrayOutputStream;
import java.util.Locale;
import java.util.Map;
import java.util.stream.Collectors;

import javax.activation.DataHandler;

import org.apache.camel.Exchange;
import org.apache.camel.attachment.AttachmentMessage;
import org.apache.camel.builder.RouteBuilder;

Expand All @@ -32,12 +36,14 @@ public void configure() {
.setBody(constant("GET: /rest-get"))
.endRest()
.post("/platform-http/rest-post")
.consumes("text/plain").produces("text/plain")
.route()
.setBody(constant("POST: /rest-post"))
.endRest();

from("platform-http:/platform-http/hello?httpMethodRestrict=GET").setBody(simple("Hello ${header.name}"));
from("platform-http:/platform-http/get-post?httpMethodRestrict=GET,POST").setBody(simple("Hello ${body}"));

from("platform-http:/platform-http/multipart?httpMethodRestrict=POST")
.to("log:multipart")
.process(e -> {
Expand All @@ -48,5 +54,30 @@ public void configure() {
e.getMessage().setBody(out.toByteArray());
}
});

from("platform-http:/platform-http/form-urlencoded?httpMethodRestrict=POST")
.to("log:form-urlencoded")
.setBody(e ->
((Map<String, Object>)e.getMessage().getBody(Map.class)).entrySet().stream()
.map(en -> en.getKey() + "=" + en.getValue().toString().toUpperCase(Locale.US))
.collect(Collectors.joining("\n")));

from("platform-http:/platform-http/header-filter-strategy?httpMethodRestrict=GET&headerFilterStrategy=#TestHeaderFilterStrategy")
.to("log:header-filter-strategy")
.setBody(simple("k1=${header.k1}\nk2=${header.k2}"));

from("platform-http:/platform-http/multi-value-params?httpMethodRestrict=GET")
.to("log:multi-value-params")
.setBody(simple("k1=${header.k1}"));

from("platform-http:/platform-http/encoding?httpMethodRestrict=POST")
.to("log:encoding")
.setBody(e -> e.getMessage().getBody(String.class))
.setHeader("Content-Type").constant("text/plain ; charset=UTF-8");

from("platform-http:/platform-http/response-code-299?httpMethodRestrict=GET")
.to("log:response-code")
.setHeader(Exchange.HTTP_RESPONSE_CODE).constant(299);

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* 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.quarkus.component.platform.http.it;

import javax.enterprise.context.ApplicationScoped;
import javax.inject.Named;

import org.apache.camel.component.platform.http.PlatformHttpHeaderFilterStrategy;

@ApplicationScoped
@Named("TestHeaderFilterStrategy")
public class TestHeaderFilterStrategy extends PlatformHttpHeaderFilterStrategy {

@Override
protected void initialize() {
super.initialize();
getInFilter().add("k1");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,14 @@
import io.restassured.RestAssured;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;

import static org.hamcrest.CoreMatchers.equalTo;

import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;

@QuarkusTest
class PlatformHttpTest {
@Test
Expand Down Expand Up @@ -53,10 +57,21 @@ public void basic() {
public void rest() throws Throwable {
RestAssured.get("/platform-http/rest-get")
.then().body(equalTo("GET: /rest-get"));
RestAssured.post("/platform-http/rest-post")
RestAssured.given()
.contentType("text/plain")
.post("/platform-http/rest-post")
.then().body(equalTo("POST: /rest-post"));
}

@Disabled("See https://github.com/apache/camel-quarkus/issues/326")
@Test
public void restConsumes() throws Throwable {
RestAssured.given()
.contentType("application/json")
.post("/platform-http/rest-post")
.then().statusCode(415);
}

@Test
public void invalidMethod() {
RestAssured.post("/platform-http/hello")
Expand All @@ -81,4 +96,58 @@ public void multipart() {
Assertions.assertArrayEquals(bytes, returnedBytes);
}

@Test
public void formUrlEncoded() {
RestAssured.given().contentType("application/x-www-form-urlencoded")
.formParam("k1", "v1")
.formParam("k2", "v2")
.post("/platform-http/form-urlencoded")
.then()
.statusCode(200)
.body(equalTo("k1=V1\nk2=V2"));
}

@Test
public void customHeaderFilterStrategy() {
RestAssured.given()
.queryParam("k1", "v1")
.queryParam("k2", "v2")
.get("/platform-http/header-filter-strategy")
.then()
.statusCode(200)
.body(equalTo("k1=\nk2=v2")); // k1 filtered out by TestHeaderFilterStrategy
}

@Test
public void multiValueParams() {
RestAssured.given()
.queryParam("k1", "v1")
.queryParam("k1", "v2")
.get("/platform-http/multi-value-params")
.then()
.statusCode(200)
.body(equalTo("k1=[v1, v2]"));
}

@Test
public void encoding() throws UnsupportedEncodingException {
final String outgoingEncoding = "ISO-8859-2";
final String bodyText = "Ťava dvojhrbá"; // Camelus bactrianus in Slovak
final byte[] returnedBytes = RestAssured.given()
.contentType("text/plain; charset="+ outgoingEncoding)
.body(bodyText.getBytes(outgoingEncoding))
.post("/platform-http/encoding")
.then()
.statusCode(200)
.extract().body().asByteArray();
Assertions.assertArrayEquals(bodyText.getBytes(StandardCharsets.UTF_8), returnedBytes);
}

@Test
public void responseCodeViaHeader() throws UnsupportedEncodingException {
RestAssured.given()
.get("/platform-http/response-code-299")
.then()
.statusCode(299);
}
}

0 comments on commit c13bf45

Please sign in to comment.