Skip to content

Commit

Permalink
Adds integration test for rate limiter
Browse files Browse the repository at this point in the history
  • Loading branch information
paulbrejla committed Dec 1, 2023
1 parent dd88f47 commit e3a35d1
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package de.paulbrejla.holidays.rest.impl

import de.paulbrejla.holidays.application.api.HolidayService
import de.paulbrejla.holidays.rest.api.HolidayWsV1
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.TestInstance
import org.junit.jupiter.api.extension.ExtendWith
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.test.context.junit.jupiter.SpringExtension
import org.springframework.test.web.servlet.MockMvc
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders
import org.springframework.test.web.servlet.result.MockMvcResultMatchers
import org.springframework.test.web.servlet.result.MockMvcResultMatchers.status


@AutoConfigureMockMvc
@SpringBootTest
@ExtendWith(
SpringExtension::class
)
@TestInstance(TestInstance.Lifecycle.PER_METHOD)
class HolidayWsV1ImplITest {
@Autowired
lateinit var mvc: MockMvc

@Test
fun `api responds with a 429 if rate limit is reached after 10 requests`() {
repeat(11) { rep ->
mvc.perform(
MockMvcRequestBuilders
.get("/api/v1/holidays/HB")
)
.andExpect {
if (rep <= 10) {
status().isOk
} else {
status().isTooManyRequests
}
}
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ internal class HolidayWsV1ImplTest {
// Then
assertNotNull(holidays)
assertEquals(expectedSize, holidays.size)

}

}
8 changes: 6 additions & 2 deletions src/test/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,13 @@ spring:
enabled: false
path: /h2-console
server:
port: 80
port: ${SERVER_PORT:80}
loader:
source: filesystem
remoteURL:
branch: master
filePath:
filePath:
rateLimit:
globalBucket:
id: ${RATE_LIMIT_GLOBAL_BUCKET_ID:global}
capacity: ${RATE_LIMIT_GLOBAL_BUCKET_CAPACITY:10}

0 comments on commit e3a35d1

Please sign in to comment.