|
| 1 | +/* |
| 2 | + * Copyright 2025 The Dapr Authors |
| 3 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | + * you may not use this file except in compliance with the License. |
| 5 | + * You may obtain a copy of the License at |
| 6 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | + * Unless required by applicable law or agreed to in writing, software |
| 8 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 9 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 10 | + * See the License for the specific language governing permissions and |
| 11 | +limitations under the License. |
| 12 | +*/ |
| 13 | + |
| 14 | +package io.dapr.client.domain; |
| 15 | + |
| 16 | +import io.dapr.client.DaprHttp; |
| 17 | +import org.junit.jupiter.api.Assertions; |
| 18 | +import org.junit.jupiter.api.DisplayName; |
| 19 | +import org.junit.jupiter.api.Test; |
| 20 | + |
| 21 | +import java.util.List; |
| 22 | +import java.util.Map; |
| 23 | + |
| 24 | +class HttpExtensionTest { |
| 25 | + |
| 26 | + |
| 27 | + @Test |
| 28 | + @DisplayName("Should encode query params correctly") |
| 29 | + void shouldEncodeQueryParamsCorrectly() { |
| 30 | + HttpExtension httpExtension = new HttpExtension(DaprHttp.HttpMethods.GET, |
| 31 | + Map.of("traceparent", List.of("00-4bf92f3577b34da6a3ce929d0e0e4733-00f067aa0ba902b7-01")), Map.of()); |
| 32 | + |
| 33 | + String encoded = httpExtension.encodeQueryString(); |
| 34 | + |
| 35 | + Assertions.assertEquals("traceparent=00-4bf92f3577b34da6a3ce929d0e0e4733-00f067aa0ba902b7-01", encoded); |
| 36 | + } |
| 37 | + |
| 38 | + @Test |
| 39 | + @DisplayName("Should encode multiple values for same query param key") |
| 40 | + void shouldEncodeMultipleValuesForSameQueryParamKey() { |
| 41 | + HttpExtension httpExtension = new HttpExtension(DaprHttp.HttpMethods.GET, |
| 42 | + Map.of("category", List.of("books", "electronics")), Map.of()); |
| 43 | + |
| 44 | + String encoded = httpExtension.encodeQueryString(); |
| 45 | + |
| 46 | + Assertions.assertEquals("category=books&category=electronics", encoded); |
| 47 | + } |
| 48 | + |
| 49 | + @Test |
| 50 | + @DisplayName("Should encode query param with spaces, accents, and special characters") |
| 51 | + void shouldEncodeQueryParamWithSpacesAndSpecialCharacters() { |
| 52 | + HttpExtension httpExtension = new HttpExtension(DaprHttp.HttpMethods.GET, |
| 53 | + Map.of("user name", List.of("John Doë & Co.")), Map.of()); |
| 54 | + |
| 55 | + String encoded = httpExtension.encodeQueryString(); |
| 56 | + |
| 57 | + Assertions.assertEquals("user+name=John+Do%C3%AB+%26+Co.", encoded); |
| 58 | + } |
| 59 | + |
| 60 | +} |
0 commit comments