-
Notifications
You must be signed in to change notification settings - Fork 564
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Special handling for identity compressor to ensure compress flag is not turned on. Identity compression is a special case, and for compatibility it should not be treated as a normal compressor. - If a gRPC service is not found, include ":status" header in response to comply with gRPC spec - A few null checks were missing in GrpcRouteHandler - New tests for all the changes above
- Loading branch information
Showing
8 changed files
with
278 additions
and
42 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
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
70 changes: 70 additions & 0 deletions
70
webserver/grpc/src/test/java/io/helidon/webserver/grpc/GrpcProtocolHandlerNotFoundTest.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,70 @@ | ||
/* | ||
* 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.grpc; | ||
|
||
import io.helidon.http.Status; | ||
import io.helidon.http.http2.FlowControl; | ||
import io.helidon.http.http2.Http2Flag; | ||
import io.helidon.http.http2.Http2FrameData; | ||
import io.helidon.http.http2.Http2Headers; | ||
import io.helidon.http.http2.Http2StreamState; | ||
import io.helidon.http.http2.Http2StreamWriter; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.hamcrest.CoreMatchers.is; | ||
|
||
class GrpcProtocolHandlerNotFoundTest { | ||
|
||
private boolean validateHeaders; | ||
|
||
@Test | ||
void testNotFoundHeaders() { | ||
Http2StreamWriter writer = new Http2StreamWriter() { | ||
@Override | ||
public void write(Http2FrameData frame) { | ||
throw new UnsupportedOperationException("Unsupported"); | ||
} | ||
|
||
@Override | ||
public void writeData(Http2FrameData frame, FlowControl.Outbound flowControl) { | ||
throw new UnsupportedOperationException("Unsupported"); | ||
|
||
} | ||
|
||
@Override | ||
public int writeHeaders(Http2Headers headers, int streamId, Http2Flag.HeaderFlags flags, FlowControl.Outbound flowControl) { | ||
validateHeaders = (headers.status() == Status.NOT_FOUND_404); | ||
try { | ||
headers.validateResponse(); | ||
} catch (Exception e) { | ||
validateHeaders = false; | ||
} | ||
return 0; | ||
} | ||
|
||
@Override | ||
public int writeHeaders(Http2Headers headers, int streamId, Http2Flag.HeaderFlags flags, Http2FrameData dataFrame, FlowControl.Outbound flowControl) { | ||
throw new UnsupportedOperationException("Unsupported"); | ||
} | ||
}; | ||
GrpcProtocolHandlerNotFound handler = new GrpcProtocolHandlerNotFound(writer, 1, Http2StreamState.OPEN); | ||
assertThat(validateHeaders, is(false)); | ||
handler.init(); | ||
assertThat(validateHeaders, is(true)); | ||
} | ||
} |
69 changes: 69 additions & 0 deletions
69
webserver/grpc/src/test/java/io/helidon/webserver/grpc/GrpcProtocolHandlerTest.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,69 @@ | ||
/* | ||
* 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.grpc; | ||
|
||
import io.helidon.http.HeaderName; | ||
import io.helidon.http.HeaderNames; | ||
import io.helidon.http.WritableHeaders; | ||
import io.helidon.http.http2.Http2Headers; | ||
import io.helidon.http.http2.Http2Settings; | ||
import io.helidon.http.http2.Http2StreamState; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.hamcrest.CoreMatchers.is; | ||
|
||
class GrpcProtocolHandlerTest { | ||
|
||
private static final HeaderName GRPC_ACCEPT_ENCODING = HeaderNames.create("grpc-accept-encoding"); | ||
|
||
@Test | ||
@SuppressWarnings("unchecked") | ||
void testIdentityCompressorFlag() { | ||
WritableHeaders<?> headers = WritableHeaders.create(); | ||
headers.add(GRPC_ACCEPT_ENCODING, "identity"); | ||
GrpcProtocolHandler handler = new GrpcProtocolHandler(null, | ||
Http2Headers.create(headers), | ||
null, | ||
1, | ||
Http2Settings.builder().build(), | ||
Http2Settings.builder().build(), | ||
null, | ||
Http2StreamState.OPEN, | ||
null); | ||
handler.initCompression(null, headers); | ||
assertThat(handler.isIdentityCompressor(), is(true)); | ||
} | ||
|
||
@Test | ||
@SuppressWarnings("unchecked") | ||
void testGzipCompressor() { | ||
WritableHeaders<?> headers = WritableHeaders.create(); | ||
headers.add(GRPC_ACCEPT_ENCODING, "gzip"); | ||
GrpcProtocolHandler handler = new GrpcProtocolHandler(null, | ||
Http2Headers.create(headers), | ||
null, | ||
1, | ||
Http2Settings.builder().build(), | ||
Http2Settings.builder().build(), | ||
null, | ||
Http2StreamState.OPEN, | ||
null); | ||
handler.initCompression(null, headers); | ||
assertThat(handler.isIdentityCompressor(), is(false)); | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
webserver/grpc/src/test/java/io/helidon/webserver/grpc/GrpcRouteHandlerTest.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,33 @@ | ||
/* | ||
* 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.grpc; | ||
|
||
import com.google.protobuf.Descriptors; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
|
||
public class GrpcRouteHandlerTest { | ||
|
||
@Test | ||
void testBadServiceNames() throws Descriptors.DescriptorValidationException { | ||
assertThrows(IllegalArgumentException.class, | ||
() -> GrpcRouteHandler.unary(Strings.getDescriptor(), "foo", "Upper", null)); | ||
assertThrows(IllegalArgumentException.class, | ||
() -> GrpcRouteHandler.unary(Strings.getDescriptor(), "StringService", "foo", null)); | ||
} | ||
} |
Oops, something went wrong.