From 3b5564a4abf8f8976d42ce11d7711cd7022b639b Mon Sep 17 00:00:00 2001 From: Daniel Beck Date: Sun, 7 Sep 2014 19:22:45 +0200 Subject: [PATCH] [JENKINS-21881] Add test --- .../FrameOptionsPageDecoratorTest.java | 41 +++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 test/src/test/java/jenkins/security/FrameOptionsPageDecoratorTest.java diff --git a/test/src/test/java/jenkins/security/FrameOptionsPageDecoratorTest.java b/test/src/test/java/jenkins/security/FrameOptionsPageDecoratorTest.java new file mode 100644 index 0000000000..aea1fab745 --- /dev/null +++ b/test/src/test/java/jenkins/security/FrameOptionsPageDecoratorTest.java @@ -0,0 +1,41 @@ +package jenkins.security; + +import com.gargoylesoftware.htmlunit.WebResponse; +import com.gargoylesoftware.htmlunit.html.HtmlPage; +import org.apache.commons.httpclient.NameValuePair; +import org.junit.Assert; +import org.junit.Rule; +import org.junit.Test; +import org.jvnet.hudson.test.JenkinsRule; +import org.xml.sax.SAXException; + +import java.io.IOException; + +public class FrameOptionsPageDecoratorTest { + @Rule + public JenkinsRule j = new JenkinsRule(); + + @Test + public void defaultHeaderPresent() throws IOException, SAXException { + JenkinsRule.WebClient wc = j.createWebClient(); + HtmlPage page = wc.goTo(""); + Assert.assertEquals("Expected different X-Frame-Options value", getFrameOptionsFromResponse(page.getWebResponse()), "sameorigin"); + } + + @Test + public void testDisabledFrameOptions() throws IOException, SAXException { + FrameOptionsPageDecorator.enabled = false; + JenkinsRule.WebClient wc = j.createWebClient(); + HtmlPage page = wc.goTo(""); + Assert.assertNull("Expected X-Frame-Options unset", getFrameOptionsFromResponse(page.getWebResponse())); + } + + private static String getFrameOptionsFromResponse(WebResponse response) { + for (NameValuePair pair : response.getResponseHeaders()) { + if (pair.getName().equals("X-Frame-Options")) { + return pair.getValue(); + } + } + return null; + } +} -- GitLab