|
| 1 | +/* |
| 2 | + * Copyright (c) 2025 Oracle and/or its affiliates. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package io.helidon.webserver.grpc; |
| 17 | + |
| 18 | +import java.util.HashSet; |
| 19 | +import java.util.List; |
| 20 | +import java.util.Set; |
| 21 | + |
| 22 | +import io.helidon.http.HeaderNames; |
| 23 | +import io.helidon.http.WritableHeaders; |
| 24 | +import io.helidon.http.http2.Http2Headers; |
| 25 | + |
| 26 | +import io.grpc.Metadata; |
| 27 | +import org.junit.jupiter.api.Test; |
| 28 | + |
| 29 | +import static org.hamcrest.CoreMatchers.hasItem; |
| 30 | +import static org.hamcrest.CoreMatchers.is; |
| 31 | +import static org.hamcrest.MatcherAssert.assertThat; |
| 32 | +import static org.mockito.Mockito.mock; |
| 33 | +import static org.mockito.Mockito.when; |
| 34 | + |
| 35 | +class GrpcHeadersUtilTest { |
| 36 | + |
| 37 | + @Test |
| 38 | + void testUpdateHeaders() { |
| 39 | + Metadata metadata = new Metadata(); |
| 40 | + Metadata.Key<String> key = Metadata.Key.of("cookie", Metadata.ASCII_STRING_MARSHALLER); |
| 41 | + metadata.put(key, "sugar"); |
| 42 | + metadata.put(key, "almond"); |
| 43 | + WritableHeaders<?> headers = WritableHeaders.create(); |
| 44 | + GrpcHeadersUtil.updateHeaders(headers, metadata); |
| 45 | + assertThat(headers.size(), is(2)); |
| 46 | + List<String> values = headers.get(HeaderNames.COOKIE).allValues(); |
| 47 | + assertThat(values, hasItem("sugar")); |
| 48 | + assertThat(values, hasItem("almond")); |
| 49 | + } |
| 50 | + |
| 51 | + @Test |
| 52 | + void testToMetadata() { |
| 53 | + WritableHeaders<?> headers = WritableHeaders.create(); |
| 54 | + headers.add(HeaderNames.COOKIE, "sugar", "almond"); |
| 55 | + Http2Headers http2Headers = mock(Http2Headers.class); |
| 56 | + when(http2Headers.httpHeaders()).thenReturn(headers); |
| 57 | + Metadata metadata = GrpcHeadersUtil.toMetadata(http2Headers); |
| 58 | + Metadata.Key<String> key = Metadata.Key.of("cookie", Metadata.ASCII_STRING_MARSHALLER); |
| 59 | + assertThat(metadata.containsKey(key), is(true)); |
| 60 | + Set<String> values = new HashSet<>(); |
| 61 | + metadata.getAll(key).forEach(values::add); |
| 62 | + assertThat(values, hasItem("sugar")); |
| 63 | + assertThat(values, hasItem("almond")); |
| 64 | + } |
| 65 | +} |
0 commit comments