Skip to content

Commit

Permalink
feat: MockWebServer 일부 테스트 코드 개선 (#69) (#70)
Browse files Browse the repository at this point in the history
  • Loading branch information
seokjin8678 authored Jan 3, 2024
1 parent 2098cfa commit a0c7515
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ class DiscordOAuth2ClientTest : DescribeSpec({
timeoutDuration = Duration.ofSeconds(10)
)

afterEach {
mockWebServer.shutdown()
}

describe("getAccessToken") {
val response = DiscordAccessTokenResponse(
accessToken = "123123",
Expand Down Expand Up @@ -100,7 +104,7 @@ class DiscordOAuth2ClientTest : DescribeSpec({
mockWebServer.enqueue {
statusCode(200)
body(response)
delay(10, TimeUnit.SECONDS)
delay(200, TimeUnit.MILLISECONDS)
}

it("InternalServerError 예외를 던진다.") {
Expand Down Expand Up @@ -176,7 +180,7 @@ class DiscordOAuth2ClientTest : DescribeSpec({
mockWebServer.enqueue {
statusCode(200)
body(response)
delay(10, TimeUnit.SECONDS)
delay(200, TimeUnit.MILLISECONDS)
}

it("InternalServerError 예외를 던진다.") {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class MockWebServerDsl {

private var statusCode: Int? = null
private var body: String? = null
private var mediaType: String? = null
private var mediaType: MediaType? = null
private var delay: Long? = null
private var timeUnit: TimeUnit? = null

Expand All @@ -24,7 +24,7 @@ class MockWebServerDsl {
}

fun mediaType(mediaType: MediaType) {
this.mediaType = mediaType.toString()
this.mediaType = mediaType
}

fun delay(delay: Long, timeUnit: TimeUnit) {
Expand All @@ -35,12 +35,13 @@ class MockWebServerDsl {
internal fun perform(mockWebServer: MockWebServer) {
val response = MockResponse()
statusCode?.also { response.setResponseCode(it) }
body?.also { response.setBody(it) }
body?.also {
response.setBody(it)
response.setHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
}
delay?.also { response.setBodyDelay(it, timeUnit!!) }
if (mediaType != null) {
response.addHeader(HttpHeaders.CONTENT_TYPE, mediaType!!)
} else {
response.addHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
mediaType?.also {
response.setHeader(HttpHeaders.CONTENT_TYPE, it)
}
mockWebServer.enqueue(response)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ class DeepLTranslatorClientTest : DescribeSpec({
timeoutDuration = Duration.ofSeconds(10)
)

afterEach {
mockWebServer.shutdown()
}

describe("requestTranslate") {
val response = DeepLResponse(
listOf(
Expand Down Expand Up @@ -124,7 +128,7 @@ class DeepLTranslatorClientTest : DescribeSpec({
mockWebServer.enqueue {
statusCode(200)
body(response)
delay(1, TimeUnit.SECONDS)
delay(200, TimeUnit.MILLISECONDS)
}

val expect = delayClient.requestTranslate(ContentFixture.create(), Language.KOREAN)
Expand Down

0 comments on commit a0c7515

Please sign in to comment.