Skip to content

Commit 8c01766

Browse files
author
Tim te Beek
committed
Add tests for Checksum hex validation and HTTP error handling
1 parent 2ae43a8 commit 8c01766

1 file changed

Lines changed: 68 additions & 0 deletions

File tree

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
2+
* Copyright 2025 the original author or authors.
3+
* <p>
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+
* <p>
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
* <p>
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 org.openrewrite;
17+
18+
import org.junit.jupiter.api.Test;
19+
import org.openrewrite.ipc.http.HttpSender;
20+
21+
import java.io.ByteArrayInputStream;
22+
import java.io.UncheckedIOException;
23+
import java.net.URI;
24+
import java.nio.charset.StandardCharsets;
25+
26+
import static org.assertj.core.api.Assertions.assertThat;
27+
import static org.assertj.core.api.Assertions.assertThatThrownBy;
28+
29+
class ChecksumTest {
30+
31+
@Test
32+
void fromHexWithValidInput() {
33+
Checksum checksum = Checksum.fromHex("SHA-256", "abcdef01");
34+
assertThat(checksum.getHexValue()).isEqualTo("abcdef01");
35+
}
36+
37+
@Test
38+
void fromHexRejectsNonHexInput() {
39+
assertThatThrownBy(() -> Checksum.fromHex("SHA-256", "<?xml version"))
40+
.isInstanceOf(IllegalArgumentException.class)
41+
.hasMessageContaining("not valid hexadecimal");
42+
}
43+
44+
@Test
45+
void fromUriRejectsNonSuccessfulResponse() {
46+
HttpSender httpSender = request -> new HttpSender.Response(
47+
403,
48+
new ByteArrayInputStream("<?xml version=\"1.0\"?>".getBytes(StandardCharsets.UTF_8)),
49+
() -> {
50+
});
51+
52+
assertThatThrownBy(() -> Checksum.fromUri(httpSender, URI.create("https://example.com/checksum.sha256")))
53+
.isInstanceOf(UncheckedIOException.class)
54+
.hasMessageContaining("403");
55+
}
56+
57+
@Test
58+
void fromUriTrimsWhitespace() {
59+
HttpSender httpSender = request -> new HttpSender.Response(
60+
200,
61+
new ByteArrayInputStream("abcdef01\n".getBytes(StandardCharsets.UTF_8)),
62+
() -> {
63+
});
64+
65+
Checksum checksum = Checksum.fromUri(httpSender, URI.create("https://example.com/checksum.sha256"));
66+
assertThat(checksum.getHexValue()).isEqualTo("abcdef01");
67+
}
68+
}

0 commit comments

Comments
 (0)