forked from opensearch-project/OpenSearch
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Craig Perkins <[email protected]>
- Loading branch information
Showing
2 changed files
with
75 additions
and
1 deletion.
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
74 changes: 74 additions & 0 deletions
74
...nsport-netty4/src/test/java/org/opensearch/http/netty4/Netty4DefaultHttpRequestTests.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,74 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
*/ | ||
|
||
package org.opensearch.http.netty4; | ||
|
||
import org.opensearch.core.common.bytes.BytesArray; | ||
import org.opensearch.core.common.bytes.BytesReference; | ||
import org.opensearch.test.OpenSearchTestCase; | ||
|
||
import java.nio.charset.StandardCharsets; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
import io.netty.buffer.ByteBuf; | ||
import io.netty.buffer.Unpooled; | ||
import io.netty.handler.codec.http.DefaultFullHttpRequest; | ||
import io.netty.handler.codec.http.DefaultHttpHeaders; | ||
import io.netty.handler.codec.http.DefaultHttpRequest; | ||
import io.netty.handler.codec.http.FullHttpRequest; | ||
import io.netty.handler.codec.http.HttpHeaders; | ||
import io.netty.handler.codec.http.HttpMethod; | ||
import io.netty.handler.codec.http.HttpVersion; | ||
|
||
public class Netty4DefaultHttpRequestTests extends OpenSearchTestCase { | ||
public void testDefaultHttpRequestHasEmptyContent() throws Exception { | ||
HttpHeaders testHeaders = new DefaultHttpHeaders(); | ||
testHeaders.add("test-header", "test-value"); | ||
DefaultHttpRequest request = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "https://localhost:9200"); | ||
Netty4DefaultHttpRequest netty4Request = new Netty4DefaultHttpRequest(request); | ||
|
||
assertEquals("Expected content to be empty", BytesArray.EMPTY, netty4Request.content()); | ||
} | ||
|
||
public void testDefaultHttpRequestAndFullHttpRequestHaveSameStructure() throws Exception { | ||
HttpHeaders testHeaders = new DefaultHttpHeaders(); | ||
testHeaders.add("test-header", "test-value"); | ||
DefaultHttpRequest request = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "https://localhost:9200", testHeaders); | ||
Netty4DefaultHttpRequest netty4Request = new Netty4DefaultHttpRequest(request); | ||
|
||
ByteBuf expectedByteBuf = Unpooled.copiedBuffer("Hello, World!".getBytes(StandardCharsets.UTF_8)); | ||
BytesReference expectedBytesArray = new BytesArray("Hello, World!".getBytes(StandardCharsets.UTF_8)); | ||
|
||
HttpHeaders trailingHeaders = new DefaultHttpHeaders(); | ||
FullHttpRequest fullRequest = new DefaultFullHttpRequest( | ||
HttpVersion.HTTP_1_1, | ||
HttpMethod.GET, | ||
"https://localhost:9200", | ||
expectedByteBuf, | ||
testHeaders, | ||
trailingHeaders | ||
); | ||
Netty4HttpRequest fullNetty4Request = new Netty4HttpRequest(fullRequest); | ||
|
||
assertEquals("Expected methods to be equal", netty4Request.method(), fullNetty4Request.method()); | ||
assertEquals("Expected URIs to be equal", netty4Request.uri(), fullNetty4Request.uri()); | ||
assertEquals("Expected Protocol Versions to be equal", netty4Request.protocolVersion(), fullNetty4Request.protocolVersion()); | ||
|
||
assertEquals("Expected headers to be of equal length", netty4Request.headers.size(), fullNetty4Request.headers.size()); | ||
for (String header : netty4Request.headers.keySet()) { | ||
assertTrue(fullNetty4Request.headers.containsKey(header)); | ||
List<String> headerValue = netty4Request.headers.get(header).stream().sorted().collect(Collectors.toList()); | ||
List<String> otherValue = fullNetty4Request.headers.get(header).stream().sorted().collect(Collectors.toList()); | ||
assertEquals(headerValue, otherValue); | ||
} | ||
|
||
assertEquals("Expected content to be empty", BytesArray.EMPTY, netty4Request.content()); | ||
assertEquals("Expected content to not be empty", expectedBytesArray, fullNetty4Request.content()); | ||
} | ||
} |