Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in / Register
Toggle navigation
S
spring-boot
Project
Project
Details
Activity
Releases
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
DEMO
spring-boot
Commits
07a50bb1
Commit
07a50bb1
authored
Aug 24, 2016
by
Andy Wilkinson
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Update the samples to make use of auto-configured TestRestTemplate
Closes gh-6730
parent
3c5cf028
Changes
47
Hide whitespace changes
Inline
Side-by-side
Showing
47 changed files
with
375 additions
and
421 deletions
+375
-421
SampleActuatorLog4J2ApplicationTests.java
...actuator/log4j2/SampleActuatorLog4J2ApplicationTests.java
+4
-5
SampleActuatorUiApplicationTests.java
.../sample/actuator/ui/SampleActuatorUiApplicationTests.java
+10
-12
EndpointsPropertiesSampleActuatorApplicationTests.java
...or/EndpointsPropertiesSampleActuatorApplicationTests.java
+7
-8
InsecureManagementSampleActuatorApplicationTests.java
...tor/InsecureManagementSampleActuatorApplicationTests.java
+6
-7
InsecureSampleActuatorApplicationTests.java
...mple/actuator/InsecureSampleActuatorApplicationTests.java
+4
-5
ManagementPathSampleActuatorApplicationTests.java
...ctuator/ManagementPathSampleActuatorApplicationTests.java
+6
-7
NoManagementSampleActuatorApplicationTests.java
.../actuator/NoManagementSampleActuatorApplicationTests.java
+6
-7
NonSensitiveHealthTests.java
...rc/test/java/sample/actuator/NonSensitiveHealthTests.java
+5
-5
SampleActuatorApplicationTests.java
.../java/sample/actuator/SampleActuatorApplicationTests.java
+35
-41
ServletPathInsecureSampleActuatorApplicationTests.java
...or/ServletPathInsecureSampleActuatorApplicationTests.java
+7
-7
ServletPathSampleActuatorApplicationTests.java
...e/actuator/ServletPathSampleActuatorApplicationTests.java
+9
-10
ShutdownSampleActuatorApplicationTests.java
...mple/actuator/ShutdownSampleActuatorApplicationTests.java
+7
-8
SampleHateoasApplicationTests.java
...st/java/sample/hateoas/SampleHateoasApplicationTests.java
+9
-12
SampleHypermediaUiApplicationTests.java
...ple/hypermedia/ui/SampleHypermediaUiApplicationTests.java
+10
-16
SampleHypermediaApplicationHomePageTests.java
.../hypermedia/SampleHypermediaApplicationHomePageTests.java
+9
-15
SampleJerseyApplicationTests.java
...test/java/sample/jersey/SampleJerseyApplicationTests.java
+9
-11
SampleJersey1ApplicationTests.java
...st/java/sample/jersey1/SampleJersey1ApplicationTests.java
+6
-7
SampleWebJspApplicationTests.java
...t/java/sample/jetty/jsp/SampleWebJspApplicationTests.java
+4
-5
SampleJettyApplicationTests.java
...c/test/java/sample/jetty/SampleJettyApplicationTests.java
+6
-10
SampleJetty8SslApplicationTests.java
...va/sample/jetty8/ssl/SampleJetty8SslApplicationTests.java
+5
-4
SampleJetty8ApplicationTests.java
...test/java/sample/jetty8/SampleJetty8ApplicationTests.java
+6
-9
SampleJetty92ApplicationTests.java
...st/java/sample/jetty92/SampleJetty92ApplicationTests.java
+6
-9
SampleServletApplicationTests.java
...st/java/sample/servlet/SampleServletApplicationTests.java
+5
-7
SampleTestNGApplicationTests.java
...test/java/sample/testng/SampleTestNGApplicationTests.java
+4
-5
SampleWebJspApplicationTests.java
.../java/sample/tomcat/jsp/SampleWebJspApplicationTests.java
+4
-5
SampleTomcatSslApplicationTests.java
...va/sample/tomcat/ssl/SampleTomcatSslApplicationTests.java
+4
-7
NonAutoConfigurationSampleTomcatApplicationTests.java
...cat/NonAutoConfigurationSampleTomcatApplicationTests.java
+4
-5
SampleTomcatApplicationTests.java
...test/java/sample/tomcat/SampleTomcatApplicationTests.java
+6
-9
SampleWebJspApplicationTests.java
...java/sample/tomcat7/jsp/SampleWebJspApplicationTests.java
+4
-5
SampleTomcatSslApplicationTests.java
...va/sample/tomcat/ssl/SampleTomcatSslApplicationTests.java
+4
-7
SampleTomcatSslApplicationTests.java
...va/sample/tomcat/ssl/SampleTomcatSslApplicationTests.java
+4
-7
SampleTraditionalApplicationTests.java
...sample/traditional/SampleTraditionalApplicationTests.java
+6
-7
SampleUndertowSslApplicationTests.java
...ample/undertow/ssl/SampleUndertowSslApplicationTests.java
+4
-7
SampleUndertowApplicationTests.java
.../java/sample/undertow/SampleUndertowApplicationTests.java
+7
-9
SampleWebFreeMarkerApplicationTests.java
...ample/freemarker/SampleWebFreeMarkerApplicationTests.java
+7
-8
SampleGroovyTemplateApplicationTests.java
...groovytemplates/SampleGroovyTemplateApplicationTests.java
+8
-6
SampleWebJspApplicationTests.java
...rc/test/java/sample/jsp/SampleWebJspApplicationTests.java
+4
-5
SampleMethodSecurityApplicationTests.java
...security/method/SampleMethodSecurityApplicationTests.java
+39
-17
SampleWebMustacheApplicationTests.java
...va/sample/mustache/SampleWebMustacheApplicationTests.java
+12
-17
SampleWebSecureCustomApplicationTests.java
.../secure/custom/SampleWebSecureCustomApplicationTests.java
+13
-11
SampleGithubApplicationTests.java
...ample/web/secure/github/SampleGithubApplicationTests.java
+7
-6
SampleWebSecureJdbcApplicationTests.java
.../web/secure/jdbc/SampleWebSecureJdbcApplicationTests.java
+13
-11
SampleSecureApplicationTests.java
.../java/sample/web/secure/SampleSecureApplicationTests.java
+13
-11
SampleWebStaticApplicationTests.java
...le/web/staticcontent/SampleWebStaticApplicationTests.java
+6
-10
SampleWebThymeleaf3ApplicationTests.java
...e/web/thymeleaf3/SampleWebThymeleaf3ApplicationTests.java
+8
-6
SampleWebUiApplicationTests.java
.../test/java/sample/web/ui/SampleWebUiApplicationTests.java
+7
-5
SampleWebVelocityApplicationTests.java
...ample/web/velocity/SampleWebVelocityApplicationTests.java
+6
-8
No files found.
spring-boot-samples/spring-boot-sample-actuator-log4j2/src/test/java/sample/actuator/log4j2/SampleActuatorLog4J2ApplicationTests.java
View file @
07a50bb1
...
...
@@ -21,7 +21,7 @@ import java.util.Map;
import
org.junit.Test
;
import
org.junit.runner.RunWith
;
import
org.springframework.b
oot.context.embedded.LocalServerPort
;
import
org.springframework.b
eans.factory.annotation.Autowired
;
import
org.springframework.boot.test.context.SpringBootTest
;
import
org.springframework.boot.test.context.SpringBootTest.WebEnvironment
;
import
org.springframework.boot.test.web.client.TestRestTemplate
;
...
...
@@ -42,14 +42,13 @@ import static org.assertj.core.api.Assertions.assertThat;
@DirtiesContext
public
class
SampleActuatorLog4J2ApplicationTests
{
@
LocalServerPort
private
int
port
;
@
Autowired
private
TestRestTemplate
restTemplate
;
@Test
public
void
testHome
()
throws
Exception
{
@SuppressWarnings
(
"rawtypes"
)
ResponseEntity
<
Map
>
entity
=
new
TestRestTemplate
()
.
getForEntity
(
"http://localhost:"
+
this
.
port
,
Map
.
class
);
ResponseEntity
<
Map
>
entity
=
this
.
restTemplate
.
getForEntity
(
"/"
,
Map
.
class
);
assertThat
(
entity
.
getStatusCode
()).
isEqualTo
(
HttpStatus
.
OK
);
@SuppressWarnings
(
"unchecked"
)
Map
<
String
,
Object
>
body
=
entity
.
getBody
();
...
...
spring-boot-samples/spring-boot-sample-actuator-ui/src/test/java/sample/actuator/ui/SampleActuatorUiApplicationTests.java
View file @
07a50bb1
...
...
@@ -22,7 +22,7 @@ import java.util.Map;
import
org.junit.Test
;
import
org.junit.runner.RunWith
;
import
org.springframework.b
oot.context.embedded.LocalServerPort
;
import
org.springframework.b
eans.factory.annotation.Autowired
;
import
org.springframework.boot.test.context.SpringBootTest
;
import
org.springframework.boot.test.context.SpringBootTest.WebEnvironment
;
import
org.springframework.boot.test.web.client.TestRestTemplate
;
...
...
@@ -47,15 +47,14 @@ import static org.assertj.core.api.Assertions.assertThat;
@DirtiesContext
public
class
SampleActuatorUiApplicationTests
{
@
LocalServerPort
private
int
port
;
@
Autowired
private
TestRestTemplate
restTemplate
;
@Test
public
void
testHome
()
throws
Exception
{
HttpHeaders
headers
=
new
HttpHeaders
();
headers
.
setAccept
(
Arrays
.
asList
(
MediaType
.
TEXT_HTML
));
ResponseEntity
<
String
>
entity
=
new
TestRestTemplate
().
exchange
(
"http://localhost:"
+
this
.
port
,
HttpMethod
.
GET
,
ResponseEntity
<
String
>
entity
=
this
.
restTemplate
.
exchange
(
"/"
,
HttpMethod
.
GET
,
new
HttpEntity
<
Void
>(
headers
),
String
.
class
);
assertThat
(
entity
.
getStatusCode
()).
isEqualTo
(
HttpStatus
.
OK
);
assertThat
(
entity
.
getBody
()).
contains
(
"<title>Hello"
);
...
...
@@ -63,8 +62,8 @@ public class SampleActuatorUiApplicationTests {
@Test
public
void
testCss
()
throws
Exception
{
ResponseEntity
<
String
>
entity
=
new
TestRestTemplate
().
getForEntity
(
"http://localhost:"
+
this
.
port
+
"/css/bootstrap.min.css"
,
String
.
class
);
ResponseEntity
<
String
>
entity
=
this
.
restTemplate
.
getForEntity
(
"/css/bootstrap.min.css"
,
String
.
class
);
assertThat
(
entity
.
getStatusCode
()).
isEqualTo
(
HttpStatus
.
OK
);
assertThat
(
entity
.
getBody
()).
contains
(
"body"
);
}
...
...
@@ -72,8 +71,8 @@ public class SampleActuatorUiApplicationTests {
@Test
public
void
testMetrics
()
throws
Exception
{
@SuppressWarnings
(
"rawtypes"
)
ResponseEntity
<
Map
>
entity
=
new
TestRestTemplate
()
.
getForEntity
(
"http://localhost:"
+
this
.
port
+
"/metrics"
,
Map
.
class
);
ResponseEntity
<
Map
>
entity
=
this
.
restTemplate
.
getForEntity
(
"/metrics"
,
Map
.
class
);
assertThat
(
entity
.
getStatusCode
()).
isEqualTo
(
HttpStatus
.
UNAUTHORIZED
);
}
...
...
@@ -81,9 +80,8 @@ public class SampleActuatorUiApplicationTests {
public
void
testError
()
throws
Exception
{
HttpHeaders
headers
=
new
HttpHeaders
();
headers
.
setAccept
(
Arrays
.
asList
(
MediaType
.
TEXT_HTML
));
ResponseEntity
<
String
>
entity
=
new
TestRestTemplate
().
exchange
(
"http://localhost:"
+
this
.
port
+
"/error"
,
HttpMethod
.
GET
,
new
HttpEntity
<
Void
>(
headers
),
String
.
class
);
ResponseEntity
<
String
>
entity
=
this
.
restTemplate
.
exchange
(
"/error"
,
HttpMethod
.
GET
,
new
HttpEntity
<
Void
>(
headers
),
String
.
class
);
assertThat
(
entity
.
getStatusCode
()).
isEqualTo
(
HttpStatus
.
INTERNAL_SERVER_ERROR
);
assertThat
(
entity
.
getBody
()).
contains
(
"<html>"
).
contains
(
"<body>"
)
.
contains
(
"Please contact the operator with the above information"
);
...
...
spring-boot-samples/spring-boot-sample-actuator/src/test/java/sample/actuator/EndpointsPropertiesSampleActuatorApplicationTests.java
View file @
07a50bb1
...
...
@@ -23,7 +23,6 @@ import org.junit.runner.RunWith;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.boot.autoconfigure.security.SecurityProperties
;
import
org.springframework.boot.context.embedded.LocalServerPort
;
import
org.springframework.boot.test.context.SpringBootTest
;
import
org.springframework.boot.test.context.SpringBootTest.WebEnvironment
;
import
org.springframework.boot.test.web.client.TestRestTemplate
;
...
...
@@ -49,14 +48,14 @@ public class EndpointsPropertiesSampleActuatorApplicationTests {
@Autowired
private
SecurityProperties
security
;
@
LocalServerPort
private
int
port
;
@
Autowired
private
TestRestTemplate
restTemplate
;
@Test
public
void
testCustomErrorPath
()
throws
Exception
{
@SuppressWarnings
(
"rawtypes"
)
ResponseEntity
<
Map
>
entity
=
new
TestRestTemplate
(
"user"
,
getPassword
())
.
getForEntity
(
"http://localhost:"
+
this
.
port
+
"/oops"
,
Map
.
class
);
ResponseEntity
<
Map
>
entity
=
this
.
restTemplate
.
withBasicAuth
(
"user"
,
getPassword
()).
getForEntity
(
"/oops"
,
Map
.
class
);
assertThat
(
entity
.
getStatusCode
()).
isEqualTo
(
HttpStatus
.
INTERNAL_SERVER_ERROR
);
@SuppressWarnings
(
"unchecked"
)
Map
<
String
,
Object
>
body
=
entity
.
getBody
();
...
...
@@ -66,9 +65,9 @@ public class EndpointsPropertiesSampleActuatorApplicationTests {
@Test
public
void
testCustomContextPath
()
throws
Exception
{
ResponseEntity
<
String
>
entity
=
new
TestRestTemplate
(
"user"
,
getPassword
())
.
getForEntity
(
"http://localhost:"
+
this
.
port
+
"/admin/health"
,
String
.
class
);
ResponseEntity
<
String
>
entity
=
this
.
restTemplate
.
withBasicAuth
(
"user"
,
getPassword
())
.
getForEntity
(
"/admin/health"
,
String
.
class
);
assertThat
(
entity
.
getStatusCode
()).
isEqualTo
(
HttpStatus
.
OK
);
assertThat
(
entity
.
getBody
()).
contains
(
"\"status\":\"UP\""
);
assertThat
(
entity
.
getBody
()).
contains
(
"\"hello\":\"world\""
);
...
...
spring-boot-samples/spring-boot-sample-actuator/src/test/java/sample/actuator/InsecureManagementSampleActuatorApplicationTests.java
View file @
07a50bb1
...
...
@@ -21,7 +21,7 @@ import java.util.Map;
import
org.junit.Test
;
import
org.junit.runner.RunWith
;
import
org.springframework.b
oot.context.embedded.LocalServerPort
;
import
org.springframework.b
eans.factory.annotation.Autowired
;
import
org.springframework.boot.test.context.SpringBootTest
;
import
org.springframework.boot.test.context.SpringBootTest.WebEnvironment
;
import
org.springframework.boot.test.web.client.TestRestTemplate
;
...
...
@@ -46,14 +46,13 @@ import static org.assertj.core.api.Assertions.assertThat;
@ActiveProfiles
(
"unsecure-management"
)
public
class
InsecureManagementSampleActuatorApplicationTests
{
@
LocalServerPort
private
int
port
;
@
Autowired
private
TestRestTemplate
restTemplate
;
@Test
public
void
testHomeIsSecure
()
throws
Exception
{
@SuppressWarnings
(
"rawtypes"
)
ResponseEntity
<
Map
>
entity
=
new
TestRestTemplate
()
.
getForEntity
(
"http://localhost:"
+
this
.
port
,
Map
.
class
);
ResponseEntity
<
Map
>
entity
=
this
.
restTemplate
.
getForEntity
(
"/"
,
Map
.
class
);
assertThat
(
entity
.
getStatusCode
()).
isEqualTo
(
HttpStatus
.
UNAUTHORIZED
);
@SuppressWarnings
(
"unchecked"
)
Map
<
String
,
Object
>
body
=
entity
.
getBody
();
...
...
@@ -70,8 +69,8 @@ public class InsecureManagementSampleActuatorApplicationTests {
// ignore;
}
@SuppressWarnings
(
"rawtypes"
)
ResponseEntity
<
Map
>
entity
=
new
TestRestTemplate
()
.
getForEntity
(
"http://localhost:"
+
this
.
port
+
"/metrics"
,
Map
.
class
);
ResponseEntity
<
Map
>
entity
=
this
.
restTemplate
.
getForEntity
(
"/metrics"
,
Map
.
class
);
assertThat
(
entity
.
getStatusCode
()).
isEqualTo
(
HttpStatus
.
OK
);
@SuppressWarnings
(
"unchecked"
)
Map
<
String
,
Object
>
body
=
entity
.
getBody
();
...
...
spring-boot-samples/spring-boot-sample-actuator/src/test/java/sample/actuator/InsecureSampleActuatorApplicationTests.java
View file @
07a50bb1
...
...
@@ -21,7 +21,7 @@ import java.util.Map;
import
org.junit.Test
;
import
org.junit.runner.RunWith
;
import
org.springframework.b
oot.context.embedded.LocalServerPort
;
import
org.springframework.b
eans.factory.annotation.Autowired
;
import
org.springframework.boot.test.context.SpringBootTest
;
import
org.springframework.boot.test.context.SpringBootTest.WebEnvironment
;
import
org.springframework.boot.test.web.client.TestRestTemplate
;
...
...
@@ -44,14 +44,13 @@ import static org.assertj.core.api.Assertions.assertThat;
@DirtiesContext
public
class
InsecureSampleActuatorApplicationTests
{
@
LocalServerPort
private
int
port
;
@
Autowired
private
TestRestTemplate
restTemplate
;
@Test
public
void
testHome
()
throws
Exception
{
@SuppressWarnings
(
"rawtypes"
)
ResponseEntity
<
Map
>
entity
=
new
TestRestTemplate
()
.
getForEntity
(
"http://localhost:"
+
this
.
port
,
Map
.
class
);
ResponseEntity
<
Map
>
entity
=
this
.
restTemplate
.
getForEntity
(
"/"
,
Map
.
class
);
assertThat
(
entity
.
getStatusCode
()).
isEqualTo
(
HttpStatus
.
OK
);
@SuppressWarnings
(
"unchecked"
)
Map
<
String
,
Object
>
body
=
entity
.
getBody
();
...
...
spring-boot-samples/spring-boot-sample-actuator/src/test/java/sample/actuator/ManagementPathSampleActuatorApplicationTests.java
View file @
07a50bb1
...
...
@@ -21,7 +21,7 @@ import java.util.Map;
import
org.junit.Test
;
import
org.junit.runner.RunWith
;
import
org.springframework.b
oot.context.embedded.LocalServerPort
;
import
org.springframework.b
eans.factory.annotation.Autowired
;
import
org.springframework.boot.test.context.SpringBootTest
;
import
org.springframework.boot.test.context.SpringBootTest.WebEnvironment
;
import
org.springframework.boot.test.web.client.TestRestTemplate
;
...
...
@@ -43,13 +43,13 @@ import static org.assertj.core.api.Assertions.assertThat;
@DirtiesContext
public
class
ManagementPathSampleActuatorApplicationTests
{
@
LocalServerPort
private
int
port
;
@
Autowired
private
TestRestTemplate
restTemplate
;
@Test
public
void
testHealth
()
throws
Exception
{
ResponseEntity
<
String
>
entity
=
new
TestRestTemplate
().
getForEntity
(
"http://localhost:"
+
this
.
port
+
"/admin/health"
,
String
.
class
);
ResponseEntity
<
String
>
entity
=
this
.
restTemplate
.
getForEntity
(
"/admin/health"
,
String
.
class
);
assertThat
(
entity
.
getStatusCode
()).
isEqualTo
(
HttpStatus
.
OK
);
assertThat
(
entity
.
getBody
()).
contains
(
"\"status\":\"UP\""
);
}
...
...
@@ -57,8 +57,7 @@ public class ManagementPathSampleActuatorApplicationTests {
@Test
public
void
testHomeIsSecure
()
throws
Exception
{
@SuppressWarnings
(
"rawtypes"
)
ResponseEntity
<
Map
>
entity
=
new
TestRestTemplate
()
.
getForEntity
(
"http://localhost:"
+
this
.
port
,
Map
.
class
);
ResponseEntity
<
Map
>
entity
=
this
.
restTemplate
.
getForEntity
(
"/"
,
Map
.
class
);
assertThat
(
entity
.
getStatusCode
()).
isEqualTo
(
HttpStatus
.
UNAUTHORIZED
);
@SuppressWarnings
(
"unchecked"
)
Map
<
String
,
Object
>
body
=
entity
.
getBody
();
...
...
spring-boot-samples/spring-boot-sample-actuator/src/test/java/sample/actuator/NoManagementSampleActuatorApplicationTests.java
View file @
07a50bb1
...
...
@@ -23,7 +23,6 @@ import org.junit.runner.RunWith;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.boot.autoconfigure.security.SecurityProperties
;
import
org.springframework.boot.context.embedded.LocalServerPort
;
import
org.springframework.boot.test.context.SpringBootTest
;
import
org.springframework.boot.test.context.SpringBootTest.WebEnvironment
;
import
org.springframework.boot.test.web.client.TestRestTemplate
;
...
...
@@ -48,14 +47,14 @@ public class NoManagementSampleActuatorApplicationTests {
@Autowired
private
SecurityProperties
security
;
@
LocalServerPort
private
int
port
=
0
;
@
Autowired
private
TestRestTemplate
restTemplate
;
@Test
public
void
testHome
()
throws
Exception
{
@SuppressWarnings
(
"rawtypes"
)
ResponseEntity
<
Map
>
entity
=
new
TestRestTemplate
(
"user"
,
getPassword
())
.
getForEntity
(
"http://localhost:"
+
this
.
port
,
Map
.
class
);
ResponseEntity
<
Map
>
entity
=
this
.
restTemplate
.
withBasicAuth
(
"user"
,
getPassword
()).
getForEntity
(
"/"
,
Map
.
class
);
assertThat
(
entity
.
getStatusCode
()).
isEqualTo
(
HttpStatus
.
OK
);
@SuppressWarnings
(
"unchecked"
)
Map
<
String
,
Object
>
body
=
entity
.
getBody
();
...
...
@@ -66,8 +65,8 @@ public class NoManagementSampleActuatorApplicationTests {
public
void
testMetricsNotAvailable
()
throws
Exception
{
testHome
();
// makes sure some requests have been made
@SuppressWarnings
(
"rawtypes"
)
ResponseEntity
<
Map
>
entity
=
new
TestRestTemplate
(
"user"
,
getPassword
())
.
getForEntity
(
"http://localhost:"
+
this
.
port
+
"/metrics"
,
Map
.
class
);
ResponseEntity
<
Map
>
entity
=
this
.
restTemplate
.
withBasicAuth
(
"user"
,
getPassword
()).
getForEntity
(
"/metrics"
,
Map
.
class
);
assertThat
(
entity
.
getStatusCode
()).
isEqualTo
(
HttpStatus
.
NOT_FOUND
);
}
...
...
spring-boot-samples/spring-boot-sample-actuator/src/test/java/sample/actuator/NonSensitiveHealthTests.java
View file @
07a50bb1
...
...
@@ -19,7 +19,7 @@ package sample.actuator;
import
org.junit.Test
;
import
org.junit.runner.RunWith
;
import
org.springframework.b
oot.context.embedded.LocalServerPort
;
import
org.springframework.b
eans.factory.annotation.Autowired
;
import
org.springframework.boot.test.context.SpringBootTest
;
import
org.springframework.boot.test.context.SpringBootTest.WebEnvironment
;
import
org.springframework.boot.test.web.client.TestRestTemplate
;
...
...
@@ -41,13 +41,13 @@ import static org.assertj.core.api.Assertions.assertThat;
@DirtiesContext
public
class
NonSensitiveHealthTests
{
@
LocalServerPort
private
int
port
;
@
Autowired
private
TestRestTemplate
restTemplate
;
@Test
public
void
testSecureHealth
()
throws
Exception
{
ResponseEntity
<
String
>
entity
=
new
TestRestTemplate
()
.
getForEntity
(
"http://localhost:"
+
this
.
port
+
"/health"
,
String
.
class
);
ResponseEntity
<
String
>
entity
=
this
.
restTemplate
.
getForEntity
(
"/health"
,
String
.
class
);
assertThat
(
entity
.
getStatusCode
()).
isEqualTo
(
HttpStatus
.
OK
);
assertThat
(
entity
.
getBody
()).
doesNotContain
(
"\"hello\":1"
);
}
...
...
spring-boot-samples/spring-boot-sample-actuator/src/test/java/sample/actuator/SampleActuatorApplicationTests.java
View file @
07a50bb1
...
...
@@ -26,7 +26,6 @@ import org.junit.runner.RunWith;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.boot.autoconfigure.jdbc.DataSourceProperties
;
import
org.springframework.boot.autoconfigure.security.SecurityProperties
;
import
org.springframework.boot.context.embedded.LocalServerPort
;
import
org.springframework.boot.test.context.SpringBootTest
;
import
org.springframework.boot.test.context.SpringBootTest.WebEnvironment
;
import
org.springframework.boot.test.web.client.TestRestTemplate
;
...
...
@@ -55,14 +54,13 @@ public class SampleActuatorApplicationTests {
@Autowired
private
SecurityProperties
security
;
@
LocalServerPort
private
int
port
;
@
Autowired
private
TestRestTemplate
restTemplate
;
@Test
public
void
testHomeIsSecure
()
throws
Exception
{
@SuppressWarnings
(
"rawtypes"
)
ResponseEntity
<
Map
>
entity
=
new
TestRestTemplate
()
.
getForEntity
(
"http://localhost:"
+
this
.
port
,
Map
.
class
);
ResponseEntity
<
Map
>
entity
=
this
.
restTemplate
.
getForEntity
(
"/"
,
Map
.
class
);
assertThat
(
entity
.
getStatusCode
()).
isEqualTo
(
HttpStatus
.
UNAUTHORIZED
);
@SuppressWarnings
(
"unchecked"
)
Map
<
String
,
Object
>
body
=
entity
.
getBody
();
...
...
@@ -73,25 +71,22 @@ public class SampleActuatorApplicationTests {
@Test
public
void
testMetricsIsSecure
()
throws
Exception
{
@SuppressWarnings
(
"rawtypes"
)
ResponseEntity
<
Map
>
entity
=
new
TestRestTemplate
()
.
getForEntity
(
"http://localhost:"
+
this
.
port
+
"/metrics"
,
Map
.
class
);
ResponseEntity
<
Map
>
entity
=
this
.
restTemplate
.
getForEntity
(
"/metrics"
,
Map
.
class
);
assertThat
(
entity
.
getStatusCode
()).
isEqualTo
(
HttpStatus
.
UNAUTHORIZED
);
entity
=
new
TestRestTemplate
()
.
getForEntity
(
"http://localhost:"
+
this
.
port
+
"/metrics/"
,
Map
.
class
);
entity
=
this
.
restTemplate
.
getForEntity
(
"/metrics/"
,
Map
.
class
);
assertThat
(
entity
.
getStatusCode
()).
isEqualTo
(
HttpStatus
.
UNAUTHORIZED
);
entity
=
new
TestRestTemplate
().
getForEntity
(
"http://localhost:"
+
this
.
port
+
"/metrics/foo"
,
Map
.
class
);
entity
=
this
.
restTemplate
.
getForEntity
(
"/metrics/foo"
,
Map
.
class
);
assertThat
(
entity
.
getStatusCode
()).
isEqualTo
(
HttpStatus
.
UNAUTHORIZED
);
entity
=
new
TestRestTemplate
().
getForEntity
(
"http://localhost:"
+
this
.
port
+
"/metrics.json"
,
Map
.
class
);
entity
=
this
.
restTemplate
.
getForEntity
(
"/metrics.json"
,
Map
.
class
);
assertThat
(
entity
.
getStatusCode
()).
isEqualTo
(
HttpStatus
.
UNAUTHORIZED
);
}
@Test
public
void
testHome
()
throws
Exception
{
@SuppressWarnings
(
"rawtypes"
)
ResponseEntity
<
Map
>
entity
=
new
TestRestTemplate
(
"user"
,
getPassword
())
.
getForEntity
(
"http://localhost:"
+
this
.
port
,
Map
.
class
);
ResponseEntity
<
Map
>
entity
=
this
.
restTemplate
.
withBasicAuth
(
"user"
,
getPassword
()).
getForEntity
(
"/"
,
Map
.
class
);
assertThat
(
entity
.
getStatusCode
()).
isEqualTo
(
HttpStatus
.
OK
);
@SuppressWarnings
(
"unchecked"
)
Map
<
String
,
Object
>
body
=
entity
.
getBody
();
...
...
@@ -102,8 +97,8 @@ public class SampleActuatorApplicationTests {
public
void
testMetrics
()
throws
Exception
{
testHome
();
// makes sure some requests have been made
@SuppressWarnings
(
"rawtypes"
)
ResponseEntity
<
Map
>
entity
=
new
TestRestTemplate
(
"user"
,
getPassword
())
.
getForEntity
(
"http://localhost:"
+
this
.
port
+
"/metrics"
,
Map
.
class
);
ResponseEntity
<
Map
>
entity
=
this
.
restTemplate
.
withBasicAuth
(
"user"
,
getPassword
()).
getForEntity
(
"/metrics"
,
Map
.
class
);
assertThat
(
entity
.
getStatusCode
()).
isEqualTo
(
HttpStatus
.
OK
);
@SuppressWarnings
(
"unchecked"
)
Map
<
String
,
Object
>
body
=
entity
.
getBody
();
...
...
@@ -113,8 +108,8 @@ public class SampleActuatorApplicationTests {
@Test
public
void
testEnv
()
throws
Exception
{
@SuppressWarnings
(
"rawtypes"
)
ResponseEntity
<
Map
>
entity
=
new
TestRestTemplate
(
"user"
,
getPassword
())
.
getForEntity
(
"http://localhost:"
+
this
.
port
+
"/env"
,
Map
.
class
);
ResponseEntity
<
Map
>
entity
=
this
.
restTemplate
.
withBasicAuth
(
"user"
,
getPassword
()).
getForEntity
(
"/env"
,
Map
.
class
);
assertThat
(
entity
.
getStatusCode
()).
isEqualTo
(
HttpStatus
.
OK
);
@SuppressWarnings
(
"unchecked"
)
Map
<
String
,
Object
>
body
=
entity
.
getBody
();
...
...
@@ -123,8 +118,8 @@ public class SampleActuatorApplicationTests {
@Test
public
void
testHealth
()
throws
Exception
{
ResponseEntity
<
String
>
entity
=
new
TestRestTemplate
()
.
getForEntity
(
"http://localhost:"
+
this
.
port
+
"/health"
,
String
.
class
);
ResponseEntity
<
String
>
entity
=
this
.
restTemplate
.
getForEntity
(
"/health"
,
String
.
class
);
assertThat
(
entity
.
getStatusCode
()).
isEqualTo
(
HttpStatus
.
OK
);
assertThat
(
entity
.
getBody
()).
contains
(
"\"status\":\"UP\""
);
assertThat
(
entity
.
getBody
()).
doesNotContain
(
"\"hello\":\"1\""
);
...
...
@@ -132,16 +127,17 @@ public class SampleActuatorApplicationTests {
@Test
public
void
testSecureHealth
()
throws
Exception
{
ResponseEntity
<
String
>
entity
=
new
TestRestTemplate
(
"user"
,
getPassword
())
.
getForEntity
(
"http://localhost:"
+
this
.
port
+
"/health"
,
String
.
class
);
ResponseEntity
<
String
>
entity
=
this
.
restTemplate
.
withBasicAuth
(
"user"
,
getPassword
())
.
getForEntity
(
"/health"
,
String
.
class
);
assertThat
(
entity
.
getStatusCode
()).
isEqualTo
(
HttpStatus
.
OK
);
assertThat
(
entity
.
getBody
()).
contains
(
"\"hello\":1"
);
}
@Test
public
void
testInfo
()
throws
Exception
{
ResponseEntity
<
String
>
entity
=
new
TestRestTemplate
()
.
getForEntity
(
"http://localhost:"
+
this
.
port
+
"/info"
,
String
.
class
);
ResponseEntity
<
String
>
entity
=
this
.
restTemplate
.
getForEntity
(
"/info"
,
String
.
class
);
assertThat
(
entity
.
getStatusCode
()).
isEqualTo
(
HttpStatus
.
OK
);
assertThat
(
entity
.
getBody
())
.
contains
(
"\"artifact\":\"spring-boot-sample-actuator\""
);
...
...
@@ -154,8 +150,8 @@ public class SampleActuatorApplicationTests {
@Test
public
void
testErrorPage
()
throws
Exception
{
ResponseEntity
<
String
>
entity
=
new
TestRestTemplate
(
"user"
,
getPassword
())
.
getForEntity
(
"http://localhost:"
+
this
.
port
+
"/foo"
,
String
.
class
);
ResponseEntity
<
String
>
entity
=
this
.
restTemplate
.
withBasicAuth
(
"user"
,
getPassword
()).
getForEntity
(
"/foo"
,
String
.
class
);
assertThat
(
entity
.
getStatusCode
()).
isEqualTo
(
HttpStatus
.
INTERNAL_SERVER_ERROR
);
String
body
=
entity
.
getBody
();
assertThat
(
body
).
contains
(
"\"error\":"
);
...
...
@@ -166,9 +162,9 @@ public class SampleActuatorApplicationTests {
HttpHeaders
headers
=
new
HttpHeaders
();
headers
.
setAccept
(
Arrays
.
asList
(
MediaType
.
TEXT_HTML
));
HttpEntity
<?>
request
=
new
HttpEntity
<
Void
>(
headers
);
ResponseEntity
<
String
>
entity
=
new
TestRestTemplate
(
"user"
,
getPassword
())
.
exchange
(
"http://localhost:"
+
this
.
port
+
"/foo"
,
HttpMethod
.
GET
,
request
,
String
.
class
);
ResponseEntity
<
String
>
entity
=
this
.
restTemplate
.
withBasicAuth
(
"user"
,
getPassword
())
.
exchange
(
"/foo"
,
HttpMethod
.
GET
,
request
,
String
.
class
);
assertThat
(
entity
.
getStatusCode
()).
isEqualTo
(
HttpStatus
.
INTERNAL_SERVER_ERROR
);
String
body
=
entity
.
getBody
();
assertThat
(
body
).
as
(
"Body was null"
).
isNotNull
();
...
...
@@ -177,11 +173,10 @@ public class SampleActuatorApplicationTests {
@Test
public
void
testTrace
()
throws
Exception
{
new
TestRestTemplate
().
getForEntity
(
"http://localhost:"
+
this
.
port
+
"/health"
,
String
.
class
);
this
.
restTemplate
.
getForEntity
(
"/health"
,
String
.
class
);
@SuppressWarnings
(
"rawtypes"
)
ResponseEntity
<
List
>
entity
=
new
TestRestTemplate
(
"user"
,
getPassword
())
.
getForEntity
(
"http://localhost:"
+
this
.
port
+
"/trace"
,
List
.
class
);
ResponseEntity
<
List
>
entity
=
this
.
restTemplate
.
withBasicAuth
(
"user"
,
getPassword
()).
getForEntity
(
"/trace"
,
List
.
class
);
assertThat
(
entity
.
getStatusCode
()).
isEqualTo
(
HttpStatus
.
OK
);
@SuppressWarnings
(
"unchecked"
)
List
<
Map
<
String
,
Object
>>
list
=
entity
.
getBody
();
...
...
@@ -195,8 +190,7 @@ public class SampleActuatorApplicationTests {
@Test
public
void
testErrorPageDirectAccess
()
throws
Exception
{
@SuppressWarnings
(
"rawtypes"
)
ResponseEntity
<
Map
>
entity
=
new
TestRestTemplate
()
.
getForEntity
(
"http://localhost:"
+
this
.
port
+
"/error"
,
Map
.
class
);
ResponseEntity
<
Map
>
entity
=
this
.
restTemplate
.
getForEntity
(
"/error"
,
Map
.
class
);
assertThat
(
entity
.
getStatusCode
()).
isEqualTo
(
HttpStatus
.
INTERNAL_SERVER_ERROR
);
@SuppressWarnings
(
"unchecked"
)
Map
<
String
,
Object
>
body
=
entity
.
getBody
();
...
...
@@ -208,8 +202,8 @@ public class SampleActuatorApplicationTests {
@SuppressWarnings
(
"unchecked"
)
public
void
testBeans
()
throws
Exception
{
@SuppressWarnings
(
"rawtypes"
)
ResponseEntity
<
List
>
entity
=
new
TestRestTemplate
(
"user"
,
getPassword
())
.
getForEntity
(
"http://localhost:"
+
this
.
port
+
"/beans"
,
List
.
class
);
ResponseEntity
<
List
>
entity
=
this
.
restTemplate
.
withBasicAuth
(
"user"
,
getPassword
()).
getForEntity
(
"/beans"
,
List
.
class
);
assertThat
(
entity
.
getStatusCode
()).
isEqualTo
(
HttpStatus
.
OK
);
assertThat
(
entity
.
getBody
()).
hasSize
(
1
);
Map
<
String
,
Object
>
body
=
(
Map
<
String
,
Object
>)
entity
.
getBody
().
get
(
0
);
...
...
@@ -219,9 +213,9 @@ public class SampleActuatorApplicationTests {
@Test
public
void
testConfigProps
()
throws
Exception
{
@SuppressWarnings
(
"rawtypes"
)
ResponseEntity
<
Map
>
entity
=
new
TestRestTemplate
(
"user"
,
getPassword
())
.
getForEntity
(
"http://localhost:"
+
this
.
port
+
"/configprops"
,
Map
.
class
);
ResponseEntity
<
Map
>
entity
=
this
.
restTemplate
.
withBasicAuth
(
"user"
,
getPassword
())
.
getForEntity
(
"/configprops"
,
Map
.
class
);
assertThat
(
entity
.
getStatusCode
()).
isEqualTo
(
HttpStatus
.
OK
);
@SuppressWarnings
(
"unchecked"
)
Map
<
String
,
Object
>
body
=
entity
.
getBody
();
...
...
spring-boot-samples/spring-boot-sample-actuator/src/test/java/sample/actuator/ServletPathInsecureSampleActuatorApplicationTests.java
View file @
07a50bb1
...
...
@@ -21,7 +21,7 @@ import java.util.Map;
import
org.junit.Test
;
import
org.junit.runner.RunWith
;
import
org.springframework.b
oot.context.embedded.LocalServerPort
;
import
org.springframework.b
eans.factory.annotation.Autowired
;
import
org.springframework.boot.test.context.SpringBootTest
;
import
org.springframework.boot.test.context.SpringBootTest.WebEnvironment
;
import
org.springframework.boot.test.web.client.TestRestTemplate
;
...
...
@@ -44,14 +44,14 @@ import static org.assertj.core.api.Assertions.assertThat;
@DirtiesContext
public
class
ServletPathInsecureSampleActuatorApplicationTests
{
@
LocalServerPort
private
int
port
;
@
Autowired
private
TestRestTemplate
restTemplate
;
@Test
public
void
testHome
()
throws
Exception
{
@SuppressWarnings
(
"rawtypes"
)
ResponseEntity
<
Map
>
entity
=
new
TestRestTemplate
()
.
getForEntity
(
"http://localhost:"
+
this
.
port
+
"/spring/"
,
Map
.
class
);
ResponseEntity
<
Map
>
entity
=
this
.
restTemplate
.
getForEntity
(
"/spring/"
,
Map
.
class
);
assertThat
(
entity
.
getStatusCode
()).
isEqualTo
(
HttpStatus
.
OK
);
@SuppressWarnings
(
"unchecked"
)
Map
<
String
,
Object
>
body
=
entity
.
getBody
();
...
...
@@ -62,8 +62,8 @@ public class ServletPathInsecureSampleActuatorApplicationTests {
@Test
public
void
testMetricsIsSecure
()
throws
Exception
{
@SuppressWarnings
(
"rawtypes"
)
ResponseEntity
<
Map
>
entity
=
new
TestRestTemplate
().
getForEntity
(
"http://localhost:"
+
this
.
port
+
"/spring/metrics"
,
Map
.
class
);
ResponseEntity
<
Map
>
entity
=
this
.
restTemplate
.
getForEntity
(
"/spring/metrics"
,
Map
.
class
);
assertThat
(
entity
.
getStatusCode
()).
isEqualTo
(
HttpStatus
.
UNAUTHORIZED
);
}
...
...
spring-boot-samples/spring-boot-sample-actuator/src/test/java/sample/actuator/ServletPathSampleActuatorApplicationTests.java
View file @
07a50bb1
...
...
@@ -21,7 +21,7 @@ import java.util.Map;
import
org.junit.Test
;
import
org.junit.runner.RunWith
;
import
org.springframework.b
oot.context.embedded.LocalServerPort
;
import
org.springframework.b
eans.factory.annotation.Autowired
;
import
org.springframework.boot.test.context.SpringBootTest
;
import
org.springframework.boot.test.context.SpringBootTest.WebEnvironment
;
import
org.springframework.boot.test.web.client.TestRestTemplate
;
...
...
@@ -43,15 +43,14 @@ import static org.assertj.core.api.Assertions.assertThat;
@DirtiesContext
public
class
ServletPathSampleActuatorApplicationTests
{
@
LocalServerPort
private
int
port
;
@
Autowired
private
TestRestTemplate
restTemplate
;
@Test
public
void
testErrorPath
()
throws
Exception
{
@SuppressWarnings
(
"rawtypes"
)
ResponseEntity
<
Map
>
entity
=
new
TestRestTemplate
(
"user"
,
"password"
)
.
getForEntity
(
"http://localhost:"
+
this
.
port
+
"/spring/error"
,
Map
.
class
);
ResponseEntity
<
Map
>
entity
=
this
.
restTemplate
.
getForEntity
(
"/spring/error"
,
Map
.
class
);
assertThat
(
entity
.
getStatusCode
()).
isEqualTo
(
HttpStatus
.
INTERNAL_SERVER_ERROR
);
@SuppressWarnings
(
"unchecked"
)
Map
<
String
,
Object
>
body
=
entity
.
getBody
();
...
...
@@ -61,8 +60,8 @@ public class ServletPathSampleActuatorApplicationTests {
@Test
public
void
testHealth
()
throws
Exception
{
ResponseEntity
<
String
>
entity
=
new
TestRestTemplate
().
getForEntity
(
"http://localhost:"
+
this
.
port
+
"/spring/health"
,
String
.
class
);
ResponseEntity
<
String
>
entity
=
this
.
restTemplate
.
getForEntity
(
"/spring/health"
,
String
.
class
);
assertThat
(
entity
.
getStatusCode
()).
isEqualTo
(
HttpStatus
.
OK
);
assertThat
(
entity
.
getBody
()).
contains
(
"\"status\":\"UP\""
);
}
...
...
@@ -70,8 +69,8 @@ public class ServletPathSampleActuatorApplicationTests {
@Test
public
void
testHomeIsSecure
()
throws
Exception
{
@SuppressWarnings
(
"rawtypes"
)
ResponseEntity
<
Map
>
entity
=
new
TestRestTemplate
()
.
getForEntity
(
"http://localhost:"
+
this
.
port
+
"/spring/"
,
Map
.
class
);
ResponseEntity
<
Map
>
entity
=
this
.
restTemplate
.
getForEntity
(
"/spring/"
,
Map
.
class
);
assertThat
(
entity
.
getStatusCode
()).
isEqualTo
(
HttpStatus
.
UNAUTHORIZED
);
@SuppressWarnings
(
"unchecked"
)
Map
<
String
,
Object
>
body
=
entity
.
getBody
();
...
...
spring-boot-samples/spring-boot-sample-actuator/src/test/java/sample/actuator/ShutdownSampleActuatorApplicationTests.java
View file @
07a50bb1
...
...
@@ -23,7 +23,6 @@ import org.junit.runner.RunWith;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.boot.autoconfigure.security.SecurityProperties
;
import
org.springframework.boot.context.embedded.LocalServerPort
;
import
org.springframework.boot.test.context.SpringBootTest
;
import
org.springframework.boot.test.context.SpringBootTest.WebEnvironment
;
import
org.springframework.boot.test.web.client.TestRestTemplate
;
...
...
@@ -47,14 +46,14 @@ public class ShutdownSampleActuatorApplicationTests {
@Autowired
private
SecurityProperties
security
;
@
LocalServerPort
private
int
port
;
@
Autowired
private
TestRestTemplate
restTemplate
;
@Test
public
void
testHome
()
throws
Exception
{
@SuppressWarnings
(
"rawtypes"
)
ResponseEntity
<
Map
>
entity
=
new
TestRestTemplate
(
"user"
,
getPassword
())
.
getForEntity
(
"http://localhost:"
+
this
.
port
,
Map
.
class
);
ResponseEntity
<
Map
>
entity
=
this
.
restTemplate
.
withBasicAuth
(
"user"
,
getPassword
()).
getForEntity
(
"/"
,
Map
.
class
);
assertThat
(
entity
.
getStatusCode
()).
isEqualTo
(
HttpStatus
.
OK
);
@SuppressWarnings
(
"unchecked"
)
Map
<
String
,
Object
>
body
=
entity
.
getBody
();
...
...
@@ -64,9 +63,9 @@ public class ShutdownSampleActuatorApplicationTests {
@Test
public
void
testShutdown
()
throws
Exception
{
@SuppressWarnings
(
"rawtypes"
)
ResponseEntity
<
Map
>
entity
=
new
TestRestTemplate
(
"user"
,
getPassword
())
.
postForEntity
(
"http://localhost:"
+
this
.
port
+
"/shutdown"
,
null
,
Map
.
class
);
ResponseEntity
<
Map
>
entity
=
this
.
restTemplate
.
withBasicAuth
(
"user"
,
getPassword
())
.
postForEntity
(
"/shutdown"
,
null
,
Map
.
class
);
assertThat
(
entity
.
getStatusCode
()).
isEqualTo
(
HttpStatus
.
OK
);
@SuppressWarnings
(
"unchecked"
)
Map
<
String
,
Object
>
body
=
entity
.
getBody
();
...
...
spring-boot-samples/spring-boot-sample-hateoas/src/test/java/sample/hateoas/SampleHateoasApplicationTests.java
View file @
07a50bb1
...
...
@@ -16,20 +16,18 @@
package
sample
.
hateoas
;
import
java.net.URI
;
import
org.junit.Test
;
import
org.junit.runner.RunWith
;
import
org.springframework.b
oot.context.embedded.LocalServerPort
;
import
org.springframework.b
eans.factory.annotation.Autowired
;
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.http.HttpEntity
;
import
org.springframework.http.HttpHeaders
;
import
org.springframework.http.HttpMethod
;
import
org.springframework.http.HttpStatus
;
import
org.springframework.http.MediaType
;
import
org.springframework.http.RequestEntity
;
import
org.springframework.http.ResponseEntity
;
import
org.springframework.test.context.junit4.SpringRunner
;
...
...
@@ -39,13 +37,13 @@ import static org.assertj.core.api.Assertions.assertThat;
@SpringBootTest
(
webEnvironment
=
WebEnvironment
.
RANDOM_PORT
)
public
class
SampleHateoasApplicationTests
{
@
LocalServerPort
private
int
port
;
@
Autowired
private
TestRestTemplate
restTemplate
;
@Test
public
void
hasHalLinks
()
throws
Exception
{
ResponseEntity
<
String
>
entity
=
new
TestRestTemplate
().
getForEntity
(
"http://localhost:"
+
this
.
port
+
"/customers/1"
,
String
.
class
);
ResponseEntity
<
String
>
entity
=
this
.
restTemplate
.
getForEntity
(
"/customers/1"
,
String
.
class
);
assertThat
(
entity
.
getStatusCode
()).
isEqualTo
(
HttpStatus
.
OK
);
assertThat
(
entity
.
getBody
()).
startsWith
(
"{\"id\":1,\"firstName\":\"Oliver\""
+
",\"lastName\":\"Gierke\""
);
...
...
@@ -56,10 +54,9 @@ public class SampleHateoasApplicationTests {
public
void
producesJsonWhenXmlIsPreferred
()
throws
Exception
{
HttpHeaders
headers
=
new
HttpHeaders
();
headers
.
set
(
HttpHeaders
.
ACCEPT
,
"application/xml;q=0.9,application/json;q=0.8"
);
RequestEntity
<?>
request
=
new
RequestEntity
<
Void
>(
headers
,
HttpMethod
.
GET
,
URI
.
create
(
"http://localhost:"
+
this
.
port
+
"/customers/1"
));
ResponseEntity
<
String
>
response
=
new
TestRestTemplate
().
exchange
(
request
,
String
.
class
);
HttpEntity
<?>
request
=
new
HttpEntity
<>(
headers
);
ResponseEntity
<
String
>
response
=
this
.
restTemplate
.
exchange
(
"/customers/1"
,
HttpMethod
.
GET
,
request
,
String
.
class
);
assertThat
(
response
.
getStatusCode
()).
isEqualTo
(
HttpStatus
.
OK
);
assertThat
(
response
.
getHeaders
().
getContentType
())
.
isEqualTo
(
MediaType
.
parseMediaType
(
"application/json;charset=UTF-8"
));
...
...
spring-boot-samples/spring-boot-sample-hypermedia-ui/src/test/java/sample/hypermedia/ui/SampleHypermediaUiApplicationTests.java
View file @
07a50bb1
...
...
@@ -16,20 +16,19 @@
package
sample
.
hypermedia
.
ui
;
import
java.net.URI
;
import
java.util.Arrays
;
import
org.junit.Test
;
import
org.junit.runner.RunWith
;
import
org.springframework.b
oot.context.embedded.LocalServerPort
;
import
org.springframework.b
eans.factory.annotation.Autowired
;
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.http.HttpEntity
;
import
org.springframework.http.HttpHeaders
;
import
org.springframework.http.HttpMethod
;
import
org.springframework.http.MediaType
;
import
org.springframework.http.RequestEntity
;
import
org.springframework.http.ResponseEntity
;
import
org.springframework.test.context.junit4.SpringRunner
;
...
...
@@ -40,20 +39,18 @@ import static org.assertj.core.api.Assertions.assertThat;
"management.context-path="
})
public
class
SampleHypermediaUiApplicationTests
{
@
LocalServerPort
private
int
port
;
@
Autowired
private
TestRestTemplate
restTemplate
;
@Test
public
void
home
()
{
String
response
=
new
TestRestTemplate
()
.
getForObject
(
"http://localhost:"
+
this
.
port
,
String
.
class
);
String
response
=
this
.
restTemplate
.
getForObject
(
"/"
,
String
.
class
);
assertThat
(
response
).
contains
(
"Hello World"
);
}
@Test
public
void
links
()
{
String
response
=
new
TestRestTemplate
().
getForObject
(
"http://localhost:"
+
this
.
port
+
"/actuator"
,
String
.
class
);
String
response
=
this
.
restTemplate
.
getForObject
(
"/actuator"
,
String
.
class
);
assertThat
(
response
).
contains
(
"\"_links\":"
);
}
...
...
@@ -61,10 +58,8 @@ public class SampleHypermediaUiApplicationTests {
public
void
linksWithJson
()
throws
Exception
{
HttpHeaders
headers
=
new
HttpHeaders
();
headers
.
setAccept
(
Arrays
.
asList
(
MediaType
.
APPLICATION_JSON
));
ResponseEntity
<
String
>
response
=
new
TestRestTemplate
().
exchange
(
new
RequestEntity
<
Void
>(
headers
,
HttpMethod
.
GET
,
new
URI
(
"http://localhost:"
+
this
.
port
+
"/actuator"
)),
String
.
class
);
ResponseEntity
<
String
>
response
=
this
.
restTemplate
.
exchange
(
"/actuator"
,
HttpMethod
.
GET
,
new
HttpEntity
<
Void
>(
headers
),
String
.
class
);
assertThat
(
response
.
getBody
()).
contains
(
"\"_links\":"
);
}
...
...
@@ -72,9 +67,8 @@ public class SampleHypermediaUiApplicationTests {
public
void
homeWithHtml
()
throws
Exception
{
HttpHeaders
headers
=
new
HttpHeaders
();
headers
.
setAccept
(
Arrays
.
asList
(
MediaType
.
TEXT_HTML
));
ResponseEntity
<
String
>
response
=
new
TestRestTemplate
()
.
exchange
(
new
RequestEntity
<
Void
>(
headers
,
HttpMethod
.
GET
,
new
URI
(
"http://localhost:"
+
this
.
port
)),
String
.
class
);
ResponseEntity
<
String
>
response
=
this
.
restTemplate
.
exchange
(
"/"
,
HttpMethod
.
GET
,
new
HttpEntity
<
Void
>(
headers
),
String
.
class
);
assertThat
(
response
.
getBody
()).
contains
(
"Hello World"
);
}
...
...
spring-boot-samples/spring-boot-sample-hypermedia/src/test/java/sample/hypermedia/SampleHypermediaApplicationHomePageTests.java
View file @
07a50bb1
...
...
@@ -16,20 +16,19 @@
package
sample
.
hypermedia
;
import
java.net.URI
;
import
java.util.Arrays
;
import
org.junit.Test
;
import
org.junit.runner.RunWith
;
import
org.springframework.b
oot.context.embedded.LocalServerPort
;
import
org.springframework.b
eans.factory.annotation.Autowired
;
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.http.HttpEntity
;
import
org.springframework.http.HttpHeaders
;
import
org.springframework.http.HttpMethod
;
import
org.springframework.http.MediaType
;
import
org.springframework.http.RequestEntity
;
import
org.springframework.http.ResponseEntity
;
import
org.springframework.test.context.junit4.SpringRunner
;
...
...
@@ -39,13 +38,12 @@ import static org.assertj.core.api.Assertions.assertThat;
@SpringBootTest
(
webEnvironment
=
WebEnvironment
.
RANDOM_PORT
)
public
class
SampleHypermediaApplicationHomePageTests
{
@
LocalServerPort
private
int
port
;
@
Autowired
private
TestRestTemplate
restTemplate
;
@Test
public
void
home
()
{
String
response
=
new
TestRestTemplate
()
.
getForObject
(
"http://localhost:"
+
this
.
port
,
String
.
class
);
String
response
=
this
.
restTemplate
.
getForObject
(
"/"
,
String
.
class
);
assertThat
(
response
).
contains
(
"404"
);
}
...
...
@@ -53,10 +51,8 @@ public class SampleHypermediaApplicationHomePageTests {
public
void
linksWithJson
()
throws
Exception
{
HttpHeaders
headers
=
new
HttpHeaders
();
headers
.
setAccept
(
Arrays
.
asList
(
MediaType
.
APPLICATION_JSON
));
ResponseEntity
<
String
>
response
=
new
TestRestTemplate
().
exchange
(
new
RequestEntity
<
Void
>(
headers
,
HttpMethod
.
GET
,
new
URI
(
"http://localhost:"
+
this
.
port
+
"/actuator"
)),
String
.
class
);
ResponseEntity
<
String
>
response
=
this
.
restTemplate
.
exchange
(
"/actuator"
,
HttpMethod
.
GET
,
new
HttpEntity
<
Void
>(
headers
),
String
.
class
);
assertThat
(
response
.
getBody
()).
contains
(
"\"_links\":"
);
}
...
...
@@ -64,10 +60,8 @@ public class SampleHypermediaApplicationHomePageTests {
public
void
halWithHtml
()
throws
Exception
{
HttpHeaders
headers
=
new
HttpHeaders
();
headers
.
setAccept
(
Arrays
.
asList
(
MediaType
.
TEXT_HTML
));
ResponseEntity
<
String
>
response
=
new
TestRestTemplate
().
exchange
(
new
RequestEntity
<
Void
>(
headers
,
HttpMethod
.
GET
,
new
URI
(
"http://localhost:"
+
this
.
port
+
"/actuator/"
)),
String
.
class
);
ResponseEntity
<
String
>
response
=
this
.
restTemplate
.
exchange
(
"/actuator/"
,
HttpMethod
.
GET
,
new
HttpEntity
<
Void
>(
headers
),
String
.
class
);
assertThat
(
response
.
getBody
()).
contains
(
"HAL Browser"
);
}
...
...
spring-boot-samples/spring-boot-sample-jersey/src/test/java/sample/jersey/SampleJerseyApplicationTests.java
View file @
07a50bb1
...
...
@@ -19,7 +19,7 @@ package sample.jersey;
import
org.junit.Test
;
import
org.junit.runner.RunWith
;
import
org.springframework.b
oot.context.embedded.LocalServerPort
;
import
org.springframework.b
eans.factory.annotation.Autowired
;
import
org.springframework.boot.test.context.SpringBootTest
;
import
org.springframework.boot.test.context.SpringBootTest.WebEnvironment
;
import
org.springframework.boot.test.web.client.TestRestTemplate
;
...
...
@@ -33,30 +33,28 @@ import static org.assertj.core.api.Assertions.assertThat;
@SpringBootTest
(
webEnvironment
=
WebEnvironment
.
RANDOM_PORT
)
public
class
SampleJerseyApplicationTests
{
@LocalServerPort
private
int
port
;
private
TestRestTemplate
restTemplate
=
new
TestRestTemplate
();
@Autowired
private
TestRestTemplate
restTemplate
;
@Test
public
void
contextLoads
()
{
ResponseEntity
<
String
>
entity
=
this
.
restTemplate
.
getForEntity
(
"http://localhost:"
+
this
.
port
+
"/hello"
,
String
.
class
);
ResponseEntity
<
String
>
entity
=
this
.
restTemplate
.
getForEntity
(
"/hello"
,
String
.
class
);
assertThat
(
entity
.
getStatusCode
()).
isEqualTo
(
HttpStatus
.
OK
);
}
@Test
public
void
reverse
()
{
ResponseEntity
<
String
>
entity
=
this
.
restTemplate
.
getForEntity
(
"http://localhost:"
+
this
.
port
+
"/reverse?input=olleh"
,
String
.
class
);
ResponseEntity
<
String
>
entity
=
this
.
restTemplate
.
getForEntity
(
"/reverse?input=olleh"
,
String
.
class
);
assertThat
(
entity
.
getStatusCode
()).
isEqualTo
(
HttpStatus
.
OK
);
assertThat
(
entity
.
getBody
()).
isEqualTo
(
"hello"
);
}
@Test
public
void
validation
()
{
ResponseEntity
<
String
>
entity
=
this
.
restTemplate
.
getForEntity
(
"http://localhost:"
+
this
.
port
+
"/reverse"
,
String
.
class
);
ResponseEntity
<
String
>
entity
=
this
.
restTemplate
.
getForEntity
(
"/reverse"
,
String
.
class
);
assertThat
(
entity
.
getStatusCode
()).
isEqualTo
(
HttpStatus
.
BAD_REQUEST
);
}
...
...
spring-boot-samples/spring-boot-sample-jersey1/src/test/java/sample/jersey1/SampleJersey1ApplicationTests.java
View file @
07a50bb1
...
...
@@ -19,7 +19,7 @@ package sample.jersey1;
import
org.junit.Test
;
import
org.junit.runner.RunWith
;
import
org.springframework.b
oot.context.embedded.LocalServerPort
;
import
org.springframework.b
eans.factory.annotation.Autowired
;
import
org.springframework.boot.test.context.SpringBootTest
;
import
org.springframework.boot.test.context.SpringBootTest.WebEnvironment
;
import
org.springframework.boot.test.web.client.TestRestTemplate
;
...
...
@@ -31,14 +31,13 @@ import static org.assertj.core.api.Assertions.assertThat;
@SpringBootTest
(
webEnvironment
=
WebEnvironment
.
RANDOM_PORT
)
public
class
SampleJersey1ApplicationTests
{
@
LocalServerPort
private
int
port
;
@
Autowired
private
TestRestTemplate
restTemplate
;
@Test
public
void
contextLoads
()
{
assertThat
(
new
TestRestTemplate
()
.
getForObject
(
"http://localhost:"
+
this
.
port
+
"/"
,
String
.
class
))
.
isEqualTo
(
"Hello World"
);
public
void
rootReturnsHelloWorld
()
{
assertThat
(
this
.
restTemplate
.
getForObject
(
"/"
,
String
.
class
))
.
isEqualTo
(
"Hello World"
);
}
}
spring-boot-samples/spring-boot-sample-jetty-jsp/src/test/java/sample/jetty/jsp/SampleWebJspApplicationTests.java
View file @
07a50bb1
...
...
@@ -19,7 +19,7 @@ package sample.jetty.jsp;
import
org.junit.Test
;
import
org.junit.runner.RunWith
;
import
org.springframework.b
oot.context.embedded.LocalServerPort
;
import
org.springframework.b
eans.factory.annotation.Autowired
;
import
org.springframework.boot.test.context.SpringBootTest
;
import
org.springframework.boot.test.context.SpringBootTest.WebEnvironment
;
import
org.springframework.boot.test.web.client.TestRestTemplate
;
...
...
@@ -40,13 +40,12 @@ import static org.assertj.core.api.Assertions.assertThat;
@DirtiesContext
public
class
SampleWebJspApplicationTests
{
@
LocalServerPort
private
int
port
;
@
Autowired
private
TestRestTemplate
restTemplate
;
@Test
public
void
testJspWithEl
()
throws
Exception
{
ResponseEntity
<
String
>
entity
=
new
TestRestTemplate
()
.
getForEntity
(
"http://localhost:"
+
this
.
port
,
String
.
class
);
ResponseEntity
<
String
>
entity
=
this
.
restTemplate
.
getForEntity
(
"/"
,
String
.
class
);
assertThat
(
entity
.
getStatusCode
()).
isEqualTo
(
HttpStatus
.
OK
);
assertThat
(
entity
.
getBody
()).
contains
(
"/resources/text.txt"
);
}
...
...
spring-boot-samples/spring-boot-sample-jetty/src/test/java/sample/jetty/SampleJettyApplicationTests.java
View file @
07a50bb1
...
...
@@ -23,7 +23,7 @@ import java.util.zip.GZIPInputStream;
import
org.junit.Test
;
import
org.junit.runner.RunWith
;
import
org.springframework.b
oot.context.embedded.LocalServerPort
;
import
org.springframework.b
eans.factory.annotation.Autowired
;
import
org.springframework.boot.test.context.SpringBootTest
;
import
org.springframework.boot.test.context.SpringBootTest.WebEnvironment
;
import
org.springframework.boot.test.web.client.TestRestTemplate
;
...
...
@@ -49,13 +49,12 @@ import static org.assertj.core.api.Assertions.assertThat;
@DirtiesContext
public
class
SampleJettyApplicationTests
{
@
LocalServerPort
private
int
port
;
@
Autowired
private
TestRestTemplate
restTemplate
;
@Test
public
void
testHome
()
throws
Exception
{
ResponseEntity
<
String
>
entity
=
new
TestRestTemplate
()
.
getForEntity
(
"http://localhost:"
+
this
.
port
,
String
.
class
);
ResponseEntity
<
String
>
entity
=
this
.
restTemplate
.
getForEntity
(
"/"
,
String
.
class
);
assertThat
(
entity
.
getStatusCode
()).
isEqualTo
(
HttpStatus
.
OK
);
assertThat
(
entity
.
getBody
()).
isEqualTo
(
"Hello World"
);
}
...
...
@@ -66,11 +65,8 @@ public class SampleJettyApplicationTests {
requestHeaders
.
set
(
"Accept-Encoding"
,
"gzip"
);
HttpEntity
<?>
requestEntity
=
new
HttpEntity
<
Object
>(
requestHeaders
);
TestRestTemplate
restTemplate
=
new
TestRestTemplate
();
ResponseEntity
<
byte
[]>
entity
=
restTemplate
.
exchange
(
"http://localhost:"
+
this
.
port
,
HttpMethod
.
GET
,
requestEntity
,
byte
[].
class
);
ResponseEntity
<
byte
[]>
entity
=
this
.
restTemplate
.
exchange
(
"/"
,
HttpMethod
.
GET
,
requestEntity
,
byte
[].
class
);
assertThat
(
entity
.
getStatusCode
()).
isEqualTo
(
HttpStatus
.
OK
);
...
...
spring-boot-samples/spring-boot-sample-jetty8-ssl/src/test/java/sample/jetty8/ssl/SampleJetty8SslApplicationTests.java
View file @
07a50bb1
...
...
@@ -19,11 +19,11 @@ package sample.jetty8.ssl;
import
org.junit.Test
;
import
org.junit.runner.RunWith
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.boot.context.embedded.LocalServerPort
;
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.test.web.client.TestRestTemplate.HttpClientOption
;
import
org.springframework.http.HttpStatus
;
import
org.springframework.http.ResponseEntity
;
import
org.springframework.test.annotation.DirtiesContext
;
...
...
@@ -41,14 +41,15 @@ import static org.assertj.core.api.Assertions.assertThat;
@DirtiesContext
public
class
SampleJetty8SslApplicationTests
{
@Autowired
private
TestRestTemplate
restTemplate
;
@LocalServerPort
private
int
port
;
@Test
public
void
testHome
()
throws
Exception
{
TestRestTemplate
testRestTemplate
=
new
TestRestTemplate
(
HttpClientOption
.
SSL
);
ResponseEntity
<
String
>
entity
=
testRestTemplate
.
getForEntity
(
"https://localhost:"
+
this
.
port
,
String
.
class
);
ResponseEntity
<
String
>
entity
=
this
.
restTemplate
.
getForEntity
(
"/"
,
String
.
class
);
assertThat
(
entity
.
getStatusCode
()).
isEqualTo
(
HttpStatus
.
OK
);
assertThat
(
entity
.
getBody
()).
isEqualTo
(
"Hello World"
);
}
...
...
spring-boot-samples/spring-boot-sample-jetty8/src/test/java/sample/jetty8/SampleJetty8ApplicationTests.java
View file @
07a50bb1
...
...
@@ -23,7 +23,7 @@ import java.util.zip.GZIPInputStream;
import
org.junit.Test
;
import
org.junit.runner.RunWith
;
import
org.springframework.b
oot.context.embedded.LocalServerPort
;
import
org.springframework.b
eans.factory.annotation.Autowired
;
import
org.springframework.boot.test.context.SpringBootTest
;
import
org.springframework.boot.test.context.SpringBootTest.WebEnvironment
;
import
org.springframework.boot.test.web.client.TestRestTemplate
;
...
...
@@ -49,13 +49,12 @@ import static org.assertj.core.api.Assertions.assertThat;
@DirtiesContext
public
class
SampleJetty8ApplicationTests
{
@
LocalServerPort
private
int
port
;
@
Autowired
private
TestRestTemplate
restTemplate
;
@Test
public
void
testHome
()
throws
Exception
{
ResponseEntity
<
String
>
entity
=
new
TestRestTemplate
()
.
getForEntity
(
"http://localhost:"
+
this
.
port
,
String
.
class
);
ResponseEntity
<
String
>
entity
=
this
.
restTemplate
.
getForEntity
(
"/"
,
String
.
class
);
assertThat
(
entity
.
getStatusCode
()).
isEqualTo
(
HttpStatus
.
OK
);
assertThat
(
entity
.
getBody
()).
isEqualTo
(
"Hello World"
);
}
...
...
@@ -65,10 +64,8 @@ public class SampleJetty8ApplicationTests {
HttpHeaders
requestHeaders
=
new
HttpHeaders
();
requestHeaders
.
set
(
"Accept-Encoding"
,
"gzip"
);
HttpEntity
<?>
requestEntity
=
new
HttpEntity
<
Object
>(
requestHeaders
);
TestRestTemplate
restTemplate
=
new
TestRestTemplate
();
ResponseEntity
<
byte
[]>
entity
=
restTemplate
.
exchange
(
"http://localhost:"
+
this
.
port
,
HttpMethod
.
GET
,
requestEntity
,
byte
[].
class
);
ResponseEntity
<
byte
[]>
entity
=
this
.
restTemplate
.
exchange
(
"/"
,
HttpMethod
.
GET
,
requestEntity
,
byte
[].
class
);
assertThat
(
entity
.
getStatusCode
()).
isEqualTo
(
HttpStatus
.
OK
);
GZIPInputStream
inflater
=
new
GZIPInputStream
(
new
ByteArrayInputStream
(
entity
.
getBody
()));
...
...
spring-boot-samples/spring-boot-sample-jetty92/src/test/java/sample/jetty92/SampleJetty92ApplicationTests.java
View file @
07a50bb1
...
...
@@ -23,7 +23,7 @@ import java.util.zip.GZIPInputStream;
import
org.junit.Test
;
import
org.junit.runner.RunWith
;
import
org.springframework.b
oot.context.embedded.LocalServerPort
;
import
org.springframework.b
eans.factory.annotation.Autowired
;
import
org.springframework.boot.test.context.SpringBootTest
;
import
org.springframework.boot.test.context.SpringBootTest.WebEnvironment
;
import
org.springframework.boot.test.web.client.TestRestTemplate
;
...
...
@@ -49,13 +49,12 @@ import static org.assertj.core.api.Assertions.assertThat;
@DirtiesContext
public
class
SampleJetty92ApplicationTests
{
@
LocalServerPort
private
int
port
;
@
Autowired
private
TestRestTemplate
restTemplate
;
@Test
public
void
testHome
()
throws
Exception
{
ResponseEntity
<
String
>
entity
=
new
TestRestTemplate
()
.
getForEntity
(
"http://localhost:"
+
this
.
port
,
String
.
class
);
ResponseEntity
<
String
>
entity
=
this
.
restTemplate
.
getForEntity
(
"/"
,
String
.
class
);
assertThat
(
entity
.
getStatusCode
()).
isEqualTo
(
HttpStatus
.
OK
);
assertThat
(
entity
.
getBody
()).
isEqualTo
(
"Hello World"
);
}
...
...
@@ -65,10 +64,8 @@ public class SampleJetty92ApplicationTests {
HttpHeaders
requestHeaders
=
new
HttpHeaders
();
requestHeaders
.
set
(
"Accept-Encoding"
,
"gzip"
);
HttpEntity
<?>
requestEntity
=
new
HttpEntity
<
Object
>(
requestHeaders
);
TestRestTemplate
restTemplate
=
new
TestRestTemplate
();
ResponseEntity
<
byte
[]>
entity
=
restTemplate
.
exchange
(
"http://localhost:"
+
this
.
port
,
HttpMethod
.
GET
,
requestEntity
,
byte
[].
class
);
ResponseEntity
<
byte
[]>
entity
=
this
.
restTemplate
.
exchange
(
"/"
,
HttpMethod
.
GET
,
requestEntity
,
byte
[].
class
);
assertThat
(
entity
.
getStatusCode
()).
isEqualTo
(
HttpStatus
.
OK
);
GZIPInputStream
inflater
=
new
GZIPInputStream
(
new
ByteArrayInputStream
(
entity
.
getBody
()));
...
...
spring-boot-samples/spring-boot-sample-servlet/src/test/java/sample/servlet/SampleServletApplicationTests.java
View file @
07a50bb1
...
...
@@ -21,7 +21,6 @@ import org.junit.runner.RunWith;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.boot.autoconfigure.security.SecurityProperties
;
import
org.springframework.boot.context.embedded.LocalServerPort
;
import
org.springframework.boot.test.context.SpringBootTest
;
import
org.springframework.boot.test.context.SpringBootTest.WebEnvironment
;
import
org.springframework.boot.test.web.client.TestRestTemplate
;
...
...
@@ -42,23 +41,22 @@ import static org.assertj.core.api.Assertions.assertThat;
@DirtiesContext
public
class
SampleServletApplicationTests
{
@
LocalServerPort
private
int
port
;
@
Autowired
private
TestRestTemplate
restTemplate
;
@Autowired
private
SecurityProperties
security
;
@Test
public
void
testHomeIsSecure
()
throws
Exception
{
ResponseEntity
<
String
>
entity
=
new
TestRestTemplate
()
.
getForEntity
(
"http://localhost:"
+
this
.
port
,
String
.
class
);
ResponseEntity
<
String
>
entity
=
this
.
restTemplate
.
getForEntity
(
"/"
,
String
.
class
);
assertThat
(
entity
.
getStatusCode
()).
isEqualTo
(
HttpStatus
.
UNAUTHORIZED
);
}
@Test
public
void
testHome
()
throws
Exception
{
ResponseEntity
<
String
>
entity
=
new
TestRestTemplate
(
"user"
,
getPassword
())
.
getForEntity
(
"http://localhost:"
+
this
.
port
,
String
.
class
);
ResponseEntity
<
String
>
entity
=
this
.
restTemplate
.
withBasicAuth
(
"user"
,
getPassword
()).
getForEntity
(
"/"
,
String
.
class
);
assertThat
(
entity
.
getStatusCode
()).
isEqualTo
(
HttpStatus
.
OK
);
assertThat
(
entity
.
getBody
()).
isEqualTo
(
"Hello World"
);
}
...
...
spring-boot-samples/spring-boot-sample-testng/src/test/java/sample/testng/SampleTestNGApplicationTests.java
View file @
07a50bb1
...
...
@@ -18,7 +18,7 @@ package sample.testng;
import
org.testng.annotations.Test
;
import
org.springframework.b
oot.context.embedded.LocalServerPort
;
import
org.springframework.b
eans.factory.annotation.Autowired
;
import
org.springframework.boot.test.context.SpringBootTest
;
import
org.springframework.boot.test.context.SpringBootTest.WebEnvironment
;
import
org.springframework.boot.test.web.client.TestRestTemplate
;
...
...
@@ -38,13 +38,12 @@ import static org.assertj.core.api.Assertions.assertThat;
@DirtiesContext
public
class
SampleTestNGApplicationTests
extends
AbstractTestNGSpringContextTests
{
@
LocalServerPort
private
int
port
;
@
Autowired
private
TestRestTemplate
restTemplate
;
@Test
public
void
testHome
()
throws
Exception
{
ResponseEntity
<
String
>
entity
=
new
TestRestTemplate
()
.
getForEntity
(
"http://localhost:"
+
this
.
port
,
String
.
class
);
ResponseEntity
<
String
>
entity
=
this
.
restTemplate
.
getForEntity
(
"/"
,
String
.
class
);
assertThat
(
entity
.
getStatusCode
()).
isEqualTo
(
HttpStatus
.
OK
);
assertThat
(
entity
.
getBody
()).
isEqualTo
(
"Hello World"
);
}
...
...
spring-boot-samples/spring-boot-sample-tomcat-jsp/src/test/java/sample/tomcat/jsp/SampleWebJspApplicationTests.java
View file @
07a50bb1
...
...
@@ -19,7 +19,7 @@ package sample.tomcat.jsp;
import
org.junit.Test
;
import
org.junit.runner.RunWith
;
import
org.springframework.b
oot.context.embedded.LocalServerPort
;
import
org.springframework.b
eans.factory.annotation.Autowired
;
import
org.springframework.boot.test.context.SpringBootTest
;
import
org.springframework.boot.test.context.SpringBootTest.WebEnvironment
;
import
org.springframework.boot.test.web.client.TestRestTemplate
;
...
...
@@ -40,13 +40,12 @@ import static org.assertj.core.api.Assertions.assertThat;
@DirtiesContext
public
class
SampleWebJspApplicationTests
{
@
LocalServerPort
private
int
port
;
@
Autowired
private
TestRestTemplate
restTemplate
;
@Test
public
void
testJspWithEl
()
throws
Exception
{
ResponseEntity
<
String
>
entity
=
new
TestRestTemplate
()
.
getForEntity
(
"http://localhost:"
+
this
.
port
,
String
.
class
);
ResponseEntity
<
String
>
entity
=
this
.
restTemplate
.
getForEntity
(
"/"
,
String
.
class
);
assertThat
(
entity
.
getStatusCode
()).
isEqualTo
(
HttpStatus
.
OK
);
assertThat
(
entity
.
getBody
()).
contains
(
"/resources/text.txt"
);
}
...
...
spring-boot-samples/spring-boot-sample-tomcat-ssl/src/test/java/sample/tomcat/ssl/SampleTomcatSslApplicationTests.java
View file @
07a50bb1
...
...
@@ -19,11 +19,10 @@ package sample.tomcat.ssl;
import
org.junit.Test
;
import
org.junit.runner.RunWith
;
import
org.springframework.b
oot.context.embedded.LocalServerPort
;
import
org.springframework.b
eans.factory.annotation.Autowired
;
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.test.web.client.TestRestTemplate.HttpClientOption
;
import
org.springframework.http.HttpStatus
;
import
org.springframework.http.ResponseEntity
;
import
org.springframework.test.annotation.DirtiesContext
;
...
...
@@ -36,14 +35,12 @@ import static org.assertj.core.api.Assertions.assertThat;
@DirtiesContext
public
class
SampleTomcatSslApplicationTests
{
@
LocalServerPort
private
int
port
;
@
Autowired
private
TestRestTemplate
restTemplate
;
@Test
public
void
testHome
()
throws
Exception
{
TestRestTemplate
testRestTemplate
=
new
TestRestTemplate
(
HttpClientOption
.
SSL
);
ResponseEntity
<
String
>
entity
=
testRestTemplate
.
getForEntity
(
"https://localhost:"
+
this
.
port
,
String
.
class
);
ResponseEntity
<
String
>
entity
=
this
.
restTemplate
.
getForEntity
(
"/"
,
String
.
class
);
assertThat
(
entity
.
getStatusCode
()).
isEqualTo
(
HttpStatus
.
OK
);
assertThat
(
entity
.
getBody
()).
isEqualTo
(
"Hello, world"
);
}
...
...
spring-boot-samples/spring-boot-sample-tomcat/src/test/java/sample/tomcat/NonAutoConfigurationSampleTomcatApplicationTests.java
View file @
07a50bb1
...
...
@@ -21,6 +21,7 @@ import org.junit.runner.RunWith;
import
sample.tomcat.service.HelloWorldService
;
import
sample.tomcat.web.SampleController
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.boot.SpringApplication
;
import
org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration
;
import
org.springframework.boot.autoconfigure.web.DispatcherServletAutoConfiguration
;
...
...
@@ -28,7 +29,6 @@ import org.springframework.boot.autoconfigure.web.EmbeddedServletContainerAutoCo
import
org.springframework.boot.autoconfigure.web.HttpMessageConvertersAutoConfiguration
;
import
org.springframework.boot.autoconfigure.web.ServerPropertiesAutoConfiguration
;
import
org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration
;
import
org.springframework.boot.context.embedded.LocalServerPort
;
import
org.springframework.boot.test.context.SpringBootTest
;
import
org.springframework.boot.test.context.SpringBootTest.WebEnvironment
;
import
org.springframework.boot.test.web.client.TestRestTemplate
;
...
...
@@ -52,8 +52,8 @@ import static org.assertj.core.api.Assertions.assertThat;
@DirtiesContext
public
class
NonAutoConfigurationSampleTomcatApplicationTests
{
@
LocalServerPort
private
int
port
;
@
Autowired
private
TestRestTemplate
restTemplate
;
@Configuration
@Import
({
EmbeddedServletContainerAutoConfiguration
.
class
,
...
...
@@ -73,8 +73,7 @@ public class NonAutoConfigurationSampleTomcatApplicationTests {
@Test
public
void
testHome
()
throws
Exception
{
ResponseEntity
<
String
>
entity
=
new
TestRestTemplate
()
.
getForEntity
(
"http://localhost:"
+
this
.
port
,
String
.
class
);
ResponseEntity
<
String
>
entity
=
this
.
restTemplate
.
getForEntity
(
"/"
,
String
.
class
);
assertThat
(
entity
.
getStatusCode
()).
isEqualTo
(
HttpStatus
.
OK
);
assertThat
(
entity
.
getBody
()).
isEqualTo
(
"Hello World"
);
}
...
...
spring-boot-samples/spring-boot-sample-tomcat/src/test/java/sample/tomcat/SampleTomcatApplicationTests.java
View file @
07a50bb1
...
...
@@ -23,7 +23,7 @@ import java.util.zip.GZIPInputStream;
import
org.junit.Test
;
import
org.junit.runner.RunWith
;
import
org.springframework.b
oot.context.embedded.LocalServerPort
;
import
org.springframework.b
eans.factory.annotation.Autowired
;
import
org.springframework.boot.test.context.SpringBootTest
;
import
org.springframework.boot.test.context.SpringBootTest.WebEnvironment
;
import
org.springframework.boot.test.web.client.TestRestTemplate
;
...
...
@@ -49,13 +49,12 @@ import static org.assertj.core.api.Assertions.assertThat;
@DirtiesContext
public
class
SampleTomcatApplicationTests
{
@
LocalServerPort
private
int
port
;
@
Autowired
private
TestRestTemplate
restTemplate
;
@Test
public
void
testHome
()
throws
Exception
{
ResponseEntity
<
String
>
entity
=
new
TestRestTemplate
()
.
getForEntity
(
"http://localhost:"
+
this
.
port
,
String
.
class
);
ResponseEntity
<
String
>
entity
=
this
.
restTemplate
.
getForEntity
(
"/"
,
String
.
class
);
assertThat
(
entity
.
getStatusCode
()).
isEqualTo
(
HttpStatus
.
OK
);
assertThat
(
entity
.
getBody
()).
isEqualTo
(
"Hello World"
);
}
...
...
@@ -65,10 +64,8 @@ public class SampleTomcatApplicationTests {
HttpHeaders
requestHeaders
=
new
HttpHeaders
();
requestHeaders
.
set
(
"Accept-Encoding"
,
"gzip"
);
HttpEntity
<?>
requestEntity
=
new
HttpEntity
<
Object
>(
requestHeaders
);
TestRestTemplate
restTemplate
=
new
TestRestTemplate
();
ResponseEntity
<
byte
[]>
entity
=
restTemplate
.
exchange
(
"http://localhost:"
+
this
.
port
,
HttpMethod
.
GET
,
requestEntity
,
byte
[].
class
);
ResponseEntity
<
byte
[]>
entity
=
this
.
restTemplate
.
exchange
(
"/"
,
HttpMethod
.
GET
,
requestEntity
,
byte
[].
class
);
assertThat
(
entity
.
getStatusCode
()).
isEqualTo
(
HttpStatus
.
OK
);
GZIPInputStream
inflater
=
new
GZIPInputStream
(
new
ByteArrayInputStream
(
entity
.
getBody
()));
...
...
spring-boot-samples/spring-boot-sample-tomcat7-jsp/src/test/java/sample/tomcat7/jsp/SampleWebJspApplicationTests.java
View file @
07a50bb1
...
...
@@ -19,7 +19,7 @@ package sample.tomcat7.jsp;
import
org.junit.Test
;
import
org.junit.runner.RunWith
;
import
org.springframework.b
oot.context.embedded.LocalServerPort
;
import
org.springframework.b
eans.factory.annotation.Autowired
;
import
org.springframework.boot.test.context.SpringBootTest
;
import
org.springframework.boot.test.context.SpringBootTest.WebEnvironment
;
import
org.springframework.boot.test.web.client.TestRestTemplate
;
...
...
@@ -40,13 +40,12 @@ import static org.assertj.core.api.Assertions.assertThat;
@DirtiesContext
public
class
SampleWebJspApplicationTests
{
@
LocalServerPort
private
int
port
;
@
Autowired
private
TestRestTemplate
restTemplate
;
@Test
public
void
testJspWithEl
()
throws
Exception
{
ResponseEntity
<
String
>
entity
=
new
TestRestTemplate
()
.
getForEntity
(
"http://localhost:"
+
this
.
port
,
String
.
class
);
ResponseEntity
<
String
>
entity
=
this
.
restTemplate
.
getForEntity
(
"/"
,
String
.
class
);
assertThat
(
entity
.
getStatusCode
()).
isEqualTo
(
HttpStatus
.
OK
);
assertThat
(
entity
.
getBody
()).
contains
(
"/resources/text.txt"
);
}
...
...
spring-boot-samples/spring-boot-sample-tomcat7-ssl/src/test/java/sample/tomcat/ssl/SampleTomcatSslApplicationTests.java
View file @
07a50bb1
...
...
@@ -19,11 +19,10 @@ package sample.tomcat.ssl;
import
org.junit.Test
;
import
org.junit.runner.RunWith
;
import
org.springframework.b
oot.context.embedded.LocalServerPort
;
import
org.springframework.b
eans.factory.annotation.Autowired
;
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.test.web.client.TestRestTemplate.HttpClientOption
;
import
org.springframework.http.HttpStatus
;
import
org.springframework.http.ResponseEntity
;
import
org.springframework.test.annotation.DirtiesContext
;
...
...
@@ -36,14 +35,12 @@ import static org.assertj.core.api.Assertions.assertThat;
@DirtiesContext
public
class
SampleTomcatSslApplicationTests
{
@
LocalServerPort
private
int
port
;
@
Autowired
private
TestRestTemplate
restTemplate
;
@Test
public
void
testHome
()
throws
Exception
{
TestRestTemplate
testRestTemplate
=
new
TestRestTemplate
(
HttpClientOption
.
SSL
);
ResponseEntity
<
String
>
entity
=
testRestTemplate
.
getForEntity
(
"https://localhost:"
+
this
.
port
,
String
.
class
);
ResponseEntity
<
String
>
entity
=
this
.
restTemplate
.
getForEntity
(
"/"
,
String
.
class
);
assertThat
(
entity
.
getStatusCode
()).
isEqualTo
(
HttpStatus
.
OK
);
assertThat
(
entity
.
getBody
()).
isEqualTo
(
"Hello, world"
);
}
...
...
spring-boot-samples/spring-boot-sample-tomcat80-ssl/src/test/java/sample/tomcat/ssl/SampleTomcatSslApplicationTests.java
View file @
07a50bb1
...
...
@@ -19,11 +19,10 @@ package sample.tomcat.ssl;
import
org.junit.Test
;
import
org.junit.runner.RunWith
;
import
org.springframework.b
oot.context.embedded.LocalServerPort
;
import
org.springframework.b
eans.factory.annotation.Autowired
;
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.test.web.client.TestRestTemplate.HttpClientOption
;
import
org.springframework.http.HttpStatus
;
import
org.springframework.http.ResponseEntity
;
import
org.springframework.test.annotation.DirtiesContext
;
...
...
@@ -36,14 +35,12 @@ import static org.assertj.core.api.Assertions.assertThat;
@DirtiesContext
public
class
SampleTomcatSslApplicationTests
{
@
LocalServerPort
private
int
port
;
@
Autowired
private
TestRestTemplate
restTemplate
;
@Test
public
void
testHome
()
throws
Exception
{
TestRestTemplate
testRestTemplate
=
new
TestRestTemplate
(
HttpClientOption
.
SSL
);
ResponseEntity
<
String
>
entity
=
testRestTemplate
.
getForEntity
(
"https://localhost:"
+
this
.
port
,
String
.
class
);
ResponseEntity
<
String
>
entity
=
this
.
restTemplate
.
getForEntity
(
"/"
,
String
.
class
);
assertThat
(
entity
.
getStatusCode
()).
isEqualTo
(
HttpStatus
.
OK
);
assertThat
(
entity
.
getBody
()).
isEqualTo
(
"Hello, world"
);
}
...
...
spring-boot-samples/spring-boot-sample-traditional/src/test/java/sample/traditional/SampleTraditionalApplicationTests.java
View file @
07a50bb1
...
...
@@ -19,7 +19,7 @@ package sample.traditional;
import
org.junit.Test
;
import
org.junit.runner.RunWith
;
import
org.springframework.b
oot.context.embedded.LocalServerPort
;
import
org.springframework.b
eans.factory.annotation.Autowired
;
import
org.springframework.boot.test.context.SpringBootTest
;
import
org.springframework.boot.test.context.SpringBootTest.WebEnvironment
;
import
org.springframework.boot.test.web.client.TestRestTemplate
;
...
...
@@ -40,13 +40,12 @@ import static org.assertj.core.api.Assertions.assertThat;
@DirtiesContext
public
class
SampleTraditionalApplicationTests
{
@
LocalServerPort
private
int
port
;
@
Autowired
private
TestRestTemplate
restTemplate
;
@Test
public
void
testHomeJsp
()
throws
Exception
{
ResponseEntity
<
String
>
entity
=
new
TestRestTemplate
()
.
getForEntity
(
"http://localhost:"
+
this
.
port
,
String
.
class
);
ResponseEntity
<
String
>
entity
=
this
.
restTemplate
.
getForEntity
(
"/"
,
String
.
class
);
assertThat
(
entity
.
getStatusCode
()).
isEqualTo
(
HttpStatus
.
OK
);
String
body
=
entity
.
getBody
();
assertThat
(
body
).
contains
(
"<html>"
).
contains
(
"<h1>Home</h1>"
);
...
...
@@ -54,8 +53,8 @@ public class SampleTraditionalApplicationTests {
@Test
public
void
testStaticPage
()
throws
Exception
{
ResponseEntity
<
String
>
entity
=
new
TestRestTemplate
().
getForEntity
(
"http://localhost:"
+
this
.
port
+
"/index.html"
,
String
.
class
);
ResponseEntity
<
String
>
entity
=
this
.
restTemplate
.
getForEntity
(
"/index.html"
,
String
.
class
);
assertThat
(
entity
.
getStatusCode
()).
isEqualTo
(
HttpStatus
.
OK
);
String
body
=
entity
.
getBody
();
assertThat
(
body
).
contains
(
"<html>"
).
contains
(
"<h1>Hello</h1>"
);
...
...
spring-boot-samples/spring-boot-sample-undertow-ssl/src/test/java/sample/undertow/ssl/SampleUndertowSslApplicationTests.java
View file @
07a50bb1
...
...
@@ -19,11 +19,10 @@ package sample.undertow.ssl;
import
org.junit.Test
;
import
org.junit.runner.RunWith
;
import
org.springframework.b
oot.context.embedded.LocalServerPort
;
import
org.springframework.b
eans.factory.annotation.Autowired
;
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.test.web.client.TestRestTemplate.HttpClientOption
;
import
org.springframework.http.HttpStatus
;
import
org.springframework.http.ResponseEntity
;
import
org.springframework.test.annotation.DirtiesContext
;
...
...
@@ -41,14 +40,12 @@ import static org.assertj.core.api.Assertions.assertThat;
@DirtiesContext
public
class
SampleUndertowSslApplicationTests
{
@
LocalServerPort
private
int
port
;
@
Autowired
private
TestRestTemplate
restTemplate
;
@Test
public
void
testHome
()
throws
Exception
{
TestRestTemplate
testRestTemplate
=
new
TestRestTemplate
(
HttpClientOption
.
SSL
);
ResponseEntity
<
String
>
entity
=
testRestTemplate
.
getForEntity
(
"https://localhost:"
+
this
.
port
,
String
.
class
);
ResponseEntity
<
String
>
entity
=
this
.
restTemplate
.
getForEntity
(
"/"
,
String
.
class
);
assertThat
(
entity
.
getStatusCode
()).
isEqualTo
(
HttpStatus
.
OK
);
assertThat
(
entity
.
getBody
()).
isEqualTo
(
"Hello World"
);
}
...
...
spring-boot-samples/spring-boot-sample-undertow/src/test/java/sample/undertow/SampleUndertowApplicationTests.java
View file @
07a50bb1
...
...
@@ -23,7 +23,7 @@ import java.util.zip.GZIPInputStream;
import
org.junit.Test
;
import
org.junit.runner.RunWith
;
import
org.springframework.b
oot.context.embedded.LocalServerPort
;
import
org.springframework.b
eans.factory.annotation.Autowired
;
import
org.springframework.boot.test.context.SpringBootTest
;
import
org.springframework.boot.test.context.SpringBootTest.WebEnvironment
;
import
org.springframework.boot.test.web.client.TestRestTemplate
;
...
...
@@ -49,8 +49,8 @@ import static org.assertj.core.api.Assertions.assertThat;
@DirtiesContext
public
class
SampleUndertowApplicationTests
{
@
LocalServerPort
private
int
port
;
@
Autowired
private
TestRestTemplate
restTemplate
;
@Test
public
void
testHome
()
throws
Exception
{
...
...
@@ -67,10 +67,8 @@ public class SampleUndertowApplicationTests {
HttpHeaders
requestHeaders
=
new
HttpHeaders
();
requestHeaders
.
set
(
"Accept-Encoding"
,
"gzip"
);
HttpEntity
<?>
requestEntity
=
new
HttpEntity
<
Object
>(
requestHeaders
);
TestRestTemplate
restTemplate
=
new
TestRestTemplate
();
ResponseEntity
<
byte
[]>
entity
=
restTemplate
.
exchange
(
"http://localhost:"
+
this
.
port
,
HttpMethod
.
GET
,
requestEntity
,
byte
[].
class
);
ResponseEntity
<
byte
[]>
entity
=
this
.
restTemplate
.
exchange
(
"/"
,
HttpMethod
.
GET
,
requestEntity
,
byte
[].
class
);
assertThat
(
entity
.
getStatusCode
()).
isEqualTo
(
HttpStatus
.
OK
);
GZIPInputStream
inflater
=
new
GZIPInputStream
(
new
ByteArrayInputStream
(
entity
.
getBody
()));
...
...
@@ -84,8 +82,8 @@ public class SampleUndertowApplicationTests {
}
private
void
assertOkResponse
(
String
path
,
String
body
)
{
ResponseEntity
<
String
>
entity
=
new
TestRestTemplate
()
.
getForEntity
(
"http://localhost:"
+
this
.
port
+
path
,
String
.
class
);
ResponseEntity
<
String
>
entity
=
this
.
restTemplate
.
getForEntity
(
path
,
String
.
class
);
assertThat
(
entity
.
getStatusCode
()).
isEqualTo
(
HttpStatus
.
OK
);
assertThat
(
entity
.
getBody
()).
isEqualTo
(
body
);
}
...
...
spring-boot-samples/spring-boot-sample-web-freemarker/src/test/java/sample/freemarker/SampleWebFreeMarkerApplicationTests.java
View file @
07a50bb1
...
...
@@ -21,7 +21,7 @@ import java.util.Arrays;
import
org.junit.Test
;
import
org.junit.runner.RunWith
;
import
org.springframework.b
oot.context.embedded.LocalServerPort
;
import
org.springframework.b
eans.factory.annotation.Autowired
;
import
org.springframework.boot.test.context.SpringBootTest
;
import
org.springframework.boot.test.context.SpringBootTest.WebEnvironment
;
import
org.springframework.boot.test.web.client.TestRestTemplate
;
...
...
@@ -47,13 +47,13 @@ import static org.assertj.core.api.Assertions.assertThat;
@DirtiesContext
public
class
SampleWebFreeMarkerApplicationTests
{
@
LocalServerPort
private
int
port
;
@
Autowired
private
TestRestTemplate
testRestTemplate
;
@Test
public
void
testFreeMarkerTemplate
()
throws
Exception
{
ResponseEntity
<
String
>
entity
=
new
TestRestTemplate
()
.
getForEntity
(
"http://localhost:"
+
this
.
port
,
String
.
class
);
ResponseEntity
<
String
>
entity
=
this
.
testRestTemplate
.
getForEntity
(
"/"
,
String
.
class
);
assertThat
(
entity
.
getStatusCode
()).
isEqualTo
(
HttpStatus
.
OK
);
assertThat
(
entity
.
getBody
()).
contains
(
"Hello, Andy"
);
}
...
...
@@ -64,9 +64,8 @@ public class SampleWebFreeMarkerApplicationTests {
headers
.
setAccept
(
Arrays
.
asList
(
MediaType
.
TEXT_HTML
));
HttpEntity
<
String
>
requestEntity
=
new
HttpEntity
<
String
>(
headers
);
ResponseEntity
<
String
>
responseEntity
=
new
TestRestTemplate
().
exchange
(
"http://localhost:"
+
this
.
port
+
"/does-not-exist"
,
HttpMethod
.
GET
,
requestEntity
,
String
.
class
);
ResponseEntity
<
String
>
responseEntity
=
this
.
testRestTemplate
.
exchange
(
"/does-not-exist"
,
HttpMethod
.
GET
,
requestEntity
,
String
.
class
);
assertThat
(
responseEntity
.
getStatusCode
()).
isEqualTo
(
HttpStatus
.
NOT_FOUND
);
assertThat
(
responseEntity
.
getBody
())
...
...
spring-boot-samples/spring-boot-sample-web-groovy-templates/src/test/java/sample/groovytemplates/SampleGroovyTemplateApplicationTests.java
View file @
07a50bb1
...
...
@@ -21,6 +21,7 @@ import java.net.URI;
import
org.junit.Test
;
import
org.junit.runner.RunWith
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.boot.context.embedded.LocalServerPort
;
import
org.springframework.boot.test.context.SpringBootTest
;
import
org.springframework.boot.test.context.SpringBootTest.WebEnvironment
;
...
...
@@ -47,10 +48,12 @@ public class SampleGroovyTemplateApplicationTests {
@LocalServerPort
private
int
port
;
@Autowired
private
TestRestTemplate
restTemplate
;
@Test
public
void
testHome
()
throws
Exception
{
ResponseEntity
<
String
>
entity
=
new
TestRestTemplate
()
.
getForEntity
(
"http://localhost:"
+
this
.
port
,
String
.
class
);
ResponseEntity
<
String
>
entity
=
this
.
restTemplate
.
getForEntity
(
"/"
,
String
.
class
);
assertThat
(
entity
.
getStatusCode
()).
isEqualTo
(
HttpStatus
.
OK
);
assertThat
(
entity
.
getBody
()).
contains
(
"<title>Messages"
);
assertThat
(
entity
.
getBody
()).
doesNotContain
(
"layout:fragment"
);
...
...
@@ -61,15 +64,14 @@ public class SampleGroovyTemplateApplicationTests {
MultiValueMap
<
String
,
String
>
map
=
new
LinkedMultiValueMap
<
String
,
String
>();
map
.
set
(
"text"
,
"FOO text"
);
map
.
set
(
"summary"
,
"FOO"
);
URI
location
=
new
TestRestTemplate
()
.
postForLocation
(
"http://localhost:"
+
this
.
port
,
map
);
URI
location
=
this
.
restTemplate
.
postForLocation
(
"/"
,
map
);
assertThat
(
location
.
toString
()).
contains
(
"localhost:"
+
this
.
port
);
}
@Test
public
void
testCss
()
throws
Exception
{
ResponseEntity
<
String
>
entity
=
new
TestRestTemplate
().
getForEntity
(
"http://localhost:"
+
this
.
port
+
"/css/bootstrap.min.css"
,
String
.
class
);
ResponseEntity
<
String
>
entity
=
this
.
restTemplate
.
getForEntity
(
"/css/bootstrap.min.css"
,
String
.
class
);
assertThat
(
entity
.
getStatusCode
()).
isEqualTo
(
HttpStatus
.
OK
);
assertThat
(
entity
.
getBody
()).
contains
(
"body"
);
}
...
...
spring-boot-samples/spring-boot-sample-web-jsp/src/test/java/sample/jsp/SampleWebJspApplicationTests.java
View file @
07a50bb1
...
...
@@ -19,7 +19,7 @@ package sample.jsp;
import
org.junit.Test
;
import
org.junit.runner.RunWith
;
import
org.springframework.b
oot.context.embedded.LocalServerPort
;
import
org.springframework.b
eans.factory.annotation.Autowired
;
import
org.springframework.boot.test.context.SpringBootTest
;
import
org.springframework.boot.test.context.SpringBootTest.WebEnvironment
;
import
org.springframework.boot.test.web.client.TestRestTemplate
;
...
...
@@ -40,13 +40,12 @@ import static org.assertj.core.api.Assertions.assertThat;
@DirtiesContext
public
class
SampleWebJspApplicationTests
{
@
LocalServerPort
private
int
port
;
@
Autowired
private
TestRestTemplate
restTemplate
;
@Test
public
void
testJspWithEl
()
throws
Exception
{
ResponseEntity
<
String
>
entity
=
new
TestRestTemplate
()
.
getForEntity
(
"http://localhost:"
+
this
.
port
,
String
.
class
);
ResponseEntity
<
String
>
entity
=
this
.
restTemplate
.
getForEntity
(
"/"
,
String
.
class
);
assertThat
(
entity
.
getStatusCode
()).
isEqualTo
(
HttpStatus
.
OK
);
assertThat
(
entity
.
getBody
()).
contains
(
"/resources/text.txt"
);
}
...
...
spring-boot-samples/spring-boot-sample-web-method-security/src/test/java/sample/security/method/SampleMethodSecurityApplicationTests.java
View file @
07a50bb1
...
...
@@ -23,6 +23,7 @@ import java.util.regex.Pattern;
import
org.junit.Test
;
import
org.junit.runner.RunWith
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.boot.context.embedded.LocalServerPort
;
import
org.springframework.boot.test.context.SpringBootTest
;
import
org.springframework.boot.test.context.SpringBootTest.WebEnvironment
;
...
...
@@ -33,6 +34,7 @@ import org.springframework.http.HttpMethod;
import
org.springframework.http.HttpStatus
;
import
org.springframework.http.MediaType
;
import
org.springframework.http.ResponseEntity
;
import
org.springframework.http.client.support.BasicAuthorizationInterceptor
;
import
org.springframework.test.annotation.DirtiesContext
;
import
org.springframework.test.context.junit4.SpringRunner
;
import
org.springframework.util.LinkedMultiValueMap
;
...
...
@@ -53,12 +55,14 @@ public class SampleMethodSecurityApplicationTests {
@LocalServerPort
private
int
port
;
@Autowired
private
TestRestTemplate
restTemplate
;
@Test
public
void
testHome
()
throws
Exception
{
HttpHeaders
headers
=
new
HttpHeaders
();
headers
.
setAccept
(
Arrays
.
asList
(
MediaType
.
TEXT_HTML
));
ResponseEntity
<
String
>
entity
=
new
TestRestTemplate
().
exchange
(
"http://localhost:"
+
this
.
port
,
HttpMethod
.
GET
,
ResponseEntity
<
String
>
entity
=
this
.
restTemplate
.
exchange
(
"/"
,
HttpMethod
.
GET
,
new
HttpEntity
<
Void
>(
headers
),
String
.
class
);
assertThat
(
entity
.
getStatusCode
()).
isEqualTo
(
HttpStatus
.
OK
);
assertThat
(
entity
.
getBody
()).
contains
(
"<title>Login"
);
...
...
@@ -72,8 +76,8 @@ public class SampleMethodSecurityApplicationTests {
form
.
set
(
"username"
,
"admin"
);
form
.
set
(
"password"
,
"admin"
);
getCsrf
(
form
,
headers
);
ResponseEntity
<
String
>
entity
=
new
TestRestTemplate
().
exchange
(
"http://localhost:"
+
this
.
port
+
"/login"
,
HttpMethod
.
POST
,
ResponseEntity
<
String
>
entity
=
this
.
restTemplate
.
exchange
(
"/login"
,
HttpMethod
.
POST
,
new
HttpEntity
<
MultiValueMap
<
String
,
String
>>(
form
,
headers
),
String
.
class
);
assertThat
(
entity
.
getStatusCode
()).
isEqualTo
(
HttpStatus
.
FOUND
);
...
...
@@ -89,14 +93,14 @@ public class SampleMethodSecurityApplicationTests {
form
.
set
(
"username"
,
"user"
);
form
.
set
(
"password"
,
"user"
);
getCsrf
(
form
,
headers
);
ResponseEntity
<
String
>
entity
=
new
TestRestTemplate
().
exchange
(
"http://localhost:"
+
this
.
port
+
"/login"
,
HttpMethod
.
POST
,
ResponseEntity
<
String
>
entity
=
this
.
restTemplate
.
exchange
(
"/login"
,
HttpMethod
.
POST
,
new
HttpEntity
<
MultiValueMap
<
String
,
String
>>(
form
,
headers
),
String
.
class
);
assertThat
(
entity
.
getStatusCode
()).
isEqualTo
(
HttpStatus
.
FOUND
);
String
cookie
=
entity
.
getHeaders
().
getFirst
(
"Set-Cookie"
);
headers
.
set
(
"Cookie"
,
cookie
);
ResponseEntity
<
String
>
page
=
new
TestRestTemplate
()
.
exchange
(
ResponseEntity
<
String
>
page
=
this
.
restTemplate
.
exchange
(
entity
.
getHeaders
().
getLocation
(),
HttpMethod
.
GET
,
new
HttpEntity
<
Void
>(
headers
),
String
.
class
);
assertThat
(
page
.
getStatusCode
()).
isEqualTo
(
HttpStatus
.
FORBIDDEN
);
...
...
@@ -105,28 +109,46 @@ public class SampleMethodSecurityApplicationTests {
@Test
public
void
testManagementProtected
()
throws
Exception
{
ResponseEntity
<
String
>
entity
=
new
TestRestTemplate
()
.
getForEntity
(
"http://localhost:"
+
this
.
port
+
"/beans"
,
String
.
class
);
ResponseEntity
<
String
>
entity
=
this
.
restTemplate
.
getForEntity
(
"/beans"
,
String
.
class
);
assertThat
(
entity
.
getStatusCode
()).
isEqualTo
(
HttpStatus
.
UNAUTHORIZED
);
}
@Test
public
void
testManagementAuthorizedAccess
()
throws
Exception
{
ResponseEntity
<
String
>
entity
=
new
TestRestTemplate
(
"admin"
,
"admin"
)
.
getForEntity
(
"http://localhost:"
+
this
.
port
+
"/beans"
,
String
.
class
);
assertThat
(
entity
.
getStatusCode
()).
isEqualTo
(
HttpStatus
.
OK
);
BasicAuthorizationInterceptor
basicAuthInterceptor
=
new
BasicAuthorizationInterceptor
(
"admin"
,
"admin"
);
this
.
restTemplate
.
getRestTemplate
().
getInterceptors
().
add
(
basicAuthInterceptor
);
try
{
ResponseEntity
<
String
>
entity
=
this
.
restTemplate
.
getForEntity
(
"/beans"
,
String
.
class
);
assertThat
(
entity
.
getStatusCode
()).
isEqualTo
(
HttpStatus
.
OK
);
}
finally
{
this
.
restTemplate
.
getRestTemplate
().
getInterceptors
()
.
remove
(
basicAuthInterceptor
);
}
}
@Test
public
void
testManagementUnauthorizedAccess
()
throws
Exception
{
ResponseEntity
<
String
>
entity
=
new
TestRestTemplate
(
"user"
,
"user"
)
.
getForEntity
(
"http://localhost:"
+
this
.
port
+
"/beans"
,
String
.
class
);
assertThat
(
entity
.
getStatusCode
()).
isEqualTo
(
HttpStatus
.
FORBIDDEN
);
BasicAuthorizationInterceptor
basicAuthInterceptor
=
new
BasicAuthorizationInterceptor
(
"user"
,
"user"
);
this
.
restTemplate
.
getRestTemplate
().
getInterceptors
().
add
(
basicAuthInterceptor
);
try
{
ResponseEntity
<
String
>
entity
=
this
.
restTemplate
.
getForEntity
(
"/beans"
,
String
.
class
);
assertThat
(
entity
.
getStatusCode
()).
isEqualTo
(
HttpStatus
.
FORBIDDEN
);
}
finally
{
this
.
restTemplate
.
getRestTemplate
().
getInterceptors
()
.
remove
(
basicAuthInterceptor
);
}
}
private
void
getCsrf
(
MultiValueMap
<
String
,
String
>
form
,
HttpHeaders
headers
)
{
ResponseEntity
<
String
>
page
=
new
TestRestTemplate
()
.
getForEntity
(
"http://localhost:"
+
this
.
port
+
"/login"
,
String
.
class
);
ResponseEntity
<
String
>
page
=
this
.
restTemplate
.
getForEntity
(
"/login"
,
String
.
class
);
String
cookie
=
page
.
getHeaders
().
getFirst
(
"Set-Cookie"
);
headers
.
set
(
"Cookie"
,
cookie
);
String
body
=
page
.
getBody
();
...
...
spring-boot-samples/spring-boot-sample-web-mustache/src/test/java/sample/mustache/SampleWebMustacheApplicationTests.java
View file @
07a50bb1
...
...
@@ -21,7 +21,7 @@ import java.util.Arrays;
import
org.junit.Test
;
import
org.junit.runner.RunWith
;
import
org.springframework.b
oot.context.embedded.LocalServerPort
;
import
org.springframework.b
eans.factory.annotation.Autowired
;
import
org.springframework.boot.test.context.SpringBootTest
;
import
org.springframework.boot.test.context.SpringBootTest.WebEnvironment
;
import
org.springframework.boot.test.web.client.TestRestTemplate
;
...
...
@@ -47,13 +47,12 @@ import static org.assertj.core.api.Assertions.assertThat;
@DirtiesContext
public
class
SampleWebMustacheApplicationTests
{
@
LocalServerPort
private
int
port
;
@
Autowired
private
TestRestTemplate
restTemplate
;
@Test
public
void
testMustacheTemplate
()
throws
Exception
{
ResponseEntity
<
String
>
entity
=
new
TestRestTemplate
()
.
getForEntity
(
"http://localhost:"
+
this
.
port
,
String
.
class
);
ResponseEntity
<
String
>
entity
=
this
.
restTemplate
.
getForEntity
(
"/"
,
String
.
class
);
assertThat
(
entity
.
getStatusCode
()).
isEqualTo
(
HttpStatus
.
OK
);
assertThat
(
entity
.
getBody
()).
contains
(
"Hello, Andy"
);
}
...
...
@@ -63,9 +62,8 @@ public class SampleWebMustacheApplicationTests {
HttpHeaders
headers
=
new
HttpHeaders
();
headers
.
setAccept
(
Arrays
.
asList
(
MediaType
.
TEXT_HTML
));
HttpEntity
<
String
>
requestEntity
=
new
HttpEntity
<
String
>(
headers
);
ResponseEntity
<
String
>
responseEntity
=
new
TestRestTemplate
().
exchange
(
"http://localhost:"
+
this
.
port
+
"/does-not-exist"
,
HttpMethod
.
GET
,
requestEntity
,
String
.
class
);
ResponseEntity
<
String
>
responseEntity
=
this
.
restTemplate
.
exchange
(
"/does-not-exist"
,
HttpMethod
.
GET
,
requestEntity
,
String
.
class
);
assertThat
(
responseEntity
.
getStatusCode
()).
isEqualTo
(
HttpStatus
.
NOT_FOUND
);
assertThat
(
responseEntity
.
getBody
())
.
contains
(
"Something went wrong: 404 Not Found"
);
...
...
@@ -76,9 +74,8 @@ public class SampleWebMustacheApplicationTests {
HttpHeaders
headers
=
new
HttpHeaders
();
headers
.
setAccept
(
Arrays
.
asList
(
MediaType
.
TEXT_HTML
));
HttpEntity
<
String
>
requestEntity
=
new
HttpEntity
<
String
>(
headers
);
ResponseEntity
<
String
>
entity
=
new
TestRestTemplate
().
exchange
(
"http://localhost:"
+
this
.
port
+
"/serviceUnavailable"
,
HttpMethod
.
GET
,
requestEntity
,
String
.
class
);
ResponseEntity
<
String
>
entity
=
this
.
restTemplate
.
exchange
(
"/serviceUnavailable"
,
HttpMethod
.
GET
,
requestEntity
,
String
.
class
);
assertThat
(
entity
.
getStatusCode
()).
isEqualTo
(
HttpStatus
.
SERVICE_UNAVAILABLE
);
assertThat
(
entity
.
getBody
()).
contains
(
"I'm a 503"
);
}
...
...
@@ -88,9 +85,8 @@ public class SampleWebMustacheApplicationTests {
HttpHeaders
headers
=
new
HttpHeaders
();
headers
.
setAccept
(
Arrays
.
asList
(
MediaType
.
TEXT_HTML
));
HttpEntity
<
String
>
requestEntity
=
new
HttpEntity
<
String
>(
headers
);
ResponseEntity
<
String
>
entity
=
new
TestRestTemplate
().
exchange
(
"http://localhost:"
+
this
.
port
+
"/bang"
,
HttpMethod
.
GET
,
requestEntity
,
String
.
class
);
ResponseEntity
<
String
>
entity
=
this
.
restTemplate
.
exchange
(
"/bang"
,
HttpMethod
.
GET
,
requestEntity
,
String
.
class
);
assertThat
(
entity
.
getStatusCode
()).
isEqualTo
(
HttpStatus
.
INTERNAL_SERVER_ERROR
);
assertThat
(
entity
.
getBody
()).
contains
(
"I'm a 5xx"
);
}
...
...
@@ -100,9 +96,8 @@ public class SampleWebMustacheApplicationTests {
HttpHeaders
headers
=
new
HttpHeaders
();
headers
.
setAccept
(
Arrays
.
asList
(
MediaType
.
TEXT_HTML
));
HttpEntity
<
String
>
requestEntity
=
new
HttpEntity
<
String
>(
headers
);
ResponseEntity
<
String
>
entity
=
new
TestRestTemplate
().
exchange
(
"http://localhost:"
+
this
.
port
+
"/insufficientStorage"
,
HttpMethod
.
GET
,
requestEntity
,
String
.
class
);
ResponseEntity
<
String
>
entity
=
this
.
restTemplate
.
exchange
(
"/insufficientStorage"
,
HttpMethod
.
GET
,
requestEntity
,
String
.
class
);
assertThat
(
entity
.
getStatusCode
()).
isEqualTo
(
HttpStatus
.
INSUFFICIENT_STORAGE
);
assertThat
(
entity
.
getBody
()).
contains
(
"I'm a 507"
);
}
...
...
spring-boot-samples/spring-boot-sample-web-secure-custom/src/test/java/sample/web/secure/custom/SampleWebSecureCustomApplicationTests.java
View file @
07a50bb1
...
...
@@ -23,6 +23,7 @@ import java.util.regex.Pattern;
import
org.junit.Test
;
import
org.junit.runner.RunWith
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.boot.context.embedded.LocalServerPort
;
import
org.springframework.boot.test.context.SpringBootTest
;
import
org.springframework.boot.test.context.SpringBootTest.WebEnvironment
;
...
...
@@ -50,6 +51,9 @@ import static org.assertj.core.api.Assertions.assertThat;
@DirtiesContext
public
class
SampleWebSecureCustomApplicationTests
{
@Autowired
private
TestRestTemplate
restTemplate
;
@LocalServerPort
private
int
port
;
...
...
@@ -57,8 +61,7 @@ public class SampleWebSecureCustomApplicationTests {
public
void
testHome
()
throws
Exception
{
HttpHeaders
headers
=
new
HttpHeaders
();
headers
.
setAccept
(
Arrays
.
asList
(
MediaType
.
TEXT_HTML
));
ResponseEntity
<
String
>
entity
=
new
TestRestTemplate
().
exchange
(
"http://localhost:"
+
this
.
port
,
HttpMethod
.
GET
,
ResponseEntity
<
String
>
entity
=
this
.
restTemplate
.
exchange
(
"/"
,
HttpMethod
.
GET
,
new
HttpEntity
<
Void
>(
headers
),
String
.
class
);
assertThat
(
entity
.
getStatusCode
()).
isEqualTo
(
HttpStatus
.
FOUND
);
assertThat
(
entity
.
getHeaders
().
getLocation
().
toString
())
...
...
@@ -69,9 +72,8 @@ public class SampleWebSecureCustomApplicationTests {
public
void
testLoginPage
()
throws
Exception
{
HttpHeaders
headers
=
new
HttpHeaders
();
headers
.
setAccept
(
Arrays
.
asList
(
MediaType
.
TEXT_HTML
));
ResponseEntity
<
String
>
entity
=
new
TestRestTemplate
().
exchange
(
"http://localhost:"
+
this
.
port
+
"/login"
,
HttpMethod
.
GET
,
new
HttpEntity
<
Void
>(
headers
),
String
.
class
);
ResponseEntity
<
String
>
entity
=
this
.
restTemplate
.
exchange
(
"/login"
,
HttpMethod
.
GET
,
new
HttpEntity
<
Void
>(
headers
),
String
.
class
);
assertThat
(
entity
.
getStatusCode
()).
isEqualTo
(
HttpStatus
.
OK
);
assertThat
(
entity
.
getBody
()).
contains
(
"_csrf"
);
}
...
...
@@ -84,8 +86,8 @@ public class SampleWebSecureCustomApplicationTests {
MultiValueMap
<
String
,
String
>
form
=
new
LinkedMultiValueMap
<
String
,
String
>();
form
.
set
(
"username"
,
"user"
);
form
.
set
(
"password"
,
"user"
);
ResponseEntity
<
String
>
entity
=
new
TestRestTemplate
().
exchange
(
"http://localhost:"
+
this
.
port
+
"/login"
,
HttpMethod
.
POST
,
ResponseEntity
<
String
>
entity
=
this
.
restTemplate
.
exchange
(
"/login"
,
HttpMethod
.
POST
,
new
HttpEntity
<
MultiValueMap
<
String
,
String
>>(
form
,
headers
),
String
.
class
);
assertThat
(
entity
.
getStatusCode
()).
isEqualTo
(
HttpStatus
.
FOUND
);
...
...
@@ -96,8 +98,8 @@ public class SampleWebSecureCustomApplicationTests {
private
HttpHeaders
getHeaders
()
{
HttpHeaders
headers
=
new
HttpHeaders
();
ResponseEntity
<
String
>
page
=
new
TestRestTemplate
()
.
getForEntity
(
"http://localhost:"
+
this
.
port
+
"/login"
,
String
.
class
);
ResponseEntity
<
String
>
page
=
this
.
restTemplate
.
getForEntity
(
"/login"
,
String
.
class
);
assertThat
(
page
.
getStatusCode
()).
isEqualTo
(
HttpStatus
.
OK
);
String
cookie
=
page
.
getHeaders
().
getFirst
(
"Set-Cookie"
);
headers
.
set
(
"Cookie"
,
cookie
);
...
...
@@ -110,8 +112,8 @@ public class SampleWebSecureCustomApplicationTests {
@Test
public
void
testCss
()
throws
Exception
{
ResponseEntity
<
String
>
entity
=
new
TestRestTemplate
().
getForEntity
(
"http://localhost:"
+
this
.
port
+
"/css/bootstrap.min.css"
,
String
.
class
);
ResponseEntity
<
String
>
entity
=
this
.
restTemplate
.
getForEntity
(
"/css/bootstrap.min.css"
,
String
.
class
);
assertThat
(
entity
.
getStatusCode
()).
isEqualTo
(
HttpStatus
.
OK
);
assertThat
(
entity
.
getBody
()).
contains
(
"body"
);
}
...
...
spring-boot-samples/spring-boot-sample-web-secure-github/src/test/java/sample/web/secure/github/SampleGithubApplicationTests.java
View file @
07a50bb1
...
...
@@ -21,6 +21,7 @@ import java.net.URI;
import
org.junit.Test
;
import
org.junit.runner.RunWith
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.boot.context.embedded.LocalServerPort
;
import
org.springframework.boot.test.context.SpringBootTest
;
import
org.springframework.boot.test.context.SpringBootTest.WebEnvironment
;
...
...
@@ -46,11 +47,12 @@ public class SampleGithubApplicationTests {
@LocalServerPort
private
int
port
;
@Autowired
private
TestRestTemplate
restTemplate
;
@Test
public
void
everythingIsSecuredByDefault
()
throws
Exception
{
TestRestTemplate
restTemplate
=
new
TestRestTemplate
();
ResponseEntity
<
Void
>
entity
=
restTemplate
.
getForEntity
(
"http://localhost:"
+
this
.
port
,
Void
.
class
);
ResponseEntity
<
Void
>
entity
=
this
.
restTemplate
.
getForEntity
(
"/"
,
Void
.
class
);
assertThat
(
entity
.
getStatusCode
()).
isEqualTo
(
HttpStatus
.
FOUND
);
assertThat
(
entity
.
getHeaders
().
getLocation
())
.
isEqualTo
(
URI
.
create
(
"http://localhost:"
+
this
.
port
+
"/login"
));
...
...
@@ -58,9 +60,8 @@ public class SampleGithubApplicationTests {
@Test
public
void
loginRedirectsToGithub
()
throws
Exception
{
TestRestTemplate
restTemplate
=
new
TestRestTemplate
();
ResponseEntity
<
Void
>
entity
=
restTemplate
.
getForEntity
(
"http://localhost:"
+
this
.
port
+
"/login"
,
Void
.
class
);
ResponseEntity
<
Void
>
entity
=
this
.
restTemplate
.
getForEntity
(
"/login"
,
Void
.
class
);
assertThat
(
entity
.
getStatusCode
()).
isEqualTo
(
HttpStatus
.
FOUND
);
assertThat
(
entity
.
getHeaders
().
getLocation
().
toString
())
.
startsWith
(
"https://github.com/login/oauth"
);
...
...
spring-boot-samples/spring-boot-sample-web-secure-jdbc/src/test/java/sample/web/secure/jdbc/SampleWebSecureJdbcApplicationTests.java
View file @
07a50bb1
...
...
@@ -23,6 +23,7 @@ import java.util.regex.Pattern;
import
org.junit.Test
;
import
org.junit.runner.RunWith
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.boot.context.embedded.LocalServerPort
;
import
org.springframework.boot.test.context.SpringBootTest
;
import
org.springframework.boot.test.context.SpringBootTest.WebEnvironment
;
...
...
@@ -50,6 +51,9 @@ import static org.assertj.core.api.Assertions.assertThat;
@DirtiesContext
public
class
SampleWebSecureJdbcApplicationTests
{
@Autowired
private
TestRestTemplate
restTemplate
;
@LocalServerPort
private
int
port
;
...
...
@@ -57,8 +61,7 @@ public class SampleWebSecureJdbcApplicationTests {
public
void
testHome
()
throws
Exception
{
HttpHeaders
headers
=
new
HttpHeaders
();
headers
.
setAccept
(
Arrays
.
asList
(
MediaType
.
TEXT_HTML
));
ResponseEntity
<
String
>
entity
=
new
TestRestTemplate
().
exchange
(
"http://localhost:"
+
this
.
port
,
HttpMethod
.
GET
,
ResponseEntity
<
String
>
entity
=
this
.
restTemplate
.
exchange
(
"/"
,
HttpMethod
.
GET
,
new
HttpEntity
<
Void
>(
headers
),
String
.
class
);
assertThat
(
entity
.
getStatusCode
()).
isEqualTo
(
HttpStatus
.
FOUND
);
assertThat
(
entity
.
getHeaders
().
getLocation
().
toString
())
...
...
@@ -69,9 +72,8 @@ public class SampleWebSecureJdbcApplicationTests {
public
void
testLoginPage
()
throws
Exception
{
HttpHeaders
headers
=
new
HttpHeaders
();
headers
.
setAccept
(
Arrays
.
asList
(
MediaType
.
TEXT_HTML
));
ResponseEntity
<
String
>
entity
=
new
TestRestTemplate
().
exchange
(
"http://localhost:"
+
this
.
port
+
"/login"
,
HttpMethod
.
GET
,
new
HttpEntity
<
Void
>(
headers
),
String
.
class
);
ResponseEntity
<
String
>
entity
=
this
.
restTemplate
.
exchange
(
"/login"
,
HttpMethod
.
GET
,
new
HttpEntity
<
Void
>(
headers
),
String
.
class
);
assertThat
(
entity
.
getStatusCode
()).
isEqualTo
(
HttpStatus
.
OK
);
assertThat
(
entity
.
getBody
()).
contains
(
"_csrf"
);
}
...
...
@@ -84,8 +86,8 @@ public class SampleWebSecureJdbcApplicationTests {
MultiValueMap
<
String
,
String
>
form
=
new
LinkedMultiValueMap
<
String
,
String
>();
form
.
set
(
"username"
,
"user"
);
form
.
set
(
"password"
,
"user"
);
ResponseEntity
<
String
>
entity
=
new
TestRestTemplate
().
exchange
(
"http://localhost:"
+
this
.
port
+
"/login"
,
HttpMethod
.
POST
,
ResponseEntity
<
String
>
entity
=
this
.
restTemplate
.
exchange
(
"/login"
,
HttpMethod
.
POST
,
new
HttpEntity
<
MultiValueMap
<
String
,
String
>>(
form
,
headers
),
String
.
class
);
assertThat
(
entity
.
getStatusCode
()).
isEqualTo
(
HttpStatus
.
FOUND
);
...
...
@@ -96,8 +98,8 @@ public class SampleWebSecureJdbcApplicationTests {
private
HttpHeaders
getHeaders
()
{
HttpHeaders
headers
=
new
HttpHeaders
();
ResponseEntity
<
String
>
page
=
new
TestRestTemplate
()
.
getForEntity
(
"http://localhost:"
+
this
.
port
+
"/login"
,
String
.
class
);
ResponseEntity
<
String
>
page
=
this
.
restTemplate
.
getForEntity
(
"/login"
,
String
.
class
);
assertThat
(
page
.
getStatusCode
()).
isEqualTo
(
HttpStatus
.
OK
);
String
cookie
=
page
.
getHeaders
().
getFirst
(
"Set-Cookie"
);
headers
.
set
(
"Cookie"
,
cookie
);
...
...
@@ -110,8 +112,8 @@ public class SampleWebSecureJdbcApplicationTests {
@Test
public
void
testCss
()
throws
Exception
{
ResponseEntity
<
String
>
entity
=
new
TestRestTemplate
().
getForEntity
(
"http://localhost:"
+
this
.
port
+
"/css/bootstrap.min.css"
,
String
.
class
);
ResponseEntity
<
String
>
entity
=
this
.
restTemplate
.
getForEntity
(
"/css/bootstrap.min.css"
,
String
.
class
);
assertThat
(
entity
.
getStatusCode
()).
isEqualTo
(
HttpStatus
.
OK
);
assertThat
(
entity
.
getBody
()).
contains
(
"body"
);
}
...
...
spring-boot-samples/spring-boot-sample-web-secure/src/test/java/sample/web/secure/SampleSecureApplicationTests.java
View file @
07a50bb1
...
...
@@ -23,6 +23,7 @@ import java.util.regex.Pattern;
import
org.junit.Test
;
import
org.junit.runner.RunWith
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.boot.context.embedded.LocalServerPort
;
import
org.springframework.boot.test.context.SpringBootTest
;
import
org.springframework.boot.test.context.SpringBootTest.WebEnvironment
;
...
...
@@ -50,6 +51,9 @@ import static org.assertj.core.api.Assertions.assertThat;
@DirtiesContext
public
class
SampleSecureApplicationTests
{
@Autowired
private
TestRestTemplate
restTemplate
;
@LocalServerPort
private
int
port
;
...
...
@@ -57,8 +61,7 @@ public class SampleSecureApplicationTests {
public
void
testHome
()
throws
Exception
{
HttpHeaders
headers
=
new
HttpHeaders
();
headers
.
setAccept
(
Arrays
.
asList
(
MediaType
.
TEXT_HTML
));
ResponseEntity
<
String
>
entity
=
new
TestRestTemplate
().
exchange
(
"http://localhost:"
+
this
.
port
,
HttpMethod
.
GET
,
ResponseEntity
<
String
>
entity
=
this
.
restTemplate
.
exchange
(
"/"
,
HttpMethod
.
GET
,
new
HttpEntity
<
Void
>(
headers
),
String
.
class
);
assertThat
(
entity
.
getStatusCode
()).
isEqualTo
(
HttpStatus
.
FOUND
);
assertThat
(
entity
.
getHeaders
().
getLocation
().
toString
())
...
...
@@ -69,9 +72,8 @@ public class SampleSecureApplicationTests {
public
void
testLoginPage
()
throws
Exception
{
HttpHeaders
headers
=
new
HttpHeaders
();
headers
.
setAccept
(
Arrays
.
asList
(
MediaType
.
TEXT_HTML
));
ResponseEntity
<
String
>
entity
=
new
TestRestTemplate
().
exchange
(
"http://localhost:"
+
this
.
port
+
"/login"
,
HttpMethod
.
GET
,
new
HttpEntity
<
Void
>(
headers
),
String
.
class
);
ResponseEntity
<
String
>
entity
=
this
.
restTemplate
.
exchange
(
"/login"
,
HttpMethod
.
GET
,
new
HttpEntity
<
Void
>(
headers
),
String
.
class
);
assertThat
(
entity
.
getStatusCode
()).
isEqualTo
(
HttpStatus
.
OK
);
assertThat
(
entity
.
getBody
()).
contains
(
"_csrf"
);
}
...
...
@@ -84,8 +86,8 @@ public class SampleSecureApplicationTests {
MultiValueMap
<
String
,
String
>
form
=
new
LinkedMultiValueMap
<
String
,
String
>();
form
.
set
(
"username"
,
"user"
);
form
.
set
(
"password"
,
"user"
);
ResponseEntity
<
String
>
entity
=
new
TestRestTemplate
().
exchange
(
"http://localhost:"
+
this
.
port
+
"/login"
,
HttpMethod
.
POST
,
ResponseEntity
<
String
>
entity
=
this
.
restTemplate
.
exchange
(
"/login"
,
HttpMethod
.
POST
,
new
HttpEntity
<
MultiValueMap
<
String
,
String
>>(
form
,
headers
),
String
.
class
);
assertThat
(
entity
.
getStatusCode
()).
isEqualTo
(
HttpStatus
.
FOUND
);
...
...
@@ -96,8 +98,8 @@ public class SampleSecureApplicationTests {
private
HttpHeaders
getHeaders
()
{
HttpHeaders
headers
=
new
HttpHeaders
();
ResponseEntity
<
String
>
page
=
new
TestRestTemplate
()
.
getForEntity
(
"http://localhost:"
+
this
.
port
+
"/login"
,
String
.
class
);
ResponseEntity
<
String
>
page
=
this
.
restTemplate
.
getForEntity
(
"/login"
,
String
.
class
);
assertThat
(
page
.
getStatusCode
()).
isEqualTo
(
HttpStatus
.
OK
);
String
cookie
=
page
.
getHeaders
().
getFirst
(
"Set-Cookie"
);
headers
.
set
(
"Cookie"
,
cookie
);
...
...
@@ -110,8 +112,8 @@ public class SampleSecureApplicationTests {
@Test
public
void
testCss
()
throws
Exception
{
ResponseEntity
<
String
>
entity
=
new
TestRestTemplate
().
getForEntity
(
"http://localhost:"
+
this
.
port
+
"/css/bootstrap.min.css"
,
String
.
class
);
ResponseEntity
<
String
>
entity
=
this
.
restTemplate
.
getForEntity
(
"/css/bootstrap.min.css"
,
String
.
class
);
assertThat
(
entity
.
getStatusCode
()).
isEqualTo
(
HttpStatus
.
OK
);
assertThat
(
entity
.
getBody
()).
contains
(
"body"
);
}
...
...
spring-boot-samples/spring-boot-sample-web-static/src/test/java/sample/web/staticcontent/SampleWebStaticApplicationTests.java
View file @
07a50bb1
...
...
@@ -19,7 +19,7 @@ package sample.web.staticcontent;
import
org.junit.Test
;
import
org.junit.runner.RunWith
;
import
org.springframework.b
oot.context.embedded.LocalServerPort
;
import
org.springframework.b
eans.factory.annotation.Autowired
;
import
org.springframework.boot.test.context.SpringBootTest
;
import
org.springframework.boot.test.context.SpringBootTest.WebEnvironment
;
import
org.springframework.boot.test.web.client.TestRestTemplate
;
...
...
@@ -41,24 +41,20 @@ import static org.assertj.core.api.Assertions.assertThat;
@DirtiesContext
public
class
SampleWebStaticApplicationTests
{
@
LocalServerPort
private
int
port
=
0
;
@
Autowired
private
TestRestTemplate
restTemplate
;
@Test
public
void
testHome
()
throws
Exception
{
ResponseEntity
<
String
>
entity
=
new
TestRestTemplate
()
.
getForEntity
(
"http://localhost:"
+
this
.
port
,
String
.
class
);
ResponseEntity
<
String
>
entity
=
this
.
restTemplate
.
getForEntity
(
"/"
,
String
.
class
);
assertThat
(
entity
.
getStatusCode
()).
isEqualTo
(
HttpStatus
.
OK
);
assertThat
(
entity
.
getBody
()).
contains
(
"<title>Static"
);
}
@Test
public
void
testCss
()
throws
Exception
{
ResponseEntity
<
String
>
entity
=
new
TestRestTemplate
()
.
getForEntity
(
"http://localhost:"
+
this
.
port
+
"/webjars/bootstrap/3.0.3/css/bootstrap.min.css"
,
String
.
class
);
ResponseEntity
<
String
>
entity
=
this
.
restTemplate
.
getForEntity
(
"/webjars/bootstrap/3.0.3/css/bootstrap.min.css"
,
String
.
class
);
assertThat
(
entity
.
getStatusCode
()).
isEqualTo
(
HttpStatus
.
OK
);
assertThat
(
entity
.
getBody
()).
contains
(
"body"
);
assertThat
(
entity
.
getHeaders
().
getContentType
())
...
...
spring-boot-samples/spring-boot-sample-web-thymeleaf3/src/test/java/sample/web/thymeleaf3/SampleWebThymeleaf3ApplicationTests.java
View file @
07a50bb1
...
...
@@ -21,6 +21,7 @@ import java.net.URI;
import
org.junit.Test
;
import
org.junit.runner.RunWith
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.boot.context.embedded.LocalServerPort
;
import
org.springframework.boot.test.context.SpringBootTest
;
import
org.springframework.boot.test.context.SpringBootTest.WebEnvironment
;
...
...
@@ -44,13 +45,15 @@ import static org.assertj.core.api.Assertions.assertThat;
@DirtiesContext
public
class
SampleWebThymeleaf3ApplicationTests
{
@Autowired
private
TestRestTemplate
restTemplate
;
@LocalServerPort
private
int
port
;
@Test
public
void
testHome
()
throws
Exception
{
ResponseEntity
<
String
>
entity
=
new
TestRestTemplate
()
.
getForEntity
(
"http://localhost:"
+
this
.
port
,
String
.
class
);
ResponseEntity
<
String
>
entity
=
this
.
restTemplate
.
getForEntity
(
"/"
,
String
.
class
);
assertThat
(
entity
.
getStatusCode
()).
isEqualTo
(
HttpStatus
.
OK
);
assertThat
(
entity
.
getBody
()).
contains
(
"<title>Messages"
);
assertThat
(
entity
.
getBody
()).
doesNotContain
(
"layout:fragment"
);
...
...
@@ -61,15 +64,14 @@ public class SampleWebThymeleaf3ApplicationTests {
MultiValueMap
<
String
,
String
>
map
=
new
LinkedMultiValueMap
<
String
,
String
>();
map
.
set
(
"text"
,
"FOO text"
);
map
.
set
(
"summary"
,
"FOO"
);
URI
location
=
new
TestRestTemplate
()
.
postForLocation
(
"http://localhost:"
+
this
.
port
,
map
);
URI
location
=
this
.
restTemplate
.
postForLocation
(
"/"
,
map
);
assertThat
(
location
.
toString
()).
contains
(
"localhost:"
+
this
.
port
);
}
@Test
public
void
testCss
()
throws
Exception
{
ResponseEntity
<
String
>
entity
=
new
TestRestTemplate
().
getForEntity
(
"http://localhost:"
+
this
.
port
+
"/css/bootstrap.min.css"
,
String
.
class
);
ResponseEntity
<
String
>
entity
=
this
.
restTemplate
.
getForEntity
(
"/css/bootstrap.min.css"
,
String
.
class
);
assertThat
(
entity
.
getStatusCode
()).
isEqualTo
(
HttpStatus
.
OK
);
assertThat
(
entity
.
getBody
()).
contains
(
"body"
);
}
...
...
spring-boot-samples/spring-boot-sample-web-ui/src/test/java/sample/web/ui/SampleWebUiApplicationTests.java
View file @
07a50bb1
...
...
@@ -21,6 +21,7 @@ import java.net.URI;
import
org.junit.Test
;
import
org.junit.runner.RunWith
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.boot.context.embedded.LocalServerPort
;
import
org.springframework.boot.test.context.SpringBootTest
;
import
org.springframework.boot.test.context.SpringBootTest.WebEnvironment
;
...
...
@@ -44,13 +45,15 @@ import static org.assertj.core.api.Assertions.assertThat;
@DirtiesContext
public
class
SampleWebUiApplicationTests
{
@Autowired
private
TestRestTemplate
restTemplate
;
@LocalServerPort
private
int
port
;
@Test
public
void
testHome
()
throws
Exception
{
ResponseEntity
<
String
>
entity
=
new
TestRestTemplate
()
.
getForEntity
(
"http://localhost:"
+
this
.
port
,
String
.
class
);
ResponseEntity
<
String
>
entity
=
this
.
restTemplate
.
getForEntity
(
"/"
,
String
.
class
);
assertThat
(
entity
.
getStatusCode
()).
isEqualTo
(
HttpStatus
.
OK
);
assertThat
(
entity
.
getBody
()).
contains
(
"<title>Messages"
);
assertThat
(
entity
.
getBody
()).
doesNotContain
(
"layout:fragment"
);
...
...
@@ -61,14 +64,13 @@ public class SampleWebUiApplicationTests {
MultiValueMap
<
String
,
String
>
map
=
new
LinkedMultiValueMap
<
String
,
String
>();
map
.
set
(
"text"
,
"FOO text"
);
map
.
set
(
"summary"
,
"FOO"
);
URI
location
=
new
TestRestTemplate
()
.
postForLocation
(
"http://localhost:"
+
this
.
port
,
map
);
URI
location
=
this
.
restTemplate
.
postForLocation
(
"/"
,
map
);
assertThat
(
location
.
toString
()).
contains
(
"localhost:"
+
this
.
port
);
}
@Test
public
void
testCss
()
throws
Exception
{
ResponseEntity
<
String
>
entity
=
new
TestRestTemplate
()
.
getForEntity
(
ResponseEntity
<
String
>
entity
=
this
.
restTemplate
.
getForEntity
(
"http://localhost:"
+
this
.
port
+
"/css/bootstrap.min.css"
,
String
.
class
);
assertThat
(
entity
.
getStatusCode
()).
isEqualTo
(
HttpStatus
.
OK
);
assertThat
(
entity
.
getBody
()).
contains
(
"body"
);
...
...
spring-boot-samples/spring-boot-sample-web-velocity/src/test/java/sample/web/velocity/SampleWebVelocityApplicationTests.java
View file @
07a50bb1
...
...
@@ -21,7 +21,7 @@ import java.util.Arrays;
import
org.junit.Test
;
import
org.junit.runner.RunWith
;
import
org.springframework.b
oot.context.embedded.LocalServerPort
;
import
org.springframework.b
eans.factory.annotation.Autowired
;
import
org.springframework.boot.test.context.SpringBootTest
;
import
org.springframework.boot.test.context.SpringBootTest.WebEnvironment
;
import
org.springframework.boot.test.web.client.TestRestTemplate
;
...
...
@@ -47,13 +47,12 @@ import static org.assertj.core.api.Assertions.assertThat;
@DirtiesContext
public
class
SampleWebVelocityApplicationTests
{
@
LocalServerPort
private
int
port
;
@
Autowired
private
TestRestTemplate
restTemplate
;
@Test
public
void
testVelocityTemplate
()
throws
Exception
{
ResponseEntity
<
String
>
entity
=
new
TestRestTemplate
()
.
getForEntity
(
"http://localhost:"
+
this
.
port
,
String
.
class
);
ResponseEntity
<
String
>
entity
=
this
.
restTemplate
.
getForEntity
(
"/"
,
String
.
class
);
assertThat
(
entity
.
getStatusCode
()).
isEqualTo
(
HttpStatus
.
OK
);
assertThat
(
entity
.
getBody
()).
contains
(
"Hello, Andy"
);
}
...
...
@@ -64,9 +63,8 @@ public class SampleWebVelocityApplicationTests {
headers
.
setAccept
(
Arrays
.
asList
(
MediaType
.
TEXT_HTML
));
HttpEntity
<
String
>
requestEntity
=
new
HttpEntity
<
String
>(
headers
);
ResponseEntity
<
String
>
responseEntity
=
new
TestRestTemplate
().
exchange
(
"http://localhost:"
+
this
.
port
+
"/does-not-exist"
,
HttpMethod
.
GET
,
requestEntity
,
String
.
class
);
ResponseEntity
<
String
>
responseEntity
=
this
.
restTemplate
.
exchange
(
"/does-not-exist"
,
HttpMethod
.
GET
,
requestEntity
,
String
.
class
);
assertThat
(
responseEntity
.
getStatusCode
()).
isEqualTo
(
HttpStatus
.
NOT_FOUND
);
assertThat
(
responseEntity
.
getBody
())
...
...
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