Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JUNIT: Migrate assets dispatcher test to Kotlin #1341

Merged
merged 7 commits into from
Dec 7, 2023
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .drone.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ steps:
- name: cron_unit_tests
image: scireum/sirius-build-jdk21
commands:
- mvn clean test
- mvn clean test -Dsun.net.http.allowRestrictedHeaders=true
volumes: *scireum_volumes
when:
event:
Expand All @@ -50,7 +50,7 @@ steps:
- name: test
image: scireum/sirius-build-jdk21
commands:
- mvn clean test -Dtest.excluded.groups=nightly
- mvn clean test -Dtest.excluded.groups=nightly -Dsun.net.http.allowRestrictedHeaders=true
volumes: *scireum_volumes
when:
event:
Expand Down
7 changes: 0 additions & 7 deletions src/test/java/TestSuite.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,4 @@
@SuiteClasses({"**/*Test.class", "**/*Spec.class"})
public class TestSuite {

@BeforeClass
public static void setUp() {
// Allow us to set the Origin: header...
// This is also set in the CORSSpec, but if the whole TestSuite is executed,
// this might be too late as a previously opened url connection might be re-used...
System.setProperty("sun.net.http.allowRestrictedHeaders", "true");
}
}
47 changes: 0 additions & 47 deletions src/test/java/sirius/web/dispatch/AssetsDispatcherSpec.groovy

This file was deleted.

3 changes: 1 addition & 2 deletions src/test/java/sirius/web/http/CORSSpec.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ class CORSSpec extends BaseSpecification {

def "expect 'Access-Control-Allow-Origin' for requests with 'origin'"() {
when:
// Allow us to set the Origin: header...
System.setProperty("sun.net.http.allowRestrictedHeaders", "true")
// Setting the "Origin: header" must be allowed by -Dsun.net.http.allowRestrictedHeaders=true
HttpURLConnection c = new URL("http://localhost:9999/system/ok").openConnection()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Optional: Rename c to connection

c.addRequestProperty("Origin", "TEST")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// Setting the "Origin: header" must be allowed by -Dsun.net.http.allowRestrictedHeaders=true
HttpURLConnection c = new URL("http://localhost:9999/system/ok").openConnection()
c.addRequestProperty("Origin", "TEST")
HttpURLConnection c = new URL("http://localhost:9999/system/ok").openConnection()
// Setting the "Origin" header must be allowed by system property sun.net.http.allowRestrictedHeaders=true
c.addRequestProperty("Origin", "TEST")


Expand Down
62 changes: 62 additions & 0 deletions src/test/kotlin/sirius/web/dispatch/AssetDispatcherTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* Made with all the love in the world
* by scireum in Remshalden, Germany
*
* Copyright by scireum GmbH
* http://www.scireum.de - info@scireum.de
*/

package sirius.web.dispatch

import io.netty.handler.codec.http.HttpHeaderNames
import org.junit.jupiter.api.extension.ExtendWith
import org.junit.jupiter.params.ParameterizedTest
import org.junit.jupiter.params.provider.CsvSource
import sirius.kernel.SiriusExtension
import sirius.web.sass.Output
import java.io.StringWriter
import java.net.URL
import org.junit.jupiter.api.Test
import kotlin.test.assertEquals


/**
* Tests the [AssetsDispatcher] class.
*/
@ExtendWith(SiriusExtension::class)
class AssetsDispatcherTest {
@CsvSource(
delimiter = '|', useHeadersInDisplayName = true, textBlock = """
uri | header
/assets/test/test.css | public, max-age=21600
/assets/test/test.txt | public, max-age=21600
/assets/test/test.js | public, max-age=21600
/assets/no-cache/test/test.css | no-cache, max-age=0
/assets/no-cache/test/test.txt | no-cache, max-age=0
/assets/no-cache/test/test.js | no-cache, max-age=0
/assets/dynamic/X/test/test.css | public, max-age=615168000
/assets/dynamic/X/test/test.txt | public, max-age=615168000
/assets/dynamic/X/test/test.js | public, max-age=615168000"""
)
@ParameterizedTest
fun `proper caching set for all kind of assets and cache-control URIs`(uri: String, header: String) {
val connection = URL("http://localhost:9999" + uri).openConnection()
assertEquals(header, connection.getHeaderField(HttpHeaderNames.CACHE_CONTROL.toString()))

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Optional: Remove empty line

}

@Test
fun `Custom base64ResourceFunction works`() {
val generator = SiriusSassGenerator()
generator.importStylesheet("/assets/test_base64.scss")
generator.compile()

val writer = StringWriter()
generator.generate(Output(writer, true))
assertEquals(
"test { background: url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADIAAAAyCAIAAACRXR/mAAAACXBIWXMAAAsTAAALEwEAmpwYAAAAB3RJTUUH4QsODw4S4KU/XgAAAB1pVFh0Q29tbWVudAAAAAAAQ3JlYXRlZCB3aXRoIEdJTVBkLmUHAAAAdElEQVRYw+3Q3Q1AMAAA4ar+qHhghq7SBQ3CPsQO5a1JS0wh+nA3wZdr5hBEfUlRZbBgwYIFCxYsWLBgwYIFCxYsWLBgwYIFCxYsWLBgfZSqDRRtWv1eC2vx2zFGV7S5pX2U+n3M2SWX1ZCNv6a+aHNL/bQvbxUXkThEKBQAAAAASUVORK5CYII=); }\n ",
writer.toString()
)
}

}