-
Notifications
You must be signed in to change notification settings - Fork 566
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds support for send(byte[],int,int) to server responses (#9569)
Adds support for send(byte[], int, int) to ServerResponse. Signed-off-by: Santiago Pericas-Geertsen <[email protected]>
- Loading branch information
Showing
6 changed files
with
244 additions
and
26 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
91 changes: 91 additions & 0 deletions
91
webserver/tests/http2/src/test/java/io/helidon/webserver/tests/http2/SendBytesTest.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,91 @@ | ||
/* | ||
* Copyright (c) 2024 Oracle and/or its affiliates. | ||
* | ||
* 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 io.helidon.webserver.tests.http2; | ||
|
||
import java.io.IOException; | ||
import java.net.URI; | ||
import java.net.http.HttpClient; | ||
import java.net.http.HttpRequest; | ||
import java.net.http.HttpResponse; | ||
import java.nio.charset.StandardCharsets; | ||
import java.time.Duration; | ||
|
||
import io.helidon.http.Status; | ||
import io.helidon.webserver.http.HttpRules; | ||
import io.helidon.webserver.testing.junit5.ServerTest; | ||
import io.helidon.webserver.testing.junit5.SetUpRoute; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.hamcrest.CoreMatchers.is; | ||
import static org.hamcrest.MatcherAssert.assertThat; | ||
|
||
@ServerTest | ||
class SendBytesTest { | ||
private static final int START = 16; | ||
private static final int LENGTH = 9; | ||
private static final String ENTITY = "The quick brown fox jumps over the lazy dog"; | ||
|
||
private final HttpClient client; | ||
private final URI uri; | ||
|
||
SendBytesTest(URI uri) { | ||
this.uri = uri; | ||
client = HttpClient.newBuilder() | ||
.version(HttpClient.Version.HTTP_2) | ||
.connectTimeout(Duration.ofSeconds(5)) | ||
.build(); | ||
} | ||
|
||
@SetUpRoute | ||
static void routing(HttpRules rules) { | ||
rules.get("/sendAll", (req, res) -> | ||
res.send(ENTITY.getBytes(StandardCharsets.UTF_8))) | ||
.get("/sendPart", (req, res) -> | ||
res.send(ENTITY.getBytes(StandardCharsets.UTF_8), START, LENGTH)); | ||
} | ||
|
||
/** | ||
* Test getting all the entity. | ||
*/ | ||
@Test | ||
void testAll() throws IOException, InterruptedException { | ||
HttpResponse<String> response = client.send(HttpRequest.newBuilder() | ||
.uri(uri.resolve("/sendAll")) | ||
.GET() | ||
.build(), HttpResponse.BodyHandlers.ofString()); | ||
assertThat(response.statusCode(), is(Status.OK_200.code())); | ||
assertThat(response.version(), is(HttpClient.Version.HTTP_2)); | ||
String entity = response.body(); | ||
assertThat(entity, is(ENTITY)); | ||
} | ||
|
||
/** | ||
* Test getting part of the entity. | ||
*/ | ||
@Test | ||
void testPart() throws IOException, InterruptedException { | ||
HttpResponse<String> response = client.send(HttpRequest.newBuilder() | ||
.uri(uri.resolve("/sendPart")) | ||
.GET() | ||
.build(), HttpResponse.BodyHandlers.ofString()); | ||
assertThat(response.statusCode(), is(Status.OK_200.code())); | ||
assertThat(response.version(), is(HttpClient.Version.HTTP_2)); | ||
String entity = response.body(); | ||
assertThat(entity, is(ENTITY.substring(START, START + LENGTH))); | ||
} | ||
} |
79 changes: 79 additions & 0 deletions
79
webserver/tests/webserver/src/test/java/io/helidon/webserver/tests/SendBytesTest.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,79 @@ | ||
/* | ||
* Copyright (c) 2024 Oracle and/or its affiliates. | ||
* | ||
* 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 io.helidon.webserver.tests; | ||
|
||
import java.nio.charset.StandardCharsets; | ||
|
||
import io.helidon.http.Status; | ||
import io.helidon.webclient.api.HttpClientResponse; | ||
import io.helidon.webclient.http1.Http1Client; | ||
import io.helidon.webserver.http.HttpRules; | ||
import io.helidon.webserver.testing.junit5.ServerTest; | ||
import io.helidon.webserver.testing.junit5.SetUpRoute; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.hamcrest.CoreMatchers.is; | ||
import static org.hamcrest.MatcherAssert.assertThat; | ||
|
||
/** | ||
* Tests sending a part of a byte array. | ||
*/ | ||
@ServerTest | ||
class SendBytesTest { | ||
private static final int START = 16; | ||
private static final int LENGTH = 9; | ||
private static final String ENTITY = "The quick brown fox jumps over the lazy dog"; | ||
|
||
private final Http1Client http1Client; | ||
|
||
SendBytesTest(Http1Client http1Client) { | ||
this.http1Client = http1Client; | ||
} | ||
|
||
@SetUpRoute | ||
static void routing(HttpRules rules) { | ||
rules.get("/sendAll", (req, res) -> | ||
res.send(ENTITY.getBytes(StandardCharsets.UTF_8))) | ||
.get("/sendPart", (req, res) -> | ||
res.send(ENTITY.getBytes(StandardCharsets.UTF_8), START, LENGTH)); | ||
} | ||
|
||
/** | ||
* Test getting all the entity. | ||
*/ | ||
@Test | ||
void testAll() { | ||
try (HttpClientResponse r = http1Client.get("/sendAll").request()) { | ||
String s = r.entity().as(String.class); | ||
assertThat(r.status(), is(Status.OK_200)); | ||
assertThat(s, is(ENTITY)); | ||
} | ||
} | ||
|
||
/** | ||
* Test getting part of the entity. | ||
*/ | ||
@Test | ||
void testPart() { | ||
try (HttpClientResponse r = http1Client.get("/sendPart").request()) { | ||
String s = r.entity().as(String.class); | ||
assertThat(r.status(), is(Status.OK_200)); | ||
assertThat(s, is(ENTITY.substring(START, START + LENGTH))); | ||
} | ||
} | ||
} |
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