public async Task TryInjectLiveReloadScriptAsync_InjectsMarkupIfBodyTagAppearsInTheMiddle() { // Arrange var expected = $@"<footer> This is the footer </footer> {WebSocketScriptInjection.InjectedScript}</body> </html>"; var stream = new MemoryStream(); var input = Encoding.UTF8.GetBytes( @"<footer> This is the footer </footer> </body> </html>"); // Act var result = await WebSocketScriptInjection.TryInjectLiveReloadScriptAsync(stream, input); // Assert Assert.True(result); var output = Encoding.UTF8.GetString(stream.ToArray()); Assert.Equal(expected, output, ignoreLineEndingDifferences: true); }
public override async Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken) { OnWrite(); if (IsHtmlResponse && !ScriptInjectionPerformed) { ScriptInjectionPerformed = await WebSocketScriptInjection.TryInjectLiveReloadScriptAsync(_baseStream, buffer.AsMemory(offset, count), cancellationToken); } else { await _baseStream.WriteAsync(buffer, offset, count, cancellationToken); } }
public override async ValueTask WriteAsync(ReadOnlyMemory <byte> buffer, CancellationToken cancellationToken = default) { OnWrite(); if (IsHtmlResponse && !ScriptInjectionPerformed) { ScriptInjectionPerformed = await WebSocketScriptInjection.TryInjectLiveReloadScriptAsync(_baseStream, buffer, cancellationToken); } else { await _baseStream.WriteAsync(buffer, cancellationToken); } }
public async Task TryInjectLiveReloadScriptAsync_DoesNotInjectMarkup_IfInputDoesNotContainBodyTag() { // Arrange var stream = new MemoryStream(); var input = Encoding.UTF8.GetBytes("<div>this is not a real body tag.</div>"); // Act var result = await WebSocketScriptInjection.TryInjectLiveReloadScriptAsync(stream, input); // Assert Assert.False(result); Assert.Equal(input, stream.ToArray()); }
public async Task TryInjectLiveReloadScriptAsync_InjectsMarkupIfBodyTagAppearsAtTheStartOfOutput() { // Arrange var expected = $"{WebSocketScriptInjection.InjectedScript}</body></html>"; var stream = new MemoryStream(); var input = Encoding.UTF8.GetBytes("</body></html>"); // Act var result = await WebSocketScriptInjection.TryInjectLiveReloadScriptAsync(stream, input); // Assert Assert.True(result); var output = Encoding.UTF8.GetString(stream.ToArray()); Assert.Equal(expected, output); }
public async Task TryInjectLiveReloadScriptAsync_WithOffsetBodyTagAppearsAtStartOfOffset() { // Arrange var expected = $"{WebSocketScriptInjection.InjectedScript}</body>"; var stream = new MemoryStream(); var input = Encoding.UTF8.GetBytes("unused</body>"); // Act var result = await WebSocketScriptInjection.TryInjectLiveReloadScriptAsync(stream, input.AsMemory(6)); // Assert Assert.True(result); var output = Encoding.UTF8.GetString(stream.ToArray()); Assert.Equal(expected, output); }
public async Task TryInjectLiveReloadScriptAsync_MultipleBodyTags() { // Arrange var expected = $"<p></body>some text</p>{WebSocketScriptInjection.InjectedScript}</body>"; var stream = new MemoryStream(); var input = Encoding.UTF8.GetBytes("abc<p></body>some text</p></body>").AsMemory(3); // Act var result = await WebSocketScriptInjection.TryInjectLiveReloadScriptAsync(stream, input); // Assert Assert.True(result); var output = Encoding.UTF8.GetString(stream.ToArray()); Assert.Equal(expected, output); }