20
20
import java .util .List ;
21
21
import java .util .Map ;
22
22
23
+ import feign .FeignException ;
23
24
import org .junit .jupiter .api .Test ;
24
25
25
26
import org .springframework .beans .factory .annotation .Autowired ;
35
36
import org .springframework .context .annotation .Bean ;
36
37
import org .springframework .context .annotation .Import ;
37
38
import org .springframework .http .HttpHeaders ;
39
+ import org .springframework .http .HttpStatus ;
40
+ import org .springframework .http .ResponseEntity ;
38
41
import org .springframework .test .context .ActiveProfiles ;
39
42
import org .springframework .web .bind .annotation .GetMapping ;
40
43
import org .springframework .web .bind .annotation .RequestHeader ;
44
47
45
48
import static java .util .Map .entry ;
46
49
import static org .assertj .core .api .Assertions .assertThat ;
50
+ import static org .assertj .core .api .Assertions .assertThatCode ;
51
+ import static org .assertj .core .api .Assertions .assertThatExceptionOfType ;
47
52
import static org .springframework .http .MediaType .APPLICATION_JSON_VALUE ;
48
53
49
54
/**
52
57
* @author Olga Maciaszek-Sharma
53
58
*/
54
59
@ SpringBootTest (classes = FeignClientFactoryBeanIntegrationTests .Application .class ,
55
- webEnvironment = SpringBootTest .WebEnvironment .RANDOM_PORT )
60
+ webEnvironment = SpringBootTest .WebEnvironment .RANDOM_PORT )
56
61
@ ActiveProfiles ("defaultstest" )
57
62
class FeignClientFactoryBeanIntegrationTests {
58
63
@@ -64,51 +69,58 @@ class FeignClientFactoryBeanIntegrationTests {
64
69
65
70
@ Test
66
71
void shouldProcessDefaultRequestHeadersPerClient () {
67
- assertThat (testClientA .headers ()).isNotNull ()
68
- .contains (entry ("x-custom-header-2" , List .of ("2 from default" )),
72
+ assertThat (testClientA .headers ()).isNotNull ().contains (entry ("x-custom-header-2" , List .of ("2 from default" )),
69
73
entry ("x-custom-header" , List .of ("from client A" )));
70
- assertThat (testClientB .headers ()).isNotNull ()
71
- .contains (entry ("x-custom-header-2" , List .of ("2 from default" )),
74
+ assertThat (testClientB .headers ()).isNotNull ().contains (entry ("x-custom-header-2" , List .of ("2 from default" )),
72
75
entry ("x-custom-header" , List .of ("from client B" )));
73
76
}
74
77
75
78
@ Test
76
79
void shouldProcessDefaultQueryParamsPerClient () {
77
- assertThat (testClientA .params ()).isNotNull ()
78
- .contains (entry ("customParam2" , "2 from default" ),
80
+ assertThat (testClientA .params ()).isNotNull ().contains (entry ("customParam2" , "2 from default" ),
79
81
entry ("customParam1" , "from client A" ));
80
- assertThat (testClientB .params ()).isNotNull ()
81
- .contains (entry ("customParam2" , "2 from default" ),
82
+ assertThat (testClientB .params ()).isNotNull ().contains (entry ("customParam2" , "2 from default" ),
82
83
entry ("customParam1" , "from client B" ));
83
84
}
84
85
86
+ @ Test
87
+ void shouldProcessDismiss404PerClient () {
88
+ assertThatExceptionOfType (FeignException .FeignClientException .class ).isThrownBy (() -> testClientA .test404 ());
89
+ assertThatCode (() -> {
90
+ ResponseEntity <String > response404 = testClientB .test404 ();
91
+ assertThat (response404 .getStatusCode ()).isEqualTo (HttpStatus .NOT_FOUND );
92
+ assertThat (response404 .getBody ()).isNull ();
93
+ }).doesNotThrowAnyException ();
94
+ }
95
+
85
96
@ FeignClient ("testClientA" )
86
- public interface TestClientA {
97
+ public interface TestClientA extends TestClient {
87
98
88
- @ GetMapping ("/headers" )
89
- Map <String , List <String >> headers ();
99
+ }
90
100
91
- @ GetMapping ( "/params " )
92
- Map < String , String > params ();
101
+ @ FeignClient ( "testClientB " )
102
+ public interface TestClientB extends TestClient {
93
103
94
104
}
95
105
96
- @ FeignClient ("testClientB" )
97
- public interface TestClientB {
106
+ public interface TestClient {
98
107
99
108
@ GetMapping ("/headers" )
100
109
Map <String , List <String >> headers ();
101
110
102
111
@ GetMapping ("/params" )
103
112
Map <String , String > params ();
104
113
114
+ @ GetMapping
115
+ ResponseEntity <String > test404 ();
116
+
105
117
}
106
118
107
119
@ EnableAutoConfiguration
108
- @ EnableFeignClients (clients = {TestClientA .class , TestClientB .class })
120
+ @ EnableFeignClients (clients = { TestClientA .class , TestClientB .class })
109
121
@ RestController
110
- @ LoadBalancerClients ({@ LoadBalancerClient (name = "testClientA" , configuration = TestClientAConfiguration .class ),
111
- @ LoadBalancerClient (name = "testClientB" , configuration = TestClientBConfiguration .class )})
122
+ @ LoadBalancerClients ({ @ LoadBalancerClient (name = "testClientA" , configuration = TestClientAConfiguration .class ),
123
+ @ LoadBalancerClient (name = "testClientB" , configuration = TestClientBConfiguration .class ) })
112
124
@ RequestMapping
113
125
@ Import (NoSecurityConfiguration .class )
114
126
protected static class Application {
@@ -123,6 +135,11 @@ public Map<String, String> headersB(@RequestParam Map<String, String> params) {
123
135
return params ;
124
136
}
125
137
138
+ @ GetMapping
139
+ ResponseEntity <String > test404 () {
140
+ return ResponseEntity .notFound ().build ();
141
+ }
142
+
126
143
}
127
144
128
145
// LoadBalancer with fixed server list for "testClientA" pointing to localhost
@@ -134,7 +151,7 @@ static class TestClientAConfiguration {
134
151
@ Bean
135
152
public ServiceInstanceListSupplier testClientAServiceInstanceListSupplier () {
136
153
return ServiceInstanceListSuppliers .from ("testClientA" ,
137
- new DefaultServiceInstance ("local-1" , "testClientA" , "localhost" , port , false ));
154
+ new DefaultServiceInstance ("local-1" , "testClientA" , "localhost" , port , false ));
138
155
}
139
156
140
157
}
@@ -148,7 +165,7 @@ static class TestClientBConfiguration {
148
165
@ Bean
149
166
public ServiceInstanceListSupplier testClientBServiceInstanceListSupplier () {
150
167
return ServiceInstanceListSuppliers .from ("testClientB" ,
151
- new DefaultServiceInstance ("local-1" , "testClientB" , "localhost" , port , false ));
168
+ new DefaultServiceInstance ("local-1" , "testClientB" , "localhost" , port , false ));
152
169
}
153
170
154
171
}
0 commit comments