Skip to content

Commit

Permalink
[CXF-8965] Apache CXF Netty Integration, URI not encoded (#1552)
Browse files Browse the repository at this point in the history
* [CXF-8965] Apache CXF Netty Integration, URI not encoded

* [CXF-8965] Apache CXF Netty Integration, URI not encoded

* Add integration test cases for encoded HTTP paths

---------

Co-authored-by: Andriy Redko <drreta@gmail.com>
  • Loading branch information
wilhelmjochen and reta authored Dec 6, 2023
1 parent 6527215 commit fce94b8
Show file tree
Hide file tree
Showing 6 changed files with 115 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public void createRequest(ByteBuf content) {
this.request =
new DefaultFullHttpRequest(HttpVersion.HTTP_1_1,
HttpMethod.valueOf(method),
uri.getPath(), content);
uri.getRawPath(), content);
// setup the default headers
request.headers().set("Connection", "keep-alive");
request.headers().set("Host", uri.getHost() + ":"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/**
* 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.cxf.transport.http.netty.client;

import java.net.URI;
import java.net.URISyntaxException;

import io.netty.buffer.ByteBufAllocator;
import io.netty.buffer.EmptyByteBuf;
import io.netty.handler.codec.http.HttpRequest;

import org.junit.Assert;
import org.junit.Test;

public class NettyHttpClientRequestTest {

@Test
public void testCreateRequest() throws URISyntaxException {

URI uri = new URI("http://localhost:8080/my%20path");
NettyHttpClientRequest nettyHttpClientRequest = new NettyHttpClientRequest(uri, "GET", false);
nettyHttpClientRequest.createRequest(new EmptyByteBuf(ByteBufAllocator.DEFAULT));

HttpRequest httpRequest = nettyHttpClientRequest.getRequest();
Assert.assertEquals("/my%20path", httpRequest.uri());
Assert.assertEquals("/my path", uri.getPath());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,21 @@ public void testBookWithHttp2() throws Exception {
client.close();
}

@Test
public void testBookEncodedWithHttp2() throws Exception {
final WebClient client = createWebClient("/web/bookstore/book%20names", true);
assertThat(WebClient.getConfig(client).getHttpConduit(), instanceOf(AsyncHTTPConduit.class));

final Response response = client
.accept("text/plain")
.get();

assertThat(response.getStatus(), equalTo(200));
assertEquals("CXF in Action", response.readEntity(String.class));

client.close();
}

@Test
public void testGetBookStreamHttp2() throws Exception {
final WebClient client = createWebClient("/web/bookstore/bookstream", true);
Expand Down Expand Up @@ -106,6 +121,18 @@ public void testBookWithHttp() throws Exception {
client.close();
}

@Test
public void testBookEncodedWithHttp() throws Exception {
final WebClient client = createWebClient("/web/bookstore/book%20names", false);

try (Response resp = client.get()) {
assertThat(resp.getStatus(), equalTo(200));
assertEquals("CXF in Action", resp.readEntity(String.class));
}

client.close();
}

@Test
public void testBookTraceWithHttp() throws Exception {
final WebClient client = createWebClient("/web/bookstore/trace", false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,14 @@ public class BookStore {
@GET
@Path("/booknames")
@Produces("text/plain")
public byte[] getBookName() {
public byte[] getBookNames() {
return "CXF in Action".getBytes();
}

@GET
@Path("/book names")
@Produces("text/plain")
public byte[] getBookNamesEncoded() {
return "CXF in Action".getBytes();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,21 @@ public void testBookWithHttp2() throws Exception {
client.close();
}

@Test
public void testBookEncodedWithHttp2() throws Exception {
final WebClient client = createWebClient("/web/bookstore/book%20names", true);
assertThat(WebClient.getConfig(client).getHttpConduit(), instanceOf(NettyHttpConduit.class));

final Response response = client
.accept("text/plain")
.get();

assertThat(response.getStatus(), equalTo(200));
assertEquals("CXF in Action", response.readEntity(String.class));

client.close();
}

@Test
public void testGetBookStreamHttp2() throws Exception {
final WebClient client = createWebClient("/web/bookstore/bookstream", true);
Expand Down Expand Up @@ -105,6 +120,18 @@ public void testBookWithHttp() throws Exception {
client.close();
}

@Test
public void testBookEncodedWithHttp() throws Exception {
final WebClient client = createWebClient("/web/bookstore/book%20names", false);

try (Response resp = client.get()) {
assertThat(resp.getStatus(), equalTo(200));
assertEquals("CXF in Action", resp.readEntity(String.class));
}

client.close();
}

@Test
public void testBookTraceWithHttp() throws Exception {
final WebClient client = createWebClient("/web/bookstore/trace", false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,14 @@ public class BookStore {
@GET
@Path("/booknames")
@Produces("text/plain")
public byte[] getBookName() {
public byte[] getBookNames() {
return "CXF in Action".getBytes();
}

@GET
@Path("/book names")
@Produces("text/plain")
public byte[] getBookNamesEncoded() {
return "CXF in Action".getBytes();
}

Expand Down

0 comments on commit fce94b8

Please sign in to comment.