Skip to content

Commit

Permalink
Fix error about response committed (halo-dev#1301)
Browse files Browse the repository at this point in the history
* Add index page request test

* Add test for first page request

* Create session before requesting content
  • Loading branch information
JohnNiang authored Mar 7, 2021
1 parent 0bcf2da commit 0e516ca
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 12 deletions.
4 changes: 3 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -101,14 +101,15 @@ ext {
huaweiObsVersion = "3.19.7"
githubApiVersion = "1.84"
templateInheritanceVersion = "0.4.RELEASE"
jsoupVersion = "1.13.1"
}

dependencies {
implementation "org.kohsuke:github-api:$githubApiVersion"
implementation "org.springframework.boot:spring-boot-starter-actuator"
implementation "org.springframework.boot:spring-boot-starter-data-jpa"
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-validation'

Expand Down Expand Up @@ -165,6 +166,7 @@ dependencies {
testImplementation("org.springframework.boot:spring-boot-starter-test") {
exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
}
testImplementation("org.jsoup:jsoup:${jsoupVersion}")

developmentOnly "org.springframework.boot:spring-boot-devtools"
}
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/run/halo/app/security/filter/ContentFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ protected String getTokenFromRequest(HttpServletRequest request) {
protected void doAuthenticate(HttpServletRequest request, HttpServletResponse response,
FilterChain filterChain) throws ServletException, IOException {
// Do nothing
// create session
request.getSession(true);
filterChain.doFilter(request, response);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public void categoryAuthorization(Integer categoryId) {

@Override
public Set<String> getAccessPermissionStore() {
return cacheStore.getAny(buildAccessPermissionKey(), Set.class).orElse(new HashSet());
return cacheStore.getAny(buildAccessPermissionKey(), Set.class).orElseGet(HashSet::new);
}

@Override
Expand Down
47 changes: 47 additions & 0 deletions src/test/java/run/halo/app/it/BaseApiTest.java
Original file line number Diff line number Diff line change
@@ -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("[email protected]");
install.setPassword("opentest");
install.setUrl("http://localhost:" + port);
install.setTitle("Test's Blog");

restTemplate.postForObject(blogUrl + "/api/admin/installations", install,
String.class);
}

}
39 changes: 39 additions & 0 deletions src/test/java/run/halo/app/it/IndexPageRequestTest.java
Original file line number Diff line number Diff line change
@@ -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());
}
}
10 changes: 0 additions & 10 deletions src/test/resources/application-test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,8 @@ spring:
show-sql: true
flyway:
enabled: false
servlet:
multipart:
max-file-size: 10MB
max-request-size: 10MB
cache:
type: none
management:
endpoints:
web:
base-path: /api/admin/actuator
exposure:
include: [ 'httptrace', 'metrics','env','logfile' ]
logging:
level:
run.halo.app: DEBUG
Expand Down

0 comments on commit 0e516ca

Please sign in to comment.