|
| 1 | +/* |
| 2 | + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 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 | + * A copy of the License is located at |
| 7 | + * |
| 8 | + * http://aws.amazon.com/apache2.0 |
| 9 | + * |
| 10 | + * or in the "license" file accompanying this file. This file is distributed |
| 11 | + * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either |
| 12 | + * express or implied. See the License for the specific language governing |
| 13 | + * permissions and limitations under the License. |
| 14 | + */ |
| 15 | + |
| 16 | +package software.amazon.awssdk.auth.signer.internal; |
| 17 | + |
| 18 | +import static org.assertj.core.api.Assertions.assertThat; |
| 19 | +import java.util.Arrays; |
| 20 | +import java.util.LinkedHashMap; |
| 21 | +import java.util.Map; |
| 22 | +import java.util.Random; |
| 23 | +import org.junit.BeforeClass; |
| 24 | +import org.junit.Test; |
| 25 | +import software.amazon.awssdk.utils.BinaryUtils; |
| 26 | +import software.amazon.eventstream.HeaderValue; |
| 27 | +import software.amazon.eventstream.Message; |
| 28 | + |
| 29 | +public class BaseEventStreamAsyncAws4SignerTest { |
| 30 | + private static Map<String, HeaderValue> headers; |
| 31 | + |
| 32 | + @BeforeClass |
| 33 | + public static void setup() { |
| 34 | + headers = new LinkedHashMap<>(); |
| 35 | + headers.put("header1", HeaderValue.fromInteger(42)); |
| 36 | + headers.put("header2", HeaderValue.fromBoolean(false)); |
| 37 | + headers.put("header3", HeaderValue.fromString("Hello world")); |
| 38 | + } |
| 39 | + |
| 40 | + @Test |
| 41 | + public void toDebugString_emptyPayload_generatesCorrectString() { |
| 42 | + Message m = new Message(headers, new byte[0]); |
| 43 | + |
| 44 | + assertThat(BaseEventStreamAsyncAws4Signer.toDebugString(m, false)) |
| 45 | + .isEqualTo("Message = {headers={header1={42}, header2={false}, header3={\"Hello world\"}}, payload=}"); |
| 46 | + } |
| 47 | + |
| 48 | + @Test |
| 49 | + public void toDebugString_noHeaders_emptyPayload_generatesCorrectString() { |
| 50 | + Message m = new Message(new LinkedHashMap<>(), new byte[0]); |
| 51 | + |
| 52 | + assertThat(BaseEventStreamAsyncAws4Signer.toDebugString(m, false)) |
| 53 | + .isEqualTo("Message = {headers={}, payload=}"); |
| 54 | + } |
| 55 | + |
| 56 | + @Test |
| 57 | + public void toDebugString_largePayload_truncate_generatesCorrectString() { |
| 58 | + byte[] payload = new byte[128]; |
| 59 | + new Random().nextBytes(payload); |
| 60 | + Message m = new Message(headers, payload); |
| 61 | + |
| 62 | + byte[] first32 = Arrays.copyOf(payload, 32); |
| 63 | + String expectedPayloadString = BinaryUtils.toHex(first32); |
| 64 | + assertThat(BaseEventStreamAsyncAws4Signer.toDebugString(m, true)) |
| 65 | + .isEqualTo("Message = {headers={header1={42}, header2={false}, header3={\"Hello world\"}}, payload=" + expectedPayloadString + "}"); |
| 66 | + } |
| 67 | + |
| 68 | + @Test |
| 69 | + public void toDebugString_largePayload_noTruncate_generatesCorrectString() { |
| 70 | + byte[] payload = new byte[128]; |
| 71 | + new Random().nextBytes(payload); |
| 72 | + Message m = new Message(headers, payload); |
| 73 | + |
| 74 | + String expectedPayloadString = BinaryUtils.toHex(payload); |
| 75 | + assertThat(BaseEventStreamAsyncAws4Signer.toDebugString(m, false)) |
| 76 | + .isEqualTo("Message = {headers={header1={42}, header2={false}, header3={\"Hello world\"}}, payload=" + expectedPayloadString + "}"); |
| 77 | + } |
| 78 | +} |
0 commit comments