Skip to content

Commit 4b304d9

Browse files
committed
add POST endpoint in spring
1 parent f1938e2 commit 4b304d9

File tree

4 files changed

+8
-39
lines changed

4 files changed

+8
-39
lines changed

examples/build.gradle.kts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
plugins {
22
id("org.springframework.boot") version "3.1.3"
33
id("io.spring.dependency-management") version "1.1.2"
4-
kotlin("jvm") version "1.6.10"
4+
kotlin("jvm") version "1.8.20"
55
}
66

77
group = "com.github.firetail-io"

examples/src/main/java/com/example/demo/DemoApplication.java

+6
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import org.springframework.boot.SpringApplication;
55
import org.springframework.boot.autoconfigure.SpringBootApplication;
66
import org.springframework.web.bind.annotation.GetMapping;
7+
import org.springframework.web.bind.annotation.PostMapping;
78
import org.springframework.web.bind.annotation.RestController;
89

910
import java.time.Clock;
@@ -23,4 +24,9 @@ public static void main(String[] args) {
2324
public String hello() {
2425
return String.format("Hello %s, utc: %s!", LocalDateTime.now(), ZonedDateTime.now(Clock.systemUTC()));
2526
}
27+
28+
@PostMapping("/hello")
29+
public String helloPost() {
30+
return String.format("Hello %s, utc: %s!", LocalDateTime.now(), ZonedDateTime.now(Clock.systemUTC()));
31+
}
2632
}

src/main/kotlin/io/firetail/logging/servlet/FiretailMapper.kt

+1-4
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,11 @@ class FiretailMapper {
2222
.mapIndexed { _, value -> value to listOf(request.getHeader(value)) }
2323
.toMap()
2424

25-
val writer = StringWriter()
26-
InputStreamReader(request.inputStream, "UTF-8").transferTo(writer)
27-
2825
return FtRequest(
2926
httpProtocol = request.protocol,
3027
method = request.method,
3128
headers = headers,
32-
body = writer.toString(),
29+
body = request.inputStream.read().toString(),
3330
ip = request.remoteAddr,
3431
resource = request.requestURI,
3532
uri = request.requestURL.toString(), // FT calls the defines the URI as URL.

src/test/kotlin/io/firetail/logging/FiretailMapperTest.kt

-34
Original file line numberDiff line numberDiff line change
@@ -14,40 +14,6 @@ import java.util.*
1414

1515
class FiretailMapperTest {
1616

17-
private val firetailMapper = FiretailMapper()
18-
19-
@Test
20-
fun fromResponse() {
21-
val mockResponse: HttpServletResponse = Mockito.mock(HttpServletResponse::class.java)
22-
Mockito.`when`(mockResponse.headerNames).thenReturn(listOf(TEST))
23-
Mockito.`when`(mockResponse.getHeader(TEST)).thenReturn(TEST_RESULTS)
24-
val result = firetailMapper.from(mockResponse)
25-
Assertions.assertThat(result.headers)
26-
.isNotNull
27-
.hasFieldOrPropertyWithValue(TEST, listOf(TEST_RESULTS))
28-
}
29-
30-
@Test
31-
fun fromRequest() {
32-
val mockRequest: HttpServletRequest = Mockito.mock(HttpServletRequest::class.java)
33-
34-
Mockito.`when`(mockRequest.protocol).thenReturn("HTTP")
35-
Mockito.`when`(mockRequest.method).thenReturn("GET")
36-
Mockito.`when`(mockRequest.requestURI).thenReturn("/")
37-
Mockito.`when`(mockRequest.requestURL).thenReturn(StringBuffer().append("http://blah.com"))
38-
Mockito.`when`(mockRequest.remoteAddr).thenReturn("127.0.0.1")
39-
Mockito.`when`(mockRequest.queryString).thenReturn("123")
40-
Mockito.`when`(mockRequest.getHeader(TEST)).thenReturn(TEST_RESULTS)
41-
Mockito.`when`(mockRequest.headerNames)
42-
.thenReturn(Collections.enumeration(Collections.singletonList(TEST)))
43-
44-
val result = firetailMapper.from(mockRequest)
45-
46-
Assertions.assertThat(result.headers)
47-
.isNotNull
48-
.hasFieldOrPropertyWithValue(TEST, listOf(TEST_RESULTS))
49-
}
50-
5117
@Test
5218
fun jsonNd() {
5319
val firetailMapper = FiretailMapper()

0 commit comments

Comments
 (0)