Skip to content

Commit ed7cc9c

Browse files
Invoke the connector from the controller
1 parent 80d2b86 commit ed7cc9c

File tree

5 files changed

+49
-10
lines changed

5 files changed

+49
-10
lines changed

src/main/kotlin/blog/codejunkie/demo/Connector.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package blog.codejunkie.demo
22

3+
import org.springframework.stereotype.Component
34
import org.springframework.web.reactive.function.client.WebClient
45

6+
@Component
57
class IpApiConnector {
68
private val client = WebClient.create("http://ip-api.com/json/")
79

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
package blog.codejunkie.demo
22

33
import org.springframework.web.bind.annotation.GetMapping
4+
import org.springframework.web.bind.annotation.RequestParam
45
import org.springframework.web.bind.annotation.RestController
56

67
@RestController
7-
class IspController {
8+
class IspController(val connector : IpApiConnector) {
89

910
@GetMapping("/isp")
10-
fun ispDetails() = mapOf(
11-
"country" to "United States",
12-
"isp" to "GoDaddy.com, LLC"
13-
)
11+
fun ispDetails(@RequestParam() host: String) = connector.invoke(host)
1412

1513
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package blog.codejunkie.demo.extra
2+
3+
import blog.codejunkie.demo.IpApiConnector
4+
import org.springframework.context.annotation.Bean
5+
import org.springframework.context.annotation.Configuration
6+
import org.springframework.http.MediaType.ALL
7+
import org.springframework.http.MediaType.APPLICATION_JSON
8+
import org.springframework.web.reactive.function.server.ServerResponse.ok
9+
import org.springframework.web.reactive.function.server.body
10+
import org.springframework.web.reactive.function.server.router
11+
12+
/**
13+
* Present how to use the new functional routing instead of a controller
14+
*/
15+
@Configuration
16+
class RoutesExample(val connector : IpApiConnector) {
17+
18+
@Bean
19+
fun router() = router {
20+
(accept(ALL)).nest {
21+
GET("/ispUsingRouter", {
22+
ok().
23+
contentType(APPLICATION_JSON).
24+
body(connector.invoke(it.queryParam("host").get()))})
25+
}
26+
}
27+
}

src/test/kotlin/blog/codejunkie/demo/E2eTests.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class SanityE2ETestsJavaStyle {
1515
@Test
1616
fun `isp details service passes sanity - Java`() {
1717
`when`().
18-
get("/isp").
18+
get("/isp?host=codejunkie.blog").
1919
then().
2020
statusCode(200)
2121
}
@@ -24,7 +24,7 @@ class SanityE2ETestsJavaStyle {
2424
class SanityE2ETestsPlain : StringSpec({
2525
"isp details service passes sanity - Plain" {
2626
`when`().
27-
get("/isp").
27+
get("/isp?host=codejunkie.blog").
2828
then().
2929
statusCode(200)
3030
}
@@ -34,7 +34,7 @@ class SanityE2ETests : StringSpec({
3434
"isp details service passes sanity - DSL" {
3535
given {
3636
on {
37-
get("/isp") itHas {
37+
get("/isp?host=codejunkie.blog") itHas {
3838
statusCode(200)
3939
}
4040
}

src/test/kotlin/blog/codejunkie/demo/IntegrationTests.kt

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,29 @@ class IntegrationTests : StringSpec() {
2525
}
2626

2727
init {
28-
"isp controller passes integration" {
28+
"codejunkie.blog return the correct details" {
2929
given {
3030
on {
31-
get("/isp") itHas {
31+
get("/isp?host=codejunkie.blog") itHas {
3232
statusCode(200)
3333
body("country", CoreMatchers.equalTo("United States"))
3434
body("isp", CoreMatchers.equalTo("GoDaddy.com, LLC"))
3535
}
3636
}
3737
}
3838
}
39+
40+
"google.com return the correct details" {
41+
given {
42+
on {
43+
get("/isp?host=google.com") itHas {
44+
statusCode(200)
45+
body("country", CoreMatchers.equalTo("United States"))
46+
body("isp", CoreMatchers.equalTo("Google"))
47+
}
48+
}
49+
}
50+
}
3951
}
4052
}
4153

0 commit comments

Comments
 (0)