Fix error about response committed (#1301)

* Add index page request test

* Add test for first page request

* Create session before requesting content
pull/1304/head
John Niang 2021-03-07 15:48:24 +08:00 committed by GitHub
parent 38093df0c0
commit 2b7ae41c02
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 92 additions and 12 deletions

View File

@ -101,6 +101,7 @@ ext {
huaweiObsVersion = "3.19.7" huaweiObsVersion = "3.19.7"
githubApiVersion = "1.84" githubApiVersion = "1.84"
templateInheritanceVersion = "0.4.RELEASE" templateInheritanceVersion = "0.4.RELEASE"
jsoupVersion = "1.13.1"
} }
dependencies { dependencies {
@ -108,7 +109,7 @@ dependencies {
implementation "org.springframework.boot:spring-boot-starter-actuator" implementation "org.springframework.boot:spring-boot-starter-actuator"
implementation "org.springframework.boot:spring-boot-starter-data-jpa" implementation "org.springframework.boot:spring-boot-starter-data-jpa"
implementation "org.springframework.boot:spring-boot-starter-web" implementation "org.springframework.boot:spring-boot-starter-web"
implementation "org.springframework.boot:spring-boot-starter-undertow" implementation "org.springframework.boot:spring-boot-starter-jetty"
implementation "org.springframework.boot:spring-boot-starter-freemarker" implementation "org.springframework.boot:spring-boot-starter-freemarker"
implementation 'org.springframework.boot:spring-boot-starter-validation' implementation 'org.springframework.boot:spring-boot-starter-validation'
@ -165,6 +166,7 @@ dependencies {
testImplementation("org.springframework.boot:spring-boot-starter-test") { testImplementation("org.springframework.boot:spring-boot-starter-test") {
exclude group: 'org.junit.vintage', module: 'junit-vintage-engine' exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
} }
testImplementation("org.jsoup:jsoup:${jsoupVersion}")
developmentOnly "org.springframework.boot:spring-boot-devtools" developmentOnly "org.springframework.boot:spring-boot-devtools"
} }

View File

@ -54,6 +54,8 @@ public class ContentFilter extends AbstractAuthenticationFilter {
protected void doAuthenticate(HttpServletRequest request, HttpServletResponse response, protected void doAuthenticate(HttpServletRequest request, HttpServletResponse response,
FilterChain filterChain) throws ServletException, IOException { FilterChain filterChain) throws ServletException, IOException {
// Do nothing // Do nothing
// create session
request.getSession(true);
filterChain.doFilter(request, response); filterChain.doFilter(request, response);
} }
} }

View File

@ -35,7 +35,7 @@ public class AuthorizationServiceImpl implements AuthorizationService {
@Override @Override
public Set<String> getAccessPermissionStore() { public Set<String> getAccessPermissionStore() {
return cacheStore.getAny(buildAccessPermissionKey(), Set.class).orElse(new HashSet()); return cacheStore.getAny(buildAccessPermissionKey(), Set.class).orElseGet(HashSet::new);
} }
@Override @Override

View File

@ -0,0 +1,47 @@
package run.halo.app.it;
import org.junit.jupiter.api.BeforeEach;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.web.server.LocalServerPort;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.web.client.RestTemplate;
import run.halo.app.model.params.InstallParam;
/**
* Base api test.
*
* @author johnniang
*/
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@ActiveProfiles("test")
class BaseApiTest {
@Autowired
RestTemplate restTemplate;
@LocalServerPort
int port;
String blogUrl;
@BeforeEach
void baseSetUp() {
blogUrl = "http://localhost:" + port;
}
void installBlog() {
InstallParam install = new InstallParam();
install.setUsername("test");
install.setNickname("test");
install.setEmail("test@test.com");
install.setPassword("opentest");
install.setUrl("http://localhost:" + port);
install.setTitle("Test's Blog");
restTemplate.postForObject(blogUrl + "/api/admin/installations", install,
String.class);
}
}

View File

@ -0,0 +1,39 @@
package run.halo.app.it;
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.io.IOException;
import lombok.extern.slf4j.Slf4j;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.junit.jupiter.api.Test;
/**
* Index page request test.
*
* @author johnniang
*/
@Slf4j
class IndexPageRequestTest extends BaseApiTest {
@Test
void indexPage() throws IOException {
installBlog();
// validate atom.xml link
Document document = Jsoup.connect(blogUrl).get();
Element atomLink = document.head().getElementsByAttributeValue("title", "atom 1.0").get(0);
assertEquals(blogUrl + "/atom.xml", atomLink.attr("href"));
// validate title link
Element titleLink = document.body().selectFirst(".logo-title > .title > h3 > a");
assertEquals(blogUrl, titleLink.attr("href"));
assertEquals("Test's Blog", titleLink.text());
// validate post link
Element postTitleLink =
document.body().selectFirst(".content > .post > .post-title > h3 > a");
assertEquals(blogUrl + "/archives/hello-halo", postTitleLink.attr("href"));
assertEquals("Hello Halo", postTitleLink.text());
}
}

View File

@ -21,18 +21,8 @@ spring:
show-sql: true show-sql: true
flyway: flyway:
enabled: false enabled: false
servlet:
multipart:
max-file-size: 10MB
max-request-size: 10MB
cache: cache:
type: none type: none
management:
endpoints:
web:
base-path: /api/admin/actuator
exposure:
include: [ 'httptrace', 'metrics','env','logfile' ]
logging: logging:
level: level:
run.halo.app: DEBUG run.halo.app: DEBUG