Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
S
spring-cloud-netflix
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
openSource
spring-cloud-netflix
Commits
d1bc20ac
Unverified
Commit
d1bc20ac
authored
Sep 25, 2017
by
Spencer Gibb
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fix spring security tests.
parent
03135579
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
97 additions
and
55 deletions
+97
-55
FeignHttpClientTests.java
...ework/cloud/netflix/feign/valid/FeignHttpClientTests.java
+17
-16
HystrixOnlyTests.java
...ringframework/cloud/netflix/hystrix/HystrixOnlyTests.java
+25
-18
HystrixSecurityTests.java
.../cloud/netflix/hystrix/security/HystrixSecurityTests.java
+12
-10
TestAutoConfiguration.java
...ngframework/cloud/netflix/test/TestAutoConfiguration.java
+40
-2
spring.factories
...netflix-core/src/test/resources/META-INF/spring.factories
+2
-2
application.yml
spring-cloud-netflix-core/src/test/resources/application.yml
+1
-7
No files found.
spring-cloud-netflix-core/src/test/java/org/springframework/cloud/netflix/feign/valid/FeignHttpClientTests.java
View file @
d1bc20ac
...
...
@@ -16,21 +16,15 @@
package
org
.
springframework
.
cloud
.
netflix
.
feign
.
valid
;
import
static
org
.
hamcrest
.
Matchers
.
equalTo
;
import
static
org
.
hamcrest
.
Matchers
.
instanceOf
;
import
static
org
.
hamcrest
.
Matchers
.
is
;
import
static
org
.
hamcrest
.
Matchers
.
notNullValue
;
import
static
org
.
junit
.
Assert
.
assertEquals
;
import
static
org
.
junit
.
Assert
.
assertNotNull
;
import
static
org
.
junit
.
Assert
.
assertThat
;
import
java.util.Objects
;
import
org.junit.Test
;
import
org.junit.runner.RunWith
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.beans.factory.annotation.Value
;
import
org.springframework.boot.autoconfigure.EnableAutoConfiguration
;
import
org.springframework.boot.test.context.SpringBootTest
;
import
org.springframework.boot.test.context.SpringBootTest.WebEnvironment
;
import
org.springframework.boot.web.server.LocalServerPort
;
import
org.springframework.cloud.netflix.feign.EnableFeignClients
;
import
org.springframework.cloud.netflix.feign.FeignClient
;
import
org.springframework.cloud.netflix.feign.ribbon.LoadBalancerFeignClient
;
...
...
@@ -38,9 +32,10 @@ import org.springframework.cloud.netflix.ribbon.RibbonClient;
import
org.springframework.cloud.netflix.ribbon.StaticServerList
;
import
org.springframework.context.annotation.Bean
;
import
org.springframework.context.annotation.Configuration
;
import
org.springframework.http.MediaType
;
import
org.springframework.http.ResponseEntity
;
import
org.springframework.test.annotation.DirtiesContext
;
import
org.springframework.test.context.junit4.Spring
JUnit4Class
Runner
;
import
org.springframework.test.context.junit4.SpringRunner
;
import
org.springframework.web.bind.annotation.PathVariable
;
import
org.springframework.web.bind.annotation.RequestBody
;
import
org.springframework.web.bind.annotation.RequestHeader
;
...
...
@@ -51,21 +46,27 @@ import org.springframework.web.bind.annotation.RestController;
import
com.netflix.loadbalancer.Server
;
import
com.netflix.loadbalancer.ServerList
;
import
feign.Client
;
import
static
org
.
hamcrest
.
Matchers
.
equalTo
;
import
static
org
.
hamcrest
.
Matchers
.
instanceOf
;
import
static
org
.
hamcrest
.
Matchers
.
is
;
import
static
org
.
hamcrest
.
Matchers
.
notNullValue
;
import
static
org
.
junit
.
Assert
.
assertEquals
;
import
static
org
.
junit
.
Assert
.
assertNotNull
;
import
static
org
.
junit
.
Assert
.
assertThat
;
import
java.util.Objects
;
import
feign.Client
;
/**
* @author Spencer Gibb
*/
@RunWith
(
Spring
JUnit4Class
Runner
.
class
)
@RunWith
(
SpringRunner
.
class
)
@SpringBootTest
(
classes
=
FeignHttpClientTests
.
Application
.
class
,
webEnvironment
=
WebEnvironment
.
RANDOM_PORT
,
value
=
{
"spring.application.name=feignclienttest"
,
"feign.hystrix.enabled=false"
,
"feign.okhttp.enabled=false"
})
@DirtiesContext
public
class
FeignHttpClientTests
{
@
Value
(
"${local.server.port}"
)
@
LocalServerPort
private
int
port
=
0
;
@Autowired
...
...
@@ -82,7 +83,7 @@ public class FeignHttpClientTests {
}
protected
interface
BaseTestClient
{
@RequestMapping
(
method
=
RequestMethod
.
GET
,
value
=
"/hello"
)
@RequestMapping
(
method
=
RequestMethod
.
GET
,
value
=
"/hello"
,
produces
=
MediaType
.
APPLICATION_JSON_VALUE
)
Hello
getHello
();
@RequestMapping
(
method
=
RequestMethod
.
PATCH
,
value
=
"/hellop"
,
consumes
=
"application/json"
)
...
...
@@ -90,7 +91,7 @@ public class FeignHttpClientTests {
}
protected
interface
UserService
{
@RequestMapping
(
method
=
RequestMethod
.
GET
,
value
=
"/users/{id}"
)
@RequestMapping
(
method
=
RequestMethod
.
GET
,
value
=
"/users/{id}"
,
produces
=
MediaType
.
APPLICATION_JSON_VALUE
)
User
getUser
(
@PathVariable
(
"id"
)
long
id
);
}
...
...
@@ -226,7 +227,7 @@ public class FeignHttpClientTests {
@Configuration
static
class
LocalRibbonClientConfiguration
{
@
Value
(
"${local.server.port}"
)
@
LocalServerPort
private
int
port
=
0
;
@Bean
...
...
spring-cloud-netflix-core/src/test/java/org/springframework/cloud/netflix/hystrix/HystrixOnlyTests.java
View file @
d1bc20ac
...
...
@@ -16,10 +16,6 @@
package
org
.
springframework
.
cloud
.
netflix
.
hystrix
;
import
static
org
.
junit
.
Assert
.
assertEquals
;
import
static
org
.
junit
.
Assert
.
assertFalse
;
import
static
org
.
junit
.
Assert
.
assertTrue
;
import
java.util.Base64
;
import
java.util.Map
;
...
...
@@ -32,28 +28,36 @@ import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import
org.springframework.boot.test.context.SpringBootTest
;
import
org.springframework.boot.test.context.SpringBootTest.WebEnvironment
;
import
org.springframework.boot.test.web.client.TestRestTemplate
;
import
org.springframework.boot.web.server.LocalServerPort
;
import
org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker
;
import
org.springframework.context.annotation.Bean
;
import
org.springframework.context.annotation.Configuration
;
import
org.springframework.http.HttpEntity
;
import
org.springframework.http.HttpHeaders
;
import
org.springframework.http.HttpMethod
;
import
org.springframework.http.ResponseEntity
;
import
org.springframework.test.annotation.DirtiesContext
;
import
org.springframework.test.context.junit4.SpringJUnit4ClassRunner
;
import
org.springframework.test.context.ActiveProfiles
;
import
org.springframework.test.context.junit4.SpringRunner
;
import
org.springframework.web.bind.annotation.RequestMapping
;
import
org.springframework.web.bind.annotation.RestController
;
import
com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand
;
import
static
org
.
assertj
.
core
.
api
.
Assertions
.
assertThat
;
import
static
org
.
junit
.
Assert
.
assertEquals
;
import
static
org
.
junit
.
Assert
.
assertFalse
;
/**
* @author Spencer Gibb
*/
@RunWith
(
Spring
JUnit4Class
Runner
.
class
)
@RunWith
(
SpringRunner
.
class
)
@SpringBootTest
(
classes
=
HystrixOnlyApplication
.
class
,
webEnvironment
=
WebEnvironment
.
RANDOM_PORT
)
@DirtiesContext
@ActiveProfiles
(
"proxysecurity"
)
public
class
HystrixOnlyTests
{
@
Value
(
"${local.server.port}"
)
@
LocalServerPort
private
int
port
;
@Value
(
"${security.user.username}"
)
...
...
@@ -64,24 +68,27 @@ public class HystrixOnlyTests {
@Test
public
void
testNormalExecution
()
{
String
s
=
new
TestRestTemplate
()
.
getFor
Object
(
"http://localhost:"
+
this
.
port
+
"/"
,
String
.
class
);
assertEquals
(
"incorrect response"
,
"Hello world"
,
s
);
ResponseEntity
<
String
>
re
s
=
new
TestRestTemplate
()
.
getFor
Entity
(
"http://localhost:"
+
this
.
port
+
"/"
,
String
.
class
);
assertEquals
(
"incorrect response"
,
"Hello world"
,
res
.
getBody
()
);
}
@Test
public
void
testFailureFallback
()
{
String
s
=
new
TestRestTemplate
()
.
getFor
Object
(
"http://localhost:"
+
this
.
port
+
"/fail"
,
String
.
class
);
assertEquals
(
"incorrect fallback"
,
"Fallback Hello world"
,
s
);
ResponseEntity
<
String
>
re
s
=
new
TestRestTemplate
()
.
getFor
Entity
(
"http://localhost:"
+
this
.
port
+
"/fail"
,
String
.
class
);
assertEquals
(
"incorrect fallback"
,
"Fallback Hello world"
,
res
.
getBody
()
);
}
@Test
@SuppressWarnings
(
"unchecked"
)
public
void
testHystrixHealth
()
{
Map
<?,
?>
map
=
getHealth
();
assertTrue
(
"Missing hystrix health key"
,
map
.
containsKey
(
"hystrix"
));
Map
<?,
?>
hystrix
=
(
Map
<?,
?>)
map
.
get
(
"hystrix"
);
assertEquals
(
"Wrong hystrix status"
,
"UP"
,
hystrix
.
get
(
"status"
));
Map
map
=
getHealth
();
assertThat
(
map
).
containsKeys
(
"details"
);
Map
details
=
(
Map
)
map
.
get
(
"details"
);
assertThat
(
details
).
containsKeys
(
"hystrix"
);
Map
hystrix
=
(
Map
)
details
.
get
(
"hystrix"
);
assertThat
(
hystrix
).
containsEntry
(
"status"
,
"UP"
);
}
@Test
...
...
@@ -94,7 +101,7 @@ public class HystrixOnlyTests {
private
Map
<?,
?>
getHealth
()
{
private
Map
getHealth
()
{
return
new
TestRestTemplate
().
exchange
(
"http://localhost:"
+
this
.
port
+
"/admin/health"
,
HttpMethod
.
GET
,
new
HttpEntity
<
Void
>(
createBasicAuthHeader
(
username
,
password
)),
...
...
spring-cloud-netflix-core/src/test/java/org/springframework/cloud/netflix/hystrix/security/HystrixSecurityTests.java
View file @
d1bc20ac
...
...
@@ -18,23 +18,25 @@ package org.springframework.cloud.netflix.hystrix.security;
import
java.util.Base64
;
import
com.netflix.hystrix.strategy.HystrixPlugins
;
import
com.netflix.hystrix.strategy.concurrency.HystrixConcurrencyStrategy
;
import
org.junit.Assert
;
import
org.junit.Test
;
import
org.junit.runner.RunWith
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.beans.factory.annotation.Value
;
import
org.springframework.boot.test.context.SpringBootTest
;
import
org.springframework.boot.test.context.SpringBootTest.WebEnvironment
;
import
org.springframework.boot.web.server.LocalServerPort
;
import
org.springframework.cloud.netflix.hystrix.security.app.CustomConcurrenyStrategy
;
import
org.springframework.http.HttpEntity
;
import
org.springframework.http.HttpHeaders
;
import
org.springframework.http.HttpMethod
;
import
org.springframework.test.annotation.DirtiesContext
;
import
org.springframework.test.context.junit4.SpringJUnit4ClassRunner
;
import
org.springframework.test.context.ActiveProfiles
;
import
org.springframework.test.context.junit4.SpringRunner
;
import
org.springframework.web.client.RestTemplate
;
import
com.netflix.hystrix.strategy.HystrixPlugins
;
import
com.netflix.hystrix.strategy.concurrency.HystrixConcurrencyStrategy
;
import
static
org
.
assertj
.
core
.
api
.
Assertions
.
assertThat
;
/**
...
...
@@ -42,23 +44,23 @@ import static org.assertj.core.api.Assertions.assertThat;
* the security context from a hystrix command.
* @author Daniel Lavoie
*/
@RunWith
(
Spring
JUnit4Class
Runner
.
class
)
@RunWith
(
SpringRunner
.
class
)
@DirtiesContext
@SpringBootTest
(
classes
=
HystrixSecurityApplication
.
class
,
webEnvironment
=
WebEnvironment
.
RANDOM_PORT
,
properties
=
{
"username.ribbon.listOfServers=localhost:${local.server.port}"
,
"feign.hystrix.enabled=true"
})
@ActiveProfiles
(
"proxysecurity"
)
public
class
HystrixSecurityTests
{
@Autowired
private
CustomConcurrenyStrategy
customConcurrenyStrategy
;
@
Value
(
"${local.server.port}"
)
@
LocalServerPort
private
String
serverPort
;
@Value
(
"${security.user.username}"
)
private
String
username
;
//TODOO: move to constants in TestAutoConfiguration
private
String
username
=
"user"
;
@Value
(
"${security.user.password}"
)
private
String
password
;
private
String
password
=
"password"
;
@Test
public
void
testSecurityConcurrencyStrategyInstalled
()
{
...
...
spring-cloud-netflix-core/src/test/java/org/springframework/cloud/netflix/test/
NoopDiscoveryClient
Configuration.java
→
spring-cloud-netflix-core/src/test/java/org/springframework/cloud/netflix/test/
TestAuto
Configuration.java
View file @
d1bc20ac
...
...
@@ -16,14 +16,52 @@
package
org
.
springframework
.
cloud
.
netflix
.
test
;
import
org.springframework.boot.autoconfigure.AutoConfigureBefore
;
import
org.springframework.boot.autoconfigure.security.SecurityAutoConfiguration
;
import
org.springframework.cloud.client.discovery.noop.NoopDiscoveryClientAutoConfiguration
;
import
org.springframework.context.annotation.Bean
;
import
org.springframework.context.annotation.Configuration
;
import
org.springframework.context.annotation.Import
;
import
org.springframework.core.Ordered
;
import
org.springframework.core.annotation.Order
;
import
org.springframework.security.config.annotation.web.builders.HttpSecurity
;
import
org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter
;
import
org.springframework.security.core.userdetails.User
;
import
org.springframework.security.core.userdetails.UserDetailsService
;
import
org.springframework.security.provisioning.InMemoryUserDetailsManager
;
/**
* @author Spencer Gibb
*/
@Configuration
@Import
(
NoopDiscoveryClientAutoConfiguration
.
class
)
public
class
NoopDiscoveryClientConfiguration
{
@Import
({
NoopDiscoveryClientAutoConfiguration
.
class
})
@AutoConfigureBefore
(
SecurityAutoConfiguration
.
class
)
public
class
TestAutoConfiguration
{
@Configuration
@Order
(
Ordered
.
HIGHEST_PRECEDENCE
)
protected
static
class
TestSecurityConfiguration
extends
WebSecurityConfigurerAdapter
{
TestSecurityConfiguration
()
{
super
(
true
);
}
@Bean
public
UserDetailsService
userDetailsService
()
{
InMemoryUserDetailsManager
manager
=
new
InMemoryUserDetailsManager
();
manager
.
createUser
(
User
.
withUsername
(
"user"
).
password
(
"password"
).
roles
(
"USER"
).
build
());
return
manager
;
}
@Override
protected
void
configure
(
HttpSecurity
http
)
throws
Exception
{
// super.configure(http);
http
.
antMatcher
(
"/proxy-username"
)
.
httpBasic
()
.
and
()
.
authorizeRequests
().
antMatchers
(
"/**"
).
permitAll
();
}
}
}
spring-cloud-netflix-core/src/test/resources/META-INF/spring.factories
View file @
d1bc20ac
org.springframework.
cloud.client.discovery.EnableDiscoveryClient
=\
org.springframework.cloud.netflix.test.
NoopDiscoveryClient
Configuration
org.springframework.
boot.autoconfigure.EnableAutoConfiguration
=\
org.springframework.cloud.netflix.test.
TestAuto
Configuration
spring-cloud-netflix-core/src/test/resources/application.yml
View file @
d1bc20ac
...
...
@@ -56,9 +56,4 @@ feignClient:
localappName
:
localapp
methodLevelRequestMappingPath
:
/hello2
myPlaceholderHeader
:
myPlaceholderHeaderValue
security
:
basic
:
path
:
/proxy-username
user
:
username
:
user
password
:
password
\ No newline at end of file
endpoints.default.web.enabled
:
true
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment