package com.atlassian.stash.internal.plugin.web.fragments;


import com.google.common.collect.ImmutableMap;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.runners.MockitoJUnitRunner;

import java.io.StringWriter;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static org.junit.Assert.assertEquals;


@RunWith(MockitoJUnitRunner.class)
public class PageDataUtilsTest {

    @Test
    public void testNotXSSable() throws Exception {
        StringWriter sw = new StringWriter();
        Map<String, Object> map = ImmutableMap.<String, Object>builder().put("</script>", "</script>").build();
        Map<String, Map<String, Object>> mapOfMaps = ImmutableMap.<String, Map<String, Object>>builder().put("</script>", map).build();
        PageDataUtils.appendScriptTag(sw, mapOfMaps);

        Matcher scriptEnds = Pattern.compile("</script>").matcher(sw.toString());
        int count = 0;
        while (scriptEnds.find()) {
            count++;
        }
        assertEquals("Should only be one closing script tag in the output", count, 1);
    }
}
