File tree Expand file tree Collapse file tree 5 files changed +49
-10
lines changed
main/kotlin/blog/codejunkie/demo
test/kotlin/blog/codejunkie/demo Expand file tree Collapse file tree 5 files changed +49
-10
lines changed Original file line number Diff line number Diff line change 1
1
package blog.codejunkie.demo
2
2
3
+ import org.springframework.stereotype.Component
3
4
import org.springframework.web.reactive.function.client.WebClient
4
5
6
+ @Component
5
7
class IpApiConnector {
6
8
private val client = WebClient .create(" http://ip-api.com/json/" )
7
9
Original file line number Diff line number Diff line change 1
1
package blog.codejunkie.demo
2
2
3
3
import org.springframework.web.bind.annotation.GetMapping
4
+ import org.springframework.web.bind.annotation.RequestParam
4
5
import org.springframework.web.bind.annotation.RestController
5
6
6
7
@RestController
7
- class IspController {
8
+ class IspController ( val connector : IpApiConnector ) {
8
9
9
10
@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)
14
12
15
13
}
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change @@ -15,7 +15,7 @@ class SanityE2ETestsJavaStyle {
15
15
@Test
16
16
fun `isp details service passes sanity - Java` () {
17
17
`when `().
18
- get(" /isp" ).
18
+ get(" /isp?host=codejunkie.blog " ).
19
19
then().
20
20
statusCode(200 )
21
21
}
@@ -24,7 +24,7 @@ class SanityE2ETestsJavaStyle {
24
24
class SanityE2ETestsPlain : StringSpec ({
25
25
" isp details service passes sanity - Plain" {
26
26
`when `().
27
- get("/isp").
27
+ get("/isp? host=codejunkie.blog ").
28
28
then().
29
29
statusCode(200)
30
30
}
@@ -34,7 +34,7 @@ class SanityE2ETests : StringSpec({
34
34
" isp details service passes sanity - DSL" {
35
35
given {
36
36
on {
37
- get("/isp") itHas {
37
+ get("/isp? host=codejunkie.blog ") itHas {
38
38
statusCode(200)
39
39
}
40
40
}
Original file line number Diff line number Diff line change @@ -25,17 +25,29 @@ class IntegrationTests : StringSpec() {
25
25
}
26
26
27
27
init {
28
- " isp controller passes integration " {
28
+ " codejunkie.blog return the correct details " {
29
29
given {
30
30
on {
31
- get(" /isp" ) itHas {
31
+ get(" /isp?host=codejunkie.blog " ) itHas {
32
32
statusCode(200 )
33
33
body(" country" , CoreMatchers .equalTo(" United States" ))
34
34
body(" isp" , CoreMatchers .equalTo(" GoDaddy.com, LLC" ))
35
35
}
36
36
}
37
37
}
38
38
}
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
+ }
39
51
}
40
52
}
41
53
You can’t perform that action at this time.
0 commit comments