Skip to content

Commit 753a543

Browse files
committed
test(presignedUrl): 테스트 추가
1 parent b20087d commit 753a543

File tree

2 files changed

+66
-1
lines changed

2 files changed

+66
-1
lines changed

src/main/java/com/somemore/global/imageupload/service/PresignedUrl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public String removeQueryString() {
1919
}
2020

2121
public static PresignedUrl from(String url) {
22-
if (url.contains("?")) {
22+
if (url != null && url.contains("?")) {
2323
return new PresignedUrl(url);
2424
}
2525
log.error("PresignedUrl({})이 올바르지 않은 형식입니다.", url);
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package com.somemore.global.imageupload.service;
2+
3+
import com.somemore.global.exception.ImageUploadException;
4+
import org.junit.jupiter.api.DisplayName;
5+
import org.junit.jupiter.api.Test;
6+
7+
import static com.somemore.global.exception.ExceptionMessage.UPLOAD_FAILED;
8+
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
9+
import static org.junit.jupiter.api.Assertions.assertEquals;
10+
import static org.junit.jupiter.api.Assertions.assertThrows;
11+
12+
class PresignedUrlTest {
13+
14+
@Test
15+
@DisplayName("쿼리 문자열이 포함된 URL에서 removeQueryString() 호출 시 쿼리 문자열 제거 후 반환")
16+
void removeQueryString_shouldReturnUrlWithoutQuery() {
17+
// given
18+
String url = "https://example.com/file.png?param=value";
19+
PresignedUrl presignedUrl = PresignedUrl.from(url);
20+
21+
// when
22+
String result = presignedUrl.removeQueryString();
23+
24+
// then
25+
assertEquals("https://example.com/file.png", result);
26+
}
27+
28+
@Test
29+
@DisplayName("쿼리 문자열이 없는 URL을 PresignedUrl.from()으로 생성하면 예외 발생")
30+
void from_shouldThrowException_whenUrlHasNoQueryString() {
31+
// given
32+
String url = "https://example.com/file.png";
33+
34+
// when
35+
// then
36+
ImageUploadException exception = assertThrows(ImageUploadException.class,
37+
() -> PresignedUrl.from(url));
38+
39+
assertEquals(UPLOAD_FAILED.getMessage(), exception.getMessage());
40+
}
41+
42+
@Test
43+
@DisplayName("null 값을 PresignedUrl.from()으로 전달하면 예외 발생")
44+
void from_shouldThrowException_whenUrlIsNull() {
45+
// given
46+
String url = null;
47+
48+
// when
49+
// then
50+
assertThrows(ImageUploadException.class,
51+
() -> PresignedUrl.from(url));
52+
}
53+
54+
@Test
55+
@DisplayName("removeQueryString() 호출 시 올바르게 동작하고 예외가 발생하지 않음")
56+
void removeQueryString_shouldWorkCorrectly() {
57+
// given
58+
String url = "https://example.com/file.png?param=value";
59+
PresignedUrl presignedUrl = PresignedUrl.from(url);
60+
61+
// when
62+
// then
63+
assertDoesNotThrow(presignedUrl::removeQueryString);
64+
}
65+
}

0 commit comments

Comments
 (0)