|
| 1 | +package jenkins.security.csp; |
| 2 | + |
| 3 | +import static org.hamcrest.MatcherAssert.assertThat; |
| 4 | +import static org.hamcrest.Matchers.containsString; |
| 5 | +import static org.hamcrest.Matchers.greaterThan; |
| 6 | +import static org.hamcrest.Matchers.hasLength; |
| 7 | +import static org.hamcrest.Matchers.is; |
| 8 | + |
| 9 | +import org.htmlunit.FailingHttpStatusCodeException; |
| 10 | +import org.htmlunit.WebClient; |
| 11 | +import org.htmlunit.html.HtmlPage; |
| 12 | +import org.junit.jupiter.api.Test; |
| 13 | +import org.junit.jupiter.api.extension.RegisterExtension; |
| 14 | +import org.jvnet.hudson.test.junit.jupiter.RealJenkinsExtension; |
| 15 | + |
| 16 | +public class WinstoneResponseHeaderLengthTest { |
| 17 | + |
| 18 | + @RegisterExtension |
| 19 | + public RealJenkinsExtension extension = new RealJenkinsExtension().addSyntheticPlugin(new RealJenkinsExtension.SyntheticPlugin(jenkins.security.csp.winstoneResponseHeaderLengthTest.ContributorImpl.class)); |
| 20 | + |
| 21 | + @Test |
| 22 | + void testLength() throws Exception { |
| 23 | + extension.startJenkins(); |
| 24 | + String lastHeader = ""; |
| 25 | + try (WebClient wc = new WebClient()) { |
| 26 | + // Hopefully speed this up a bit: |
| 27 | + wc.getOptions().setJavaScriptEnabled(false); |
| 28 | + wc.getOptions().setCssEnabled(false); |
| 29 | + wc.getOptions().setDownloadImages(false); |
| 30 | + wc.getPage(extension.getUrl()); // request once outside try/catch to ensure it works in principle |
| 31 | + try { |
| 32 | + while (true) { |
| 33 | + final HtmlPage htmlPage = wc.getPage(extension.getUrl()); |
| 34 | + lastHeader = htmlPage.getWebResponse().getResponseHeaderValue("Content-Security-Policy"); |
| 35 | + } |
| 36 | + } catch (FailingHttpStatusCodeException e) { |
| 37 | + assertThat(e.getStatusCode(), is(500)); |
| 38 | + assertThat(e.getResponse().getContentAsString(), containsString("Error 500 Response Header Fields Too Large")); |
| 39 | + |
| 40 | + assertThat(lastHeader, hasLength(greaterThan(30_000))); |
| 41 | + } |
| 42 | + } |
| 43 | + } |
| 44 | +} |
0 commit comments