|
18 | 18 |
|
19 | 19 | import static org.hamcrest.MatcherAssert.assertThat; |
20 | 20 | import static org.hamcrest.Matchers.containsString; |
| 21 | +import static org.hamcrest.Matchers.not; |
21 | 22 |
|
22 | 23 | @ExtendWith(MockitoExtension.class) |
23 | 24 | class AbstractSwaggerIndexTransformerTest { |
@@ -67,4 +68,63 @@ void setApiDocUrlCorrectly() throws IOException { |
67 | 68 | var html = underTest.defaultTransformations(new SwaggerUiConfigParameters(swaggerUiConfig), is); |
68 | 69 | assertThat(html, containsString(apiDocUrl)); |
69 | 70 | } |
| 71 | + |
| 72 | + @Test |
| 73 | + void documentTitle_whenSet_addsDocumentTitleScript() throws IOException { |
| 74 | + swaggerUiConfig.setDocumentTitle("My Custom API Documentation"); |
| 75 | + InputStream inputStream = new ByteArrayInputStream(swaggerInitJs.getBytes(StandardCharsets.UTF_8)); |
| 76 | + var html = underTest.defaultTransformations(new SwaggerUiConfigParameters(swaggerUiConfig), inputStream); |
| 77 | + assertThat(html, containsString("document.title = 'My Custom API Documentation';")); |
| 78 | + } |
| 79 | + |
| 80 | + @Test |
| 81 | + void documentTitle_whenNotSet_doesNotAddScript() throws IOException { |
| 82 | + swaggerUiConfig.setDocumentTitle(null); |
| 83 | + InputStream inputStream = new ByteArrayInputStream(swaggerInitJs.getBytes(StandardCharsets.UTF_8)); |
| 84 | + var html = underTest.defaultTransformations(new SwaggerUiConfigParameters(swaggerUiConfig), inputStream); |
| 85 | + assertThat(html, not(containsString("document.title"))); |
| 86 | + } |
| 87 | + |
| 88 | + @Test |
| 89 | + void documentTitle_whenEmpty_doesNotAddScript() throws IOException { |
| 90 | + swaggerUiConfig.setDocumentTitle(""); |
| 91 | + InputStream inputStream = new ByteArrayInputStream(swaggerInitJs.getBytes(StandardCharsets.UTF_8)); |
| 92 | + var html = underTest.defaultTransformations(new SwaggerUiConfigParameters(swaggerUiConfig), inputStream); |
| 93 | + assertThat(html, not(containsString("document.title"))); |
| 94 | + } |
| 95 | + |
| 96 | + @Test |
| 97 | + void documentTitle_escapesSpecialCharacters() throws IOException { |
| 98 | + swaggerUiConfig.setDocumentTitle("Test's API \\ Documentation"); |
| 99 | + InputStream inputStream = new ByteArrayInputStream(swaggerInitJs.getBytes(StandardCharsets.UTF_8)); |
| 100 | + var html = underTest.defaultTransformations(new SwaggerUiConfigParameters(swaggerUiConfig), inputStream); |
| 101 | + assertThat(html, containsString("document.title = 'Test\\'s API \\\\ Documentation';")); |
| 102 | + } |
| 103 | + |
| 104 | + @Test |
| 105 | + void documentTitle_escapesNewlines() throws IOException { |
| 106 | + swaggerUiConfig.setDocumentTitle("Test\nAPI\rDocs\tTitle"); |
| 107 | + InputStream inputStream = new ByteArrayInputStream(swaggerInitJs.getBytes(StandardCharsets.UTF_8)); |
| 108 | + var html = underTest.defaultTransformations(new SwaggerUiConfigParameters(swaggerUiConfig), inputStream); |
| 109 | + assertThat(html, containsString("document.title = 'Test\\nAPI\\rDocs\\tTitle';")); |
| 110 | + } |
| 111 | + |
| 112 | + @Test |
| 113 | + void documentTitle_escapesScriptTags() throws IOException { |
| 114 | + swaggerUiConfig.setDocumentTitle("</script><script>alert('xss')</script>"); |
| 115 | + InputStream inputStream = new ByteArrayInputStream(swaggerInitJs.getBytes(StandardCharsets.UTF_8)); |
| 116 | + var html = underTest.defaultTransformations(new SwaggerUiConfigParameters(swaggerUiConfig), inputStream); |
| 117 | + assertThat(html, not(containsString("</script><script>"))); |
| 118 | + assertThat(html, containsString("\\u003c/script\\u003e\\u003cscript\\u003ealert")); |
| 119 | + } |
| 120 | + |
| 121 | + @Test |
| 122 | + void documentTitle_whenMarkerMissing_returnsOriginalHtml() throws IOException { |
| 123 | + String htmlWithoutMarker = "window.onload = function() { window.ui = SwaggerUIBundle({}); };"; |
| 124 | + swaggerUiConfig.setDocumentTitle("My Title"); |
| 125 | + swaggerUiConfig.setUrl(null); |
| 126 | + InputStream inputStream = new ByteArrayInputStream(htmlWithoutMarker.getBytes(StandardCharsets.UTF_8)); |
| 127 | + var html = underTest.defaultTransformations(new SwaggerUiConfigParameters(swaggerUiConfig), inputStream); |
| 128 | + assertThat(html, not(containsString("document.title"))); |
| 129 | + } |
70 | 130 | } |
0 commit comments