This repository has been archived by the owner on Dec 31, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 82
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
63 delegation leads to unexpected result (#122)
* fix test * fix map argument handling --------- Co-authored-by: Artem Labazin <[email protected]>
- Loading branch information
Showing
6 changed files
with
250 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
feign-form/src/test/java/feign/form/issues/Issue63Test.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/* | ||
* Copyright 2024 the original author or authors. | ||
* | ||
* Licensed 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 feign.form.issues; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import io.undertow.server.HttpServerExchange; | ||
import lombok.val; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import feign.Feign; | ||
import feign.Headers; | ||
import feign.RequestLine; | ||
import feign.form.FormEncoder; | ||
import feign.form.utils.UndertowServer; | ||
import feign.jackson.JacksonEncoder; | ||
|
||
// https://github.com/OpenFeign/feign-form/issues/63 | ||
class Issue63Test { | ||
|
||
@Test | ||
void test () { | ||
try (val server = UndertowServer.builder().callback(this::handleRequest).start()) { | ||
val client = Feign.builder() | ||
.encoder(new FormEncoder(new JacksonEncoder())) | ||
.target(Client.class, server.getConnectUrl()); | ||
|
||
val data = new HashMap<String, String>(); | ||
data.put("from", "+987654321"); | ||
data.put("to", "+123456789"); | ||
data.put("body", "hello world"); | ||
|
||
assertThat(client.map(data)) | ||
.isEqualTo("ok"); | ||
} | ||
} | ||
|
||
private void handleRequest (HttpServerExchange exchange, byte[] message) { | ||
// assert request | ||
assertThat(exchange.getRequestHeaders().getFirst(io.undertow.util.Headers.CONTENT_TYPE)) | ||
.isEqualTo("application/x-www-form-urlencoded; charset=UTF-8"); | ||
assertThat(message) | ||
.asString() | ||
.isEqualTo("from=%2B987654321&to=%2B123456789&body=hello+world"); | ||
|
||
// build response | ||
exchange.getResponseHeaders() | ||
.put(io.undertow.util.Headers.CONTENT_TYPE, "text/plain"); | ||
exchange.getResponseSender() | ||
.send("ok"); | ||
} | ||
|
||
interface Client { | ||
|
||
@RequestLine("POST") | ||
@Headers("Content-Type: application/x-www-form-urlencoded; charset=utf-8") | ||
String map (Map<String, String> data); | ||
} | ||
} |
101 changes: 101 additions & 0 deletions
101
feign-form/src/test/java/feign/form/utils/UndertowServer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
/* | ||
* Copyright 2024 the original author or authors. | ||
* | ||
* Licensed 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 feign.form.utils; | ||
|
||
import java.net.InetSocketAddress; | ||
|
||
import io.appulse.utils.SocketUtils; | ||
import io.undertow.Undertow; | ||
import io.undertow.io.Receiver.FullBytesCallback; | ||
import io.undertow.server.HttpHandler; | ||
import io.undertow.server.HttpServerExchange; | ||
import io.undertow.server.handlers.BlockingHandler; | ||
import io.undertow.util.Headers; | ||
import lombok.Builder; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.val; | ||
|
||
public final class UndertowServer implements AutoCloseable { | ||
|
||
private final Undertow undertow; | ||
|
||
@Builder(buildMethodName = "start") | ||
private UndertowServer (FullBytesCallback callback) { | ||
val port = SocketUtils.findFreePort() | ||
.orElseThrow(() -> new IllegalStateException("no available port to start server")); | ||
|
||
undertow = Undertow.builder() | ||
.addHttpListener(port, "localhost") | ||
.setHandler( | ||
new BlockingHandler( | ||
new ReadAllBytesHandler(callback) | ||
) | ||
) | ||
.build(); | ||
|
||
undertow.start(); | ||
} | ||
|
||
/** | ||
* Returns server connect URL. | ||
* | ||
* @return listining server's url. | ||
*/ | ||
public String getConnectUrl () { | ||
val listenerInfo = undertow.getListenerInfo() | ||
.iterator() | ||
.next(); | ||
|
||
val address = (InetSocketAddress) listenerInfo.getAddress(); | ||
|
||
return String.format( | ||
"%s://%s:%d", | ||
listenerInfo.getProtcol(), | ||
address.getHostString(), | ||
address.getPort() | ||
); | ||
} | ||
|
||
@Override | ||
public void close () { | ||
undertow.stop(); | ||
} | ||
|
||
@RequiredArgsConstructor | ||
private static final class ReadAllBytesHandler implements HttpHandler { | ||
|
||
private final FullBytesCallback callback; | ||
|
||
@Override | ||
public void handleRequest (HttpServerExchange exchange) throws Exception { | ||
exchange.getRequestReceiver() | ||
.receiveFullBytes(this::handleBytes); | ||
} | ||
|
||
private void handleBytes (HttpServerExchange exchange, byte[] message) { | ||
try { | ||
callback.handle(exchange, message); | ||
} catch (Throwable ex) { | ||
exchange.setStatusCode(500); | ||
exchange.getResponseHeaders() | ||
.put(Headers.CONTENT_TYPE, "text/plain"); | ||
exchange.getResponseSender() | ||
.send(ex.getMessage()); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters