-
Notifications
You must be signed in to change notification settings - Fork 167
Optimize isCloudEventsHeader check
#445
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| /* | ||
| * Copyright 2018-Present The CloudEvents Authors | ||
| * <p> | ||
| * 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 | ||
| * <p> | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * <p> | ||
| * 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.cloudevents.core.impl; | ||
|
|
||
| import javax.annotation.Nonnull; | ||
|
|
||
| final public class StringUtils { | ||
|
|
||
| private StringUtils() { | ||
| // Prevent construction. | ||
| } | ||
|
|
||
| public static boolean startsWithIgnoreCase(@Nonnull final String s, @Nonnull final String prefix) { | ||
| return s.regionMatches(true /* ignoreCase */, 0, prefix, 0, prefix.length()); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| /* | ||
| * Copyright 2018-Present The CloudEvents Authors | ||
| * <p> | ||
| * 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 | ||
| * <p> | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * <p> | ||
| * 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.cloudevents.core.impl; | ||
|
|
||
| import org.assertj.core.api.Assertions; | ||
| import org.junit.jupiter.params.ParameterizedTest; | ||
| import org.junit.jupiter.params.provider.Arguments; | ||
| import org.junit.jupiter.params.provider.MethodSource; | ||
|
|
||
| import java.util.stream.Stream; | ||
|
|
||
| public class StringUtilsTest { | ||
|
|
||
| @ParameterizedTest | ||
| @MethodSource("startsWithIgnoreCaseArgs") | ||
| public void startsWithIgnoreCase(final String s, final String prefix, final boolean expected) { | ||
| Assertions.assertThat(StringUtils.startsWithIgnoreCase(s, prefix)).isEqualTo(expected); | ||
| } | ||
|
|
||
| private static Stream<Arguments> startsWithIgnoreCaseArgs() { | ||
| return Stream.of( | ||
| Arguments.of("s", "s", true), | ||
| Arguments.of("sa", "S", true), | ||
| Arguments.of("saS", "As", false), | ||
| Arguments.of("sasso", "SASO", false), | ||
| Arguments.of("sasso", "SaSsO", true) | ||
| ); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -44,7 +44,7 @@ protected boolean isContentTypeHeader(String key) { | |
|
|
||
| @Override | ||
| protected boolean isCloudEventsHeader(String key) { | ||
| return key.length() > 3 && key.substring(0, KafkaHeaders.CE_PREFIX.length()).startsWith(KafkaHeaders.CE_PREFIX); | ||
| return key.length() > KafkaHeaders.CE_PREFIX.length() && key.startsWith(KafkaHeaders.CE_PREFIX); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Any particular reason this isn't using your new method ? Also ... Might be worth adding the null-check on
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
it wasn't case-insensitive before like other ones and I think the reason is that other methods are HTTP based (headers normalization) vs this one being Kafka based. Do you know if the spec is case-insensitive for every defined protocol binding? Honestly, from a user perspective, I would be surprised if a header isn't considered a CE header due to the case, so I agree with you.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hey @JemDay since most of the comments aren't strictly related to the original goal of the PR and the code is strictly equivalent to the existing one, I think, we can follow up on these comments in a different PR/issue. |
||
| } | ||
|
|
||
| @Override | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.